InstancedMesh: duplicated call to refreshBoundingInfo in clone()

In InstancedMesh.clone(), it calls refreshBoundingInfo after the cloned instance constructed.

But createInstance , which internally called Mesh._instancedMeshFactory, which called the InstancedMesh constructor, where refreshBoundingInfo is called when needed.

Since refreshBoundingInfo is a heavy option that iterates every vertex from source mesh, this duplicated call should be avoided.

Also link the previous thread about this here:

A quick blame shows that this call origins 11 years ago where the InstancedMesh was created.

1 Like

Great catch and nice analysis! Would you be up for creating a PR? That would be awesome. :wink:

I would propose a simple removal of that call, I’ll submit a PR if that’s ok, or we can discuss further here.

Let’s dig deeper:

You propose to remove to call this.refreshBoundingInfo() at line 582 if I am right.

I’m not sure whether it’s not a safety call to prepare the BB before cloning the mesh/meshes. The vertex data might have been altered without calling this function on the source mesh. I’m also not sure whether removing it could affect the cloning process (with incorrect BB). @kzhsw did you do some tests already?

On the other hand clone was always considered as a CPU heavy operation, so the programmer should be aware of this.

Maybe you could add a parameter to the clone funtion called doNotRefreshBoundingInfo: boolean to give the programmer better control of the clone function.

EDIT: maybe this could be a better option so we don’t end up with too many parameters and we don’t break backwards compatibility:

public override clone(name: string, newParent: Nullable = null, doNotCloneChildrenOrOptions?: boolean | { doNotCloneChildren?: boolean, doNotRefreshBoundingInfo?: boolean }, newSourceMesh?: Mesh): InstancedMesh 

@Deltakosh @sebavan Any thoughts?

I would agree with @roland here

1 Like

So do you know the original reason to do a full rebuild of bounding info from vertex here?

let me check deeply :smiley:

After further checking, I think we are fine to go

I’ll let all the PR tests run to be sure though:

Remove unnecessary BBox computation by deltakosh · Pull Request #16996 · BabylonJS/Babylon.js

1 Like