webgl_loader_collada_kinematics.html 4.1 KB

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