Copy BufferGeometry/Mesh in place

I like to copy a 3dObject in place but all matrixs are getting reset and the copied object is somewhere else in the scene. What do i wrong?

Originalobject:

1. oa {uuid: "900269EB-3D05-4A79-87FC-7596EEC94918", name: "", type: "Mesh", parent: Mr, children: Array(0), …}

  1. castShadow: true
  2. children: []
  3. frustumCulled: true
  4. geometry: ki {uuid: "E495D41C-F204-4D41-A53F-32AFC59FEE0C", name: "", type: "BufferGeometry", index: Li, attributes: {…}, …}
  5. layers: lr {mask: 1}
  6. material: il {uuid: "504CB1D7-717E-42E4-AE85-7584E355F292", name: "gem", type: "MeshPhysicalMaterial", fog: true, blending: 1, …}
  7. matrix: ar

    1. elements: (16) [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
    2. __proto__: Object

  8. matrixAutoUpdate: true
  9. matrixWorld: ar

    1. elements: (16) [1, 0, 0, 0, 0, 2.220446049250313e-16, -1, 0, 0, 1, 2.220446049250313e-16, 0, -12.898780822753906, 10.827563285827638, -0.07714200019836186, 1]
    2. __proto__: Object

  10. matrixWorldNeedsUpdate: false
  11. name: ""
  12. parent: Mr {uuid: "CF28A87B-ADA3-4C46-B52D-46865554F6EB", name: "", type: "Object3D", parent: Ao, children: Array(1), …}
  13. position: Qn {x: 0, y: 0, z: 0}
  14. quaternion: Yn {_x: 0, _y: 0, _z: 0, _w: 1, _onChangeCallback: ƒ}
  15. receiveShadow: false
  16. renderOrder: 0
  17. rotation: cr {_x: 0, _y: 0, _z: 0, _order: "XYZ", _onChangeCallback: ƒ}
  18. scale: Qn {x: 1, y: 1, z: 1}
  19. type: "Mesh"
  20. up: Qn {x: 0, y: 1, z: 0}
  21. userData: {}
  22. uuid: "900269EB-3D05-4A79-87FC-7596EEC94918"
  23. visible: true
  24. drawMode: (...)
  25. eulerOrder: (...)
  26. id: 7
  27. modelViewMatrix: ar

    1. elements: (16) [0.7071067811865477, 0.3545308169249882, -0.6118070773131842, 0, -0.7071067811865475, 0.3545308169249884, -0.6118070773131842, 0, 1.570092458683775e-16, 0.8652258662921498, 0.5013822895745312, 0, -9.175363020262832, -4.547232208794878, -481.92182505610884, 1]
    2. __proto__: Object

  28. normalMatrix: Gn

    1. elements: (9) [0.7071067811865474, 0.35453081692498795, -0.6118070773131841, -0.7071067811865474, 0.3545308169249883, -0.611807077313184, 1.110223024625156e-16, 0.8652258662921495, 0.5013822895745311]
    2. __proto__: Object

  29. useQuaternion: (...)
  30. __proto__: Mr

Copied Object:

1. oa {uuid: "7E7757B9-3B1B-46FD-88D6-4720F13B7125", name: "", type: "Mesh", parent: null, children: Array(0), …}

  1. castShadow: true
  2. children: []
  3. frustumCulled: true
  4. geometry: ki {uuid: "E495D41C-F204-4D41-A53F-32AFC59FEE0C", name: "", type: "BufferGeometry", index: Li, attributes: {…}, …}
  5. layers: lr {mask: 1}
  6. material: Rr {uuid: "74992A47-B0AA-4218-8575-37DC3C62E0DA", name: "", type: "MeshBasicMaterial", fog: true, blending: 1, …}
  7. matrix: ar {elements: Array(16)}
  8. matrixAutoUpdate: true
  9. matrixWorld: ar {elements: Array(16)}
  10. matrixWorldNeedsUpdate: false
  11. name: ""
  12. parent: On

    1. autoUpdate: true
    2. background: wr {r: 1, g: 1, b: 1}
    3. castShadow: false
    4. children: (4) [vh, Ao, Mr, oa]
    5. environment: Yi {uuid: "6CC4BA54-AE89-40D9-9FE8-119DC6E528A7", name: "", image: Array(6), mipmaps: Array(0), mapping: 301, …}
    6. fog: null
    7. frustumCulled: true
    8. layers: vn {mask: 1}
    9. matrix: dn

      1. elements: (16) [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
      2. __proto__: Object

    10. matrixAutoUpdate: true
    11. matrixWorld: dn

      1. elements: (16) [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
      2. __proto__: Object

    12. matrixWorldNeedsUpdate: false
    13. name: ""
    14. overrideMaterial: null
    15. parent: null
    16. position: an {x: 0, y: 0, z: 0}
    17. quaternion: tn {_x: 0, _y: 0, _z: 0, _w: 1, _onChangeCallback: ƒ}
    18. receiveShadow: false
    19. renderOrder: 0
    20. rotation: gn {_x: 0, _y: 0, _z: 0, _order: "XYZ", _onChangeCallback: ƒ}
    21. scale: an {x: 1, y: 1, z: 1}
    22. type: "Scene"
    23. up: an {x: 0, y: 1, z: 0}
    24. userData: {}
    25. uuid: "9CFDFDF4-01A6-4653-ACBB-9231190581EF"
    26. visible: true
    27. _listeners: {dispose: Array(2)}
    28. eulerOrder: (...)
    29. id: 6
    30. modelViewMatrix: dn

      1. elements: (16) [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
      2. __proto__: Object

    31. normalMatrix: Yt

      1. elements: (9) [1, 0, 0, 0, 1, 0, 0, 0, 1]
      2. __proto__: Object

    32. useQuaternion: (...)
    33. __proto__: Cn

  13. position: Qn {x: 0, y: 0, z: 0}
  14. quaternion: Yn {_x: 0, _y: 0, _z: 0, _w: 1, _onChangeCallback: ƒ}
  15. receiveShadow: false
  16. renderOrder: 0
  17. rotation: cr {_x: -0, _y: 0, _z: -0, _order: "XYZ", _onChangeCallback: ƒ}
  18. scale: Qn {x: 1.05, y: 1.05, z: 1.05}
  19. type: "Mesh"
  20. up: Qn {x: 0, y: 1, z: 0}
  21. userData: {}
  22. uuid: "7E7757B9-3B1B-46FD-88D6-4720F13B7125"
  23. visible: true
  24. drawMode: (...)
  25. eulerOrder: (...)
  26. id: 28
  27. modelViewMatrix: ar {elements: Array(16)}
  28. normalMatrix: Gn {elements: Array(9)}
  29. useQuaternion: (...)
  30. __proto__: Mr

Code:

let nMesh = oldMesh.clone();
nMesh.material = outlineMaterial;
nMesh.material.needsUpdate = true;
nMesh.matrix.set(oldMesh.matrix);
nMesh.matrixWorld.set(oldMesh.matrixWorld);
nMesh.matrixWorldNeedsUpdate = true;

If you’d like to clone an object and add it to a different parent while having it retain the same position in the world look into using the Object3D.attach function.

1 Like

set() does expected a series of numbers representing the matrix elements. You can’t use a matrix as an argument.

Thanks for helping but i really don’t get the basic. If i copy a object and attach it to the same parent i expect that the object is on the same place. Therefor i have stripped down the code to this:

let oldMesh = group.children[0].children[0];
console.log(oldMesh);
let nMesh = oldMesh.clone();
nMesh.material = outlineMaterial;
nMesh.material.needsUpdate = true;
group.children[0].attach(nMesh);
console.log(nMesh);

but the new mesh is on a complete different place.

Old Mesh:

1. oa

  1. castShadow: true
  2. children: []
  3. frustumCulled: true
  4. geometry: ki {uuid: "FAB188C3-A648-43AA-935B-76A0DA0F95E7", name: "", type: "BufferGeometry", index: Li, attributes: {…}, …}
  5. layers: lr {mask: 1}
  6. material: il {uuid: "E2977014-C8B0-408B-9683-1C54C0E75D3F", name: "gem", type: "MeshPhysicalMaterial", fog: true, blending: 1, …}
  7. matrix: ar {elements: Array(16)}
  8. matrixAutoUpdate: true
  9. matrixWorld: ar {elements: Array(16)}
  10. matrixWorldNeedsUpdate: false
  11. name: ""
  12. parent: Mr {uuid: "FB1D8BF2-27BD-4B3B-9B23-A40447555E9B", name: "", type: "Object3D", parent: Ao, children: Array(2), …}
  13. position: Qn {x: 0, y: 0, z: 0}
  14. quaternion: Yn {_x: 0, _y: 0, _z: 0, _w: 1, _onChangeCallback: ƒ}
  15. receiveShadow: false
  16. renderOrder: 0
  17. rotation: cr {_x: 0, _y: 0, _z: 0, _order: "XYZ", _onChangeCallback: ƒ}
  18. scale: Qn {x: 1, y: 1, z: 1}
  19. type: "Mesh"
  20. up: Qn {x: 0, y: 1, z: 0}
  21. userData: {}
  22. uuid: "BF792C98-D776-4614-A468-8DFE88CA36AE"
  23. visible: true
  24. drawMode: (...)
  25. eulerOrder: (...)
  26. id: 7
  27. modelViewMatrix: ar {elements: Array(16)}
  28. normalMatrix: Gn {elements: Array(9)}
  29. useQuaternion: (...)
  30. __proto__: Mr

new Mesh:

1. oa

  1. castShadow: true
  2. children: []
  3. frustumCulled: true
  4. geometry: ki {uuid: "FAB188C3-A648-43AA-935B-76A0DA0F95E7", name: "", type: "BufferGeometry", index: Li, attributes: {…}, …}
  5. layers: lr {mask: 1}
  6. material: Rr {uuid: "052BA9F5-951D-48C9-B1E7-81C555A65E26", name: "", type: "MeshBasicMaterial", fog: true, blending: 1, …}
  7. matrix: ar {elements: Array(16)}
  8. matrixAutoUpdate: true
  9. matrixWorld: ar {elements: Array(16)}
  10. matrixWorldNeedsUpdate: false
  11. name: ""
  12. parent: Mr {uuid: "FB1D8BF2-27BD-4B3B-9B23-A40447555E9B", name: "", type: "Object3D", parent: Ao, children: Array(2), …}
  13. position: Qn {x: 12.898780822753906, y: -0.07714200019836426, z: -10.827563285827638}
  14. quaternion: Yn {_x: 0.7071067811865475, _y: 0, _z: 0, _w: 0.7071067811865476, _onChangeCallback: ƒ}
  15. receiveShadow: false
  16. renderOrder: 0
  17. rotation: cr {_x: 1.5707963267948963, _y: 0, _z: -0, _order: "XYZ", _onChangeCallback: ƒ}
  18. scale: Qn {x: 1, y: 1, z: 1}
  19. type: "Mesh"
  20. up: Qn {x: 0, y: 1, z: 0}
  21. userData: {}
  22. uuid: "B7611B38-FA9C-4F63-A00D-7DEBC3075869"
  23. visible: true
  24. drawMode: (...)
  25. eulerOrder: (...)
  26. id: 28
  27. modelViewMatrix: ar {elements: Array(16)}
  28. normalMatrix: Gn {elements: Array(9)}
  29. useQuaternion: (...)
  30. __proto__: Mr

What do i need to do to get a copy on the same place?

There are two ways to add children in three.js: Object3D.attach and Object3D.add. attach modifies the position and rotation of the child object such that it will retain its position in world space relative to it’s current world space while add uses them as the transformation relative to the parent. Keep in mind that when you clone an object it does not have a parent and it copies the position and rotation from the original object. It sounds like you want add in this case.

thanks now i understand a little bit more. Working now