Java Code Examples for com.jme3.scene.Mesh#updateBound()
The following examples show how to use
com.jme3.scene.Mesh#updateBound() .
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: Converter.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static Mesh convert(IndexedMesh mesh) { Mesh jmeMesh = new Mesh(); jmeMesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(mesh.numTriangles * 3)); jmeMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(mesh.numVertices * 3)); IndexBuffer indicess = jmeMesh.getIndexBuffer(); FloatBuffer vertices = jmeMesh.getFloatBuffer(Type.Position); for (int i = 0; i < mesh.numTriangles * 3; i++) { indicess.put(i, mesh.triangleIndexBase.getInt(i * 4)); } for (int i = 0; i < mesh.numVertices * 3; i++) { vertices.put(i, mesh.vertexBase.getFloat(i * 4)); } jmeMesh.updateCounts(); jmeMesh.updateBound(); jmeMesh.getFloatBuffer(Type.Position).clear(); return jmeMesh; }
Example 2
Source File: Converter.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public static Mesh convert(IndexedMesh mesh) { Mesh jmeMesh = new Mesh(); jmeMesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(mesh.numTriangles * 3)); jmeMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(mesh.numVertices * 3)); IndexBuffer indicess = jmeMesh.getIndexBuffer(); FloatBuffer vertices = jmeMesh.getFloatBuffer(Type.Position); for (int i = 0; i < mesh.numTriangles * 3; i++) { indicess.put(i, mesh.triangleIndexBase.getInt(i * 4)); } for (int i = 0; i < mesh.numVertices * 3; i++) { vertices.put(i, mesh.vertexBase.getFloat(i * 4)); } jmeMesh.updateCounts(); jmeMesh.updateBound(); jmeMesh.getFloatBuffer(Type.Position).clear(); return jmeMesh; }
Example 3
Source File: GeoMap.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Mesh createMesh(Vector3f scale, Vector2f tcScale, boolean center){ FloatBuffer pb = writeVertexArray(null, scale, center); FloatBuffer tb = writeTexCoordArray(null, Vector2f.ZERO, tcScale); FloatBuffer nb = writeNormalArray(null, scale); IntBuffer ib = writeIndexArray(null); Mesh m = new Mesh(); m.setBuffer(Type.Position, 3, pb); m.setBuffer(Type.Normal, 3, nb); m.setBuffer(Type.TexCoord, 2, tb); m.setBuffer(Type.Index, 3, ib); m.setStatic(); m.updateBound(); return m; }
Example 4
Source File: TestRayCasting.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void simpleInitApp() { // flyCam.setEnabled(false); // load material Material mat = assetManager.loadMaterial("Interface/Logo/Logo.j3m"); Mesh q = new Mesh(); q.setBuffer(Type.Position, 3, new float[] { 1, 0, 0, 0, 1.5f, 0, -1, 0, 0 } ); q.setBuffer(Type.Index, 3, new int[]{ 0, 1, 2 }); q.setBound(new BoundingSphere()); q.updateBound(); // Geometry teapot = new Geometry("MyGeom", q); teapot = assetManager.loadModel("Models/Teapot/Teapot.mesh.xml"); // teapot.scale(2f, 2f, 2f); // teapot.move(2f, 2f, -.5f); teapot.rotate(FastMath.HALF_PI, FastMath.HALF_PI, FastMath.HALF_PI); teapot.setMaterial(mat); rootNode.attachChild(teapot); // cam.setLocation(cam.getLocation().add(0,1,0)); // cam.lookAt(teapot.getWorldBound().getCenter(), Vector3f.UNIT_Y); tracer = new RayTrace(rootNode, cam, 160, 128); tracer.show(); tracer.update(); }
Example 5
Source File: TestTangentGen.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
private Mesh createTriangleStripMesh() { Mesh strip = new Mesh(); strip.setMode(Mode.TriangleStrip); FloatBuffer vb = BufferUtils.createFloatBuffer(3*3*3); // 3 rows * 3 columns * 3 floats vb.rewind(); vb.put(new float[]{0,2,0}); vb.put(new float[]{1,2,0}); vb.put(new float[]{2,2,0}); vb.put(new float[]{0,1,0}); vb.put(new float[]{1,1,0}); vb.put(new float[]{2,1,0}); vb.put(new float[]{0,0,0}); vb.put(new float[]{1,0,0}); vb.put(new float[]{2,0,0}); FloatBuffer nb = BufferUtils.createFloatBuffer(3*3*3); nb.rewind(); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); FloatBuffer tb = BufferUtils.createFloatBuffer(3*3*2); tb.rewind(); tb.put(new float[]{0,0}); tb.put(new float[]{0.5f,0}); tb.put(new float[]{1,0}); tb.put(new float[]{0,0.5f}); tb.put(new float[]{0.5f,0.5f}); tb.put(new float[]{1,0.5f}); tb.put(new float[]{0,1}); tb.put(new float[]{0.5f,1}); tb.put(new float[]{1,1}); int[] indexes = new int[]{0,3,1,4,2,5, 5,3, 3,6,4,7,5,8}; IntBuffer ib = BufferUtils.createIntBuffer(indexes.length); ib.put(indexes); strip.setBuffer(Type.Position, 3, vb); strip.setBuffer(Type.Normal, 3, nb); strip.setBuffer(Type.TexCoord, 2, tb); strip.setBuffer(Type.Index, 3, ib); strip.updateBound(); return strip; }
Example 6
Source File: LODGeomap.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) { FloatBuffer pb = writeVertexArray(null, scale, center); FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize); FloatBuffer nb = writeNormalArray(null, scale); IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize); FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3); FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3); writeTangentArray(nb, tanb, bb, texb, scale); Mesh m = new Mesh(); m.setMode(Mode.TriangleStrip); m.setBuffer(Type.Position, 3, pb); m.setBuffer(Type.Normal, 3, nb); m.setBuffer(Type.Tangent, 3, tanb); m.setBuffer(Type.Binormal, 3, bb); m.setBuffer(Type.TexCoord, 2, texb); switch (ib.getFormat()) { case UnsignedInt: m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer()); break; case UnsignedShort: m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer()); break; case UnsignedByte: m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer()); break; } m.setStatic(); m.updateBound(); return m; }
Example 7
Source File: TestTangentGen.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
private Mesh createTriangleStripMesh() { Mesh strip = new Mesh(); strip.setMode(Mode.TriangleStrip); FloatBuffer vb = BufferUtils.createFloatBuffer(3*3*3); // 3 rows * 3 columns * 3 floats vb.rewind(); vb.put(new float[]{0,2,0}); vb.put(new float[]{1,2,0}); vb.put(new float[]{2,2,0}); vb.put(new float[]{0,1,0}); vb.put(new float[]{1,1,0}); vb.put(new float[]{2,1,0}); vb.put(new float[]{0,0,0}); vb.put(new float[]{1,0,0}); vb.put(new float[]{2,0,0}); FloatBuffer nb = BufferUtils.createFloatBuffer(3*3*3); nb.rewind(); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); nb.put(new float[]{0,0,1}); FloatBuffer tb = BufferUtils.createFloatBuffer(3*3*2); tb.rewind(); tb.put(new float[]{0,0}); tb.put(new float[]{0.5f,0}); tb.put(new float[]{1,0}); tb.put(new float[]{0,0.5f}); tb.put(new float[]{0.5f,0.5f}); tb.put(new float[]{1,0.5f}); tb.put(new float[]{0,1}); tb.put(new float[]{0.5f,1}); tb.put(new float[]{1,1}); int[] indexes = new int[]{0,3,1,4,2,5, 5,3, 3,6,4,7,5,8}; IntBuffer ib = BufferUtils.createIntBuffer(indexes.length); ib.put(indexes); strip.setBuffer(Type.Position, 3, vb); strip.setBuffer(Type.Normal, 3, nb); strip.setBuffer(Type.TexCoord, 2, tb); strip.setBuffer(Type.Index, 3, ib); strip.updateBound(); return strip; }
Example 8
Source File: TestRayCasting.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void simpleInitApp() { // flyCam.setEnabled(false); // load material Material mat = (Material) assetManager.loadMaterial("Interface/Logo/Logo.j3m"); Mesh q = new Mesh(); q.setBuffer(Type.Position, 3, new float[] { 1, 0, 0, 0, 1.5f, 0, -1, 0, 0 } ); q.setBuffer(Type.Index, 3, new int[]{ 0, 1, 2 }); q.setBound(new BoundingSphere()); q.updateBound(); // Geometry teapot = new Geometry("MyGeom", q); teapot = assetManager.loadModel("Models/Teapot/Teapot.mesh.xml"); // teapot.scale(2f, 2f, 2f); // teapot.move(2f, 2f, -.5f); teapot.rotate(FastMath.HALF_PI, FastMath.HALF_PI, FastMath.HALF_PI); teapot.setMaterial(mat); rootNode.attachChild(teapot); // cam.setLocation(cam.getLocation().add(0,1,0)); // cam.lookAt(teapot.getWorldBound().getCenter(), Vector3f.UNIT_Y); tracer = new RayTrace(rootNode, cam, 160, 128); tracer.show(); tracer.update(); }
Example 9
Source File: UVCoordinatesGenerator.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
/** * This method returns the bounding sphere of the given mesh. * * @param mesh * the mesh * @return bounding sphere of the given mesh */ /* package */static BoundingSphere getBoundingSphere(Mesh mesh) { mesh.updateBound(); BoundingVolume bv = mesh.getBound(); if (bv instanceof BoundingBox) { BoundingBox bb = (BoundingBox) bv; float r = Math.max(bb.getXExtent(), bb.getYExtent()); r = Math.max(r, bb.getZExtent()); return new BoundingSphere(r, bb.getCenter()); } else if (bv instanceof BoundingSphere) { return (BoundingSphere) bv; } else { throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName()); } }
Example 10
Source File: TextureAtlas.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Creates one geometry out of the given root spatial and merges all single * textures into one texture of the given size. * @param spat The root spatial of the scene to batch * @param mgr An assetmanager that can be used to create the material. * @param atlasSize A size for the atlas texture, it has to be large enough to hold all single textures. * @return A new geometry that uses the generated texture atlas and merges all meshes of the root spatial, null if the atlas cannot be created because not all textures fit. */ public static Geometry makeAtlasBatch(Spatial spat, AssetManager mgr, int atlasSize) { List<Geometry> geometries = new ArrayList<Geometry>(); GeometryBatchFactory.gatherGeoms(spat, geometries); TextureAtlas atlas = createAtlas(spat, atlasSize); if (atlas == null) { return null; } Geometry geom = new Geometry(); Mesh mesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geometries, mesh); applyAtlasCoords(geometries, mesh, atlas); mesh.updateCounts(); mesh.updateBound(); geom.setMesh(mesh); Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md"); Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); Texture normalMap = atlas.getAtlasTexture("NormalMap"); Texture specularMap = atlas.getAtlasTexture("SpecularMap"); if (diffuseMap != null) { mat.setTexture("DiffuseMap", diffuseMap); } if (normalMap != null) { mat.setTexture("NormalMap", normalMap); } if (specularMap != null) { mat.setTexture("SpecularMap", specularMap); } mat.setFloat("Shininess", 16.0f); geom.setMaterial(mat); return geom; }
Example 11
Source File: UVCoordinatesGenerator.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
/** * This method returns the bounding box of the given mesh. * * @param mesh * the mesh * @return bounding box of the given mesh */ /* package */static BoundingBox getBoundingBox(Mesh mesh) { mesh.updateBound(); BoundingVolume bv = mesh.getBound(); if (bv instanceof BoundingBox) { return (BoundingBox) bv; } else if (bv instanceof BoundingSphere) { BoundingSphere bs = (BoundingSphere) bv; float r = bs.getRadius(); return new BoundingBox(bs.getCenter(), r, r, r); } else { throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName()); } }
Example 12
Source File: GeoMap.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
public Mesh createMesh(Vector3f scale, Vector2f tcScale, boolean center){ FloatBuffer pb = writeVertexArray(null, scale, center); FloatBuffer tb = writeTexCoordArray(null, Vector2f.ZERO, tcScale); FloatBuffer nb = writeNormalArray(null, scale); IntBuffer ib = writeIndexArray(null); Mesh m = new Mesh(); m.setBuffer(Type.Position, 3, pb); m.setBuffer(Type.Normal, 3, nb); m.setBuffer(Type.TexCoord, 2, tb); m.setBuffer(Type.Index, 3, ib); m.setStatic(); m.updateBound(); return m; }
Example 13
Source File: GeometryBatchFactory.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
/** * Batches a collection of Geometries so that all with the same material get combined. * @param geometries The Geometries to combine * @return A List of newly created Geometries, each with a distinct material */ public static List<Geometry> makeBatches(Collection<Geometry> geometries, boolean useLods) { ArrayList<Geometry> retVal = new ArrayList<Geometry>(); HashMap<Material, List<Geometry>> matToGeom = new HashMap<Material, List<Geometry>>(); for (Geometry geom : geometries) { List<Geometry> outList = matToGeom.get(geom.getMaterial()); if (outList == null) { outList = new ArrayList<Geometry>(); matToGeom.put(geom.getMaterial(), outList); } outList.add(geom); } int batchNum = 0; for (Map.Entry<Material, List<Geometry>> entry : matToGeom.entrySet()) { Material mat = entry.getKey(); List<Geometry> geomsForMat = entry.getValue(); Mesh mesh = new Mesh(); mergeGeometries(geomsForMat, mesh); // lods if (useLods) { makeLods(geomsForMat, mesh); } mesh.updateCounts(); mesh.updateBound(); Geometry out = new Geometry("batch[" + (batchNum++) + "]", mesh); out.setMaterial(mat); retVal.add(out); } return retVal; }
Example 14
Source File: ModelConverter.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
public static void optimize(Mesh mesh, boolean toFixed){ // update any data that need updating mesh.updateBound(); mesh.updateCounts(); // set all buffers into STATIC_DRAW mode mesh.setStatic(); if (mesh.getBuffer(Type.Index) != null){ // compress index buffer from UShort to UByte (if possible) FloatToFixed.compressIndexBuffer(mesh); // generate triangle strips stitched with degenerate tris generateStrips(mesh, false, false, 16, 0); } IntMap<VertexBuffer> bufs = mesh.getBuffers(); for (Entry<VertexBuffer> entry : bufs){ VertexBuffer vb = entry.getValue(); if (vb == null || vb.getBufferType() == Type.Index) continue; if (vb.getFormat() == Format.Float){ if (vb.getBufferType() == Type.Color){ // convert the color buffer to UByte vb = FloatToFixed.convertToUByte(vb); vb.setNormalized(true); }else if (toFixed){ // convert normals, positions, and texcoords // to fixed-point (16.16) vb = FloatToFixed.convertToFixed(vb); // vb = FloatToFixed.convertToFloat(vb); } mesh.clearBuffer(vb.getBufferType()); mesh.setBuffer(vb); } } mesh.setInterleaved(); }
Example 15
Source File: TestCustomMesh.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 4 votes |
@Override public void simpleInitApp() { Mesh m = new Mesh(); // Vertex positions in space Vector3f [] vertices = new Vector3f[4]; vertices[0] = new Vector3f(0,0,0); vertices[1] = new Vector3f(3,0,0); vertices[2] = new Vector3f(0,3,0); vertices[3] = new Vector3f(3,3,0); // Texture coordinates Vector2f [] texCoord = new Vector2f[4]; texCoord[0] = new Vector2f(0,0); texCoord[1] = new Vector2f(1,0); texCoord[2] = new Vector2f(0,1); texCoord[3] = new Vector2f(1,1); // Indexes. We define the order in which mesh should be constructed int [] indexes = {2,0,1,1,3,2}; // Setting buffers m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord)); m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes)); m.updateBound(); // ************************************************************************* // First mesh uses one solid color // ************************************************************************* // Creating a geometry, and apply a single color material to it Geometry geom = new Geometry("OurMesh", m); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); geom.setMaterial(mat); // Attaching our geometry to the root node. rootNode.attachChild(geom); // ************************************************************************* // Second mesh uses vertex colors to color each vertex // ************************************************************************* Mesh cMesh = m.clone(); Geometry coloredMesh = new Geometry ("ColoredMesh", cMesh); Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matVC.setBoolean("VertexColor", true); //We have 4 vertices and 4 color values for each of them. //If you have more vertices, you need 'new float[yourVertexCount * 4]' here! float[] colorArray = new float[4*4]; int colorIndex = 0; //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f for(int i = 0; i < 4; i++){ // Red value (is increased by .2 on each next vertex here) colorArray[colorIndex++]= 0.1f+(.2f*i); // Green value (is reduced by .2 on each next vertex) colorArray[colorIndex++]= 0.9f-(0.2f*i); // Blue value (remains the same in our case) colorArray[colorIndex++]= 0.5f; // Alpha value (no transparency set here) colorArray[colorIndex++]= 1.0f; } // Set the color buffer cMesh.setBuffer(Type.Color, 4, colorArray); coloredMesh.setMaterial(matVC); // move mesh a bit so that it doesn't intersect with the first one coloredMesh.setLocalTranslation(4, 0, 0); rootNode.attachChild(coloredMesh); // /** Alternatively, you can show the mesh vertixes as points // * instead of coloring the faces. */ // cMesh.setMode(Mesh.Mode.Points); // cMesh.setPointSize(10f); // cMesh.updateBound(); // cMesh.setStatic(); // Geometry points = new Geometry("Points", m); // points.setMaterial(mat); // rootNode.attachChild(points); // ************************************************************************* // Third mesh will use a wireframe shader to show wireframe // ************************************************************************* Mesh wfMesh = m.clone(); Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh); Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWireframe.setColor("Color", ColorRGBA.Green); matWireframe.getAdditionalRenderState().setWireframe(true); wfGeom.setMaterial(matWireframe); wfGeom.setLocalTranslation(4, 4, 0); rootNode.attachChild(wfGeom); }
Example 16
Source File: TranslateUtil.java From OpenRTS with MIT License | 4 votes |
public static Mesh toJMEMesh(MyMesh m) { Mesh res = new Mesh(); float vertices[] = new float[m.vertices.size() * 3]; float textCoord[] = new float[m.textCoord.size() * 2]; float normals[] = new float[m.normals.size() * 3]; int indices[] = new int[m.indices.size()]; int j = 0; for (int i = 0; i < m.vertices.size() * 3; i += 3) { vertices[i] = (float) m.vertices.get(j).x; vertices[i + 1] = (float) m.vertices.get(j).y; vertices[i + 2] = (float) m.vertices.get(j).z; j++; } j = 0; for (int i = 0; i < m.textCoord.size() * 2; i += 2) { textCoord[i] = (float) m.textCoord.get(j).x; textCoord[i + 1] = (float) m.textCoord.get(j).y; j++; } j = 0; for (int i = 0; i < m.normals.size() * 3; i += 3) { normals[i] = (float) m.normals.get(j).x; normals[i + 1] = (float) m.normals.get(j).y; normals[i + 2] = (float) m.normals.get(j).z; j++; } for (int i = 0; i < m.indices.size(); i++) { // indices[i] = (short) (int) m.indices.get(i); indices[i] = m.indices.get(i); } res.setBuffer(Type.Position, 3, vertices); res.setBuffer(Type.TexCoord, 2, textCoord); res.setBuffer(Type.Normal, 3, normals); res.setBuffer(Type.Index, 3, indices); res.updateBound(); return res; }
Example 17
Source File: TestTextureArrayCompressed.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void simpleInitApp() { Material mat = new Material(assetManager, "jme3test/texture/UnshadedArray.j3md"); for (Caps caps : renderManager.getRenderer().getCaps()) { System.out.println(caps.name()); } if(!renderManager.getRenderer().getCaps().contains(Caps.TextureArray)){ throw new UnsupportedOperationException("Your hardware does not support TextureArray"); } Texture tex1 = assetManager.loadTexture( "Textures/Terrain/Pond/Pond_dxt5.dds"); Texture tex2 = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall_dxt5.dds"); List<Image> images = new ArrayList<Image>(); images.add(tex1.getImage()); images.add(tex2.getImage()); TextureArray tex3 = new TextureArray(images); tex3.setMinFilter(Texture.MinFilter.Trilinear); mat.setTexture("ColorMap", tex3); Mesh m = new Mesh(); Vector3f[] vertices = new Vector3f[8]; vertices[0] = new Vector3f(0, 0, 0); vertices[1] = new Vector3f(3, 0, 0); vertices[2] = new Vector3f(0, 3, 0); vertices[3] = new Vector3f(3, 3, 0); vertices[4] = new Vector3f(3, 0, 0); vertices[5] = new Vector3f(6, 0, 0); vertices[6] = new Vector3f(3, 3, 0); vertices[7] = new Vector3f(6, 3, 0); Vector3f[] texCoord = new Vector3f[8]; texCoord[0] = new Vector3f(0, 0, 0); texCoord[1] = new Vector3f(1, 0, 0); texCoord[2] = new Vector3f(0, 1, 0); texCoord[3] = new Vector3f(1, 1, 0); texCoord[4] = new Vector3f(0, 0, 1); texCoord[5] = new Vector3f(1, 0, 1); texCoord[6] = new Vector3f(0, 1, 1); texCoord[7] = new Vector3f(1, 1, 1); int[] indexes = { 2, 0, 1, 1, 3, 2 , 6, 4, 5, 5, 7, 6}; m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); m.setBuffer(Type.TexCoord, 3, BufferUtils.createFloatBuffer(texCoord)); m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes)); m.updateBound(); Geometry geom = new Geometry("Mesh", m); geom.setMaterial(mat); rootNode.attachChild(geom); }
Example 18
Source File: TestTextureArray.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void simpleInitApp() { Material mat = new Material(assetManager, "jme3test/texture/UnshadedArray.j3md"); for (Caps caps : renderManager.getRenderer().getCaps()) { System.out.println(caps.name()); } if(!renderManager.getRenderer().getCaps().contains(Caps.TextureArray)){ throw new UnsupportedOperationException("Your hardware does not support TextureArray"); } Texture tex1 = assetManager.loadTexture( "Textures/Terrain/Pond/Pond.jpg"); Texture tex2 = assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg"); List<Image> images = new ArrayList<Image>(); images.add(tex1.getImage()); images.add(tex2.getImage()); TextureArray tex3 = new TextureArray(images); tex3.setMinFilter(Texture.MinFilter.Trilinear); mat.setTexture("ColorMap", tex3); Mesh m = new Mesh(); Vector3f[] vertices = new Vector3f[8]; vertices[0] = new Vector3f(0, 0, 0); vertices[1] = new Vector3f(3, 0, 0); vertices[2] = new Vector3f(0, 3, 0); vertices[3] = new Vector3f(3, 3, 0); vertices[4] = new Vector3f(3, 0, 0); vertices[5] = new Vector3f(6, 0, 0); vertices[6] = new Vector3f(3, 3, 0); vertices[7] = new Vector3f(6, 3, 0); Vector3f[] texCoord = new Vector3f[8]; texCoord[0] = new Vector3f(0, 0, 0); texCoord[1] = new Vector3f(1, 0, 0); texCoord[2] = new Vector3f(0, 1, 0); texCoord[3] = new Vector3f(1, 1, 0); texCoord[4] = new Vector3f(0, 0, 1); texCoord[5] = new Vector3f(1, 0, 1); texCoord[6] = new Vector3f(0, 1, 1); texCoord[7] = new Vector3f(1, 1, 1); int[] indexes = { 2, 0, 1, 1, 3, 2 , 6, 4, 5, 5, 7, 6}; m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); m.setBuffer(Type.TexCoord, 3, BufferUtils.createFloatBuffer(texCoord)); m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes)); m.updateBound(); Geometry geom = new Geometry("Mesh", m); geom.setMaterial(mat); rootNode.attachChild(geom); }
Example 19
Source File: PhysicsTestHelper.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
private static Mesh createFloorMesh(int meshDetail, float floorDimensions) { if (meshDetail < 10) { meshDetail = 10; } int numVertices = meshDetail * meshDetail * 2 * 3;//width * depth * two tris * 3 verts per tri int[] indexBuf = new int[numVertices]; int i = 0; for (int x = 0; x < meshDetail; x++) { for (int z = 0; z < meshDetail; z++) { indexBuf[i] = i++; indexBuf[i] = i++; indexBuf[i] = i++; indexBuf[i] = i++; indexBuf[i] = i++; indexBuf[i] = i++; } } float[] vertBuf = new float[numVertices * 3]; float xIncrement = floorDimensions / meshDetail; float zIncrement = floorDimensions / meshDetail; int j = 0; for (int x = 0; x < meshDetail; x++) { float xPos = x * xIncrement; for (int z = 0; z < meshDetail; z++) { float zPos = z * zIncrement; //First tri vertBuf[j++] = xPos; vertBuf[j++] = getY(xPos, zPos, floorDimensions); vertBuf[j++] = zPos; vertBuf[j++] = xPos; vertBuf[j++] = getY(xPos, zPos + zIncrement, floorDimensions); vertBuf[j++] = zPos + zIncrement; vertBuf[j++] = xPos + xIncrement; vertBuf[j++] = getY(xPos + xIncrement, zPos, floorDimensions); vertBuf[j++] = zPos; //Second tri vertBuf[j++] = xPos; vertBuf[j++] = getY(xPos, zPos + zIncrement, floorDimensions); vertBuf[j++] = zPos + zIncrement; vertBuf[j++] = xPos + xIncrement; vertBuf[j++] = getY(xPos + xIncrement, zPos + zIncrement, floorDimensions); vertBuf[j++] = zPos + zIncrement; vertBuf[j++] = xPos + xIncrement; vertBuf[j++] = getY(xPos + xIncrement, zPos, floorDimensions); vertBuf[j++] = zPos; } } Mesh m = new Mesh(); m.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexBuf)); m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertBuf)); m.updateBound(); return m; }
Example 20
Source File: TestCustomMesh.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void simpleInitApp() { Mesh m = new Mesh(); // Vertex positions in space Vector3f [] vertices = new Vector3f[4]; vertices[0] = new Vector3f(0,0,0); vertices[1] = new Vector3f(3,0,0); vertices[2] = new Vector3f(0,3,0); vertices[3] = new Vector3f(3,3,0); // Texture coordinates Vector2f [] texCoord = new Vector2f[4]; texCoord[0] = new Vector2f(0,0); texCoord[1] = new Vector2f(1,0); texCoord[2] = new Vector2f(0,1); texCoord[3] = new Vector2f(1,1); // Indexes. We define the order in which mesh should be constructed short[] indexes = {2, 0, 1, 1, 3, 2}; // Setting buffers m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord)); m.setBuffer(Type.Index, 1, BufferUtils.createShortBuffer(indexes)); m.updateBound(); // ************************************************************************* // First mesh uses one solid color // ************************************************************************* // Creating a geometry, and apply a single color material to it Geometry geom = new Geometry("OurMesh", m); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); geom.setMaterial(mat); // Attaching our geometry to the root node. rootNode.attachChild(geom); // ************************************************************************* // Second mesh uses vertex colors to color each vertex // ************************************************************************* Mesh cMesh = m.clone(); Geometry coloredMesh = new Geometry ("ColoredMesh", cMesh); Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matVC.setBoolean("VertexColor", true); //We have 4 vertices and 4 color values for each of them. //If you have more vertices, you need 'new float[yourVertexCount * 4]' here! float[] colorArray = new float[4*4]; int colorIndex = 0; //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f for(int i = 0; i < 4; i++){ // Red value (is increased by .2 on each next vertex here) colorArray[colorIndex++]= 0.1f+(.2f*i); // Green value (is reduced by .2 on each next vertex) colorArray[colorIndex++]= 0.9f-(0.2f*i); // Blue value (remains the same in our case) colorArray[colorIndex++]= 0.5f; // Alpha value (no transparency set here) colorArray[colorIndex++]= 1.0f; } // Set the color buffer cMesh.setBuffer(Type.Color, 4, colorArray); coloredMesh.setMaterial(matVC); // move mesh a bit so that it doesn't intersect with the first one coloredMesh.setLocalTranslation(4, 0, 0); rootNode.attachChild(coloredMesh); // /** Alternatively, you can show the mesh vertixes as points // * instead of coloring the faces. */ // cMesh.setMode(Mesh.Mode.Points); // cMesh.setPointSize(10f); // cMesh.updateBound(); // cMesh.setStatic(); // Geometry points = new Geometry("Points", m); // points.setMaterial(mat); // rootNode.attachChild(points); // ************************************************************************* // Third mesh will use a wireframe shader to show wireframe // ************************************************************************* Mesh wfMesh = m.clone(); Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh); Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWireframe.setColor("Color", ColorRGBA.Green); matWireframe.getAdditionalRenderState().setWireframe(true); wfGeom.setMaterial(matWireframe); wfGeom.setLocalTranslation(4, 4, 0); rootNode.attachChild(wfGeom); }