--- /dev/null
+package org.simantics.g3d.jme.shape;\r
+\r
+import javax.vecmath.Vector3d;\r
+\r
+import org.simantics.g3d.shape.Color4d;\r
+\r
+import com.jme3.app.Application;\r
+import com.jme3.material.Material;\r
+import com.jme3.math.ColorRGBA;\r
+import com.jme3.scene.Geometry;\r
+import com.jme3.scene.Mesh;\r
+import com.jme3.scene.VertexBuffer.Type;\r
+\r
+public class MeshNode extends Geometry {\r
+ Application app;\r
+ \r
+ \r
+ public MeshNode(Application app) {\r
+ this.app = app;\r
+ }\r
+ \r
+ public void setMesh(org.simantics.g3d.shape.Mesh mesh) {\r
+ \r
+ Mesh shape = new Mesh();\r
+ \r
+ float points[] = new float[mesh.getVertices().size()*3];\r
+ for (int i = 0; i < mesh.getVertices().size(); i++) {\r
+ Vector3d v = mesh.getVertices().get(i);\r
+ points[i * 3] = (float)v.x;\r
+ points[i * 3+1] = (float)v.y;\r
+ points[i * 3+2] = (float)v.z;\r
+ }\r
+ shape.setBuffer(Type.Position, 3, points);\r
+ \r
+ if (mesh.getNormals() == null)\r
+ mesh.createNormals();\r
+ float normals[] = new float[mesh.getNormals().size()*3];\r
+ for (int i = 0; i < mesh.getNormals().size(); i++) {\r
+ Vector3d v = mesh.getNormals().get(i);\r
+ normals[i * 3] = (float)v.x;\r
+ normals[i * 3+1] = (float)v.y;\r
+ normals[i * 3+2] = (float)v.z;\r
+ }\r
+ shape.setBuffer(Type.Normal, 3, normals);\r
+ \r
+ int index[] = new int[mesh.getIndices().size()];\r
+ for (int i = 0; i < index.length; i++) {\r
+ index[i] = mesh.getIndices().get(i);\r
+ }\r
+ shape.setBuffer(Type.Index, 3, index);\r
+ \r
+ if (mesh.getColors() != null) {\r
+ float colors[] = new float[mesh.getColors().size()*4];\r
+ for (int i = 0; i < mesh.getColors().size(); i++) {\r
+ Color4d v = mesh.getColors().get(i);\r
+ colors[i * 4] = (float)v.x;\r
+ colors[i * 4+1] = (float)v.y;\r
+ colors[i * 4+2] = (float)v.z;\r
+ colors[i * 4+3] = (float)v.w;\r
+ }\r
+ shape.setBuffer(Type.Color, 4, colors);\r
+ }\r
+ \r
+ setMesh(shape);\r
+ updateModelBound();\r
+ \r
+ //Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");\r
+ Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");\r
+ \r
+ \r
+ if (true) {\r
+ ColorRGBA color = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);\r
+ mat.setColor("Diffuse", color);\r
+ mat.setColor("Ambient", color);\r
+ mat.setColor("Specular", new ColorRGBA(1, 1, 1, 1));\r
+ mat.setBoolean("UseMaterialColors", true);\r
+ }\r
+ if (mesh.getColors() != null)\r
+ mat.setBoolean("UseVertexColor", true);\r
+ setMaterial(mat);\r
+ \r
+ }\r
+\r
+}\r