Hey! I am developing a bike racing game in which i want to destroy the objects which are left behind as bike moves forward.
I was wondering if i can do it using any constraint of camera field of view or viewPoint?
Hi @saif,
If you have have frustum culling enabled in your camera, then you can subscribe to the onPostCull layer callback and check the visibleThisFrame property on each mesh instance to determine if it’s visible or not.
Check this post for more details:
Thanks @Leonidas it worked. Just a small question, i have many cars and trucks in the scene so i have to bind each entity in order to check its visibility? Or there is any other property of camera that returns all the instances its rendering currently?
Good question, the onPostCull callback works on the layer level. So you can get a list of all visible mesh instances (and consequently entities) on a layer by doing something like this:
var worldLayer = this.app.scene.layers.getLayerByName("World");
worldLayer.onPostCull = (cameraIndex) => {
var visibleMeshInstances = [];
worldLayer.opaqueMeshInstances.forEach( function(meshInstance){
if( meshInstance.visibleThisFrame === true){
visibleMeshInstances.push(meshInstance);
}
});
}
This will return all opaque mesh instances of the world layer that are visible. From there you can find the entity that is holding the related model component, and tag it as still visible.
The opaqueMeshInstances property isn’t documented (much like the transparentMeshInstances), not sure if there is a better way to get a list of all mesh instances.
Of course I’d say it will be a much better practice to create a list of your entities that you would like to test against, using tags (car, truck etc). So you loop through these entities only and if all of their mesh instances aren’t visible, then it’s hidden from the camera and you can destroy it.
Thanks for comprehensive reply,just need to ask about the solution you proposed, don’t you think it will so costly to check each entities mesh instances on each frame. Like if there are 20 vehicles, all those entities mesh instances will be checked in each frame whether they are visible or not which will be a costly process?
Good thinking ahead! Don’t worry about these checks though, they are simple true/false checks against a property that has already been calculated. For this to get slightly slow, you will have to be checking against thousands of instances.
Something similar is happening already on the engine level for frustum culling to be working.
I think if you would like to micro manage this, you could try to hook your way in some lists that are already available on each layer (private properties), used by frustum culling:
I had a similar need in removing entities. The way I did it was that after I cull, I save the current camera position. Then, during the gameplay my camera moves. After the distance difference reaches some value, I do the cull check to return the non-visible objects to the pool. This way, each frame I only test 1 number - the distance from the previous cull.
Edit: suggestion from Leonidas with subscribing the internal properties of the layer, would probably give you more immediate results, as you can remove the entities on the next frame it gets out of camera. In my approach the entity would still live until the camera moves far enough.
That’s a good suggestion @LeXXik, thanks for sharing.
I would say if your project uses frustum culling, using the visibleInFrame property is super cheap, since it was calculated already.
But if you aren’t using frustum culling (which indeed comes with a CPU overhead), then game specific solutions like doing a distance check can work great.