Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InstancedMesh: Support copying/cloning #19455

Closed
wants to merge 2 commits into from
Closed

Conversation

aknoerig
Copy link
Contributor

In order to copy/clone an InstancedMesh succesfully, its additional properties need to be copied, too.

(This commit is untested, but hopefully straightforward enough to not warrant me to setup a full dev/test environment?)

@mrdoob mrdoob added this to the r117 milestone May 25, 2020
@WestLangley
Copy link
Collaborator

This commit is untested

So you filed a PR that you have not tested?

It is based on Mesh.copy() which does not even work correctly. #16224

@Mugen87
Copy link
Collaborator

Mugen87 commented May 25, 2020

I agree that the existing issues with copy() and clone() are a problem. For consistency reasons, I think it's still okay to add InstancedMesh.copy().

@WestLangley
Copy link
Collaborator

For consistency reasons, I think it's still okay to add InstancedMesh.copy()

The method does not work.

@aknoerig
Copy link
Contributor Author

Well, I followed the current pattern and it's working for me.

@Mugen87
Copy link
Collaborator

Mugen87 commented May 26, 2020

The method does not work.

Do you mind elaborating in more detail what exactly does not work? 😇

@WestLangley
Copy link
Collaborator

WestLangley commented May 26, 2020

For example, .copy() does not work.

var mesh1 = new THREE.InstancedMesh(
	new THREE.SphereBufferGeometry( 4, 32, 16 ),
	new THREE.MeshBasicMaterial(),
	1
);

mesh1.setMatrixAt( 0, new THREE.Matrix4() ); // identity
mesh1.position.set( - 5, 0, 0 );
scene.add( mesh1 );

var mesh2 = new THREE.InstancedMesh(
	new THREE.BoxBufferGeometry( 5, 5, 5 ),
	new THREE.MeshNormalMaterial(),
	1
);

mesh2.copy( mesh1 ); // both geometry and material differ from that of mesh1
mesh2.position.set( 5, 0, 0 );
scene.add( mesh2 );

mesh2 = mesh1.clone(); does not work either. In fact it is worse -- the instance matrix is zero.

@Mugen87
Copy link
Collaborator

Mugen87 commented May 26, 2020

Ah yes, now I remember. You're right, the problem was the fact that Mesh.copy() does not honor the material and geometry. The method only works in combination with Mesh.clone() which is obviously strange.

Argh, we should really fix this in Mesh.

@Mugen87
Copy link
Collaborator

Mugen87 commented May 26, 2020

@WestLangley Do you mind reopening #16224?

@WestLangley
Copy link
Collaborator

Well, I followed the current pattern and it's working for me.

@aknoerig Can you please provide the code pattern that works for you?

@aknoerig
Copy link
Contributor Author

I was actually looking for a way to clone an InstancedMesh, with a copied geometry. What worked for me, doing it manually, was this:

let mesh = new THREE.InstancedMesh(
	new THREE.SphereBufferGeometry( 4, 32, 16 ),
	new THREE.MeshBasicMaterial(),
	1
)

let meshClone = mesh.clone()

// add missing properties for InstancedMesh
meshClone.instanceMatrix = mesh.instanceMatrix
meshClone.count = mesh.count

// selectively copy the geometry
meshClone.geometry = mesh.geometry.clone()

Since this worked well for me, I attempted to contribute back and this pull-request is my amateur attempt at that. The pull-request itself is untested, as it looked straightforward enough to me. I didn't realize that there was a philosophical discussion around cloning/copying. Sorry if my patch is actually making things worse or more confusing...!

The way I understood it, is that clone() is always a shallow clone, which makes sense to me. I'd then expect copy() to be a deep clone. Implementation-wise, I expected clone() to internally uses copy() non-recursively.

@WestLangley
Copy link
Collaborator

@aknoerig Thank you for your PR, anyway!

We do request that everyone test their PRs, however. :-)

@mrdoob mrdoob modified the milestones: r117, r118 May 27, 2020
@Mugen87
Copy link
Collaborator

Mugen87 commented May 27, 2020

Closing in favor of #19471.

@Mugen87 Mugen87 closed this May 27, 2020
@Mugen87 Mugen87 removed this from the r118 milestone May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants