GeometryBatchFactory now retain original Lod informations thanks to Rickard
see post : http://jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/lods-for-geometrybatchfactory/ git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7579 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
f42b7e8f83
commit
3f707fb44a
90
engine/src/test/jme3test/stress/TestBatchLod.java
Normal file
90
engine/src/test/jme3test/stress/TestBatchLod.java
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2009-2010 jMonkeyEngine
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package jme3test.stress;
|
||||||
|
|
||||||
|
import com.jme3.app.SimpleApplication;
|
||||||
|
import com.jme3.input.KeyInput;
|
||||||
|
import com.jme3.light.DirectionalLight;
|
||||||
|
import com.jme3.material.Material;
|
||||||
|
import com.jme3.math.Quaternion;
|
||||||
|
import com.jme3.math.Vector3f;
|
||||||
|
import com.jme3.scene.Geometry;
|
||||||
|
import com.jme3.scene.Node;
|
||||||
|
import com.jme3.scene.control.LodControl;
|
||||||
|
import jme3tools.optimize.GeometryBatchFactory;
|
||||||
|
|
||||||
|
public class TestBatchLod extends SimpleApplication {
|
||||||
|
|
||||||
|
private boolean lod = false;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
TestBatchLod app = new TestBatchLod();
|
||||||
|
app.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void simpleInitApp() {
|
||||||
|
// inputManager.registerKeyBinding("USELOD", KeyInput.KEY_L);
|
||||||
|
|
||||||
|
DirectionalLight dl = new DirectionalLight();
|
||||||
|
dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
|
||||||
|
rootNode.addLight(dl);
|
||||||
|
|
||||||
|
Node teapotNode = (Node) assetManager.loadModel("Models/Teapot/Teapot.mesh.xml");
|
||||||
|
Geometry teapot = (Geometry) teapotNode.getChild(0);
|
||||||
|
|
||||||
|
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
|
||||||
|
mat.setFloat("Shininess", 16f);
|
||||||
|
mat.setBoolean("VertexLighting", true);
|
||||||
|
teapot.setMaterial(mat);
|
||||||
|
|
||||||
|
// show normals as material
|
||||||
|
//Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
|
||||||
|
flyCam.setMoveSpeed(5);
|
||||||
|
for (int y = -5; y < 5; y++) {
|
||||||
|
for (int x = -5; x < 5; x++) {
|
||||||
|
Geometry clonePot = teapot.clone();
|
||||||
|
|
||||||
|
//clonePot.setMaterial(mat);
|
||||||
|
clonePot.setLocalTranslation(x * .5f, 0, y * .5f);
|
||||||
|
clonePot.setLocalScale(.15f);
|
||||||
|
clonePot.setMaterial(mat);
|
||||||
|
rootNode.attachChild(clonePot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GeometryBatchFactory.optimize(rootNode, true);
|
||||||
|
LodControl control = new LodControl();
|
||||||
|
rootNode.getChild(0).addControl(control);
|
||||||
|
cam.setLocation(new Vector3f(-1.0748308f, 1.35778f, -1.5380064f));
|
||||||
|
cam.setRotation(new Quaternion(0.18343268f, 0.34531063f, -0.069015436f, 0.9177962f));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,6 @@
|
|||||||
package jme3tools.optimize;
|
package jme3tools.optimize;
|
||||||
|
|
||||||
import com.jme3.font.BitmapText;
|
|
||||||
import com.jme3.material.Material;
|
import com.jme3.material.Material;
|
||||||
import com.jme3.math.FastMath;
|
|
||||||
import com.jme3.math.Matrix4f;
|
import com.jme3.math.Matrix4f;
|
||||||
import com.jme3.math.Vector3f;
|
import com.jme3.math.Vector3f;
|
||||||
import com.jme3.scene.Geometry;
|
import com.jme3.scene.Geometry;
|
||||||
@ -17,10 +15,11 @@ import com.jme3.scene.VertexBuffer.Usage;
|
|||||||
import com.jme3.scene.mesh.IndexBuffer;
|
import com.jme3.scene.mesh.IndexBuffer;
|
||||||
import com.jme3.scene.mesh.VirtualIndexBuffer;
|
import com.jme3.scene.mesh.VirtualIndexBuffer;
|
||||||
import com.jme3.scene.mesh.WrappedIndexBuffer;
|
import com.jme3.scene.mesh.WrappedIndexBuffer;
|
||||||
import com.jme3.scene.shape.Quad;
|
import com.jme3.util.BufferUtils;
|
||||||
import com.jme3.util.IntMap.Entry;
|
import com.jme3.util.IntMap.Entry;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
|
import java.nio.ShortBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -138,8 +137,9 @@ public class GeometryBatchFactory {
|
|||||||
|
|
||||||
// generate output buffers based on retrieved info
|
// generate output buffers based on retrieved info
|
||||||
for (int i = 0; i < compsForBuf.length; i++) {
|
for (int i = 0; i < compsForBuf.length; i++) {
|
||||||
if (compsForBuf[i] == 0)
|
if (compsForBuf[i] == 0) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Buffer data;
|
Buffer data;
|
||||||
if (i == Type.Index.ordinal()) {
|
if (i == Type.Index.ordinal()) {
|
||||||
@ -168,8 +168,9 @@ public class GeometryBatchFactory {
|
|||||||
VertexBuffer inBuf = inMesh.getBuffer(Type.values()[bufType]);
|
VertexBuffer inBuf = inMesh.getBuffer(Type.values()[bufType]);
|
||||||
VertexBuffer outBuf = outMesh.getBuffer(Type.values()[bufType]);
|
VertexBuffer outBuf = outMesh.getBuffer(Type.values()[bufType]);
|
||||||
|
|
||||||
if (outBuf == null)
|
if (outBuf == null) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (Type.Index.ordinal() == bufType) {
|
if (Type.Index.ordinal() == bufType) {
|
||||||
int components = compsForBuf[bufType];
|
int components = compsForBuf[bufType];
|
||||||
@ -209,13 +210,66 @@ public class GeometryBatchFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void makeLods(Collection<Geometry> geometries, Mesh outMesh) {
|
||||||
|
int lodLevels = 0;
|
||||||
|
int[] lodSize = null;
|
||||||
|
int index = 0;
|
||||||
|
for (Geometry g : geometries) {
|
||||||
|
if (lodLevels == 0) {
|
||||||
|
lodLevels = g.getMesh().getNumLodLevels();
|
||||||
|
}
|
||||||
|
if (lodSize == null) {
|
||||||
|
lodSize = new int[lodLevels];
|
||||||
|
}
|
||||||
|
for (int i = 0; i < lodLevels; i++) {
|
||||||
|
lodSize[i] += g.getMesh().getLodLevel(i).getData().capacity();
|
||||||
|
//if( i == 0) System.out.println(index + " " +lodSize[i]);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
int[][] lodData = new int[lodLevels][];
|
||||||
|
for (int i = 0; i < lodLevels; i++) {
|
||||||
|
lodData[i] = new int[lodSize[i]];
|
||||||
|
}
|
||||||
|
VertexBuffer[] lods = new VertexBuffer[lodLevels];
|
||||||
|
int bufferPos[] = new int[lodLevels];
|
||||||
|
//int index = 0;
|
||||||
|
int numOfVertices = 0;
|
||||||
|
int curGeom = 0;
|
||||||
|
for (Geometry g : geometries) {
|
||||||
|
if (numOfVertices == 0) {
|
||||||
|
numOfVertices = g.getVertexCount();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < lodLevels; i++) {
|
||||||
|
ShortBuffer buffer = (ShortBuffer) g.getMesh().getLodLevel(i).getData();
|
||||||
|
buffer.rewind();
|
||||||
|
//System.out.println("buffer: " + buffer.capacity() + " limit: " + lodSize[i] + " " + index);
|
||||||
|
for (int j = 0; j < buffer.capacity(); j++) {
|
||||||
|
lodData[i][bufferPos[i] + j] = buffer.get() + numOfVertices * curGeom;
|
||||||
|
//bufferPos[i]++;
|
||||||
|
}
|
||||||
|
bufferPos[i] += buffer.capacity();
|
||||||
|
}
|
||||||
|
curGeom++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < lodLevels; i++) {
|
||||||
|
lods[i] = new VertexBuffer(Type.Index);
|
||||||
|
lods[i].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(lodData[i]));
|
||||||
|
}
|
||||||
|
System.out.println(lods.length);
|
||||||
|
outMesh.setLodLevels(lods);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Geometry> makeBatches(Collection<Geometry> geometries) {
|
||||||
|
return makeBatches(geometries, false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Batches a collection of Geometries so that all with the same material get combined.
|
* Batches a collection of Geometries so that all with the same material get combined.
|
||||||
* @param geometries The Geometries to combine
|
* @param geometries The Geometries to combine
|
||||||
* @return A List of newly created Geometries, each with a distinct material
|
* @return A List of newly created Geometries, each with a distinct material
|
||||||
*/
|
*/
|
||||||
public static List<Geometry> makeBatches(Collection<Geometry> geometries){
|
public static List<Geometry> makeBatches(Collection<Geometry> geometries, boolean useLods) {
|
||||||
ArrayList<Geometry> retVal = new ArrayList<Geometry>();
|
ArrayList<Geometry> retVal = new ArrayList<Geometry>();
|
||||||
HashMap<Material, List<Geometry>> matToGeom = new HashMap<Material, List<Geometry>>();
|
HashMap<Material, List<Geometry>> matToGeom = new HashMap<Material, List<Geometry>>();
|
||||||
|
|
||||||
@ -234,6 +288,10 @@ public class GeometryBatchFactory {
|
|||||||
List<Geometry> geomsForMat = entry.getValue();
|
List<Geometry> geomsForMat = entry.getValue();
|
||||||
Mesh mesh = new Mesh();
|
Mesh mesh = new Mesh();
|
||||||
mergeGeometries(geomsForMat, mesh);
|
mergeGeometries(geomsForMat, mesh);
|
||||||
|
// lods
|
||||||
|
if (useLods) {
|
||||||
|
makeLods(geomsForMat, mesh);
|
||||||
|
}
|
||||||
mesh.updateCounts();
|
mesh.updateCounts();
|
||||||
mesh.updateBound();
|
mesh.updateBound();
|
||||||
|
|
||||||
@ -263,11 +321,24 @@ public class GeometryBatchFactory {
|
|||||||
* @param scene The scene to optimize
|
* @param scene The scene to optimize
|
||||||
* @return The newly created optimized geometries attached to a node
|
* @return The newly created optimized geometries attached to a node
|
||||||
*/
|
*/
|
||||||
public static Node optimize(Node scene){
|
public static Spatial optimize(Node scene) {
|
||||||
|
return optimize(scene, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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>();
|
ArrayList<Geometry> geoms = new ArrayList<Geometry>();
|
||||||
|
|
||||||
gatherGeoms(scene, geoms);
|
gatherGeoms(scene, geoms);
|
||||||
|
|
||||||
List<Geometry> batchedGeoms = makeBatches(geoms);
|
List<Geometry> batchedGeoms = makeBatches(geoms, useLods);
|
||||||
for (Geometry geom : batchedGeoms) {
|
for (Geometry geom : batchedGeoms) {
|
||||||
scene.attachChild(geom);
|
scene.attachChild(geom);
|
||||||
}
|
}
|
||||||
@ -283,8 +354,9 @@ public class GeometryBatchFactory {
|
|||||||
public static void printMesh(Mesh mesh) {
|
public static void printMesh(Mesh mesh) {
|
||||||
for (int bufType = 0; bufType < Type.values().length; bufType++) {
|
for (int bufType = 0; bufType < Type.values().length; bufType++) {
|
||||||
VertexBuffer outBuf = mesh.getBuffer(Type.values()[bufType]);
|
VertexBuffer outBuf = mesh.getBuffer(Type.values()[bufType]);
|
||||||
if (outBuf == null)
|
if (outBuf == null) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println(outBuf.getBufferType() + ": ");
|
System.out.println(outBuf.getBufferType() + ": ");
|
||||||
for (int vert = 0; vert < outBuf.getNumElements(); vert++) {
|
for (int vert = 0; vert < outBuf.getNumElements(); vert++) {
|
||||||
@ -294,9 +366,10 @@ public class GeometryBatchFactory {
|
|||||||
outBuf.setElementComponent(vert, comp, val);
|
outBuf.setElementComponent(vert, comp, val);
|
||||||
val = outBuf.getElementComponent(vert, comp);
|
val = outBuf.getElementComponent(vert, comp);
|
||||||
str += val;
|
str += val;
|
||||||
if (comp != outBuf.getNumComponents()-1)
|
if (comp != outBuf.getNumComponents() - 1) {
|
||||||
str += ", ";
|
str += ", ";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
str += "]";
|
str += "]";
|
||||||
System.out.println(str);
|
System.out.println(str);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user