4040import com .jme3 .math .Matrix3f ;
4141import com .jme3 .math .Matrix4f ;
4242import com .jme3 .math .Quaternion ;
43+ import com .jme3 .renderer .Camera ;
44+ import com .jme3 .renderer .Camera .FrustumIntersect ;
4345import com .jme3 .scene .Geometry ;
4446import com .jme3 .scene .Spatial ;
4547import com .jme3 .scene .VertexBuffer ;
@@ -63,6 +65,8 @@ public class InstancedGeometry extends Geometry {
6365 private Geometry [] geometries = new Geometry [1 ];
6466
6567 private int firstUnusedIndex = 0 ;
68+ private int numCulledGeometries = 0 ;
69+ private Camera cam ;
6670
6771 public InstancedGeometry () {
6872 super ();
@@ -208,7 +212,7 @@ public int getMaxNumInstances() {
208212 }
209213
210214 public int getActualNumInstances () {
211- return firstUnusedIndex ;
215+ return firstUnusedIndex - numCulledGeometries ;
212216 }
213217
214218 private void swap (int idx1 , int idx2 ) {
@@ -250,6 +254,7 @@ public void updateInstances() {
250254 fb .limit (fb .capacity ());
251255 fb .position (0 );
252256
257+ numCulledGeometries = 0 ;
253258 TempVars vars = TempVars .get ();
254259 {
255260 float [] temp = vars .matrixWrite ;
@@ -271,6 +276,19 @@ public void updateInstances() {
271276 }
272277 }
273278
279+ if (cam != null ) {
280+ BoundingVolume bv = geom .getWorldBound ();
281+ int save = cam .getPlaneState ();
282+ cam .setPlaneState (0 );
283+ FrustumIntersect intersect = cam .contains (bv );
284+ cam .setPlaneState (save );
285+
286+ if (intersect == FrustumIntersect .Outside ) {
287+ numCulledGeometries ++;
288+ continue ;
289+ }
290+ }
291+
274292 Matrix4f worldMatrix = geom .getWorldMatrix ();
275293 updateInstance (worldMatrix , temp , 0 , vars .tempMat3 , vars .quat1 );
276294 fb .put (temp );
@@ -280,7 +298,7 @@ public void updateInstances() {
280298
281299 fb .flip ();
282300
283- if (fb .limit () / INSTANCE_SIZE != firstUnusedIndex ) {
301+ if (fb .limit () / INSTANCE_SIZE != ( firstUnusedIndex - numCulledGeometries ) ) {
284302 throw new AssertionError ();
285303 }
286304
@@ -368,6 +386,12 @@ public VertexBuffer[] getAllInstanceData() {
368386 return allData .toArray (new VertexBuffer [allData .size ()]);
369387 }
370388
389+ @ Override
390+ public boolean checkCulling (Camera cam ) {
391+ this .cam = cam ;
392+ return super .checkCulling (cam );
393+ }
394+
371395 /**
372396 * Called internally by com.jme3.util.clone.Cloner. Do not call directly.
373397 */
0 commit comments