webgl_loader_collada_kinematics.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, 0xc1c1c1, 0x8d8d8d );
  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( 0xfff7f7, 0x494966 );
  67. scene.add( light );
  68. const pointLight = new THREE.PointLight( 0xffffff, 0.3 );
  69. particleLight.add( pointLight );
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. container.appendChild( renderer.domElement );
  74. stats = new Stats();
  75. container.appendChild( stats.dom );
  76. setupTween();
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function setupTween() {
  81. const duration = THREE.MathUtils.randInt( 1000, 5000 );
  82. const target = {};
  83. for ( const prop in kinematics.joints ) {
  84. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  85. if ( ! kinematics.joints[ prop ].static ) {
  86. const joint = kinematics.joints[ prop ];
  87. const old = tweenParameters[ prop ];
  88. const position = old ? old : joint.zeroPosition;
  89. tweenParameters[ prop ] = position;
  90. target[ prop ] = THREE.MathUtils.randInt( joint.limits.min, joint.limits.max );
  91. }
  92. }
  93. }
  94. kinematicsTween = new TWEEN.Tween( tweenParameters ).to( target, duration ).easing( TWEEN.Easing.Quadratic.Out );
  95. kinematicsTween.onUpdate( function ( object ) {
  96. for ( const prop in kinematics.joints ) {
  97. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  98. if ( ! kinematics.joints[ prop ].static ) {
  99. kinematics.setJointValue( prop, object[ prop ] );
  100. }
  101. }
  102. }
  103. } );
  104. kinematicsTween.start();
  105. setTimeout( setupTween, duration );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. //
  113. function animate() {
  114. requestAnimationFrame( animate );
  115. render();
  116. stats.update();
  117. TWEEN.update();
  118. }
  119. function render() {
  120. const timer = Date.now() * 0.0001;
  121. camera.position.x = Math.cos( timer ) * 20;
  122. camera.position.y = 10;
  123. camera.position.z = Math.sin( timer ) * 20;
  124. camera.lookAt( 0, 5, 0 );
  125. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  126. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  127. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>