webgl_collada.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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: #000000;
  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.003;
  33. //dae.rotation.x = -Math.PI/2;
  34. dae.updateMatrix();
  35. init();
  36. animate();
  37. }
  38. function init() {
  39. container = document.createElement('div');
  40. document.body.appendChild(container);
  41. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  42. camera.position.x = 2;
  43. camera.position.y = 2;
  44. camera.position.z = 3;
  45. scene = new THREE.Scene();
  46. // Grid
  47. var line_material = new THREE.LineBasicMaterial( { color: 0x0, opacity: 0.2 } ),
  48. geometry = new THREE.Geometry(),
  49. floor = -0.04, step = 1, size = 14;
  50. for ( var i = 0; i <= size / step * 2; i ++ ) {
  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( size, floor, i * step - size ) ) );
  53. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, -size ) ) );
  54. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - size, floor, size ) ) );
  55. }
  56. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  57. scene.addObject( line );
  58. // Materials
  59. var generatedTexture = new THREE.Texture( generateTexture() );
  60. generatedTexture.needsUpdate = true;
  61. var materials = [];
  62. materials.push( new THREE.MeshLambertMaterial( { map: generatedTexture } ) );
  63. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  64. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  65. materials.push( new THREE.MeshNormalMaterial( ) );
  66. materials.push( new THREE.MeshBasicMaterial( { color: 0x665500, blending: THREE.AdditiveBlending } ) );
  67. //materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  68. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  69. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading } ) );
  70. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  71. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  72. materials.push( new THREE.MeshDepthMaterial() );
  73. materials.push( new THREE.MeshBasicMaterial( { map: generatedTexture } ) );
  74. // Spheres geometry
  75. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  76. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  77. var geometry_pieces = new THREE.SphereGeometry( 70, 32, 16 ); // Extra geometry to be broken down for MeshFaceMaterial
  78. for ( var i = 0, l = geometry_pieces.faces.length; i < l; i ++ ) {
  79. var face = geometry_pieces.faces[ i ];
  80. if ( Math.random() > 0.7 ) face.materials = [ materials[ Math.floor( Math.random() * materials.length ) ] ];
  81. }
  82. materials.push( new THREE.MeshFaceMaterial() );
  83. objects = [];
  84. var sphere, geometry, material;
  85. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  86. material = materials[ i ];
  87. geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces :
  88. ( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth );
  89. sphere = new THREE.Mesh( geometry, material );
  90. sphere.position.x = ( i % 4 ) * 200 - 400;
  91. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  92. sphere.rotation.x = Math.random() * 200 - 100;
  93. sphere.rotation.y = Math.random() * 200 - 100;
  94. sphere.rotation.z = Math.random() * 200 - 100;
  95. objects.push( sphere );
  96. //scene.addObject( sphere );
  97. }
  98. //dae.rotation.x = -Math.PI/2;
  99. scene.addObject(dae);
  100. //var wall = dae_geometries['wall-geometry'][0];
  101. //var dae = new THREE.Mesh( wall, materials[3] );
  102. //dae.scale.x = dae.scale.y = dae.scale.z = 100.0;
  103. //scene.addObject(dae);
  104. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  105. scene.addObject( particleLight );
  106. // Lights
  107. scene.addLight( new THREE.AmbientLight( 0x202020 ) );
  108. var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xcccccc);
  109. directionalLight.position.x = Math.random() - 0.5;
  110. directionalLight.position.y = Math.random() - 0.5;
  111. directionalLight.position.z = Math.random() - 0.5;
  112. directionalLight.position.normalize();
  113. scene.addLight( directionalLight );
  114. pointLight = new THREE.PointLight( 0xdddddd, 0.6 );
  115. scene.addLight( pointLight );
  116. renderer = new THREE.WebGLRenderer();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. container.appendChild( renderer.domElement );
  119. stats = new Stats();
  120. stats.domElement.style.position = 'absolute';
  121. stats.domElement.style.top = '0px';
  122. container.appendChild( stats.domElement );
  123. }
  124. function generateTexture() {
  125. var canvas = document.createElement( 'canvas' );
  126. canvas.width = 256;
  127. canvas.height = 256;
  128. var context = canvas.getContext( '2d' );
  129. var image = context.getImageData( 0, 0, 256, 256 );
  130. var x = 0, y = 0;
  131. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  132. x = j % 256;
  133. y = x == 0 ? y + 1 : y;
  134. image.data[ i + 2 ] = Math.floor( x ^ y );
  135. image.data[ i + 3 ] = 255;
  136. }
  137. context.putImageData( image, 0, 0 );
  138. return canvas;
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. var timer = new Date().getTime() * 0.0005;
  148. camera.position.x = Math.cos( timer ) * 10;
  149. camera.position.y = 2;
  150. camera.position.z = Math.sin( timer ) * 10;
  151. particleLight.position.x = Math.sin( timer * 4 ) * 300;
  152. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  153. particleLight.position.z = Math.cos( timer * 4 ) * 300;
  154. pointLight.position.x = particleLight.position.x;
  155. pointLight.position.y = particleLight.position.y;
  156. pointLight.position.z = particleLight.position.z;
  157. renderer.render( scene, camera );
  158. }
  159. </script>
  160. </body>
  161. </html>