webgl_animation_skinning_ik.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. <!-- Import maps polyfill -->
  22. <!-- Remove this when import maps will be widely supported -->
  23. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  38. import { CCDIKSolver, CCDIKHelper } from './jsm/animation/CCDIKSolver.js';
  39. import Stats from 'three/addons/libs/stats.module.js';
  40. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  41. let scene, camera, renderer, orbitControls, transformControls;
  42. let mirrorSphereCamera;
  43. const OOI = {};
  44. let IKSolver;
  45. let stats, gui, conf;
  46. const v0 = new THREE.Vector3();
  47. init().then( animate );
  48. async function init() {
  49. conf = {
  50. followSphere: false,
  51. turnHead: true,
  52. ik_solver: true,
  53. update: updateIK
  54. };
  55. scene = new THREE.Scene();
  56. scene.fog = new THREE.FogExp2( 0xffffff, .17 );
  57. scene.background = new THREE.Color( 0xdddddd );
  58. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.001, 5000 );
  59. camera.position.set( 0.9728517749133652, 1.1044765132727201, 0.7316689528482836 );
  60. camera.lookAt( scene.position );
  61. const ambientLight = new THREE.AmbientLight( 0xffffff, 8 ); // soft white light
  62. scene.add( ambientLight );
  63. renderer = new THREE.WebGLRenderer( { antialias: true, logarithmicDepthBuffer: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.useLegacyLights = false;
  67. document.body.appendChild( renderer.domElement );
  68. stats = new Stats();
  69. document.body.appendChild( stats.dom );
  70. orbitControls = new OrbitControls( camera, renderer.domElement );
  71. orbitControls.minDistance = 0.2;
  72. orbitControls.maxDistance = 1.5;
  73. orbitControls.enableDamping = true;
  74. const dracoLoader = new DRACOLoader();
  75. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  76. const gltfLoader = new GLTFLoader();
  77. gltfLoader.setDRACOLoader( dracoLoader );
  78. const gltf = await gltfLoader.loadAsync( 'models/gltf/kira.glb' );
  79. gltf.scene.traverse( n => {
  80. if ( n.name === 'head' ) OOI.head = n;
  81. if ( n.name === 'lowerarm_l' ) OOI.lowerarm_l = n;
  82. if ( n.name === 'Upperarm_l' ) OOI.Upperarm_l = n;
  83. if ( n.name === 'hand_l' ) OOI.hand_l = n;
  84. if ( n.name === 'target_hand_l' ) OOI.target_hand_l = n;
  85. if ( n.name === 'boule' ) OOI.sphere = n;
  86. if ( n.name === 'Kira_Shirt_left' ) OOI.kira = n;
  87. } );
  88. scene.add( gltf.scene );
  89. orbitControls.target.copy( OOI.sphere.position ); // orbit controls lookAt the sphere
  90. OOI.hand_l.attach( OOI.sphere );
  91. // mirror sphere cube-camera
  92. const cubeRenderTarget = new THREE.WebGLCubeRenderTarget( 1024 );
  93. mirrorSphereCamera = new THREE.CubeCamera( 0.05, 50, cubeRenderTarget );
  94. scene.add( mirrorSphereCamera );
  95. const mirrorSphereMaterial = new THREE.MeshBasicMaterial( { envMap: cubeRenderTarget.texture } );
  96. OOI.sphere.material = mirrorSphereMaterial;
  97. transformControls = new TransformControls( camera, renderer.domElement );
  98. transformControls.size = 0.75;
  99. transformControls.showX = false;
  100. transformControls.space = 'world';
  101. transformControls.attach( OOI.target_hand_l );
  102. scene.add( transformControls );
  103. // disable orbitControls while using transformControls
  104. transformControls.addEventListener( 'mouseDown', () => orbitControls.enabled = false );
  105. transformControls.addEventListener( 'mouseUp', () => orbitControls.enabled = true );
  106. OOI.kira.add( OOI.kira.skeleton.bones[ 0 ] );
  107. const iks = [
  108. {
  109. target: 22, // "target_hand_l"
  110. effector: 6, // "hand_l"
  111. links: [
  112. {
  113. index: 5, // "lowerarm_l"
  114. rotationMin: new THREE.Vector3( 1.2, - 1.8, - .4 ),
  115. rotationMax: new THREE.Vector3( 1.7, - 1.1, .3 )
  116. },
  117. {
  118. index: 4, // "Upperarm_l"
  119. rotationMin: new THREE.Vector3( 0.1, - 0.7, - 1.8 ),
  120. rotationMax: new THREE.Vector3( 1.1, 0, - 1.4 )
  121. },
  122. ],
  123. }
  124. ];
  125. IKSolver = new CCDIKSolver( OOI.kira, iks );
  126. const ccdikhelper = new CCDIKHelper( OOI.kira, iks, 0.01 );
  127. scene.add( ccdikhelper );
  128. gui = new GUI();
  129. gui.add( conf, 'followSphere' ).name( 'follow sphere' );
  130. gui.add( conf, 'turnHead' ).name( 'turn head' );
  131. gui.add( conf, 'ik_solver' ).name( 'IK auto update' );
  132. gui.add( conf, 'update' ).name( 'IK manual update()' );
  133. gui.open();
  134. window.addEventListener( 'resize', onWindowResize, false );
  135. }
  136. function animate( ) {
  137. if ( OOI.sphere && mirrorSphereCamera ) {
  138. OOI.sphere.visible = false;
  139. OOI.sphere.getWorldPosition( mirrorSphereCamera.position );
  140. mirrorSphereCamera.update( renderer, scene );
  141. OOI.sphere.visible = true;
  142. }
  143. if ( OOI.sphere && conf.followSphere ) {
  144. // orbitControls follows the sphere
  145. OOI.sphere.getWorldPosition( v0 );
  146. orbitControls.target.lerp( v0, 0.1 );
  147. }
  148. if ( OOI.head && OOI.sphere && conf.turnHead ) {
  149. // turn head
  150. OOI.sphere.getWorldPosition( v0 );
  151. OOI.head.lookAt( v0 );
  152. OOI.head.rotation.set( OOI.head.rotation.x, OOI.head.rotation.y + Math.PI, OOI.head.rotation.z );
  153. }
  154. if ( conf.ik_solver ) {
  155. updateIK();
  156. }
  157. orbitControls.update();
  158. renderer.render( scene, camera );
  159. stats.update(); // fps stats
  160. requestAnimationFrame( animate );
  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>