webgpu_animation_retargeting_readyplayer.html 5.9 KB

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