webgpu_animation_retargeting.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, Fn, vec2, length, atan2, float, sin, cos, vec3, sub, mul, pow, dodge, normalWorld } 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. export const lightSpeed = /*#__PURE__*/ Fn( ( [ suv_immutable ] ) => {
  42. // forked from https://www.shadertoy.com/view/7ly3D1
  43. const time = timerLocal( 1 );
  44. const suv = vec2( suv_immutable );
  45. const uv = vec2( length( suv ), atan2( suv.y, suv.x ) );
  46. const offset = float( float( .1 ).mul( sin( uv.y.mul( 10. ).sub( time.mul( .6 ) ) ) ).mul( cos( uv.y.mul( 48. ).add( time.mul( .3 ) ) ) ).mul( cos( uv.y.mul( 3.7 ).add( time ) ) ) );
  47. const rays = vec3( vec3( sin( uv.y.mul( 150. ).add( time ) ).mul( .5 ).add( .5 ) ).mul( vec3( sin( uv.y.mul( 80. ).sub( time.mul( 0.6 ) ) ).mul( .5 ).add( .5 ) ) ).mul( vec3( sin( uv.y.mul( 45. ).add( time.mul( 0.8 ) ) ).mul( .5 ).add( .5 ) ) ).mul( vec3( sub( 1., cos( uv.y.add( mul( 22., time ).sub( pow( uv.x.add( offset ), .3 ).mul( 60. ) ) ) ) ) ) ).mul( vec3( uv.x.mul( 2. ) ) ) );
  48. return rays;
  49. } ).setLayout( {
  50. name: 'lightSpeed',
  51. type: 'vec3',
  52. inputs: [
  53. { name: 'suv', type: 'vec2' }
  54. ]
  55. } );
  56. // scene
  57. const scene = new THREE.Scene();
  58. // background
  59. const coloredVignette = screenUV.distance( .5 ).mix( hue( color( 0x0175ad ), timerLocal( .1 ) ), hue( color( 0x02274f ), timerLocal( .5 ) ) );
  60. const lightSpeedEffect = lightSpeed( normalWorld ).clamp();
  61. const lightSpeedSky = normalWorld.y.remapClamp( -.1, 1 ).mix( 0, lightSpeedEffect );
  62. const composedBackground = dodge( coloredVignette, lightSpeedSky );
  63. scene.backgroundNode = composedBackground;
  64. //
  65. const helpers = new THREE.Group();
  66. helpers.visible = false;
  67. scene.add( helpers );
  68. const light = new THREE.HemisphereLight( 0xe9c0a5, 0x0175ad, 5 );
  69. scene.add( light );
  70. const dirLight = new THREE.DirectionalLight( 0xfff9ea, 4 );
  71. dirLight.position.set( 2, 5, 2 );
  72. scene.add( dirLight );
  73. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, .25, 50 );
  74. camera.position.set( 0, 1, 4 );
  75. // add models to scene
  76. scene.add( sourceModel.scene );
  77. scene.add( targetModel.scene );
  78. // reposition models
  79. sourceModel.scene.position.x -= .8;
  80. targetModel.scene.position.x += .7;
  81. targetModel.scene.position.z -= .1;
  82. // reajust model
  83. targetModel.scene.scale.setScalar( .01 );
  84. // flip model
  85. sourceModel.scene.rotation.y = Math.PI / 2;
  86. targetModel.scene.rotation.y = - Math.PI / 2;
  87. // retarget
  88. const source = getSource( sourceModel );
  89. const mixer = retargetModel( source, targetModel );
  90. // floor
  91. const reflection = reflector();
  92. reflection.target.rotateX( - Math.PI / 2 );
  93. scene.add( reflection.target );
  94. const floorMaterial = new THREE.NodeMaterial();
  95. floorMaterial.colorNode = reflection;
  96. floorMaterial.opacity = .2;
  97. floorMaterial.transparent = true;
  98. const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
  99. floor.receiveShadow = true;
  100. floor.position.set( 0, 0, 0 );
  101. scene.add( floor );
  102. // renderer
  103. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  104. renderer.toneMapping = THREE.NeutralToneMapping;
  105. renderer.setAnimationLoop( animate );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. document.body.appendChild( renderer.domElement );
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.minDistance = 3;
  111. controls.maxDistance = 12;
  112. controls.target.set( 0, 1, 0 );
  113. controls.maxPolarAngle = Math.PI / 2;
  114. const gui = new GUI();
  115. gui.add( helpers, 'visible' ).name( 'helpers' );
  116. //
  117. function getSource( sourceModel ) {
  118. const clip = sourceModel.animations[ 0 ]
  119. const helper = new THREE.SkeletonHelper( sourceModel.scene );
  120. helpers.add( helper );
  121. const skeleton = new THREE.Skeleton( helper.bones );
  122. const mixer = new THREE.AnimationMixer( sourceModel.scene );
  123. mixer.clipAction( sourceModel.animations[ 0 ] ).play();
  124. return { clip, skeleton, mixer };
  125. }
  126. function retargetModel( sourceModel, targetModel ) {
  127. const targetSkin = targetModel.scene.children[ 0 ].children[ 0 ];
  128. const targetSkelHelper = new THREE.SkeletonHelper( targetModel.scene );
  129. helpers.add( targetSkelHelper );
  130. const rotateCW45 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( 45 ) );
  131. const rotateCCW180 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( - 180 ) );
  132. const rotateCW180 = new THREE.Matrix4().makeRotationY( THREE.MathUtils.degToRad( 180 ) );
  133. const rotateFoot = new THREE.Matrix4().makeRotationFromEuler( new THREE.Euler( THREE.MathUtils.degToRad( 45 ), THREE.MathUtils.degToRad( 180 ), THREE.MathUtils.degToRad( 0 ) ) );
  134. const retargetOptions = {
  135. // specify the name of the source's hip bone.
  136. hip: 'mixamorigHips',
  137. // specify the influence of the source's hip bone.
  138. // use ( 0, 1, 0 ) to ignore xz hip movement.
  139. //hipInfluence: new THREE.Vector3( 0, 1, 0 ),
  140. // specify an animation range in seconds.
  141. //trim: [ 3.0, 4.0 ],
  142. // preserve the scale of the target model
  143. scale: 1 / targetModel.scene.scale.y,
  144. // offset target bones -> { targetBoneName: offsetMatrix }
  145. localOffsets: {
  146. mixamorigLeftShoulder: rotateCW45,
  147. mixamorigRightShoulder: rotateCCW180,
  148. mixamorigLeftArm: rotateCW45,
  149. mixamorigRightArm: rotateCCW180,
  150. mixamorigLeftForeArm: rotateCW45,
  151. mixamorigRightForeArm: rotateCCW180,
  152. mixamorigLeftHand: rotateCW45,
  153. mixamorigRightHand: rotateCCW180,
  154. mixamorigLeftUpLeg: rotateCW180,
  155. mixamorigRightUpLeg: rotateCW180,
  156. mixamorigLeftLeg: rotateCW180,
  157. mixamorigRightLeg: rotateCW180,
  158. mixamorigLeftFoot: rotateFoot,
  159. mixamorigRightFoot: rotateFoot,
  160. mixamorigLeftToeBase: rotateCW180,
  161. mixamorigRightToeBase: rotateCW180
  162. },
  163. // Map of target's bone names to source's bone names -> { targetBoneName: sourceBoneName }
  164. names: {
  165. mixamorigHips: 'mixamorigHips',
  166. mixamorigSpine: 'mixamorigSpine',
  167. mixamorigSpine2: 'mixamorigSpine2',
  168. mixamorigHead: 'mixamorigHead',
  169. mixamorigLeftShoulder: 'mixamorigLeftShoulder',
  170. mixamorigRightShoulder: 'mixamorigRightShoulder',
  171. mixamorigLeftArm: 'mixamorigLeftArm',
  172. mixamorigRightArm: 'mixamorigRightArm',
  173. mixamorigLeftForeArm: 'mixamorigLeftForeArm',
  174. mixamorigRightForeArm: 'mixamorigRightForeArm',
  175. mixamorigLeftHand: 'mixamorigLeftHand',
  176. mixamorigRightHand: 'mixamorigRightHand',
  177. mixamorigLeftUpLeg: 'mixamorigLeftUpLeg',
  178. mixamorigRightUpLeg: 'mixamorigRightUpLeg',
  179. mixamorigLeftLeg: 'mixamorigLeftLeg',
  180. mixamorigRightLeg: 'mixamorigRightLeg',
  181. mixamorigLeftFoot: 'mixamorigLeftFoot',
  182. mixamorigRightFoot: 'mixamorigRightFoot',
  183. mixamorigLeftToeBase: 'mixamorigLeftToeBase',
  184. mixamorigRightToeBase: 'mixamorigRightToeBase'
  185. }
  186. };
  187. const retargetedClip = SkeletonUtils.retargetClip( targetSkin, sourceModel.skeleton, sourceModel.clip, retargetOptions );
  188. // Apply the mixer directly to the SkinnedMesh, not any
  189. // ancestor node, because that's what
  190. // SkeletonUtils.retargetClip outputs the clip to be
  191. // compatible with.
  192. const mixer = new THREE.AnimationMixer( targetSkin );
  193. mixer.clipAction( retargetedClip ).play();
  194. return mixer;
  195. }
  196. window.onresize = function () {
  197. camera.aspect = window.innerWidth / window.innerHeight;
  198. camera.updateProjectionMatrix();
  199. renderer.setSize( window.innerWidth, window.innerHeight );
  200. };
  201. function animate() {
  202. const delta = clock.getDelta();
  203. source.mixer.update( delta );
  204. mixer.update( delta );
  205. controls.update();
  206. stats.update();
  207. renderer.render( scene, camera );
  208. }
  209. </script>
  210. </body>
  211. </html>