webgl_loader_collada.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
  29. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a>
  30. </div>
  31. <script src="../build/Three.js"></script>
  32. <script src="js/loaders/ColladaLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/Stats.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var container, stats;
  38. var camera, scene, renderer, objects;
  39. var particleLight, pointLight;
  40. var dae, skin;
  41. var loader = new THREE.ColladaLoader();
  42. loader.options.convertUpAxis = true;
  43. loader.load( './models/collada/monster/monster.dae', function colladaReady( collada ) {
  44. dae = collada.scene;
  45. skin = collada.skins[ 0 ];
  46. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  47. dae.updateMatrix();
  48. init();
  49. animate();
  50. } );
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. scene = new THREE.Scene();
  55. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  56. camera.position.set( 2, 2, 3 );
  57. scene.add( camera );
  58. // Grid
  59. var size = 14, step = 1;
  60. var geometry = new THREE.Geometry();
  61. var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
  62. for ( var i = - size; i <= size; i += step ) {
  63. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  64. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  65. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  66. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  67. }
  68. var line = new THREE.Line( geometry, material, THREE.LinePieces );
  69. scene.add( line );
  70. // Add the COLLADA
  71. scene.add( dae );
  72. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  73. scene.add( particleLight );
  74. // Lights
  75. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  76. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  77. directionalLight.position.x = Math.random() - 0.5;
  78. directionalLight.position.y = Math.random() - 0.5;
  79. directionalLight.position.z = Math.random() - 0.5;
  80. directionalLight.position.normalize();
  81. scene.add( directionalLight );
  82. pointLight = new THREE.PointLight( 0xffffff, 4 );
  83. pointLight.position = particleLight.position;
  84. scene.add( pointLight );
  85. renderer = new THREE.WebGLRenderer();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. container.appendChild( renderer.domElement );
  88. stats = new Stats();
  89. stats.domElement.style.position = 'absolute';
  90. stats.domElement.style.top = '0px';
  91. container.appendChild( stats.domElement );
  92. //
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. //
  101. var t = 0;
  102. var clock = new THREE.Clock();
  103. function animate() {
  104. var delta = clock.getDelta();
  105. requestAnimationFrame( animate );
  106. if ( t > 1 ) t = 0;
  107. if ( skin ) {
  108. // guess this can be done smarter...
  109. // (Indeed, there are way more frames than needed and interpolation is not used at all
  110. // could be something like - one morph per each skinning pose keyframe, or even less,
  111. // animation could be resampled, morphing interpolation handles sparse keyframes quite well.
  112. // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)
  113. for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {
  114. skin.morphTargetInfluences[ i ] = 0;
  115. }
  116. skin.morphTargetInfluences[ Math.floor( t * 30 ) ] = 1;
  117. t += delta;
  118. }
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. var timer = Date.now() * 0.0005;
  124. camera.position.x = Math.cos( timer ) * 10;
  125. camera.position.y = 2;
  126. camera.position.z = Math.sin( timer ) * 10;
  127. camera.lookAt( scene.position );
  128. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  129. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  130. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>