webgl_loader_collada_kinematics.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada - kinematics</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> collada loader - kinematics<br/>
  12. robot from <a href="https://github.com/rdiankov/collada_robots" target="_blank" rel="noopener">collada robots</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import TWEEN from 'three/addons/libs/tween.module.js';
  29. import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
  30. let container, stats;
  31. let camera, scene, renderer;
  32. let particleLight;
  33. let dae;
  34. let kinematics;
  35. let kinematicsTween;
  36. const tweenParameters = {};
  37. const loader = new ColladaLoader();
  38. loader.load( './models/collada/abb_irb52_7_120.dae', function ( collada ) {
  39. dae = collada.scene;
  40. dae.traverse( function ( child ) {
  41. if ( child.isMesh ) {
  42. // model does not have normals
  43. child.material.flatShading = true;
  44. }
  45. } );
  46. dae.scale.x = dae.scale.y = dae.scale.z = 10.0;
  47. dae.updateMatrix();
  48. kinematics = collada.kinematics;
  49. init();
  50. animate();
  51. } );
  52. function init() {
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  56. camera.position.set( 2, 2, 3 );
  57. scene = new THREE.Scene();
  58. // Grid
  59. const grid = new THREE.GridHelper( 20, 20, 0x888888, 0x444444 );
  60. scene.add( grid );
  61. // Add the COLLADA
  62. scene.add( dae );
  63. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  64. scene.add( particleLight );
  65. // Lights
  66. const light = new THREE.HemisphereLight( 0xffeeee, 0x111122 );
  67. scene.add( light );
  68. const pointLight = new THREE.PointLight( 0xffffff, 0.3 );
  69. particleLight.add( pointLight );
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.outputEncoding = THREE.sRGBEncoding;
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. setupTween();
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function setupTween() {
  82. const duration = THREE.MathUtils.randInt( 1000, 5000 );
  83. const target = {};
  84. for ( const prop in kinematics.joints ) {
  85. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  86. if ( ! kinematics.joints[ prop ].static ) {
  87. const joint = kinematics.joints[ prop ];
  88. const old = tweenParameters[ prop ];
  89. const position = old ? old : joint.zeroPosition;
  90. tweenParameters[ prop ] = position;
  91. target[ prop ] = THREE.MathUtils.randInt( joint.limits.min, joint.limits.max );
  92. }
  93. }
  94. }
  95. kinematicsTween = new TWEEN.Tween( tweenParameters ).to( target, duration ).easing( TWEEN.Easing.Quadratic.Out );
  96. kinematicsTween.onUpdate( function ( object ) {
  97. for ( const prop in kinematics.joints ) {
  98. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  99. if ( ! kinematics.joints[ prop ].static ) {
  100. kinematics.setJointValue( prop, object[ prop ] );
  101. }
  102. }
  103. }
  104. } );
  105. kinematicsTween.start();
  106. setTimeout( setupTween, duration );
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. //
  114. function animate() {
  115. requestAnimationFrame( animate );
  116. render();
  117. stats.update();
  118. TWEEN.update();
  119. }
  120. function render() {
  121. const timer = Date.now() * 0.0001;
  122. camera.position.x = Math.cos( timer ) * 20;
  123. camera.position.y = 10;
  124. camera.position.z = Math.sin( timer ) * 20;
  125. camera.lookAt( 0, 5, 0 );
  126. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  127. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  128. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>