webgpu_animation_retargeting.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - animation retargeting</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - animation retargeting
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { color, screenUV, hue, timerLocal, reflector } from 'three/tsl';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  29. const [ sourceModel, targetModel ] = await Promise.all( [
  30. new Promise( ( resolve, reject ) => {
  31. new GLTFLoader().load( './models/gltf/Michelle.glb', resolve, undefined, reject );
  32. } ),
  33. new Promise( ( resolve, reject ) => {
  34. new GLTFLoader().load( './models/gltf/Soldier.glb', resolve, undefined, reject );
  35. } )
  36. ] );
  37. //
  38. const clock = new THREE.Clock();
  39. const stats = new Stats();
  40. document.body.appendChild( stats.dom );
  41. // scene
  42. const scene = new THREE.Scene();
  43. scene.backgroundNode = screenUV.distance( .5 ).mix( hue( color( 0x0175ad ), timerLocal( .1 ) ), hue( color( 0x02274f ), timerLocal( .5 ) ) );
  44. const helpers = new THREE.Group();
  45. helpers.visible = false;
  46. scene.add( helpers );
  47. const light = new THREE.HemisphereLight( 0xe9c0a5, 0x0175ad, 5 );
  48. scene.add( light );
  49. const dirLight = new THREE.DirectionalLight( 0xfff9ea, 4 );
  50. dirLight.position.set( 2, 5, 2 );
  51. scene.add( dirLight );
  52. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, .25, 50 );
  53. camera.position.set( - 2, 2, 4 );
  54. // add models to scene
  55. scene.add( sourceModel.scene );
  56. scene.add( targetModel.scene );
  57. // reposition models
  58. sourceModel.scene.position.x -= 1;
  59. targetModel.scene.position.x += 1;
  60. // reajust model
  61. targetModel.scene.scale.setScalar( .01 );
  62. // retarget
  63. const source = getSource( sourceModel );
  64. const mixer = retargetModel( source, targetModel );
  65. // floor
  66. const reflection = reflector();
  67. reflection.target.rotateX( - Math.PI / 2 );
  68. scene.add( reflection.target );
  69. const floorMaterial = new THREE.NodeMaterial();
  70. floorMaterial.colorNode = reflection;
  71. floorMaterial.opacity = .2;
  72. floorMaterial.transparent = true;
  73. const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
  74. floor.receiveShadow = true;
  75. floor.position.set( 0, 0, 0 );
  76. scene.add( floor );
  77. // renderer
  78. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  79. renderer.toneMapping = THREE.NeutralToneMapping;
  80. renderer.setAnimationLoop( animate );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. document.body.appendChild( renderer.domElement );
  84. const controls = new OrbitControls( camera, renderer.domElement );
  85. controls.minDistance = 3;
  86. controls.maxDistance = 12;
  87. controls.target.set( 0, 1, 0 );
  88. controls.maxPolarAngle = Math.PI / 2;
  89. const gui = new GUI();
  90. gui.add( helpers, 'visible' ).name( 'helpers' );
  91. //
  92. function getSource( sourceModel ) {
  93. const clip = sourceModel.animations[ 0 ]
  94. const helper = new THREE.SkeletonHelper( sourceModel.scene );
  95. helpers.add( helper );
  96. const skeleton = new THREE.Skeleton( helper.bones );
  97. const mixer = new THREE.AnimationMixer( sourceModel.scene );
  98. mixer.clipAction( sourceModel.animations[ 0 ] ).play();
  99. return { clip, skeleton, mixer };
  100. }
  101. function retargetModel( sourceModel, targetModel ) {
  102. const targetSkin = targetModel.scene.children[ 0 ].children[ 0 ];
  103. const targetSkelHelper = new THREE.SkeletonHelper( targetModel.scene );
  104. helpers.add( targetSkelHelper );
  105. const rotateCW45 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( 45 ) );
  106. const rotateCCW180 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( - 180 ) );
  107. const rotateCW180 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( 180 ) );
  108. const rotateFoot = new THREE.Matrix4().makeRotationFromEuler( new THREE.Euler( THREE.MathUtils.degToRad( 45 ), THREE.MathUtils.degToRad( 180 ), THREE.MathUtils.degToRad( 0 ) ) );
  109. const retargetOptions = {
  110. preserveHipPosition: false, // lock XZ position of the hip bone to the origin
  111. // specify the name of the source's hip bone.
  112. hip: 'mixamorigHips',
  113. // preserve the scale of the target model
  114. scale: 1 / targetModel.scene.scale.y,
  115. // offset target bones -> { targetBoneName: offsetMatrix }
  116. localOffsets: {
  117. mixamorigLeftShoulder: rotateCW45,
  118. mixamorigRightShoulder: rotateCCW180,
  119. mixamorigLeftArm: rotateCW45,
  120. mixamorigRightArm: rotateCCW180,
  121. mixamorigLeftForeArm: rotateCW45,
  122. mixamorigRightForeArm: rotateCCW180,
  123. mixamorigLeftHand: rotateCW45,
  124. mixamorigRightHand: rotateCCW180,
  125. mixamorigLeftUpLeg: rotateCW180,
  126. mixamorigRightUpLeg: rotateCW180,
  127. mixamorigLeftLeg: rotateCW180,
  128. mixamorigRightLeg: rotateCW180,
  129. mixamorigLeftFoot: rotateFoot,
  130. mixamorigRightFoot: rotateFoot,
  131. mixamorigLeftToeBase: rotateCW180,
  132. mixamorigRightToeBase: rotateCW180
  133. },
  134. // Map of target's bone names to source's bone names -> { targetBoneName: sourceBoneName }
  135. names: {
  136. mixamorigHips: 'mixamorigHips',
  137. mixamorigSpine: 'mixamorigSpine',
  138. mixamorigSpine2: 'mixamorigSpine2',
  139. mixamorigHead: 'mixamorigHead',
  140. mixamorigLeftShoulder: 'mixamorigLeftShoulder',
  141. mixamorigRightShoulder: 'mixamorigRightShoulder',
  142. mixamorigLeftArm: 'mixamorigLeftArm',
  143. mixamorigRightArm: 'mixamorigRightArm',
  144. mixamorigLeftForeArm: 'mixamorigLeftForeArm',
  145. mixamorigRightForeArm: 'mixamorigRightForeArm',
  146. mixamorigLeftHand: 'mixamorigLeftHand',
  147. mixamorigRightHand: 'mixamorigRightHand',
  148. mixamorigLeftUpLeg: 'mixamorigLeftUpLeg',
  149. mixamorigRightUpLeg: 'mixamorigRightUpLeg',
  150. mixamorigLeftLeg: 'mixamorigLeftLeg',
  151. mixamorigRightLeg: 'mixamorigRightLeg',
  152. mixamorigLeftFoot: 'mixamorigLeftFoot',
  153. mixamorigRightFoot: 'mixamorigRightFoot',
  154. mixamorigLeftToeBase: 'mixamorigLeftToeBase',
  155. mixamorigRightToeBase: 'mixamorigRightToeBase'
  156. }
  157. };
  158. const retargetedClip = SkeletonUtils.retargetClip( targetSkin, sourceModel.skeleton, sourceModel.clip, retargetOptions );
  159. // Apply the mixer directly to the SkinnedMesh, not any
  160. // ancestor node, because that's what
  161. // SkeletonUtils.retargetClip outputs the clip to be
  162. // compatible with.
  163. const mixer = new THREE.AnimationMixer( targetSkin );
  164. mixer.clipAction( retargetedClip ).play();
  165. return mixer;
  166. }
  167. window.onresize = function () {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. };
  172. function animate() {
  173. const delta = clock.getDelta();
  174. source.mixer.update( delta );
  175. mixer.update( delta );
  176. controls.update();
  177. stats.update();
  178. renderer.render( scene, camera );
  179. }
  180. </script>
  181. </body>
  182. </html>
粤ICP备19079148号