webgl_materials.html 6.8 KB

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