Three.js: Clone mesh doesn't clone its material and geometry objects

Created on 5 Jun 2018  Â·  2Comments  Â·  Source: mrdoob/three.js

Description of the problem

When I try to clone a mesh object I expect it will be full cloned, with "new" material and geometry objects. Is this behavior on purpose?

Three.js version
  • [ ] Dev
  • [x] r93
  • [ ] ...
Browser
  • [ ] All of them
  • [ ] Chrome
  • [ ] Firefox
  • [ ] Internet Explorer
  • [x] chromium (nw js actually)
OS
  • [ ] All of them
  • [ ] Windows
  • [ ] macOS
  • [x] Linux
  • [ ] Android
  • [ ] iOS
    captura de tela de 2018-06-04 21-59-15
Question

Most helpful comment

Hi @marcobraghim, this is as intended. There is a high performance cost to deeply cloning materials, textures, and geometry — not just while duplicating them, but higher cost to render after that. Because you generally want to have as few materials and geometries as possible, it's better to clone only what you need:

mesh2 = mesh1.clone();
mesh2.traverse((node) => {
  if (node.isMesh) {
    node.material = node.material.clone();
  }
});

All 2 comments

Hi @marcobraghim, this is as intended. There is a high performance cost to deeply cloning materials, textures, and geometry — not just while duplicating them, but higher cost to render after that. Because you generally want to have as few materials and geometries as possible, it's better to clone only what you need:

mesh2 = mesh1.clone();
mesh2.traverse((node) => {
  if (node.isMesh) {
    node.material = node.material.clone();
  }
});

It's really interesting and make me think if I need to change my strategy related to the materials and whole performance stuff at all... Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jlaquinte picture jlaquinte  Â·  3Comments

makc picture makc  Â·  3Comments

fuzihaofzh picture fuzihaofzh  Â·  3Comments

scrubs picture scrubs  Â·  3Comments

akshaysrin picture akshaysrin  Â·  3Comments