webgl_animation_skinning_ik.html 7.0 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. <!-- 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.outputEncoding = THREE.sRGBEncoding;
  67. renderer.useLegacyLights = false;
  68. document.body.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. document.body.appendChild( stats.dom );
  71. orbitControls = new OrbitControls( camera, renderer.domElement );
  72. orbitControls.minDistance = 0.2;
  73. orbitControls.maxDistance = 1.5;
  74. orbitControls.enableDamping = true;
  75. const dracoLoader = new DRACOLoader();
  76. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  77. const gltfLoader = new GLTFLoader();
  78. gltfLoader.setDRACOLoader( dracoLoader );
  79. const gltf = await gltfLoader.loadAsync( 'models/gltf/kira.glb' );
  80. gltf.scene.traverse( n => {
  81. if ( n.name === 'head' ) OOI.head = n;
  82. if ( n.name === 'lowerarm_l' ) OOI.lowerarm_l = n;
  83. if ( n.name === 'Upperarm_l' ) OOI.Upperarm_l = n;
  84. if ( n.name === 'hand_l' ) OOI.hand_l = n;
  85. if ( n.name === 'target_hand_l' ) OOI.target_hand_l = n;
  86. if ( n.name === 'boule' ) OOI.sphere = n;
  87. if ( n.name === 'Kira_Shirt_left' ) OOI.kira = n;
  88. } );
  89. scene.add( gltf.scene );
  90. orbitControls.target.copy( OOI.sphere.position ); // orbit controls lookAt the sphere
  91. OOI.hand_l.attach( OOI.sphere );
  92. // mirror sphere cube-camera
  93. const cubeRenderTarget = new THREE.WebGLCubeRenderTarget( 1024 );
  94. mirrorSphereCamera = new THREE.CubeCamera( 0.05, 50, cubeRenderTarget );
  95. scene.add( mirrorSphereCamera );
  96. const mirrorSphereMaterial = new THREE.MeshBasicMaterial( { envMap: cubeRenderTarget.texture } );
  97. OOI.sphere.material = mirrorSphereMaterial;
  98. transformControls = new TransformControls( camera, renderer.domElement );
  99. transformControls.size = 0.75;
  100. transformControls.showX = false;
  101. transformControls.space = 'world';
  102. transformControls.attach( OOI.target_hand_l );
  103. scene.add( transformControls );
  104. // disable orbitControls while using transformControls
  105. transformControls.addEventListener( 'mouseDown', () => orbitControls.enabled = false );
  106. transformControls.addEventListener( 'mouseUp', () => orbitControls.enabled = true );
  107. OOI.kira.add( OOI.kira.skeleton.bones[ 0 ] );
  108. const iks = [
  109. {
  110. target: 22, // "target_hand_l"
  111. effector: 6, // "hand_l"
  112. links: [
  113. {
  114. index: 5, // "lowerarm_l"
  115. rotationMin: new THREE.Vector3( 1.2, - 1.8, - .4 ),
  116. rotationMax: new THREE.Vector3( 1.7, - 1.1, .3 )
  117. },
  118. {
  119. index: 4, // "Upperarm_l"
  120. rotationMin: new THREE.Vector3( 0.1, - 0.7, - 1.8 ),
  121. rotationMax: new THREE.Vector3( 1.1, 0, - 1.4 )
  122. },
  123. ],
  124. }
  125. ];
  126. IKSolver = new CCDIKSolver( OOI.kira, iks );
  127. const ccdikhelper = new CCDIKHelper( OOI.kira, iks, 0.01 );
  128. scene.add( ccdikhelper );
  129. gui = new GUI();
  130. gui.add( conf, 'followSphere' ).name( 'follow sphere' );
  131. gui.add( conf, 'turnHead' ).name( 'turn head' );
  132. gui.add( conf, 'ik_solver' ).name( 'IK auto update' );
  133. gui.add( conf, 'update' ).name( 'IK manual update()' );
  134. gui.open();
  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. requestAnimationFrame( animate );
  162. }
  163. function updateIK() {
  164. if ( IKSolver ) IKSolver.update();
  165. scene.traverse( function ( object ) {
  166. if ( object.isSkinnedMesh ) object.computeBoundingSphere();
  167. } );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. </script>
  175. </body>
  176. </html>