How to clone a Skinned Mesh?












5















I need to have multiple identical, animated models on a scene. If possible, I would like them to have a shared geometry and material, but if it is impossible, having them instanced per model will suffice too.



Unfortunately, the only way to achieve this result I found is to go through JSONLoader for every model instance.



SkinnedMesh does have a clone() method, but it seems not to be fully implemented yet. If used and both original and cloned mesh are present on the scene, only one will appear, and cloned one will be without animation.



I have attempted to use this example with shared skeletons:
https://github.com/mrdoob/three.js/pull/11666



...and indeed it works, but I need to be able to play different animations for every model instance, having them all play the same one is not sufficient, sadly. I hoped I could do similar hax and insert my own skeleton (made out of bones from the JSON file), but it behaves very much like if I just used clone() from SkinnedMesh.



I am using this code:
https://github.com/arturitu/threejs-animation-workflow/blob/master/js/main.js



Basically what I'd like to achieve is



 var onLoad = function (geometry, materials) {
window.geometry = geometry;

character = new THREE.SkinnedMesh(
geometry,
new THREE.MeshFaceMaterial(materials)
);

character2 = character.someMagicalClone();

scene.add(character);
scene.add(character2);

(...)


I need any clue... and while I wait for help, I am busily deconstructing constructor for SkinnedMesh and JSONLoader for clues ;)



Thanks in advance!










share|improve this question



























    5















    I need to have multiple identical, animated models on a scene. If possible, I would like them to have a shared geometry and material, but if it is impossible, having them instanced per model will suffice too.



    Unfortunately, the only way to achieve this result I found is to go through JSONLoader for every model instance.



    SkinnedMesh does have a clone() method, but it seems not to be fully implemented yet. If used and both original and cloned mesh are present on the scene, only one will appear, and cloned one will be without animation.



    I have attempted to use this example with shared skeletons:
    https://github.com/mrdoob/three.js/pull/11666



    ...and indeed it works, but I need to be able to play different animations for every model instance, having them all play the same one is not sufficient, sadly. I hoped I could do similar hax and insert my own skeleton (made out of bones from the JSON file), but it behaves very much like if I just used clone() from SkinnedMesh.



    I am using this code:
    https://github.com/arturitu/threejs-animation-workflow/blob/master/js/main.js



    Basically what I'd like to achieve is



     var onLoad = function (geometry, materials) {
    window.geometry = geometry;

    character = new THREE.SkinnedMesh(
    geometry,
    new THREE.MeshFaceMaterial(materials)
    );

    character2 = character.someMagicalClone();

    scene.add(character);
    scene.add(character2);

    (...)


    I need any clue... and while I wait for help, I am busily deconstructing constructor for SkinnedMesh and JSONLoader for clues ;)



    Thanks in advance!










    share|improve this question

























      5












      5








      5








      I need to have multiple identical, animated models on a scene. If possible, I would like them to have a shared geometry and material, but if it is impossible, having them instanced per model will suffice too.



      Unfortunately, the only way to achieve this result I found is to go through JSONLoader for every model instance.



      SkinnedMesh does have a clone() method, but it seems not to be fully implemented yet. If used and both original and cloned mesh are present on the scene, only one will appear, and cloned one will be without animation.



      I have attempted to use this example with shared skeletons:
      https://github.com/mrdoob/three.js/pull/11666



      ...and indeed it works, but I need to be able to play different animations for every model instance, having them all play the same one is not sufficient, sadly. I hoped I could do similar hax and insert my own skeleton (made out of bones from the JSON file), but it behaves very much like if I just used clone() from SkinnedMesh.



      I am using this code:
      https://github.com/arturitu/threejs-animation-workflow/blob/master/js/main.js



      Basically what I'd like to achieve is



       var onLoad = function (geometry, materials) {
      window.geometry = geometry;

      character = new THREE.SkinnedMesh(
      geometry,
      new THREE.MeshFaceMaterial(materials)
      );

      character2 = character.someMagicalClone();

      scene.add(character);
      scene.add(character2);

      (...)


      I need any clue... and while I wait for help, I am busily deconstructing constructor for SkinnedMesh and JSONLoader for clues ;)



      Thanks in advance!










      share|improve this question














      I need to have multiple identical, animated models on a scene. If possible, I would like them to have a shared geometry and material, but if it is impossible, having them instanced per model will suffice too.



      Unfortunately, the only way to achieve this result I found is to go through JSONLoader for every model instance.



      SkinnedMesh does have a clone() method, but it seems not to be fully implemented yet. If used and both original and cloned mesh are present on the scene, only one will appear, and cloned one will be without animation.



      I have attempted to use this example with shared skeletons:
      https://github.com/mrdoob/three.js/pull/11666



      ...and indeed it works, but I need to be able to play different animations for every model instance, having them all play the same one is not sufficient, sadly. I hoped I could do similar hax and insert my own skeleton (made out of bones from the JSON file), but it behaves very much like if I just used clone() from SkinnedMesh.



      I am using this code:
      https://github.com/arturitu/threejs-animation-workflow/blob/master/js/main.js



      Basically what I'd like to achieve is



       var onLoad = function (geometry, materials) {
      window.geometry = geometry;

      character = new THREE.SkinnedMesh(
      geometry,
      new THREE.MeshFaceMaterial(materials)
      );

      character2 = character.someMagicalClone();

      scene.add(character);
      scene.add(character2);

      (...)


      I need any clue... and while I wait for help, I am busily deconstructing constructor for SkinnedMesh and JSONLoader for clues ;)



      Thanks in advance!







      animation three.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 29 '17 at 20:30









      ScharnvirkScharnvirk

      966




      966
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I found a solution in this pull request:
          https://github.com/mrdoob/three.js/pull/14494



          in short, there are two functions added:



          function cloneAnimated( source ) {

          var cloneLookup = new Map();

          var clone = source.clone();

          parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {

          cloneLookup.set( sourceNode, clonedNode );

          } );

          source.traverse( function ( sourceMesh ) {

          if ( ! sourceMesh.isSkinnedMesh ) return;

          var sourceBones = sourceMesh.skeleton.bones;
          var clonedMesh = cloneLookup.get( sourceMesh );

          clonedMesh.skeleton = sourceMesh.skeleton.clone();

          clonedMesh.skeleton.bones = sourceBones.map( function ( sourceBone ) {

          if ( ! cloneLookup.has( sourceBone ) ) {

          throw new Error( 'THREE.AnimationUtils: Required bones are not descendants of the given object.' );

          }

          return cloneLookup.get( sourceBone );

          } );

          clonedMesh.bind( clonedMesh.skeleton, sourceMesh.bindMatrix );

          } );

          return clone;

          }

          function parallelTraverse( a, b, callback ) {

          callback( a, b );

          for ( var i = 0; i < a.children.length; i ++ ) {

          parallelTraverse( a.children[ i ], b.children[ i ], callback );

          }

          }


          As I understand it rebinds cloned skeleton to the cloned mesh.
          so topic example could look like:



          var onLoad = function (geometry, materials) {
          window.geometry = geometry;
          character = new THREE.SkinnedMesh(
          geometry,
          new THREE.MeshFaceMaterial(materials)
          );

          character2 = cloneAnimated(character); // <-- used that new function

          scene.add(character);
          scene.add(character2);
          (...)





          share|improve this answer


























          • Can you please edit the answer and add some more informations about what the functions do?

            – gratien asimbahwe
            Nov 23 '18 at 19:04











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45393765%2fhow-to-clone-a-skinned-mesh%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I found a solution in this pull request:
          https://github.com/mrdoob/three.js/pull/14494



          in short, there are two functions added:



          function cloneAnimated( source ) {

          var cloneLookup = new Map();

          var clone = source.clone();

          parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {

          cloneLookup.set( sourceNode, clonedNode );

          } );

          source.traverse( function ( sourceMesh ) {

          if ( ! sourceMesh.isSkinnedMesh ) return;

          var sourceBones = sourceMesh.skeleton.bones;
          var clonedMesh = cloneLookup.get( sourceMesh );

          clonedMesh.skeleton = sourceMesh.skeleton.clone();

          clonedMesh.skeleton.bones = sourceBones.map( function ( sourceBone ) {

          if ( ! cloneLookup.has( sourceBone ) ) {

          throw new Error( 'THREE.AnimationUtils: Required bones are not descendants of the given object.' );

          }

          return cloneLookup.get( sourceBone );

          } );

          clonedMesh.bind( clonedMesh.skeleton, sourceMesh.bindMatrix );

          } );

          return clone;

          }

          function parallelTraverse( a, b, callback ) {

          callback( a, b );

          for ( var i = 0; i < a.children.length; i ++ ) {

          parallelTraverse( a.children[ i ], b.children[ i ], callback );

          }

          }


          As I understand it rebinds cloned skeleton to the cloned mesh.
          so topic example could look like:



          var onLoad = function (geometry, materials) {
          window.geometry = geometry;
          character = new THREE.SkinnedMesh(
          geometry,
          new THREE.MeshFaceMaterial(materials)
          );

          character2 = cloneAnimated(character); // <-- used that new function

          scene.add(character);
          scene.add(character2);
          (...)





          share|improve this answer


























          • Can you please edit the answer and add some more informations about what the functions do?

            – gratien asimbahwe
            Nov 23 '18 at 19:04
















          0














          I found a solution in this pull request:
          https://github.com/mrdoob/three.js/pull/14494



          in short, there are two functions added:



          function cloneAnimated( source ) {

          var cloneLookup = new Map();

          var clone = source.clone();

          parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {

          cloneLookup.set( sourceNode, clonedNode );

          } );

          source.traverse( function ( sourceMesh ) {

          if ( ! sourceMesh.isSkinnedMesh ) return;

          var sourceBones = sourceMesh.skeleton.bones;
          var clonedMesh = cloneLookup.get( sourceMesh );

          clonedMesh.skeleton = sourceMesh.skeleton.clone();

          clonedMesh.skeleton.bones = sourceBones.map( function ( sourceBone ) {

          if ( ! cloneLookup.has( sourceBone ) ) {

          throw new Error( 'THREE.AnimationUtils: Required bones are not descendants of the given object.' );

          }

          return cloneLookup.get( sourceBone );

          } );

          clonedMesh.bind( clonedMesh.skeleton, sourceMesh.bindMatrix );

          } );

          return clone;

          }

          function parallelTraverse( a, b, callback ) {

          callback( a, b );

          for ( var i = 0; i < a.children.length; i ++ ) {

          parallelTraverse( a.children[ i ], b.children[ i ], callback );

          }

          }


          As I understand it rebinds cloned skeleton to the cloned mesh.
          so topic example could look like:



          var onLoad = function (geometry, materials) {
          window.geometry = geometry;
          character = new THREE.SkinnedMesh(
          geometry,
          new THREE.MeshFaceMaterial(materials)
          );

          character2 = cloneAnimated(character); // <-- used that new function

          scene.add(character);
          scene.add(character2);
          (...)





          share|improve this answer


























          • Can you please edit the answer and add some more informations about what the functions do?

            – gratien asimbahwe
            Nov 23 '18 at 19:04














          0












          0








          0







          I found a solution in this pull request:
          https://github.com/mrdoob/three.js/pull/14494



          in short, there are two functions added:



          function cloneAnimated( source ) {

          var cloneLookup = new Map();

          var clone = source.clone();

          parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {

          cloneLookup.set( sourceNode, clonedNode );

          } );

          source.traverse( function ( sourceMesh ) {

          if ( ! sourceMesh.isSkinnedMesh ) return;

          var sourceBones = sourceMesh.skeleton.bones;
          var clonedMesh = cloneLookup.get( sourceMesh );

          clonedMesh.skeleton = sourceMesh.skeleton.clone();

          clonedMesh.skeleton.bones = sourceBones.map( function ( sourceBone ) {

          if ( ! cloneLookup.has( sourceBone ) ) {

          throw new Error( 'THREE.AnimationUtils: Required bones are not descendants of the given object.' );

          }

          return cloneLookup.get( sourceBone );

          } );

          clonedMesh.bind( clonedMesh.skeleton, sourceMesh.bindMatrix );

          } );

          return clone;

          }

          function parallelTraverse( a, b, callback ) {

          callback( a, b );

          for ( var i = 0; i < a.children.length; i ++ ) {

          parallelTraverse( a.children[ i ], b.children[ i ], callback );

          }

          }


          As I understand it rebinds cloned skeleton to the cloned mesh.
          so topic example could look like:



          var onLoad = function (geometry, materials) {
          window.geometry = geometry;
          character = new THREE.SkinnedMesh(
          geometry,
          new THREE.MeshFaceMaterial(materials)
          );

          character2 = cloneAnimated(character); // <-- used that new function

          scene.add(character);
          scene.add(character2);
          (...)





          share|improve this answer















          I found a solution in this pull request:
          https://github.com/mrdoob/three.js/pull/14494



          in short, there are two functions added:



          function cloneAnimated( source ) {

          var cloneLookup = new Map();

          var clone = source.clone();

          parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {

          cloneLookup.set( sourceNode, clonedNode );

          } );

          source.traverse( function ( sourceMesh ) {

          if ( ! sourceMesh.isSkinnedMesh ) return;

          var sourceBones = sourceMesh.skeleton.bones;
          var clonedMesh = cloneLookup.get( sourceMesh );

          clonedMesh.skeleton = sourceMesh.skeleton.clone();

          clonedMesh.skeleton.bones = sourceBones.map( function ( sourceBone ) {

          if ( ! cloneLookup.has( sourceBone ) ) {

          throw new Error( 'THREE.AnimationUtils: Required bones are not descendants of the given object.' );

          }

          return cloneLookup.get( sourceBone );

          } );

          clonedMesh.bind( clonedMesh.skeleton, sourceMesh.bindMatrix );

          } );

          return clone;

          }

          function parallelTraverse( a, b, callback ) {

          callback( a, b );

          for ( var i = 0; i < a.children.length; i ++ ) {

          parallelTraverse( a.children[ i ], b.children[ i ], callback );

          }

          }


          As I understand it rebinds cloned skeleton to the cloned mesh.
          so topic example could look like:



          var onLoad = function (geometry, materials) {
          window.geometry = geometry;
          character = new THREE.SkinnedMesh(
          geometry,
          new THREE.MeshFaceMaterial(materials)
          );

          character2 = cloneAnimated(character); // <-- used that new function

          scene.add(character);
          scene.add(character2);
          (...)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 23:25

























          answered Nov 23 '18 at 18:44









          zapkinzapkin

          13




          13













          • Can you please edit the answer and add some more informations about what the functions do?

            – gratien asimbahwe
            Nov 23 '18 at 19:04



















          • Can you please edit the answer and add some more informations about what the functions do?

            – gratien asimbahwe
            Nov 23 '18 at 19:04

















          Can you please edit the answer and add some more informations about what the functions do?

          – gratien asimbahwe
          Nov 23 '18 at 19:04





          Can you please edit the answer and add some more informations about what the functions do?

          – gratien asimbahwe
          Nov 23 '18 at 19:04




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45393765%2fhow-to-clone-a-skinned-mesh%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno