webgpu_animation_retargeting.html 9.2 KB

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