webgl_loader_collada_kinematics.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> -
  29. robot from <a href="https://github.com/rdiankov/collada_robots" target="_blank">collada robots</a>
  30. </div>
  31. <script src="../build/three.min.js"></script>
  32. <script src="js/loaders/ColladaLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var container, stats;
  38. var camera, scene, renderer, objects;
  39. var particleLight;
  40. var dae;
  41. var kinematics;
  42. var jointIndex;
  43. var joint;
  44. var jointValue;
  45. var jointStep;
  46. var loader = new THREE.ColladaLoader();
  47. loader.options.convertUpAxis = true;
  48. loader.load( './models/collada/kawada-hironx.dae', function ( collada ) {
  49. dae = collada.scene;
  50. dae.scale.x = dae.scale.y = dae.scale.z = 10.0;
  51. dae.updateMatrix();
  52. kinematics = collada.kinematics;
  53. jointIndex = 0;
  54. joint = kinematics.joints[jointIndex];
  55. jointValue = joint.limits.min;
  56. jointStep = ( joint.limits.max - joint.limits.min ) / 100.0;
  57. init();
  58. animate();
  59. } );
  60. function init() {
  61. container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  64. camera.position.set( 2, 2, 3 );
  65. scene = new THREE.Scene();
  66. // Grid
  67. var size = 14, step = 1;
  68. var geometry = new THREE.Geometry();
  69. var material = new THREE.LineBasicMaterial( { color: 0x303030 } );
  70. for ( var i = - size; i <= size; i += step ) {
  71. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  72. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  73. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  74. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  75. }
  76. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  77. scene.add( line );
  78. // Add the COLLADA
  79. scene.add( dae );
  80. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  81. scene.add( particleLight );
  82. // Lights
  83. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  84. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  85. directionalLight.position.x = Math.random() - 0.5;
  86. directionalLight.position.y = Math.random() - 0.5;
  87. directionalLight.position.z = Math.random() - 0.5;
  88. directionalLight.position.normalize();
  89. scene.add( directionalLight );
  90. var pointLight = new THREE.PointLight( 0xffffff, 1 );
  91. particleLight.add( pointLight );
  92. renderer = new THREE.WebGLRenderer();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. container.appendChild( renderer.domElement );
  95. stats = new Stats();
  96. stats.domElement.style.position = 'absolute';
  97. stats.domElement.style.top = '0px';
  98. container.appendChild( stats.domElement );
  99. //
  100. window.addEventListener( 'resize', onWindowResize, false );
  101. }
  102. function moveJoints() {
  103. if ( jointValue >= joint.limits.max || joint.static ) {
  104. if ( !joint.static ) {
  105. kinematics.setJointValue( jointIndex, joint.zeroPosition );
  106. }
  107. jointIndex++;
  108. // go back to the beginning if we're past the number of joints
  109. if ( jointIndex >= kinematics.joints.length ) {
  110. jointIndex = 0;
  111. } else {
  112. joint = kinematics.joints[ jointIndex ];
  113. jointValue = joint.limits.min;
  114. jointStep = ( joint.limits.max - joint.limits.min ) / 100.0;
  115. }
  116. }
  117. jointValue += jointStep;
  118. kinematics.setJointValue( jointIndex, jointValue );
  119. };
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. var timer = Date.now() * 0.0001;
  133. camera.position.x = Math.cos( timer ) * 17;
  134. camera.position.y = 10;
  135. camera.position.z = Math.sin( timer ) * 17;
  136. camera.lookAt( scene.position );
  137. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  138. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  139. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  140. moveJoints();
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>