webgpu_animation_retargeting_readyplayer.html 5.7 KB

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