webgpu_animation_retargeting.html 9.5 KB

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