webgl_collada.html 7.2 KB

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