webgl_materials.html 7.2 KB

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