Skip to content

Instantly share code, notes, and snippets.

@riccardobl
Created August 29, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardobl/343eb6e659525e179add250421a0842d to your computer and use it in GitHub Desktop.
Save riccardobl/343eb6e659525e179add250421a0842d to your computer and use it in GitHub Desktop.
package tests;
import java.util.ArrayList;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.instancing.InstancedNode;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
public class TestInstanceNode extends SimpleApplication {
//####################################################
private final boolean INSTANCING = true;
private final boolean USE_NODE_PARENT=true;
//####################################################
private Mesh mesh;
private Material material ;
private Node instancedNode;
private ArrayList<Spatial> spatials=new ArrayList<Spatial>();
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() {
Geometry geometry = new Geometry("randomGeom", mesh);
geometry.setMaterial(material);
return geometry;
}
@Override
public void simpleInitApp() {
mesh = new Box(0.4f, 0.4f, 0.4f);
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
if(INSTANCING){
material.setBoolean("UseInstancing", INSTANCING);
}
material.setColor("Color", ColorRGBA.Red);
instancedNode = INSTANCING? new InstancedNode("instanced_node"):new Node("node");
rootNode.attachChild(instancedNode);
int extent = 100;
for (int y = -extent; y < extent; y++) {
for (int x = -extent; x < extent; x++) {
Geometry instance = createInstance();
if(USE_NODE_PARENT){
Node parent=new Node();
parent.attachChild(instance);
parent.setLocalTranslation(x,0,y);
spatials.add(parent);
instancedNode.attachChild(parent);
}else{
instance.setLocalTranslation(x,0,y);
spatials.add(instance);
instancedNode.attachChild(instance);
}
}
}
if (INSTANCING) {
((InstancedNode)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);
}
// float t=.2f;
@Override
public void simpleUpdate(float tpf) {
spatials.remove(0).removeFromParent();
// t-=tpf;
// if(t<=0){
// t=.2f;
// System.out.println("Remove!");
// nodes.remove(0).removeFromParent();
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment