1
0

webgl_loader_collada_kinematics.html 3.9 KB

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