Skip to content

Commit 02f1613

Browse files
authored
[Fix] Fix to clone functionality of Entity with RenderComponent (playcanvas#2950)
1 parent a727b4d commit 02f1613

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/framework/components/render/system.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,27 @@ class RenderComponentSystem extends ComponentSystem {
7171
}
7272

7373
cloneComponent(entity, clone) {
74-
var i;
75-
var data = {};
7674

77-
for (i = 0; i < _properties.length; i++) {
75+
// copy properties
76+
const data = {};
77+
for (let i = 0; i < _properties.length; i++) {
7878
data[_properties[i]] = entity.render[_properties[i]];
7979
}
8080

81-
// we cannot copy mesh instances, delete them and component recreates them properly
81+
// mesh instances cannot be used this way, remove them and manually clone them later
8282
delete data.meshInstances;
8383

84-
var component = this.addComponent(clone, data);
85-
86-
// TODO: we should copy all relevant meshinstance properties here
87-
if (entity.render) {
88-
var meshInstances = entity.render.meshInstances;
89-
var meshInstancesClone = component.meshInstances;
90-
for (i = 0; i < meshInstances.length && i < meshInstancesClone.length; i++) {
91-
meshInstancesClone[i].mask = meshInstances[i].mask;
92-
meshInstancesClone[i].material = meshInstances[i].material;
93-
meshInstancesClone[i].layer = meshInstances[i].layer;
94-
meshInstancesClone[i].receiveShadow = meshInstances[i].receiveShadow;
95-
}
84+
// clone component
85+
const component = this.addComponent(clone, data);
86+
87+
// clone mesh instances
88+
const srcMeshInstances = entity.render.meshInstances;
89+
const meshes = srcMeshInstances.map(mi => mi.mesh);
90+
component._onSetMeshes(meshes);
91+
92+
// assign materials
93+
for (let m = 0; m < srcMeshInstances.length; m++) {
94+
component.meshInstances[m].material = srcMeshInstances[m].material;
9695
}
9796
}
9897

0 commit comments

Comments
 (0)