webgl_loader_collada_kinematics.html 4.5 KB

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