Instancing/Scatter example - request for code explanation

Hi,
I’m trying to understand the code under this example: instancing/scatter as found on github
Reproducing it is working quite fine, but I came upon an error and realised I do not understand a part that is happening here:

Why is the loaded geometry copied to prototype?
Is the geometry type being casted here? Why?

Also, I’ve never encountered .prototype.copy,call method, is it like .assign() ?

Thanks for explaining, preferably in simple words :slight_smile:

By the way, the error I get is image
I would like to understand better the logic and methods before I troubleshoot it (unless you may have any idea).

The geometry of _stemMesh and _blossomMesh are of type BufferGeometry. stemGeometry and blossomGeometry are of a more specific type InstancedBufferGeometry. You could write the code like so:

stemGeometry.copy( _stemMesh.geometry );
blossomGeometry.copy( _blossomMesh.geometry );

However, the copy() method of InstancedBufferGeometry would copy and undefined value to instanceCount (since BufferGeometry does not have the property instanceCount),

I suggest you study a bit the semantics of Function.prototype.call() then this approach will become clear. In this particular use case, the syntax THREE.BufferGeometry.prototype.copy.call() means we call BufferGeometry.copy() in context of stemGeometry (meaning this points to stemGeometry) with _stemMesh.geometry as the methods actual parameter. In some sense, it’s like calling the super method of InstancedBufferGeometry.copy().

2 Likes

Thank you, I get it now!