webgl_materials.html 6.8 KB

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