webgl_materials.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/Detector.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script>
  21. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var particleLight;
  25. var objects = [], materials = [];
  26. init();
  27. animate();
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  32. camera.position.set( 0, 200, 800 );
  33. scene = new THREE.Scene();
  34. // Grid
  35. var line_material = new THREE.LineBasicMaterial( { color: 0x303030 } ),
  36. geometry = new THREE.Geometry(),
  37. floor = -75, step = 25;
  38. for ( var i = 0; i <= 40; i ++ ) {
  39. geometry.vertices.push( new THREE.Vector3( - 500, floor, i * step - 500 ) );
  40. geometry.vertices.push( new THREE.Vector3( 500, floor, i * step - 500 ) );
  41. geometry.vertices.push( new THREE.Vector3( i * step - 500, floor, -500 ) );
  42. geometry.vertices.push( new THREE.Vector3( i * step - 500, floor, 500 ) );
  43. }
  44. var line = new THREE.LineSegments( geometry, line_material );
  45. scene.add( line );
  46. // Materials
  47. var texture = new THREE.Texture( generateTexture() );
  48. texture.needsUpdate = true;
  49. materials.push( new THREE.MeshLambertMaterial( { map: texture, transparent: true } ) );
  50. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  51. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  52. materials.push( new THREE.MeshNormalMaterial( ) );
  53. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true, 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( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading, map: texture, transparent: true } ) );
  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.MeshLambertMaterial( { color: 0x666666, emissive: 0xff0000, shading: THREE.SmoothShading } ) );
  61. materials.push( new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0xff0000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.9, transparent: true } ) );
  62. materials.push( new THREE.MeshBasicMaterial( { map: texture, transparent: true } ) );
  63. // Spheres geometry
  64. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  65. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  66. var geometry_pieces = new THREE.SphereGeometry( 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. face.materialIndex = Math.floor( Math.random() * materials.length );
  70. }
  71. objects = [];
  72. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  73. var material = materials[ i ];
  74. if ( material.shading == THREE.FlatShading ) {
  75. addMesh( geometry_flat, material );
  76. } else {
  77. addMesh( geometry_smooth, material );
  78. }
  79. }
  80. addMesh( geometry_pieces, new THREE.MeshFaceMaterial( materials ) );
  81. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  82. scene.add( particleLight );
  83. // Lights
  84. scene.add( new THREE.AmbientLight( 0x111111 ) );
  85. var directionalLight = new THREE.DirectionalLight( /*Math.random() * */ 0xffffff, 0.125 );
  86. directionalLight.position.x = Math.random() - 0.5;
  87. directionalLight.position.y = Math.random() - 0.5;
  88. directionalLight.position.z = Math.random() - 0.5;
  89. directionalLight.position.normalize();
  90. scene.add( directionalLight );
  91. var pointLight = new THREE.PointLight( 0xffffff, 1 );
  92. particleLight.add( pointLight );
  93. //
  94. renderer = new THREE.WebGLRenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. //
  99. stats = new Stats();
  100. stats.domElement.style.position = 'absolute';
  101. stats.domElement.style.top = '0px';
  102. container.appendChild( stats.domElement );
  103. //
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. }
  106. function addMesh( geometry, material ) {
  107. var mesh = new THREE.Mesh( geometry, material );
  108. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  109. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  110. mesh.rotation.x = Math.random() * 200 - 100;
  111. mesh.rotation.y = Math.random() * 200 - 100;
  112. mesh.rotation.z = Math.random() * 200 - 100;
  113. objects.push( mesh );
  114. scene.add( mesh );
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function generateTexture() {
  122. var canvas = document.createElement( 'canvas' );
  123. canvas.width = 256;
  124. canvas.height = 256;
  125. var context = canvas.getContext( '2d' );
  126. var image = context.getImageData( 0, 0, 256, 256 );
  127. var x = 0, y = 0;
  128. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  129. x = j % 256;
  130. y = x == 0 ? y + 1 : y;
  131. image.data[ i ] = 255;
  132. image.data[ i + 1 ] = 255;
  133. image.data[ i + 2 ] = 255;
  134. image.data[ i + 3 ] = Math.floor( x ^ y );
  135. }
  136. context.putImageData( image, 0, 0 );
  137. return canvas;
  138. }
  139. //
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. render();
  143. stats.update();
  144. }
  145. function render() {
  146. var timer = 0.0001 * Date.now();
  147. camera.position.x = Math.cos( timer ) * 1000;
  148. camera.position.z = Math.sin( timer ) * 1000;
  149. camera.lookAt( scene.position );
  150. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  151. var object = objects[ i ];
  152. object.rotation.x += 0.01;
  153. object.rotation.y += 0.005;
  154. }
  155. materials[ materials.length - 2 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) );
  156. materials[ materials.length - 3 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) );
  157. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  158. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  159. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  160. renderer.render( scene, camera );
  161. }
  162. </script>
  163. </body>
  164. </html>