Skip to content

Commit 013fd08

Browse files
committed
Merge pull request #426 from InShadow/cleanup-batch-node
Batch node improvements
2 parents 9966505 + 1073eba commit 013fd08

File tree

1 file changed

+33
-94
lines changed

1 file changed

+33
-94
lines changed

jme3-core/src/main/java/com/jme3/scene/BatchNode.java

+33-94
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
*/
3232
package com.jme3.scene;
3333

34-
import com.jme3.export.*;
35-
import com.jme3.material.Material;
36-
import com.jme3.math.Matrix4f;
37-
import com.jme3.math.Vector3f;
38-
import com.jme3.scene.mesh.IndexBuffer;
39-
import com.jme3.util.SafeArrayList;
40-
import com.jme3.util.TempVars;
41-
import java.io.IOException;
4234
import java.nio.Buffer;
4335
import java.nio.FloatBuffer;
4436
import java.util.ArrayList;
@@ -48,13 +40,22 @@
4840
import java.util.logging.Level;
4941
import java.util.logging.Logger;
5042

43+
import com.jme3.collision.Collidable;
44+
import com.jme3.collision.CollisionResults;
45+
import com.jme3.material.Material;
46+
import com.jme3.math.Matrix4f;
47+
import com.jme3.math.Vector3f;
48+
import com.jme3.scene.mesh.IndexBuffer;
49+
import com.jme3.util.SafeArrayList;
50+
import com.jme3.util.TempVars;
51+
5152
/**
5253
* BatchNode holds geometries that are a batched version of all the geometries that are in its sub scenegraph.
5354
* There is one geometry per different material in the sub tree.
5455
* The geometries are directly attached to the node in the scene graph.
5556
* Usage is like any other node except you have to call the {@link #batch()} method once all the geometries have been attached to the sub scene graph and their material set
5657
* (see todo more automagic for further enhancements)
57-
* All the geometries that have been batched are set to {@link CullHint#Always} to not render them.
58+
* All the geometries that have been batched are set to not be rendered - {@link CullHint} is left intact.
5859
* The sub geometries can be transformed as usual, their transforms are used to update the mesh of the geometryBatch.
5960
* Sub geoms can be removed but it may be slower than the normal spatial removing
6061
* Sub geoms can be added after the batch() method has been called but won't be batched and will just be rendered as normal geometries.
@@ -72,7 +73,7 @@ public class BatchNode extends GeometryGroupNode {
7273
*/
7374
protected SafeArrayList<Batch> batches = new SafeArrayList<Batch>(Batch.class);
7475
/**
75-
* a map storing he batches by geometry to quickly acces the batch when updating
76+
* a map for storing the batches by geometry to quickly access the batch when updating
7677
*/
7778
protected Map<Geometry, Batch> batchesByGeom = new HashMap<Geometry, Batch>();
7879
/**
@@ -118,7 +119,6 @@ public void onMeshChange(Geometry geom) {
118119
public void onGeoemtryUnassociated(Geometry geom) {
119120
setNeedsFullRebatch(true);
120121
}
121-
122122

123123
protected Matrix4f getTransformMatrix(Geometry g){
124124
return g.cachedWorldMat;
@@ -166,18 +166,18 @@ protected void updateSubBatch(Geometry bg) {
166166
*/
167167
public void batch() {
168168
doBatch();
169-
//we set the batch geometries to ignore transforms to avoid transforms of parent nodes to be applied twice
169+
//we set the batch geometries to ignore transforms to avoid transforms of parent nodes to be applied twice
170170
for (Batch batch : batches.getArray()) {
171171
batch.geometry.setIgnoreTransform(true);
172172
batch.geometry.setUserData(UserData.JME_PHYSICSIGNORE, true);
173173
}
174174
}
175175

176176
protected void doBatch() {
177-
Map<Material, List<Geometry>> matMap = new HashMap<Material, List<Geometry>>();
177+
Map<Material, List<Geometry>> matMap = new HashMap<Material, List<Geometry>>();
178178
int nbGeoms = 0;
179179

180-
gatherGeomerties(matMap, this, needsFullRebatch);
180+
gatherGeometries(matMap, this, needsFullRebatch);
181181
if (needsFullRebatch) {
182182
for (Batch batch : batches.getArray()) {
183183
batch.geometry.removeFromParent();
@@ -221,7 +221,7 @@ protected void doBatch() {
221221

222222
batch.geometry.setMesh(m);
223223
batch.geometry.getMesh().updateCounts();
224-
batch.geometry.updateModelBound();
224+
batch.geometry.updateModelBound();
225225
batches.add(batch);
226226
}
227227
if (batches.size() > 0) {
@@ -271,7 +271,7 @@ private void unbatchSubGraph(Spatial s) {
271271
}
272272

273273

274-
private void gatherGeomerties(Map<Material, List<Geometry>> map, Spatial n, boolean rebatch) {
274+
private void gatherGeometries(Map<Material, List<Geometry>> map, Spatial n, boolean rebatch) {
275275

276276
if (n instanceof Geometry) {
277277

@@ -304,7 +304,7 @@ private void gatherGeomerties(Map<Material, List<Geometry>> map, Spatial n, bool
304304
if (child instanceof BatchNode) {
305305
continue;
306306
}
307-
gatherGeomerties(map, child, rebatch);
307+
gatherGeometries(map, child, rebatch);
308308
}
309309
}
310310

@@ -319,7 +319,7 @@ private Batch findBatchByMaterial(Material m) {
319319
return null;
320320
}
321321

322-
private boolean isBatch(Spatial s) {
322+
public final boolean isBatch(Spatial s) {
323323
for (Batch batch : batches.getArray()) {
324324
if (batch.geometry == s) {
325325
return true;
@@ -336,9 +336,6 @@ private boolean isBatch(Spatial s) {
336336
*/
337337
@Override
338338
public void setMaterial(Material material) {
339-
// for (Batch batch : batches.values()) {
340-
// batch.geometry.setMaterial(material);
341-
// }
342339
throw new UnsupportedOperationException("Unsupported for now, please set the material on the geoms before batching");
343340
}
344341

@@ -356,74 +353,7 @@ public Material getMaterial() {
356353
Batch b = batches.iterator().next();
357354
return b.geometry.getMaterial();
358355
}
359-
return null;//material;
360-
}
361-
362-
// /**
363-
// * Sets the material to the a specific batch of this BatchNode
364-
// *
365-
// *
366-
// * @param material the material to use for this geometry
367-
// */
368-
// public void setMaterial(Material material,int batchIndex) {
369-
// if (!batches.isEmpty()) {
370-
//
371-
// }
372-
//
373-
// }
374-
//
375-
// /**
376-
// * Returns the material that is used for the first batch of this BatchNode
377-
// *
378-
// * use getMaterial(Material material,int batchIndex) to get a material from a specific batch
379-
// *
380-
// * @return the material that is used for the first batch of this BatchNode
381-
// *
382-
// * @see #setMaterial(com.jme3.material.Material)
383-
// */
384-
// public Material getMaterial(int batchIndex) {
385-
// if (!batches.isEmpty()) {
386-
// Batch b = batches.get(batches.keySet().iterator().next());
387-
// return b.geometry.getMaterial();
388-
// }
389-
// return null;//material;
390-
// }
391-
@Override
392-
public void write(JmeExporter ex) throws IOException {
393-
super.write(ex);
394-
OutputCapsule oc = ex.getCapsule(this);
395-
//
396-
// if (material != null) {
397-
// oc.write(material.getAssetName(), "materialName", null);
398-
// }
399-
// oc.write(material, "material", null);
400-
401-
}
402-
403-
@Override
404-
public void read(JmeImporter im) throws IOException {
405-
super.read(im);
406-
InputCapsule ic = im.getCapsule(this);
407-
408-
409-
// material = null;
410-
// String matName = ic.readString("materialName", null);
411-
// if (matName != null) {
412-
// // Material name is set,
413-
// // Attempt to load material via J3M
414-
// try {
415-
// material = im.getAssetManager().loadMaterial(matName);
416-
// } catch (AssetNotFoundException ex) {
417-
// // Cannot find J3M file.
418-
// logger.log(Level.FINE, "Could not load J3M file {0} for Geometry.",
419-
// matName);
420-
// }
421-
// }
422-
// // If material is NULL, try to load it from the geometry
423-
// if (material == null) {
424-
// material = (Material) ic.readSavable("material", null);
425-
// }
426-
356+
return null;
427357
}
428358

429359
/**
@@ -494,7 +424,7 @@ private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
494424
if (mode != null && mode != listMode) {
495425
throw new UnsupportedOperationException("Cannot combine different"
496426
+ " primitive types: " + mode + " != " + listMode);
497-
}
427+
}
498428
mode = listMode;
499429
if (mode == Mesh.Mode.Lines) {
500430
if (lineWidth != 1f && listLineWidth != lineWidth) {
@@ -510,8 +440,7 @@ private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
510440
outMesh.setMode(mode);
511441
outMesh.setLineWidth(lineWidth);
512442
if (totalVerts >= 65536) {
513-
// make sure we create an UnsignedInt buffer so
514-
// we can fit all of the meshes
443+
// make sure we create an UnsignedInt buffer so we can fit all of the meshes
515444
formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedInt;
516445
} else {
517446
formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
@@ -733,7 +662,6 @@ private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf, int
733662
}
734663

735664
protected class Batch {
736-
737665
/**
738666
* update the batchesByGeom map for this batch with the given List of geometries
739667
* @param list
@@ -745,7 +673,7 @@ void updateGeomList(List<Geometry> list) {
745673
}
746674
}
747675
}
748-
Geometry geometry;
676+
Geometry geometry;
749677
}
750678

751679
protected void setNeedsFullRebatch(boolean needsFullRebatch) {
@@ -771,4 +699,15 @@ public Node clone(boolean cloneMaterials) {
771699
}
772700
return clone;
773701
}
702+
703+
@Override
704+
public int collideWith(Collidable other, CollisionResults results) {
705+
int total = 0;
706+
for (Spatial child : children.getArray()){
707+
if (!isBatch(child)) {
708+
total += child.collideWith(other, results);
709+
}
710+
}
711+
return total;
712+
}
774713
}

0 commit comments

Comments
 (0)