webgl_loader_collada_kinematics.html 4.4 KB

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