Drawing clones of a loaded model works fine until trying to reduce the number of objects.
Regardless of whether or not I use GeometryBatchFactory or BatchNode, no optimized objects are drawn.
It’s like once batched, the optimized Geometry (or the batches) all use cullhint.Always.
Isolated code snippet is below to help recreate the issue.
When using GeometryBatchFactory, the number of children under the ‘models’ node drops from 10 to 1 as expected (merged geometry) but doesn’t get drawn. When using BatchNode, the number of children in BatchNode is set to 11 which seems right (originals set to cullhint=Always plus the merged geometry).
In both cases, even setting the single mesh/only non-original mesh to cullhint.Never does not draw anything.
[java]
for (int i=0; i < models.getChildren().size(); i++) {
Geometry geo = (Geometry)models.getChild(i);
if (!geo.isBatched()) {
geo.setCullHint(Spatial.CullHint.Never);
}
}
[/java]
Suggestions anyone?
[java]
public class Main extends SimpleApplication {
void optimizeTest() {
cam.setLocation(new Vector3f(-235f, 840f, 1497f));
cam.setRotation(new Quaternion(0.06555404f, 0.9252483f, -0.18774521f, 0.32306334f));
cam.lookAtDirection(new Vector3f(0.5732127f, -0.3897781f, -0.72076356f), new Vector3f(0, 1, 0));
cam.setFrustumFar(tilesize * 32);
flyCam.setRotationSpeed(flyCam.getRotationSpeed() * 2);
flyCam.setMoveSpeed(1000);
sun = new DirectionalLight();
amb = new AmbientLight();
Spatial s = assetManager.loadModel(“Models/anymodelyouwant.j3o”);
s.addLight(sun);
s.addLight(amb);
s.setShadowMode(ShadowMode.CastAndReceive);
Node models = new Node(“all_loaded_models”);
BatchNode bn = new BatchNode(“batchNode”);
for (int i=0; i < 10; i++) {
Spatial clone = s.clone(false);
clone.move((i256)+(i32), 0, 0);
clone.setName(“model_clone_”+i);
models.attachChild((Geometry)clone);
}
rootNode.attachChild(models);
System.out.println("Unoptimized models: "+models.getChildren().size());
// Commenting out the line below draws the models
Node optimized = (Node)GeometryBatchFactory.optimize(models);
System.out.println("Original models Node == optimized Node? "+(optimized == models));
System.out.println("Optimized models: "+models.getChildren().size());
}
}
[/java]
[SOLVED] Clones of loaded models: no draw if using GeometryBatchFactory or BatchNode (cullhint=Alway
You have to attach either the “bn” or the “optimized” node to the rootNode.
attaching “models” is useless.
Attaching either the BatchNode or the optimized node to the rootNode does nothing either.
Nothing is drawn.
ok…just to be clear how batchNode works
You create a batch node, attach geometries to it, then call the batch() method of the batchNode, attach it to the rootNode or any child of the rootNode.
From what i see in the previous code posted it can’t work indeed.
I find it funny how people move from wanting to solve their problem to wanting to prove the answering guy wrong ^^
http://www.mikeash.com/getting_answers.html
normen: I tried the advice and it didn’t work, that’s all I’m saying. I had already tried what nehon had replied with.
Attaching the batchNode version of the code below.
Is there something fundamental I’m missing?
[java]
void optimizeTest() {
cam.setLocation(new Vector3f(-235f, 840f, 1497f));
cam.setRotation(new Quaternion(0.06555404f, 0.9252483f, -0.18774521f, 0.32306334f));
cam.lookAtDirection(new Vector3f(0.5732127f, -0.3897781f, -0.72076356f), new Vector3f(0, 1, 0));
cam.setFrustumFar(tilesize * 32);
flyCam.setRotationSpeed(flyCam.getRotationSpeed() * 2);
flyCam.setMoveSpeed(1000);
sun = new DirectionalLight();
amb = new AmbientLight();
Spatial s = assetManager.loadModel(“Models/block.j3o”);
s.addLight(sun);
s.addLight(amb);
s.setShadowMode(ShadowMode.CastAndReceive);
BatchNode bn = new BatchNode(“batchNode”);
for (int i=0; i < 10; i++) {
Spatial clone = s.clone();
clone.move((i256)+(i32), 0, 0);
clone.setName(“model_clone_”+i);
bn.attachChild((Geometry)clone);
}
bn.batch();
rootNode.attachChild(bn);
}
[/java]
ok, maybe a culling issue then. I see you set the frustum far to tilesize * 32, what if you keep the default 1000?
Edit : a test case might be handy
The lights are added directly to the children so I think they will be lost when they are batched. Why not add the lights right to root or at least the batch node?
nehon: Nope, culling is fine when not batching. The difference between batching/not batching is the issue.
pspeed: Well done indeed! That’s exactly it. Adding the lights to root works fine:
[java]
void optimizeTest() {
cam.setLocation(new Vector3f(-235f, 840f, 1497f));
cam.setRotation(new Quaternion(0.06555404f, 0.9252483f, -0.18774521f, 0.32306334f));
cam.lookAtDirection(new Vector3f(0.5732127f, -0.3897781f, -0.72076356f), new Vector3f(0, 1, 0));
cam.setFrustumFar(tilesize * 32);
flyCam.setRotationSpeed(flyCam.getRotationSpeed() * 2);
flyCam.setMoveSpeed(1000);
sun = new DirectionalLight();
amb = new AmbientLight();
Spatial s = assetManager.loadModel(“Models/block.j3o”);
BatchNode bn = new BatchNode(“batchNode”);
for (int i=0; i < 10; i++) {
Spatial clone = s.clone();
clone.move((i256)+(i32), 0, 0);
clone.setName(“model_clone_”+i);
bn.attachChild((Geometry)clone);
}
bn.batch();
rootNode.attachChild(bn);
rootNode.addLight(sun);
rootNode.addLight(amb);
}
[/java]
Now the one object is rendered instead of the 10 objects. Remarkable. Ok I need to remember batching is only for geometry
Thank you all for your time!
nehon: I think a test case is a very good idea for future posts. I’ll remember that, thank you!
@picttarge said:
nehon: I think a test case is a very good idea for future posts. I'll remember that, thank you!
hehe always is ;)
@pspeed nice catch!