com.badlogic.gdx.graphics.g3d.model.NodePart Java Examples

The following examples show how to use com.badlogic.gdx.graphics.g3d.model.NodePart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GameScene.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and adds the navmesh to this scene.
 */
private void setNavmesh(GameObjectBlueprint bp) {
	// We need to set the node transforms before calculating the navmesh shape
	GameModel gameModel = new GameModel(bp.model, bp.name, bp.position, bp.rotation, bp.scale);

	Array<NodePart> nodes = gameModel.modelInstance.model.getNode("navmesh").parts;
	// Sort the model meshParts array according to material name
	nodes.sort(new NavMeshNodeSorter());

	// The model transform must be applied to the meshparts for shape generation to work correctly.
	gameModel.modelInstance.calculateTransforms();
	Matrix4 transform = new Matrix4();
	for (Node node : gameModel.modelInstance.nodes) {
		transform.set(node.globalTransform).inv();
		for (NodePart nodePart : node.parts) {
			nodePart.meshPart.mesh.transform(transform);
		}
	}
	navMesh = new NavMesh(gameModel.modelInstance.model);
	btCollisionShape shape = navMesh.getShape();

	navmeshBody = new InvisibleBody("navmesh",
			shape, 0, gameModel.modelInstance.transform, GameEngine.NAVMESH_FLAG, GameEngine.NAVMESH_FLAG, false, false);
	worldBounds.set(gameModel.boundingBox);
	gameModel.dispose();
}
 
Example #2
Source File: GLTFDemoUI.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
protected void setNode(String nodeID) {
	nodeTable.clearChildren();
	selectedNode = null;
	if(nodeID == null || nodeID.isEmpty()){
		showHideOtherNodes(false);
		return;
	}
	
	final Node node = nodeMap.get(nodeID);
	
	selectedNode = node;
	
	showHideOtherNodes(btNodeExclusive.isChecked());
	
	if(node instanceof NodePlus){
		final NodePlus np = (NodePlus)node;
		if(np.weights != null){
			WeightsUI weightEditor = new WeightsUI(getSkin(), np.weights, np.morphTargetNames);
			nodeTable.add("Morph Targets").row();
			nodeTable.add(weightEditor);
			weightEditor.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					for(NodePart part : np.parts){
						NodePartPlus npp = (NodePartPlus)part;
						if(npp.morphTargets != null){
							npp.morphTargets.set(np.weights);
						}
					}
				}
			});
			
		}
	}
}
 
Example #3
Source File: HeadlessModel.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
private Node loadNode (Node parent, ModelNode modelNode) {
	Node node = new Node();
	node.id = modelNode.id;
	node.parent = parent;

	if (modelNode.translation != null) node.translation.set(modelNode.translation);
	if (modelNode.rotation != null) node.rotation.set(modelNode.rotation);
	if (modelNode.scale != null) node.scale.set(modelNode.scale);
	// FIXME create temporary maps for faster lookup?
	if (modelNode.parts != null) {
		for (ModelNodePart modelNodePart : modelNode.parts) {
			MeshPart meshPart = null;

			if (modelNodePart.meshPartId != null) {
				for (MeshPart part : meshParts) {
					if (modelNodePart.meshPartId.equals(part.id)) {
						meshPart = part;
						break;
					}
				}
			}

			if (meshPart == null) throw new GdxRuntimeException("Invalid node: " + node.id);
			NodePart nodePart = new NodePart();
			nodePart.meshPart = meshPart;
			// nodePart.material = meshMaterial;
			node.parts.add(nodePart);
			if (modelNodePart.bones != null) nodePartBones.put(nodePart, modelNodePart.bones);
		}
	}

	if (modelNode.children != null) {
		for (ModelNode child : modelNode.children) {
			node.children.add(loadNode(node, child));
		}
	}

	return node;
}
 
Example #4
Source File: HeadlessModel.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
private void loadNodes (Iterable<ModelNode> modelNodes) {
	nodePartBones.clear();
	for (ModelNode node : modelNodes) {
		nodes.add(loadNode(null, node));
	}
	for (ObjectMap.Entry<NodePart, ArrayMap<String, Matrix4>> e : nodePartBones.entries()) {
		if (e.key.invBoneBindTransforms == null)
			e.key.invBoneBindTransforms = new ArrayMap<Node, Matrix4>(Node.class, Matrix4.class);
		e.key.invBoneBindTransforms.clear();
		for (ObjectMap.Entry<String, Matrix4> b : e.value.entries())
			e.key.invBoneBindTransforms.put(getNode(b.key), new Matrix4(b.value).inv());
	}
}
 
Example #5
Source File: ModelFactory.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
/**
 * Translate each vertex along its normal by specified amount.
 *
 * @param model
 * @param amount
 */
public static void fatten(Model model, float amount) {
	Vector3 pos = new Vector3();
	Vector3 nor = new Vector3();
	for (Node node : model.nodes) {
		for (NodePart n : node.parts) {
			Mesh mesh = n.meshPart.mesh;
			FloatBuffer buf = mesh.getVerticesBuffer();
			int lastFloat = mesh.getNumVertices() * mesh.getVertexSize() / 4;
			int vertexFloats = (mesh.getVertexSize() / 4);
			VertexAttribute posAttr = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position);
			VertexAttribute norAttr = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Normal);
			if (posAttr == null || norAttr == null) {
				throw new IllegalArgumentException("Position/normal vertex attribute not found");
			}
			int pOff = posAttr.offset / 4;
			int nOff = norAttr.offset / 4;

			for (int i = 0; i < lastFloat; i += vertexFloats) {
				pos.x = buf.get(pOff + i);
				pos.y = buf.get(pOff + i + 1);
				pos.z = buf.get(pOff + i + 2);

				nor.x = buf.get(nOff + i);
				nor.y = buf.get(nOff + i + 1);
				nor.z = buf.get(nOff + i + 2);

				nor.nor().scl(amount);

				buf.put(pOff + i, pos.x + nor.x);
				buf.put(pOff + i + 1, pos.y + nor.y);
				buf.put(pOff + i + 2, pos.z + nor.z);
			}
		}
	}
}
 
Example #6
Source File: GameScene.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
private void setVColorBlendAttributes() {
	Array<String> modelsIdsInScene = assets.getPlaceholderIdsByType(BlenderModel.class);
	Array<BlenderModel> instancesWithId = new Array<BlenderModel>();
	for (String id : modelsIdsInScene) {
		instancesWithId.clear();
		assets.getPlaceholders(id, BlenderModel.class, instancesWithId);
		for (BlenderModel blenderModel : instancesWithId) {
			// Maybe check if
			// renderable.meshPart.mesh.getVertexAttribute(VertexAttributes.Usage.ColorUnpacked) != null
			if (blenderModel.custom_properties.containsKey("v_color_material_blend")) {
				Model model = assets.getAsset(id, Model.class);
				String redMaterialName = blenderModel.custom_properties.get("v_color_material_red");
				String greenMaterialName = blenderModel.custom_properties.get("v_color_material_green");
				String blueMaterialName = blenderModel.custom_properties.get("v_color_material_blue");

				TextureAttribute redTexAttr = (TextureAttribute)
						model.getMaterial(redMaterialName).get(TextureAttribute.Diffuse);
				TextureAttribute greenTexAttr = (TextureAttribute)
						model.getMaterial(greenMaterialName).get(TextureAttribute.Diffuse);
				TextureAttribute blueTexAttr = (TextureAttribute)
						model.getMaterial(blueMaterialName).get(TextureAttribute.Diffuse);
				VertexColorTextureBlend redAttribute =
						new VertexColorTextureBlend(VertexColorTextureBlend.Red,
								redTexAttr.textureDescription.texture);
				VertexColorTextureBlend greenAttribute =
						new VertexColorTextureBlend(VertexColorTextureBlend.Green,
								greenTexAttr.textureDescription.texture);
				VertexColorTextureBlend blueAttribute =
						new VertexColorTextureBlend(VertexColorTextureBlend.Blue,
								blueTexAttr.textureDescription.texture);
				for (Node node : model.nodes) {
					for (NodePart nodePart : node.parts) {
						nodePart.material.set(redAttribute, greenAttribute, blueAttribute);
					}
				}
				break;
			}
		}
	}
}
 
Example #7
Source File: GLTFMaterialExporter.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public void export(Iterable<Node> nodes) {
	for(Node node : nodes){
		for(NodePart nodePart : node.parts){
			export(nodePart.material);
		}
		export(node.getChildren());
	}
}
 
Example #8
Source File: SkinLoader.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
private void load(GLTFSkin glSkin, GLTFNode glNode, Node node, NodeResolver nodeResolver, DataResolver dataResolver){
	
	Array<Matrix4> ibms = new Array<Matrix4>();
	Array<Integer> joints = new Array<Integer>();
	
	int bonesCount = glSkin.joints.size;
	
	FloatBuffer floatBuffer = dataResolver.getBufferFloat(glSkin.inverseBindMatrices);
	
	for(int i=0 ; i<bonesCount ; i++){
		float [] matrixData = new float[16];
		floatBuffer.get(matrixData);
		ibms.add(new Matrix4(matrixData));
	}
	joints.addAll(glSkin.joints);
	
	if(ibms.size > 0){
		for(NodePart nodePart : node.parts){
			nodePart.bones = new Matrix4[ibms.size];
			nodePart.invBoneBindTransforms = new ArrayMap<Node, Matrix4>();
			for(int n=0 ; n<joints.size ; n++){
				nodePart.bones[n] = new Matrix4().idt();
				int nodeIndex = joints.get(n);
				Node key = nodeResolver.get(nodeIndex);
				if(key == null) throw new GLTFIllegalException("node not found for bone: " + nodeIndex);
				nodePart.invBoneBindTransforms.put(key, ibms.get(n));
			}
		}
	}
}
 
Example #9
Source File: ModelInstanceHack.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
public Renderable getRenderable (final Renderable out, final Node node, final NodePart nodePart) {
	super.getRenderable(out, node, nodePart);
	if(nodePart instanceof NodePartPlus){
		out.userData = ((NodePartPlus) nodePart).morphTargets;
	}
	return out;
}
 
Example #10
Source File: NodePartPlus.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
@Override
protected NodePart set(NodePart other) {
	super.set(other);
	if(other instanceof NodePartPlus){
		morphTargets = ((NodePartPlus) other).morphTargets;
	}
	return this;
}
 
Example #11
Source File: AnimationControllerHack.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
private final static void applyNodeAnimationDirectly (final NodeAnimation nodeAnim, final float time) {
	final Node node = nodeAnim.node;
	node.isAnimated = true;
	final Transform transform = getNodeAnimationTransform(nodeAnim, time);
	transform.toMatrix4(node.localTransform);
	if(node instanceof NodePlus){
		if(((NodePlus)node).weights != null){
			((NodePlus)node).weights.set(transform.weights);
			for(NodePart part : node.parts){
				((NodePartPlus)part).morphTargets.set(transform.weights);
			}
		}
	}
}
 
Example #12
Source File: GLTFDemoUI.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
protected void showHideOtherNodes(boolean hide) {
	for(Entry<String, Node> entry : nodeMap){
		for(NodePart part : entry.value.parts){
			part.enabled = selectedNode == null || !hide || entry.value == selectedNode;
		}
	}
}
 
Example #13
Source File: GLTFExporter.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
private Array<Integer> exportNodes(GLTFScene scene, Iterable<Node> nodes) {
	Array<Integer> indices = null;
	for(Node node : nodes)
	{
		// create node
		GLTFNode data = obtainNode();
		nodeMapping.add(node);
		data.name = node.id;
		
		// transform, either a matrix or individual component (we use individual components but it might be an option)
		if(!node.translation.isZero()){
			data.translation = GLTFExportTypes.toArray(node.translation);
		}
		if(!node.scale.epsilonEquals(1, 1, 1)){
			data.scale = GLTFExportTypes.toArray(node.scale);
		}
		if(!node.rotation.isIdentity()){
			data.rotation = GLTFExportTypes.toArray(node.rotation);
		}
		
		// indexing node
		if(indices == null) indices = new Array<Integer>();
		indices.add(root.nodes.size-1);
		
		// create mesh
		if(node.parts.size > 0){
			GLTFMesh gltfMesh = obtainMesh();
			data.mesh = root.meshes.size-1;
			
			gltfMesh.primitives = new Array<GLTFPrimitive>();
			for(NodePart nodePart : node.parts){
				GLTFPrimitive primitive = meshExporter.exportMeshPart(nodePart.meshPart);
				int materialIndex = materialMapping.indexOf(nodePart.material, true);
				if(materialIndex < 0) throw new GdxRuntimeException("material not found");
				primitive.material = materialIndex;
				gltfMesh.primitives.add(primitive);
			}
		}
		
		// recursive children export
		data.children = exportNodes(scene, node.getChildren());
	}
	return indices;
}
 
Example #14
Source File: Chunk.java    From Radix with MIT License 4 votes vote down vote up
private void updateModelInstances() {
    if(opaqueFaces != null) {
        if(opaqueModel != null)
            opaqueModel.dispose();

        Mesh opaqueMesh = mesher.meshFaces(opaqueFaces, meshBuilder);
        modelBuilder.begin();
        modelBuilder.part(String.format("c-%d,%d", startPosition.x, startPosition.z), opaqueMesh, GL20.GL_TRIANGLES,
                new Material(TextureAttribute.createDiffuse(NormalBlockRenderer.getBlockMap())));
        opaqueModel = modelBuilder.end();

        opaqueModelInstance = new ModelInstance(opaqueModel) {
            @Override
            public Renderable getRenderable(final Renderable out, final Node node,
                                            final NodePart nodePart) {
                super.getRenderable(out, node, nodePart);
                if(RadixClient.getInstance().isWireframe()) {
                    out.primitiveType = GL20.GL_LINES;
                } else {
                    out.primitiveType = GL20.GL_TRIANGLES;
                }
                return out;
            }
        };

        opaqueFaces = null;
    }

    if(translucentFaces != null) {
        if(translucentModel != null)
            translucentModel.dispose();

        Mesh translucentMesh = mesher.meshFaces(translucentFaces, meshBuilder);
        modelBuilder.begin();
        modelBuilder.part(String.format("c-%d,%d-t", startPosition.x, startPosition.z), translucentMesh, GL20.GL_TRIANGLES,
                new Material(TextureAttribute.createDiffuse(NormalBlockRenderer.getBlockMap()),
                        new BlendingAttribute(),
                        FloatAttribute.createAlphaTest(0.25f)));
        translucentModel = modelBuilder.end();

        translucentModelInstance = new ModelInstance(translucentModel) {
            @Override
            public Renderable getRenderable(final Renderable out, final Node node,
                                            final NodePart nodePart) {
                super.getRenderable(out, node, nodePart);
                if(RadixClient.getInstance().isWireframe()) {
                    out.primitiveType = GL20.GL_LINES;
                } else {
                    out.primitiveType = GL20.GL_TRIANGLES;
                }
                return out;
            }
        };

        translucentFaces = null;
    }
}
 
Example #15
Source File: GameScene.java    From GdxDemo3D with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(NodePart a, NodePart b) {
	return a.material.id.compareTo(b.material.id);
}
 
Example #16
Source File: NodePartPlus.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
@Override
public NodePart copy() {
	return new NodePartPlus().set(this);
}
 
Example #17
Source File: GLTFInspector.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
private void logScenes(SceneAsset asset) {
	int nBones = 0;
	int nNodes = 0;
	int meshNodes = 0;
	int nMeshParts = 0;
	int nMesh = 0;
	int nVert = 0;
	int nTri = 0;
	int nRealTri = 0;
	ObjectSet<Material> mapSet = new ObjectSet<Material>();
	for(SceneModel scene : asset.scenes){
		nMesh += scene.model.meshes.size;
		
		allCameras.addAll(scene.cameras.values().toArray());
		allLights.addAll(scene.lights.values().toArray());
		
		for(Mesh mesh : scene.model.meshes ){
			nVert += mesh.getNumVertices();
			nTri += mesh.getNumIndices() / 3;
		}
		
		Array<Node> nodes = NodeUtil.getAllNodes(new Array<Node>(), scene.model);
		nNodes += nodes.size;
		for(Node node : nodes){
			if(node.parts.size > 0){
				nMeshParts += node.parts.size;
				for(NodePart np : node.parts){
					nRealTri += np.meshPart.size / 3;
					mapSet.add(np.material);
					nBones += np.bones != null ? np.bones.length : 0;
				}
				meshNodes++;
			}
		}
	}
	int nMaterials = mapSet.size;
	log("Scene Graph", "scenes", asset.scenes.size, "nodes", nNodes, "empty", nNodes - meshNodes);
	if(nMesh > 0) log("Mesh", "count", nMesh, "parts", nMeshParts, "Vertices", nVert, "Tris", nTri, "Rendered", nRealTri);
	if(nMaterials > 0) log("Materials", "count", nMaterials);
	if(asset.maxBones > 0) log("Bones", "max", asset.maxBones, "count", nBones);
}