1 package org.simantics.g3d.jme.shape;
\r
3 import javax.vecmath.Vector3d;
\r
5 import org.simantics.g3d.shape.Color4d;
\r
7 import com.jme3.app.Application;
\r
8 import com.jme3.material.Material;
\r
9 import com.jme3.math.ColorRGBA;
\r
10 import com.jme3.scene.Geometry;
\r
11 import com.jme3.scene.Mesh;
\r
12 import com.jme3.scene.VertexBuffer.Type;
\r
14 public class MeshNode extends Geometry {
\r
18 public MeshNode(Application app) {
\r
22 public void setMesh(org.simantics.g3d.shape.Mesh mesh) {
\r
24 Mesh shape = new Mesh();
\r
26 float points[] = new float[mesh.getVertices().size()*3];
\r
27 for (int i = 0; i < mesh.getVertices().size(); i++) {
\r
28 Vector3d v = mesh.getVertices().get(i);
\r
29 points[i * 3] = (float)v.x;
\r
30 points[i * 3+1] = (float)v.y;
\r
31 points[i * 3+2] = (float)v.z;
\r
33 shape.setBuffer(Type.Position, 3, points);
\r
35 if (mesh.getNormals() == null)
\r
36 mesh.createNormals();
\r
37 float normals[] = new float[mesh.getNormals().size()*3];
\r
38 for (int i = 0; i < mesh.getNormals().size(); i++) {
\r
39 Vector3d v = mesh.getNormals().get(i);
\r
40 normals[i * 3] = (float)v.x;
\r
41 normals[i * 3+1] = (float)v.y;
\r
42 normals[i * 3+2] = (float)v.z;
\r
44 shape.setBuffer(Type.Normal, 3, normals);
\r
46 int index[] = new int[mesh.getIndices().size()];
\r
47 for (int i = 0; i < index.length; i++) {
\r
48 index[i] = mesh.getIndices().get(i);
\r
50 shape.setBuffer(Type.Index, 3, index);
\r
52 if (mesh.getColors() != null) {
\r
53 float colors[] = new float[mesh.getColors().size()*4];
\r
54 for (int i = 0; i < mesh.getColors().size(); i++) {
\r
55 Color4d v = mesh.getColors().get(i);
\r
56 colors[i * 4] = (float)v.x;
\r
57 colors[i * 4+1] = (float)v.y;
\r
58 colors[i * 4+2] = (float)v.z;
\r
59 colors[i * 4+3] = (float)v.w;
\r
61 shape.setBuffer(Type.Color, 4, colors);
\r
67 //Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
\r
68 Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
\r
72 ColorRGBA color = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
\r
73 mat.setColor("Diffuse", color);
\r
74 mat.setColor("Ambient", color);
\r
75 mat.setColor("Specular", new ColorRGBA(1, 1, 1, 1));
\r
76 mat.setBoolean("UseMaterialColors", true);
\r
78 if (mesh.getColors() != null)
\r
79 mat.setBoolean("UseVertexColor", true);
\r