webgpu_animation_retargeting_readyplayer.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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<br />
  11. <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo</a> to <a href="https://readyplayer.me/" target="_blank" rel="noopener">readyplayer.me</a>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { screenUV, color, vec2, reflector } from 'three/tsl';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  28. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  29. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  30. const [ sourceModel, targetModel ] = await Promise.all( [
  31. new Promise( ( resolve, reject ) => {
  32. new FBXLoader().load( './models/fbx/mixamo.fbx', resolve, undefined, reject );
  33. } ),
  34. new Promise( ( resolve, reject ) => {
  35. new GLTFLoader().load( './models/gltf/readyplayer.me.glb', resolve, undefined, reject );
  36. } )
  37. ] );
  38. //
  39. const clock = new THREE.Clock();
  40. const stats = new Stats();
  41. document.body.appendChild( stats.dom );
  42. // scene
  43. const scene = new THREE.Scene();
  44. // background
  45. const horizontalEffect = screenUV.x.mix( color( 0x13172b ), color( 0x311649 ) );
  46. const lightEffect = screenUV.distance( vec2( 0.5, 1.0 ) ).oneMinus().mul( color( 0x0c5d68 ) );
  47. scene.backgroundNode = horizontalEffect.add( lightEffect );
  48. //
  49. const light = new THREE.HemisphereLight( 0x311649, 0x0c5d68, 10 );
  50. scene.add( light );
  51. const backLight = new THREE.DirectionalLight( 0xffffff, 10 );
  52. backLight.position.set( 0, 5, - 5 );
  53. scene.add( backLight );
  54. const keyLight = new THREE.DirectionalLight( 0xfff9ea, 4 );
  55. keyLight.position.set( 3, 5, 3 );
  56. scene.add( keyLight );
  57. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, .25, 50 );
  58. camera.position.set( 0, 3, 5 );
  59. // add models to scene
  60. scene.add( sourceModel );
  61. scene.add( targetModel.scene );
  62. // reposition models
  63. sourceModel.position.x -= .9;
  64. targetModel.scene.position.x += .9;
  65. // reajust model - mixamo use centimeters, readyplayer.me use meters (three.js scale is meters)
  66. sourceModel.scale.setScalar( .01 );
  67. // retarget
  68. const source = getSource( sourceModel );
  69. const mixer = retargetModel( source, targetModel );
  70. // renderer
  71. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  72. renderer.toneMapping = THREE.NeutralToneMapping;
  73. renderer.setAnimationLoop( animate );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. document.body.appendChild( renderer.domElement );
  77. const controls = new OrbitControls( camera, renderer.domElement );
  78. controls.minDistance = 3;
  79. controls.maxDistance = 12;
  80. controls.target.set( 0, 1, 0 );
  81. controls.maxPolarAngle = Math.PI / 2;
  82. // floor
  83. const reflection = reflector();
  84. reflection.target.rotateX( - Math.PI / 2 );
  85. scene.add( reflection.target );
  86. const floorMaterial = new THREE.NodeMaterial();
  87. floorMaterial.colorNode = reflection;
  88. floorMaterial.opacity = .2;
  89. floorMaterial.transparent = true;
  90. const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
  91. floor.receiveShadow = true;
  92. floor.position.set( 0, 0, 0 );
  93. scene.add( floor );
  94. //
  95. function getSource( sourceModel ) {
  96. const clip = sourceModel.animations[ 0 ]
  97. const helper = new THREE.SkeletonHelper( sourceModel );
  98. const skeleton = new THREE.Skeleton( helper.bones );
  99. const mixer = new THREE.AnimationMixer( sourceModel );
  100. mixer.clipAction( sourceModel.animations[ 0 ] ).play();
  101. return { clip, skeleton, mixer };
  102. }
  103. function retargetModel( sourceModel, targetModel ) {
  104. const targetSkin = targetModel.scene.children[ 0 ].children[ 1 ];
  105. const retargetOptions = {
  106. // specify the name of the source's hip bone.
  107. hip: 'mixamorigHips',
  108. // preserve the scale of the target model
  109. scale: .01,
  110. // use ( 0, 1, 0 ) to ignore xz hip movement.
  111. //hipInfluence: new THREE.Vector3( 0, 1, 0 ),
  112. // Map of target's bone names to source's bone names -> { targetBoneName: sourceBoneName }
  113. getBoneName: function ( bone ) {
  114. return 'mixamorig' + bone.name;
  115. }
  116. };
  117. const retargetedClip = SkeletonUtils.retargetClip( targetSkin, sourceModel.skeleton, sourceModel.clip, retargetOptions );
  118. const mixer = new THREE.AnimationMixer( targetSkin );
  119. mixer.clipAction( retargetedClip ).play();
  120. return mixer;
  121. }
  122. window.onresize = function () {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. };
  127. function animate() {
  128. const delta = clock.getDelta();
  129. source.mixer.update( delta );
  130. mixer.update( delta );
  131. controls.update();
  132. stats.update();
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>