webgl_loader_collada_kinematics.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 dae;
  33. let kinematics;
  34. let kinematicsTween;
  35. const tweenParameters = {};
  36. const loader = new ColladaLoader();
  37. loader.load( './models/collada/abb_irb52_7_120.dae', function ( collada ) {
  38. dae = collada.scene;
  39. dae.traverse( function ( child ) {
  40. if ( child.isMesh ) {
  41. // model does not have normals
  42. child.material.flatShading = true;
  43. }
  44. } );
  45. dae.scale.x = dae.scale.y = dae.scale.z = 10.0;
  46. dae.updateMatrix();
  47. kinematics = collada.kinematics;
  48. init();
  49. animate();
  50. } );
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  55. camera.position.set( 2, 2, 3 );
  56. scene = new THREE.Scene();
  57. // Grid
  58. const grid = new THREE.GridHelper( 20, 20, 0xc1c1c1, 0x8d8d8d );
  59. scene.add( grid );
  60. // Add the COLLADA
  61. scene.add( dae );
  62. // Lights
  63. const light = new THREE.HemisphereLight( 0xfff7f7, 0x494966, 3 );
  64. scene.add( light );
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. setupTween();
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function setupTween() {
  76. const duration = THREE.MathUtils.randInt( 1000, 5000 );
  77. const target = {};
  78. for ( const prop in kinematics.joints ) {
  79. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  80. if ( ! kinematics.joints[ prop ].static ) {
  81. const joint = kinematics.joints[ prop ];
  82. const old = tweenParameters[ prop ];
  83. const position = old ? old : joint.zeroPosition;
  84. tweenParameters[ prop ] = position;
  85. target[ prop ] = THREE.MathUtils.randInt( joint.limits.min, joint.limits.max );
  86. }
  87. }
  88. }
  89. kinematicsTween = new TWEEN.Tween( tweenParameters ).to( target, duration ).easing( TWEEN.Easing.Quadratic.Out );
  90. kinematicsTween.onUpdate( function ( object ) {
  91. for ( const prop in kinematics.joints ) {
  92. if ( kinematics.joints.hasOwnProperty( prop ) ) {
  93. if ( ! kinematics.joints[ prop ].static ) {
  94. kinematics.setJointValue( prop, object[ prop ] );
  95. }
  96. }
  97. }
  98. } );
  99. kinematicsTween.start();
  100. setTimeout( setupTween, duration );
  101. }
  102. function onWindowResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. //
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. render();
  111. stats.update();
  112. TWEEN.update();
  113. }
  114. function render() {
  115. const timer = Date.now() * 0.0001;
  116. camera.position.x = Math.cos( timer ) * 20;
  117. camera.position.y = 10;
  118. camera.position.z = Math.sin( timer ) * 20;
  119. camera.lookAt( 0, 5, 0 );
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>