webgpu_animation_retargeting.html 9.4 KB

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