X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.jme%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fjme%2Fshape%2FMeshNode.java;fp=org.simantics.g3d.jme%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fjme%2Fshape%2FMeshNode.java;h=b6baa7982186c29739e35bc6f73988d84e191b6a;hb=5b4454ce3dac8d0cf04caaaa696f300c16be99a0;hp=0000000000000000000000000000000000000000;hpb=2e9c37990c979fe7a877a6a9d0de65ee27f3b56f;p=simantics%2F3d.git diff --git a/org.simantics.g3d.jme/src/org/simantics/g3d/jme/shape/MeshNode.java b/org.simantics.g3d.jme/src/org/simantics/g3d/jme/shape/MeshNode.java new file mode 100644 index 00000000..b6baa798 --- /dev/null +++ b/org.simantics.g3d.jme/src/org/simantics/g3d/jme/shape/MeshNode.java @@ -0,0 +1,84 @@ +package org.simantics.g3d.jme.shape; + +import javax.vecmath.Vector3d; + +import org.simantics.g3d.shape.Color4d; + +import com.jme3.app.Application; +import com.jme3.material.Material; +import com.jme3.math.ColorRGBA; +import com.jme3.scene.Geometry; +import com.jme3.scene.Mesh; +import com.jme3.scene.VertexBuffer.Type; + +public class MeshNode extends Geometry { + Application app; + + + public MeshNode(Application app) { + this.app = app; + } + + public void setMesh(org.simantics.g3d.shape.Mesh mesh) { + + Mesh shape = new Mesh(); + + float points[] = new float[mesh.getVertices().size()*3]; + for (int i = 0; i < mesh.getVertices().size(); i++) { + Vector3d v = mesh.getVertices().get(i); + points[i * 3] = (float)v.x; + points[i * 3+1] = (float)v.y; + points[i * 3+2] = (float)v.z; + } + shape.setBuffer(Type.Position, 3, points); + + if (mesh.getNormals() == null) + mesh.createNormals(); + float normals[] = new float[mesh.getNormals().size()*3]; + for (int i = 0; i < mesh.getNormals().size(); i++) { + Vector3d v = mesh.getNormals().get(i); + normals[i * 3] = (float)v.x; + normals[i * 3+1] = (float)v.y; + normals[i * 3+2] = (float)v.z; + } + shape.setBuffer(Type.Normal, 3, normals); + + int index[] = new int[mesh.getIndices().size()]; + for (int i = 0; i < index.length; i++) { + index[i] = mesh.getIndices().get(i); + } + shape.setBuffer(Type.Index, 3, index); + + if (mesh.getColors() != null) { + float colors[] = new float[mesh.getColors().size()*4]; + for (int i = 0; i < mesh.getColors().size(); i++) { + Color4d v = mesh.getColors().get(i); + colors[i * 4] = (float)v.x; + colors[i * 4+1] = (float)v.y; + colors[i * 4+2] = (float)v.z; + colors[i * 4+3] = (float)v.w; + } + shape.setBuffer(Type.Color, 4, colors); + } + + setMesh(shape); + updateModelBound(); + + //Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); + Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); + + + if (true) { + ColorRGBA color = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f); + mat.setColor("Diffuse", color); + mat.setColor("Ambient", color); + mat.setColor("Specular", new ColorRGBA(1, 1, 1, 1)); + mat.setBoolean("UseMaterialColors", true); + } + if (mesh.getColors() != null) + mat.setBoolean("UseVertexColor", true); + setMaterial(mat); + + } + +}