/** * Optimizes a scene by combining Geometry with the same material. * All Geometries found in the scene are detached from their parent and * a new Node containing the optimized Geometries is attached. * @param scene The scene to optimize * @return The newly created optimized geometries attached to a node */ public static Spatial optimize(Node scene) { return optimize(scene, false); }
public static void main(String[] args) { Mesh mesh = new Mesh(); mesh.setBuffer(Type.Position, 3, new float[]{ 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 }); mesh.setBuffer(Type.Index, 2, new short[]{ 0, 1, 1, 2, 2, 3, 3, 0 }); Geometry g1 = new Geometry("g1", mesh); ArrayList<Geometry> geoms = new ArrayList<Geometry>(); geoms.add(g1); Mesh outMesh = new Mesh(); mergeGeometries(geoms, outMesh); printMesh(outMesh); }
/** * Optimizes a scene by combining Geometry with the same material. * All Geometries found in the scene are detached from their parent and * a new Node containing the optimized Geometries is attached. * @param scene The scene to optimize * @param useLods true if you want the resulting geometry to keep lod information * @return The newly created optimized geometries attached to a node */ public static Node optimize(Node scene, boolean useLods) { ArrayList<Geometry> geoms = new ArrayList<Geometry>(); gatherGeoms(scene, geoms); List<Geometry> batchedGeoms = makeBatches(geoms, useLods); for (Geometry geom : batchedGeoms) { scene.attachChild(geom); } for (Iterator<Geometry> it = geoms.iterator(); it.hasNext();) { Geometry geometry = it.next(); geometry.removeFromParent(); } // Since the scene is returned unaltered the transform must be reset scene.setLocalTransform(Transform.IDENTITY); return scene; }
List<Geometry> geomsForMat = entry.getValue(); Mesh mesh = new Mesh(); mergeGeometries(geomsForMat, mesh); makeLods(geomsForMat, mesh);
GeometryBatchFactory.gatherGeoms(spat, geometries); TextureAtlas atlas = createAtlas(spat, atlasSize); if (atlas == null) { GeometryBatchFactory.mergeGeometries(geometries, mesh); applyAtlasCoords(geometries, mesh, atlas); mesh.updateCounts();
public static void gatherGeoms(Spatial scene, List<Geometry> geoms) { if (scene instanceof Node) { Node node = (Node) scene; for (Spatial child : node.getChildren()) { gatherGeoms(child, geoms); } } else if (scene instanceof Geometry) { geoms.add((Geometry) scene); } }
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Normal.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Tangent.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); int components = inBuf.getNumComponents(); doTransformTangents(inPos, globalVertIndex, components, outPos, worldMatrix); } else { inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
public Mesh makeCompositeMesh(List<MeshComponent> components) { List geoCollection = new ArrayList<Geometry>(); for (MeshComponent component : components) { Geometry componentGeometry = new Geometry(null, component.myMesh); componentGeometry.setLocalRotation(component.myOrientation); componentGeometry.setLocalTranslation(component.myOffset); geoCollection.add(componentGeometry); } Mesh newMesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geoCollection, newMesh); return newMesh; }
FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Normal.ordinal() == bufType || Type.Tangent.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); } else { for (int vert = 0; vert < geomVertCount; vert++) {
public static List<Geometry> makeBatches(Collection<Geometry> geometries) { return makeBatches(geometries, false); }
GeometryBatchFactory.gatherGeoms(spatial, geometries); Mesh outMesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geometries, outMesh);
List<Geometry> geomsForMat = entry.getValue(); Mesh mesh = new Mesh(); mergeGeometries(geomsForMat, mesh); makeLods(geomsForMat, mesh);
/** * Create a texture atlas for the given root node, containing DiffuseMap, NormalMap and SpecularMap. * @param root The rootNode to create the atlas for. * @param atlasSize The size of the atlas (width and height). * @return Null if the atlas cannot be created because not all textures fit. */ public static TextureAtlas createAtlas(Spatial root, int atlasSize) { List<Geometry> geometries = new ArrayList<Geometry>(); GeometryBatchFactory.gatherGeoms(root, geometries); TextureAtlas atlas = new TextureAtlas(atlasSize, atlasSize); for (Geometry geometry : geometries) { if (!atlas.addGeometry(geometry)) { logger.log(Level.WARNING, "Texture atlas size too small, cannot add all textures"); return null; } } return atlas; }
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Normal.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Tangent.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); int components = inBuf.getNumComponents(); doTransformTangents(inPos, globalVertIndex, components, outPos, worldMatrix); } else { inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
public static List<Geometry> makeBatches(Collection<Geometry> geometries) { return makeBatches(geometries, false); }
GeometryBatchFactory.optimize(rootNode, true); LodControl control = new LodControl(); rootNode.getChild(0).addControl(control);
GeometryBatchFactory.gatherGeoms(spatial, geometries); Mesh outMesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geometries, outMesh);
public static void main(String[] args) { Mesh mesh = new Mesh(); mesh.setBuffer(Type.Position, 3, new float[]{ 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 }); mesh.setBuffer(Type.Index, 2, new short[]{ 0, 1, 1, 2, 2, 3, 3, 0 }); Geometry g1 = new Geometry("g1", mesh); ArrayList<Geometry> geoms = new ArrayList<Geometry>(); geoms.add(g1); Mesh outMesh = new Mesh(); mergeGeometries(geoms, outMesh); printMesh(outMesh); } }
List<Geometry> geomsForMat = entry.getValue(); Mesh mesh = new Mesh(); mergeGeometries(geomsForMat, mesh); makeLods(geomsForMat, mesh);
/** * Optimizes a scene by combining Geometry with the same material. * All Geometries found in the scene are detached from their parent and * a new Node containing the optimized Geometries is attached. * @param scene The scene to optimize * @param useLods true if you want the resulting geometry to keep lod information * @return The newly created optimized geometries attached to a node */ public static Node optimize(Node scene, boolean useLods) { ArrayList<Geometry> geoms = new ArrayList<Geometry>(); gatherGeoms(scene, geoms); List<Geometry> batchedGeoms = makeBatches(geoms, useLods); for (Geometry geom : batchedGeoms) { scene.attachChild(geom); } for (Iterator<Geometry> it = geoms.iterator(); it.hasNext();) { Geometry geometry = it.next(); geometry.removeFromParent(); } // Since the scene is returned unaltered the transform must be reset scene.setLocalTransform(Transform.IDENTITY); return scene; }