webgl_animation_skinning_ik.html 7.0 KB

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