/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ 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); } }