materials_gl.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - materials WebGL</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #000;
  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/ThreeExtras.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <script type="text/javascript">
  21. var container, stats;
  22. var camera, scene, renderer, objects;
  23. var particleLight, pointLight;
  24. init();
  25. setInterval( loop, 1000 / 60 );
  26. function init() {
  27. container = document.createElement('div');
  28. document.body.appendChild(container);
  29. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  30. camera.position.y = 200;
  31. camera.position.z = 800;
  32. scene = new THREE.Scene();
  33. // Grid
  34. var geometry = new THREE.Geometry();
  35. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  36. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  37. for ( var i = 0; i <= 10; i ++ ) {
  38. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
  39. line.position.y = - 120;
  40. line.position.z = ( i * 100 ) - 500;
  41. scene.addObject( line );
  42. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
  43. line.position.x = ( i * 100 ) - 500;
  44. line.position.y = - 120;
  45. line.rotation.y = 90 * Math.PI / 180;
  46. scene.addObject( line );
  47. }
  48. var generatedTexture = new THREE.Texture( generateTexture() );
  49. generatedTexture.image.loaded = 1;
  50. var materials = [];
  51. materials.push( { material: new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ), overdraw: true, doubleSided: false } );
  52. materials.push( { material: new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ), overdraw: true, doubleSided: false } );
  53. materials.push( { material: new THREE.MeshNormalMaterial( ), overdraw: true, doubleSided: false } );
  54. materials.push( { material: new THREE.MeshBasicMaterial( { color: 0x665500, blending: THREE.AdditiveBlending } ), overdraw: false, doubleSided: true } );
  55. //materials.push( { material: new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ), overdraw: false, doubleSided: true } );
  56. materials.push( { material: new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ), overdraw: true, doubleSided: false } );
  57. materials.push( { material: new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading } ), overdraw: true, doubleSided: false } );
  58. materials.push( { material: new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ), overdraw: true, doubleSided: false } );
  59. materials.push( { material: new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ), overdraw: false, doubleSided: true } );
  60. materials.push( { material: new THREE.MeshDepthMaterial(), overdraw: true, doubleSided: false } );
  61. materials.push( { material: new THREE.MeshBasicMaterial( { map: generatedTexture } ), overdraw: true, doubleSided: false } );
  62. materials.push( { material: new THREE.MeshLambertMaterial( { map: generatedTexture } ), overdraw: true, doubleSided: false } );
  63. // Spheres geometry
  64. var geometry_smooth = new Sphere( 70, 32, 16 );
  65. var geometry_flat = new Sphere( 70, 32, 16 );
  66. var geometry_pieces = new Sphere( 70, 32, 16 ); // Extra geometry to be broken down for MeshFaceMaterial
  67. for ( var i = 0, l = geometry_pieces.faces.length; i < l; i ++ ) {
  68. var face = geometry_pieces.faces[ i ];
  69. if ( Math.random() > 0.7 ) face.material = [ materials[ Math.floor( Math.random() * materials.length ) ].material ];
  70. }
  71. materials.push( { material: new THREE.MeshFaceMaterial(), overdraw: false, doubleSided: true } );
  72. objects = [];
  73. var sphere, geometry, material;
  74. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  75. material = materials[ i ].material;
  76. geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces :
  77. ( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth );
  78. sphere = new THREE.Mesh( geometry, material );
  79. sphere.overdraw = material.overdraw;
  80. sphere.doubleSided = material.doubleSided;
  81. sphere.position.x = ( i % 4 ) * 200 - 400;
  82. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  83. sphere.rotation.x = Math.random() * 200 - 100;
  84. sphere.rotation.y = Math.random() * 200 - 100;
  85. sphere.rotation.z = Math.random() * 200 - 100;
  86. objects.push( sphere );
  87. scene.addObject( sphere );
  88. }
  89. particleLight = new THREE.Mesh( new Sphere( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  90. scene.addObject( particleLight );
  91. // Lights
  92. scene.addLight( new THREE.AmbientLight( 0x202020 ) );
  93. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  94. directionalLight.position.x = Math.random() - 0.5;
  95. directionalLight.position.y = Math.random() - 0.5;
  96. directionalLight.position.z = Math.random() - 0.5;
  97. directionalLight.position.normalize();
  98. scene.addLight( directionalLight );
  99. pointLight = new THREE.PointLight( 0xffffff, 1 );
  100. scene.addLight( pointLight );
  101. renderer = new THREE.WebGLRenderer();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. container.appendChild( renderer.domElement );
  104. var debugCanvas = document.createElement( 'canvas' );
  105. debugCanvas.width = 512;
  106. debugCanvas.height = 512;
  107. debugCanvas.style.position = 'absolute';
  108. debugCanvas.style.top = '0px';
  109. debugCanvas.style.left = '0px';
  110. container.appendChild( debugCanvas );
  111. debugContext = debugCanvas.getContext( '2d' );
  112. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  113. debugContext.strokeStyle = '#000000';
  114. stats = new Stats();
  115. stats.domElement.style.position = 'absolute';
  116. stats.domElement.style.top = '0px';
  117. container.appendChild(stats.domElement);
  118. }
  119. function generateTexture() {
  120. var canvas = document.createElement( 'canvas' );
  121. canvas.width = 256;
  122. canvas.height = 256;
  123. var context = canvas.getContext( '2d' );
  124. var image = context.getImageData( 0, 0, 256, 256 );
  125. var x = 0, y = 0;
  126. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  127. x = j % 256;
  128. y = x == 0 ? y + 1 : y;
  129. image.data[ i + 2 ] = Math.floor( x ^ y );
  130. image.data[ i + 3 ] = 255;
  131. }
  132. context.putImageData( image, 0, 0 );
  133. return canvas;
  134. }
  135. //
  136. function loop() {
  137. var timer = new Date().getTime() * 0.0001;
  138. camera.position.x = Math.cos( timer ) * 1000;
  139. camera.position.z = Math.sin( timer ) * 1000;
  140. for ( var i = 0, l = objects.length; i < l; i++ ) {
  141. var object = objects[ i ];
  142. object.rotation.x += 0.01;
  143. object.rotation.y += 0.005;
  144. }
  145. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  146. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  147. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  148. pointLight.position.x = particleLight.position.x;
  149. pointLight.position.y = particleLight.position.y;
  150. pointLight.position.z = particleLight.position.z;
  151. renderer.render( scene, camera );
  152. stats.update();
  153. }
  154. function log( text ) {
  155. var e = document.getElementById("log");
  156. e.innerHTML = text + "<br/>" + e.innerHTML;
  157. }
  158. </script>
  159. </body>
  160. </html>