* Added InstancedNode: easy to use instancing with similar API to BatchNode. The underlying scene graph will be automatically optimized so instancing is used as much as possible, thus reducing number of draw calls and improving performance. Unlike BatchNode, it does not copy the geometry's mesh around, but only its transform. In order for it to work, it requires the Renderer to support the GeometryInstancing capability.
* Replaced existing instancing test with TestInstanceNode, which demostrates how to use the new InstancedNode by changing the transform, mesh, and material of every instance periodically. * The lower-level InstancedGeometry API rewritten: Users don't need to manage the number of instances they have and their indices. Instead, they can use addInstance() and removeInstance() to add and remove instances as desired. Unlike InstancedNode, InstancedGeometry requires all Geometries to have the same mesh and material, but they can have different transforms. * Instancing.glsllib now requires the InstanceData to have world transforms instead of world view transforms. As a consequence, users of instancing must specify in the material the world parameters ViewProjectionMatrix and ViewMatrix instead of ProjectionMatrix.experimental
parent
52b93ba933
commit
eee43b470e
@ -0,0 +1,259 @@ |
|||||||
|
package com.jme3.scene.instancing; |
||||||
|
|
||||||
|
import com.jme3.material.Material; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.scene.BatchNode; |
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
import com.jme3.scene.GeometryGroupNode; |
||||||
|
import com.jme3.scene.Mesh; |
||||||
|
import com.jme3.scene.Node; |
||||||
|
import com.jme3.scene.Spatial; |
||||||
|
import com.jme3.scene.UserData; |
||||||
|
import com.jme3.scene.control.AbstractControl; |
||||||
|
import com.jme3.scene.control.Control; |
||||||
|
import java.util.HashMap; |
||||||
|
|
||||||
|
public class InstancedNode extends GeometryGroupNode { |
||||||
|
|
||||||
|
static int getGeometryStartIndex2(Geometry geom) { |
||||||
|
return getGeometryStartIndex(geom); |
||||||
|
} |
||||||
|
|
||||||
|
static void setGeometryStartIndex2(Geometry geom, int startIndex) { |
||||||
|
setGeometryStartIndex(geom, startIndex); |
||||||
|
} |
||||||
|
|
||||||
|
private static class InstanceTypeKey implements Cloneable { |
||||||
|
|
||||||
|
Mesh mesh; |
||||||
|
Material material; |
||||||
|
int lodLevel; |
||||||
|
|
||||||
|
public InstanceTypeKey(Mesh mesh, Material material, int lodLevel) { |
||||||
|
this.mesh = mesh; |
||||||
|
this.material = material; |
||||||
|
this.lodLevel = lodLevel; |
||||||
|
} |
||||||
|
|
||||||
|
public InstanceTypeKey(){ |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
int hash = 3; |
||||||
|
hash = 41 * hash + this.mesh.hashCode(); |
||||||
|
hash = 41 * hash + this.material.hashCode(); |
||||||
|
hash = 41 * hash + this.lodLevel; |
||||||
|
return hash; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object obj) { |
||||||
|
final InstanceTypeKey other = (InstanceTypeKey) obj; |
||||||
|
if (this.mesh != other.mesh) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (this.material != other.material) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (this.lodLevel != other.lodLevel) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public InstanceTypeKey clone() { |
||||||
|
try { |
||||||
|
return (InstanceTypeKey) super.clone(); |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class InstancedNodeControl extends AbstractControl { |
||||||
|
|
||||||
|
private InstancedNode node; |
||||||
|
|
||||||
|
public InstancedNodeControl() { |
||||||
|
} |
||||||
|
|
||||||
|
public InstancedNodeControl(InstancedNode node) { |
||||||
|
this.node = node; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Control cloneForSpatial(Spatial spatial) { |
||||||
|
return this; |
||||||
|
// WARNING: Sets wrong control on spatial. Will be
|
||||||
|
// fixed automatically by InstancedNode.clone() method.
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlUpdate(float tpf) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) { |
||||||
|
node.renderFromControl(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected final HashMap<Geometry, InstancedGeometry> igByGeom |
||||||
|
= new HashMap<Geometry, InstancedGeometry>(); |
||||||
|
|
||||||
|
private final InstanceTypeKey lookUp = new InstanceTypeKey(); |
||||||
|
|
||||||
|
private final HashMap<InstanceTypeKey, InstancedGeometry> instancesMap = |
||||||
|
new HashMap<InstanceTypeKey, InstancedGeometry>(); |
||||||
|
|
||||||
|
public InstancedNode() { |
||||||
|
super(); |
||||||
|
// NOTE: since we are deserializing,
|
||||||
|
// the control is going to be added automatically here.
|
||||||
|
} |
||||||
|
|
||||||
|
public InstancedNode(String name) { |
||||||
|
super(name); |
||||||
|
addControl(new InstancedNodeControl(this)); |
||||||
|
} |
||||||
|
|
||||||
|
private void renderFromControl() { |
||||||
|
for (InstancedGeometry ig : instancesMap.values()) { |
||||||
|
ig.updateInstances(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static boolean isInstancedGeometry(Geometry geom) { |
||||||
|
return geom instanceof InstancedGeometry; |
||||||
|
} |
||||||
|
|
||||||
|
private InstancedGeometry lookUpByGeometry(Geometry geom) { |
||||||
|
lookUp.mesh = geom.getMesh(); |
||||||
|
lookUp.material = geom.getMaterial(); |
||||||
|
lookUp.lodLevel = geom.getLodLevel(); |
||||||
|
|
||||||
|
InstancedGeometry ig = instancesMap.get(lookUp); |
||||||
|
|
||||||
|
if (ig == null) { |
||||||
|
ig = new InstancedGeometry( |
||||||
|
"material-" + lookUp.material.getMaterialDef().getName() + "," |
||||||
|
+ "lod-" + lookUp.lodLevel); |
||||||
|
ig.setMaterial(lookUp.material); |
||||||
|
ig.setMesh(lookUp.mesh); |
||||||
|
ig.setUserData(UserData.JME_PHYSICSIGNORE, true); |
||||||
|
ig.setCullHint(CullHint.Never); |
||||||
|
instancesMap.put(lookUp.clone(), ig); |
||||||
|
attachChild(ig); |
||||||
|
} |
||||||
|
|
||||||
|
return ig; |
||||||
|
} |
||||||
|
|
||||||
|
private void removeFromInstancedGeometry(Geometry geom) { |
||||||
|
InstancedGeometry ig = igByGeom.remove(geom); |
||||||
|
if (ig != null) { |
||||||
|
ig.deleteInstance(geom); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void ungroupSceneGraph(Spatial s) { |
||||||
|
if (s instanceof Node) { |
||||||
|
for (Spatial sp : ((Node) s).getChildren()) { |
||||||
|
ungroupSceneGraph(sp); |
||||||
|
} |
||||||
|
} else if (s instanceof Geometry) { |
||||||
|
Geometry g = (Geometry) s; |
||||||
|
if (g.isGrouped()) { |
||||||
|
// Will invoke onGeometryUnassociated automatically.
|
||||||
|
g.unassociateFromGroupNode(); |
||||||
|
if (InstancedNode.getGeometryStartIndex(g) != -1) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Spatial detachChildAt(int index) { |
||||||
|
Spatial s = super.detachChildAt(index); |
||||||
|
if (s instanceof Node) { |
||||||
|
ungroupSceneGraph(s); |
||||||
|
} |
||||||
|
return s; |
||||||
|
} |
||||||
|
|
||||||
|
private void instance(Spatial n) { |
||||||
|
if (n instanceof Geometry) { |
||||||
|
Geometry g = (Geometry) n; |
||||||
|
if (!g.isGrouped() && g.getBatchHint() != BatchHint.Never) { |
||||||
|
InstancedGeometry ig = lookUpByGeometry(g); |
||||||
|
igByGeom.put(g, ig); |
||||||
|
g.associateWithGroupNode(this, 0); |
||||||
|
ig.addInstance(g); |
||||||
|
} |
||||||
|
} else if (n instanceof Node) { |
||||||
|
for (Spatial child : ((Node) n).getChildren()) { |
||||||
|
if (child instanceof GeometryGroupNode) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
instance(child); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void instance() { |
||||||
|
instance(this); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Node clone(boolean cloneMaterials) { |
||||||
|
InstancedNode clone = (InstancedNode)super.clone(cloneMaterials); |
||||||
|
if (instancesMap.size() > 0) { |
||||||
|
// Remove all instanced geometries from the clone
|
||||||
|
for (int i = 0; i < clone.children.size(); i++) { |
||||||
|
if (clone.children.get(i) instanceof InstancedGeometry) { |
||||||
|
clone.children.remove(i); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Clear state (which is incorrect)
|
||||||
|
clone.igByGeom.clear(); |
||||||
|
clone.instancesMap.clear(); |
||||||
|
clone.instance(); |
||||||
|
} |
||||||
|
return clone; |
||||||
|
} |
||||||
|
|
||||||
|
private void majorChange(Geometry geom) { |
||||||
|
InstancedGeometry oldIG = igByGeom.get(geom); |
||||||
|
InstancedGeometry newIG = lookUpByGeometry(geom); |
||||||
|
if (oldIG != newIG) { |
||||||
|
oldIG.deleteInstance(geom); |
||||||
|
newIG.addInstance(geom); |
||||||
|
igByGeom.put(geom, newIG); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onTransformChange(Geometry geom) { |
||||||
|
// Handled automatically
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onMaterialChange(Geometry geom) { |
||||||
|
majorChange(geom); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onMeshChange(Geometry geom) { |
||||||
|
majorChange(geom); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onGeoemtryUnassociated(Geometry geom) { |
||||||
|
removeFromInstancedGeometry(geom); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,186 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2012 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.scene.instancing; |
||||||
|
|
||||||
|
import com.jme3.app.SimpleApplication; |
||||||
|
import com.jme3.material.Material; |
||||||
|
import com.jme3.math.ColorRGBA; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Quaternion; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
import com.jme3.scene.Mesh; |
||||||
|
import com.jme3.scene.Spatial; |
||||||
|
import com.jme3.scene.Spatial.CullHint; |
||||||
|
import com.jme3.scene.instancing.InstancedGeometry; |
||||||
|
import com.jme3.scene.instancing.InstancedNode; |
||||||
|
import com.jme3.scene.shape.Box; |
||||||
|
import com.jme3.scene.shape.Sphere; |
||||||
|
import com.jme3.system.AppSettings; |
||||||
|
|
||||||
|
public class TestInstanceNode extends SimpleApplication { |
||||||
|
|
||||||
|
private Mesh mesh1; |
||||||
|
private Mesh mesh2; |
||||||
|
private final Material[] materials = new Material[6]; |
||||||
|
private InstancedNode instancedNode; |
||||||
|
private float time = 0; |
||||||
|
|
||||||
|
public static void main(String[] args){ |
||||||
|
TestInstanceNode app = new TestInstanceNode(); |
||||||
|
AppSettings settings = new AppSettings(true); |
||||||
|
settings.setVSync(false); |
||||||
|
app.setSettings(settings); |
||||||
|
app.start(); |
||||||
|
} |
||||||
|
|
||||||
|
private Geometry createInstance(float x, float z) { |
||||||
|
Mesh mesh; |
||||||
|
if (FastMath.nextRandomInt(0, 1) == 1) mesh = mesh2; |
||||||
|
else mesh = mesh1; |
||||||
|
Geometry geometry = new Geometry("randomGeom", mesh); |
||||||
|
geometry.setMaterial(materials[FastMath.nextRandomInt(0, materials.length - 1)]); |
||||||
|
geometry.setLocalTranslation(x, 0, z); |
||||||
|
return geometry; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void simpleInitApp() { |
||||||
|
mesh1 = new Sphere(13, 13, 0.4f, true, false); |
||||||
|
mesh2 = new Box(0.4f, 0.4f, 0.4f); |
||||||
|
|
||||||
|
materials[0] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[0].setBoolean("UseInstancing", true); |
||||||
|
materials[0].setColor("Color", ColorRGBA.Red); |
||||||
|
|
||||||
|
materials[1] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[1].setBoolean("UseInstancing", true); |
||||||
|
materials[1].setColor("Color", ColorRGBA.Green); |
||||||
|
|
||||||
|
materials[2] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[2].setBoolean("UseInstancing", true); |
||||||
|
materials[2].setColor("Color", ColorRGBA.Blue); |
||||||
|
|
||||||
|
materials[3] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[3].setBoolean("UseInstancing", true); |
||||||
|
materials[3].setColor("Color", ColorRGBA.Cyan); |
||||||
|
|
||||||
|
materials[4] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[4].setBoolean("UseInstancing", true); |
||||||
|
materials[4].setColor("Color", ColorRGBA.Magenta); |
||||||
|
|
||||||
|
materials[5] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
materials[5].setBoolean("UseInstancing", true); |
||||||
|
materials[5].setColor("Color", ColorRGBA.Yellow); |
||||||
|
|
||||||
|
instancedNode = new InstancedNode("instanced_node"); |
||||||
|
instancedNode.setCullHint(CullHint.Never); |
||||||
|
|
||||||
|
rootNode.attachChild(instancedNode); |
||||||
|
|
||||||
|
int extent = 30; |
||||||
|
|
||||||
|
for (int y = -extent; y < extent; y++) { |
||||||
|
for (int x = -extent; x < extent; x++) { |
||||||
|
Geometry instance = createInstance(x, y); |
||||||
|
|
||||||
|
float height = (smoothstep(0, 1, FastMath.nextRandomFloat()) * 2.5f) - 1.25f; |
||||||
|
instance.setUserData("height", height); |
||||||
|
instance.setUserData("dir", 1f); |
||||||
|
|
||||||
|
instancedNode.attachChild(instance); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
instancedNode.instance(); |
||||||
|
|
||||||
|
cam.setLocation(new Vector3f(38.373516f, 6.689055f, 38.482082f)); |
||||||
|
cam.setRotation(new Quaternion(-0.04004206f, 0.918326f, -0.096310444f, -0.38183528f)); |
||||||
|
flyCam.setMoveSpeed(15); |
||||||
|
//flyCam.setEnabled(false);
|
||||||
|
} |
||||||
|
|
||||||
|
private float smoothstep(float edge0, float edge1, float x) { |
||||||
|
// Scale, bias and saturate x to 0..1 range
|
||||||
|
x = FastMath.clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f); |
||||||
|
// Evaluate polynomial
|
||||||
|
return x * x * (3 - 2 * x); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void simpleUpdate(float tpf) { |
||||||
|
time += tpf; |
||||||
|
|
||||||
|
if (time > 1f) { |
||||||
|
time = 0f; |
||||||
|
|
||||||
|
for (Spatial instance : instancedNode.getChildren()) { |
||||||
|
if (!(instance instanceof InstancedGeometry)) { |
||||||
|
Geometry geom = (Geometry) instance; |
||||||
|
geom.setMaterial(materials[FastMath.nextRandomInt(0, materials.length - 1)]); |
||||||
|
|
||||||
|
Mesh mesh; |
||||||
|
if (FastMath.nextRandomInt(0, 1) == 1) mesh = mesh2; |
||||||
|
else mesh = mesh1; |
||||||
|
geom.setMesh(mesh); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for (Spatial child : instancedNode.getChildren()) { |
||||||
|
if (!(child instanceof InstancedGeometry)) { |
||||||
|
float val = child.getUserData("height"); |
||||||
|
float dir = child.getUserData("dir"); |
||||||
|
|
||||||
|
val += (dir + ((FastMath.nextRandomFloat() * 0.5f) - 0.25f)) * tpf; |
||||||
|
|
||||||
|
if (val > 1f) { |
||||||
|
val = 1f; |
||||||
|
dir = -dir; |
||||||
|
} else if (val < 0f) { |
||||||
|
val = 0f; |
||||||
|
dir = -dir; |
||||||
|
} |
||||||
|
|
||||||
|
Vector3f translation = child.getLocalTranslation(); |
||||||
|
translation.y = (smoothstep(0, 1, val) * 2.5f) - 1.25f; |
||||||
|
|
||||||
|
child.setUserData("height", val); |
||||||
|
child.setUserData("dir", dir); |
||||||
|
|
||||||
|
child.setLocalTranslation(translation); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,146 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2009-2012 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.scene.instancing; |
|
||||||
|
|
||||||
import com.jme3.app.SimpleApplication; |
|
||||||
import com.jme3.input.KeyInput; |
|
||||||
import com.jme3.input.controls.ActionListener; |
|
||||||
import com.jme3.input.controls.AnalogListener; |
|
||||||
import com.jme3.input.controls.KeyTrigger; |
|
||||||
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.Spatial; |
|
||||||
import com.jme3.scene.Spatial.CullHint; |
|
||||||
import com.jme3.scene.instancing.InstancedGeometry; |
|
||||||
import com.jme3.scene.shape.Sphere; |
|
||||||
|
|
||||||
public class TestInstancing extends SimpleApplication { |
|
||||||
|
|
||||||
private InstancedGeometry instancedGeometry; |
|
||||||
private Node instancedGeoms; |
|
||||||
private Material material; |
|
||||||
private boolean enabled = true; |
|
||||||
|
|
||||||
public static void main(String[] args){ |
|
||||||
TestInstancing app = new TestInstancing(); |
|
||||||
//app.setShowSettings(false);
|
|
||||||
//app.setDisplayFps(false);
|
|
||||||
//app.setDisplayStatView(false);
|
|
||||||
app.start(); |
|
||||||
} |
|
||||||
|
|
||||||
private Geometry createInstance(float x, float z) { |
|
||||||
// Note: it doesn't matter what mesh or material we set here.
|
|
||||||
Geometry geometry = new Geometry("randomGeom", instancedGeometry.getMesh()); |
|
||||||
geometry.setMaterial(instancedGeometry.getMaterial()); |
|
||||||
geometry.setLocalTranslation(x, 0, z); |
|
||||||
return geometry; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void simpleInitApp() { |
|
||||||
initInputs(); |
|
||||||
|
|
||||||
Sphere sphere = new Sphere(10, 10, 0.5f, true, false); |
|
||||||
material = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md"); |
|
||||||
material.setBoolean("UseInstancing", true); |
|
||||||
|
|
||||||
instancedGeometry = new InstancedGeometry(InstancedGeometry.Mode.Auto, "instanced_geom"); |
|
||||||
instancedGeometry.setMaxNumInstances(60 * 60); |
|
||||||
instancedGeometry.setCurrentNumInstances(60 * 60); |
|
||||||
instancedGeometry.setCullHint(CullHint.Never); |
|
||||||
instancedGeometry.setMesh(sphere); |
|
||||||
instancedGeometry.setMaterial(material); |
|
||||||
rootNode.attachChild(instancedGeometry); |
|
||||||
|
|
||||||
instancedGeoms = new Node("instances_node"); |
|
||||||
|
|
||||||
// Important: Do not render these geometries, only
|
|
||||||
// use their world transforms to instance them via
|
|
||||||
// InstancedGeometry.
|
|
||||||
instancedGeoms.setCullHint(CullHint.Always); |
|
||||||
|
|
||||||
for (int y = -30; y < 30; y++) { |
|
||||||
for (int x = -30; x < 30; x++) { |
|
||||||
Geometry instance = createInstance(x, y); |
|
||||||
instancedGeoms.attachChild(instance); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
rootNode.attachChild(instancedGeoms); |
|
||||||
rootNode.setCullHint(CullHint.Never); |
|
||||||
|
|
||||||
int instanceIndex = 0; |
|
||||||
for (Spatial child : instancedGeoms.getChildren()) { |
|
||||||
if (instanceIndex < instancedGeometry.getMaxNumInstances()) { |
|
||||||
instancedGeometry.setInstanceTransform(instanceIndex++, child.getWorldTransform()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
instancedGeometry.setCurrentNumInstances(instanceIndex); |
|
||||||
|
|
||||||
cam.setLocation(new Vector3f(38.373516f, 6.689055f, 38.482082f)); |
|
||||||
cam.setRotation(new Quaternion(-0.04004206f, 0.918326f, -0.096310444f, -0.38183528f)); |
|
||||||
flyCam.setMoveSpeed(15); |
|
||||||
} |
|
||||||
|
|
||||||
private void initInputs() { |
|
||||||
inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE)); |
|
||||||
|
|
||||||
ActionListener acl = new ActionListener() { |
|
||||||
|
|
||||||
public void onAction(String name, boolean keyPressed, float tpf) { |
|
||||||
if (name.equals("toggle") && keyPressed) { |
|
||||||
if (enabled) { |
|
||||||
enabled = false; |
|
||||||
instancedGeoms.setCullHint(CullHint.Dynamic); |
|
||||||
instancedGeometry.setCullHint(CullHint.Always); |
|
||||||
material.setBoolean("UseInstancing", false); |
|
||||||
System.out.println("Instancing OFF"); |
|
||||||
} else { |
|
||||||
enabled = true; |
|
||||||
instancedGeoms.setCullHint(CullHint.Always); |
|
||||||
instancedGeometry.setCullHint(CullHint.Never); |
|
||||||
material.setBoolean("UseInstancing", true); |
|
||||||
System.out.println("Instancing ON"); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
inputManager.addListener(acl, "toggle"); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue