webgl_collada.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #ffffff;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #log { color:#fff; position:absolute; top:50px; text-align:left; display:block; z-index:100; pointer-events:none; }
  14. </style>
  15. </head>
  16. <body>
  17. <pre id="log"></pre>
  18. <script type="text/javascript" src="../build/Three.js"></script>
  19. <script type="text/javascript" src="js/Detector.js"></script>
  20. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  21. <script type="text/javascript" src="js/Stats.js"></script>
  22. <script type="text/javascript" src="../src/extras/collada/dae.js"></script>
  23. <script type="text/javascript">
  24. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  25. var container, stats;
  26. var camera, scene, renderer, objects;
  27. var particleLight, pointLight;
  28. var dae;
  29. DAE.load('./models/monster.dae', colladaReady);
  30. function colladaReady(collada) {
  31. dae = collada.scene;
  32. dae.scale.x = dae.scale.y = dae.scale.z = 0.005;
  33. dae.updateMatrix();
  34. init();
  35. animate();
  36. }
  37. function init() {
  38. container = document.createElement('div');
  39. document.body.appendChild(container);
  40. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  41. camera.position.x = 2;
  42. camera.position.y = 2;
  43. camera.position.z = 3;
  44. scene = new THREE.Scene();
  45. // Grid
  46. var line_material = new THREE.LineBasicMaterial( { color: 0x0, opacity: 0.2 } ),
  47. geometry = new THREE.Geometry(),
  48. floor = -0.04, step = 1, size = 14;
  49. for ( var i = 0; i <= size / step * 2; i ++ ) {
  50. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - size, floor, i * step - size ) ) );
  51. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( size, floor, i * step - size ) ) );
  52. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, -size ) ) );
  53. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, size ) ) );
  54. }
  55. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  56. scene.addObject( line );
  57. // Materials
  58. var generatedTexture = new THREE.Texture( generateTexture() );
  59. generatedTexture.needsUpdate = true;
  60. var materials = [];
  61. materials.push( new THREE.MeshLambertMaterial( { map: generatedTexture } ) );
  62. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  63. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  64. materials.push( new THREE.MeshNormalMaterial( ) );
  65. materials.push( new THREE.MeshBasicMaterial( { color: 0x665500, blending: THREE.AdditiveBlending } ) );
  66. //materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  67. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  68. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading } ) );
  69. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  70. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  71. materials.push( new THREE.MeshDepthMaterial() );
  72. materials.push( new THREE.MeshBasicMaterial( { map: generatedTexture } ) );
  73. // Spheres geometry
  74. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  75. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  76. var geometry_pieces = new THREE.SphereGeometry( 70, 32, 16 ); // Extra geometry to be broken down for MeshFaceMaterial
  77. for ( var i = 0, l = geometry_pieces.faces.length; i < l; i ++ ) {
  78. var face = geometry_pieces.faces[ i ];
  79. if ( Math.random() > 0.7 ) face.materials = [ materials[ Math.floor( Math.random() * materials.length ) ] ];
  80. }
  81. materials.push( new THREE.MeshFaceMaterial() );
  82. objects = [];
  83. var sphere, geometry, material;
  84. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  85. material = materials[ i ];
  86. geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces :
  87. ( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth );
  88. sphere = new THREE.Mesh( geometry, material );
  89. sphere.position.x = ( i % 4 ) * 200 - 400;
  90. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  91. sphere.rotation.x = Math.random() * 200 - 100;
  92. sphere.rotation.y = Math.random() * 200 - 100;
  93. sphere.rotation.z = Math.random() * 200 - 100;
  94. objects.push( sphere );
  95. //scene.addObject( sphere );
  96. }
  97. //dae.rotation.x = -Math.PI/2;
  98. scene.addObject(dae);
  99. //var wall = dae_geometries['wall-geometry'][0];
  100. //var dae = new THREE.Mesh( wall, materials[3] );
  101. //dae.scale.x = dae.scale.y = dae.scale.z = 100.0;
  102. //scene.addObject(dae);
  103. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  104. scene.addObject( particleLight );
  105. // Lights
  106. scene.addLight( new THREE.AmbientLight( 0x202020 ) );
  107. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xcccccc);
  108. directionalLight.position.x = Math.random() - 0.5;
  109. directionalLight.position.y = Math.random() - 0.5;
  110. directionalLight.position.z = Math.random() - 0.5;
  111. directionalLight.position.normalize();
  112. scene.addLight( directionalLight );
  113. pointLight = new THREE.PointLight( 0xdddddd, 0.6 );
  114. scene.addLight( pointLight );
  115. renderer = new THREE.WebGLRenderer();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. container.appendChild( renderer.domElement );
  118. stats = new Stats();
  119. stats.domElement.style.position = 'absolute';
  120. stats.domElement.style.top = '0px';
  121. container.appendChild( stats.domElement );
  122. }
  123. function generateTexture() {
  124. var canvas = document.createElement( 'canvas' );
  125. canvas.width = 256;
  126. canvas.height = 256;
  127. var context = canvas.getContext( '2d' );
  128. var image = context.getImageData( 0, 0, 256, 256 );
  129. var x = 0, y = 0;
  130. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  131. x = j % 256;
  132. y = x == 0 ? y + 1 : y;
  133. image.data[ i + 2 ] = Math.floor( x ^ y );
  134. image.data[ i + 3 ] = 255;
  135. }
  136. context.putImageData( image, 0, 0 );
  137. return canvas;
  138. }
  139. //
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. render();
  143. stats.update();
  144. }
  145. function render() {
  146. var timer = new Date().getTime() * 0.0005;
  147. camera.position.x = Math.cos( timer ) * 10;
  148. camera.position.y = 2;
  149. camera.position.z = Math.sin( timer ) * 10;
  150. for ( var i = 0, l = objects.length; i < l; i++ ) {
  151. var object = objects[ i ];
  152. // object.rotation.x += 0.01;
  153. // object.rotation.y += 0.005;
  154. }
  155. particleLight.position.x = Math.sin( timer * 4 ) * 300;
  156. particleLight.position.y = 800;//Math.cos( timer * 5 ) * 400;
  157. particleLight.position.z = Math.cos( timer * 4 ) * 300;
  158. pointLight.position.x = particleLight.position.x;
  159. pointLight.position.y = particleLight.position.y;
  160. pointLight.position.z = particleLight.position.z;
  161. renderer.render( scene, camera );
  162. }
  163. </script>
  164. </body>
  165. </html>