webgl_animation_skinning_ik.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - skinning - ik</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta name="author" content="Antoine BERNIER (abernier)" />
  8. <link type="text/css" rel="stylesheet" href="main.css">
  9. <style>
  10. body {color:white;}
  11. #info a {
  12. color:#4d6675;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="info">
  18. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - inverse kinematics<br />
  19. Character model by <a href="https://assetstore.unity.com/packages/3d/characters/humanoids/humans/kira-lowpoly-character-100303" target="_blank" rel="noopener">Aki</a>, furnitures from <a href="https://poly.pizza" target="_blank" rel="noopener">poly.pizza</a>, scene by <a href="https://abernier.name/three.js/examples/webgl_esher.html" target="_blank" rel="noopener">abernier</a>. CC0.
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  33. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  34. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  35. import { CCDIKSolver, CCDIKHelper } from 'three/addons/animation/CCDIKSolver.js';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  38. let scene, camera, renderer, orbitControls, transformControls;
  39. let mirrorSphereCamera;
  40. const OOI = {};
  41. let IKSolver;
  42. let stats, gui, conf;
  43. const v0 = new THREE.Vector3();
  44. init();
  45. async function init() {
  46. conf = {
  47. followSphere: false,
  48. turnHead: true,
  49. ik_solver: true,
  50. update: updateIK
  51. };
  52. scene = new THREE.Scene();
  53. scene.fog = new THREE.FogExp2( 0xffffff, .17 );
  54. scene.background = new THREE.Color( 0xffffff );
  55. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.001, 5000 );
  56. camera.position.set( 0.9728517749133652, 1.1044765132727201, 0.7316689528482836 );
  57. camera.lookAt( scene.position );
  58. const ambientLight = new THREE.AmbientLight( 0xffffff, 8 ); // soft white light
  59. scene.add( ambientLight );
  60. const dracoLoader = new DRACOLoader();
  61. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  62. const gltfLoader = new GLTFLoader();
  63. gltfLoader.setDRACOLoader( dracoLoader );
  64. const gltf = await gltfLoader.loadAsync( 'models/gltf/kira.glb' );
  65. gltf.scene.traverse( n => {
  66. if ( n.name === 'head' ) OOI.head = n;
  67. if ( n.name === 'lowerarm_l' ) OOI.lowerarm_l = n;
  68. if ( n.name === 'Upperarm_l' ) OOI.Upperarm_l = n;
  69. if ( n.name === 'hand_l' ) OOI.hand_l = n;
  70. if ( n.name === 'target_hand_l' ) OOI.target_hand_l = n;
  71. if ( n.name === 'boule' ) OOI.sphere = n;
  72. if ( n.name === 'Kira_Shirt_left' ) OOI.kira = n;
  73. } );
  74. scene.add( gltf.scene );
  75. const targetPosition = OOI.sphere.position.clone(); // for orbit controls
  76. OOI.hand_l.attach( OOI.sphere );
  77. // mirror sphere cube-camera
  78. const cubeRenderTarget = new THREE.WebGLCubeRenderTarget( 1024 );
  79. mirrorSphereCamera = new THREE.CubeCamera( 0.05, 50, cubeRenderTarget );
  80. scene.add( mirrorSphereCamera );
  81. const mirrorSphereMaterial = new THREE.MeshBasicMaterial( { envMap: cubeRenderTarget.texture } );
  82. OOI.sphere.material = mirrorSphereMaterial;
  83. OOI.kira.add( OOI.kira.skeleton.bones[ 0 ] );
  84. const iks = [
  85. {
  86. target: 22, // "target_hand_l"
  87. effector: 6, // "hand_l"
  88. links: [
  89. {
  90. index: 5, // "lowerarm_l"
  91. rotationMin: new THREE.Vector3( 1.2, - 1.8, - .4 ),
  92. rotationMax: new THREE.Vector3( 1.7, - 1.1, .3 )
  93. },
  94. {
  95. index: 4, // "Upperarm_l"
  96. rotationMin: new THREE.Vector3( 0.1, - 0.7, - 1.8 ),
  97. rotationMax: new THREE.Vector3( 1.1, 0, - 1.4 )
  98. },
  99. ],
  100. }
  101. ];
  102. IKSolver = new CCDIKSolver( OOI.kira, iks );
  103. const ccdikhelper = new CCDIKHelper( OOI.kira, iks, 0.01 );
  104. scene.add( ccdikhelper );
  105. gui = new GUI();
  106. gui.add( conf, 'followSphere' ).name( 'follow sphere' );
  107. gui.add( conf, 'turnHead' ).name( 'turn head' );
  108. gui.add( conf, 'ik_solver' ).name( 'IK auto update' );
  109. gui.add( conf, 'update' ).name( 'IK manual update()' );
  110. gui.open();
  111. //
  112. renderer = new THREE.WebGLRenderer( { antialias: true } );
  113. renderer.setPixelRatio( window.devicePixelRatio );
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. renderer.setAnimationLoop( animate );
  116. document.body.appendChild( renderer.domElement );
  117. //
  118. orbitControls = new OrbitControls( camera, renderer.domElement );
  119. orbitControls.minDistance = 0.2;
  120. orbitControls.maxDistance = 1.5;
  121. orbitControls.enableDamping = true;
  122. orbitControls.target.copy( targetPosition );
  123. transformControls = new TransformControls( camera, renderer.domElement );
  124. transformControls.size = 0.75;
  125. transformControls.showX = false;
  126. transformControls.space = 'world';
  127. transformControls.attach( OOI.target_hand_l );
  128. scene.add( transformControls.getHelper() );
  129. // disable orbitControls while using transformControls
  130. transformControls.addEventListener( 'mouseDown', () => orbitControls.enabled = false );
  131. transformControls.addEventListener( 'mouseUp', () => orbitControls.enabled = true );
  132. //
  133. stats = new Stats();
  134. document.body.appendChild( stats.dom );
  135. window.addEventListener( 'resize', onWindowResize, false );
  136. }
  137. function animate( ) {
  138. if ( OOI.sphere && mirrorSphereCamera ) {
  139. OOI.sphere.visible = false;
  140. OOI.sphere.getWorldPosition( mirrorSphereCamera.position );
  141. mirrorSphereCamera.update( renderer, scene );
  142. OOI.sphere.visible = true;
  143. }
  144. if ( OOI.sphere && conf.followSphere ) {
  145. // orbitControls follows the sphere
  146. OOI.sphere.getWorldPosition( v0 );
  147. orbitControls.target.lerp( v0, 0.1 );
  148. }
  149. if ( OOI.head && OOI.sphere && conf.turnHead ) {
  150. // turn head
  151. OOI.sphere.getWorldPosition( v0 );
  152. OOI.head.lookAt( v0 );
  153. OOI.head.rotation.set( OOI.head.rotation.x, OOI.head.rotation.y + Math.PI, OOI.head.rotation.z );
  154. }
  155. if ( conf.ik_solver ) {
  156. updateIK();
  157. }
  158. orbitControls.update();
  159. renderer.render( scene, camera );
  160. stats.update(); // fps stats
  161. }
  162. function updateIK() {
  163. if ( IKSolver ) IKSolver.update();
  164. scene.traverse( function ( object ) {
  165. if ( object.isSkinnedMesh ) object.computeBoundingSphere();
  166. } );
  167. }
  168. function onWindowResize() {
  169. camera.aspect = window.innerWidth / window.innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. }
  173. </script>
  174. </body>
  175. </html>