webgl_loader_collada.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/Detector.js"></script>
  33. <script src="js/Stats.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var container, stats;
  37. var camera, scene, renderer, objects;
  38. var particleLight, pointLight;
  39. var dae, skin;
  40. var loader = new THREE.ColladaLoader();
  41. loader.options.convertUpAxis = true;
  42. loader.load( './models/monster.dae', function colladaReady( collada ) {
  43. dae = collada.scene;
  44. skin = collada.skins[ 0 ];
  45. dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
  46. dae.updateMatrix();
  47. init();
  48. animate();
  49. } );
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. scene = new THREE.Scene();
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  55. camera.position.set( 2, 2, 3 );
  56. scene.add( camera );
  57. // Grid
  58. var line_material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } ),
  59. geometry = new THREE.Geometry(),
  60. floor = -0.04, step = 1, size = 14;
  61. for ( var i = 0; i <= size / step * 2; i ++ ) {
  62. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - size, floor, i * step - size ) ) );
  63. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( size, floor, i * step - size ) ) );
  64. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, -size ) ) );
  65. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, size ) ) );
  66. }
  67. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  68. scene.add( line );
  69. // Add the COLLADA
  70. scene.add( dae );
  71. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  72. scene.add( particleLight );
  73. // Lights
  74. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  75. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee );
  76. directionalLight.position.x = Math.random() - 0.5;
  77. directionalLight.position.y = Math.random() - 0.5;
  78. directionalLight.position.z = Math.random() - 0.5;
  79. directionalLight.position.normalize();
  80. scene.add( directionalLight );
  81. pointLight = new THREE.PointLight( 0xffffff, 4 );
  82. pointLight.position = particleLight.position;
  83. scene.add( pointLight );
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. container.appendChild( renderer.domElement );
  87. stats = new Stats();
  88. stats.domElement.style.position = 'absolute';
  89. stats.domElement.style.top = '0px';
  90. container.appendChild( stats.domElement );
  91. }
  92. //
  93. var t = 0;
  94. function animate() {
  95. requestAnimationFrame( animate );
  96. if ( t > 30 ) t = 0;
  97. if ( skin ) {
  98. // guess this can be done smarter...
  99. // (Indeed, there are way more frames than needed and interpolation is not used at all
  100. // could be something like - one morph per each skinning pose keyframe, or even less,
  101. // animation could be resampled, morphing interpolation handles sparse keyframes quite well.
  102. // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)
  103. for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {
  104. skin.morphTargetInfluences[ i ] = 0;
  105. }
  106. skin.morphTargetInfluences[ Math.floor( t ) ] = 1;
  107. t += 0.5;
  108. }
  109. render();
  110. stats.update();
  111. }
  112. function render() {
  113. var timer = Date.now() * 0.0005;
  114. camera.position.x = Math.cos( timer ) * 10;
  115. camera.position.y = 2;
  116. camera.position.z = Math.sin( timer ) * 10;
  117. camera.lookAt( scene.position );
  118. particleLight.position.x = Math.sin( timer * 4 ) * 3009;
  119. particleLight.position.y = Math.cos( timer * 5 ) * 4000;
  120. particleLight.position.z = Math.cos( timer * 4 ) * 3009;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>