[SOLVED] Question about geometry instancing

Hey guys

Noob question! :upside_down_face:

Suppose I have 100 trees of the same model spawned all over the scene in different locations. And these trees are instanced using the InstanceData vertex buffer of the mesh which contains all instances transforms.

AFAIK when instanced, GPU will render all these 100 trees in just one draw call.

Now my question is what if not all of these trees are inside camera view, for example, say only 10 trees are inside camera view. Now does GPU renders only these 10 trees or all of the 100 trees in one draw call?
In other words, is this InstanceData buffer updated each frame based on visible models before sending it to GPU?

Pretty sure: no

Are you using JME’s Instance node stuff or rolling your own buffers. For JME’s InstanceNode or whatever, I’m only reasonably sure (95%). For your own rolled buffers, definitely 1 draw call with all of the instances. That code doesn’t even know what’s in the buffers.

It will draw all of them but the gpu should skip the fragment shader for those that are out of view.

1 Like

And the same for vertex shader?

No,because if you skip the vertex shader you don’t know what is inside the view.

1 Like


myself i thought JME also first check bounding if partially in view before GPU check via vertex shader.

so its kind of problematic then.

1 Like

Can’t it be done by a boundbox checking from JME before it try to render mesh?

Edit: ninja’d

But then instead of just changing the values in an existing buffer JME would have to resize it all the time? Or rearrange the entries at least.

I think just batch your instances spatially. Is this on Android or desktop?

With the current implementation instances are updated every frame anyway. It is possible to implement culling. You’ll have to skip position update for the culled instances and modify the value returned by getActualNumInstances. This will defacto rearrange the entries. No need to resize the buffer.

1 Like

Have not tried it yet in neither of them. My example was just a question to understand how this instancing works in JME.

@RiccardoBlb appreciate your help. Interested to give this a try at some point. Unfortunately, I have not much understanding of underlying stuff so I won’t be able to do it on my own atm.

One more question.

I don’t get one thing here!

It seems after calling InstancedNode.instance() method it creates InstancedGeometry and adds instances into it, but it also keeps the original geometries attached to the parent.

Aren’t they going to be rendered twice? Once by instancing and once as a regular geometry?

I wonder why you include the code that has nothing to do with what you are claiming.

You would have to look deeper. As I recall, there is some weird juggling that happens in the instance node/geometry classes. It has to keep the original Geometry around but it swaps it with the instanced one as some critical point.

I could be wrong but that’s what I remember. I don’t use them personally as I always just roll my own buffers without all of the magic.

1 Like

After looking for 20 minutes I could not find where it happens. So I thought someone may know where it is. :slightly_smiling_face:

Ah, sorry I was posting the wrong link :man_facepalming:, fixed it. You know there are around 20 tabs open in browser :wink:

1 Like

Hah, weakling! :slight_smile: Over 200 and counting… Though, I’m sure this is a decease of sorts…

1 Like
    public boolean checkCulling(Camera cam) {
        if (isGrouped()) {
            return false;
        return super.checkCulling(cam);

in Geometry.java

1 Like

Ah, I see. Thank you so much, bro :slightly_smiling_face: