webgpu_animation_retargeting.html 9.8 KB

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