webgl_materials.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.js"></script>
  18. <script src="js/Detector.js"></script>
  19. <script src="js/RequestAnimationFrame.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  22. if ( ! Detector.webgl ) 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.add( line );
  47. // Materials
  48. var generatedTexture = new THREE.Texture( generateTexture() );
  49. generatedTexture.needsUpdate = true;
  50. var materials = [];
  51. materials.push( new THREE.MeshLambertMaterial( { map: generatedTexture } ) );
  52. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  53. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  54. materials.push( new THREE.MeshNormalMaterial( ) );
  55. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true, blending: THREE.AdditiveBlending } ) );
  56. //materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  57. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  58. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ) } ) );
  59. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  60. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  61. materials.push( new THREE.MeshDepthMaterial() );
  62. materials.push( new THREE.MeshBasicMaterial( { map: generatedTexture } ) );
  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. if ( Math.random() > 0.7 ) face.materials = [ materials[ Math.floor( Math.random() * materials.length ) ] ];
  70. }
  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.add( sphere );
  86. }
  87. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  88. scene.add( particleLight );
  89. // Lights
  90. scene.add( 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.add( directionalLight );
  97. pointLight = new THREE.PointLight( 0xffffff, 1 );
  98. scene.add( pointLight );
  99. renderer = new THREE.WebGLRenderer();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. stats = new Stats();
  103. stats.domElement.style.position = 'absolute';
  104. stats.domElement.style.top = '0px';
  105. container.appendChild( stats.domElement );
  106. }
  107. function generateTexture() {
  108. var canvas = document.createElement( 'canvas' );
  109. canvas.width = 256;
  110. canvas.height = 256;
  111. var context = canvas.getContext( '2d' );
  112. var image = context.getImageData( 0, 0, 256, 256 );
  113. var x = 0, y = 0;
  114. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  115. x = j % 256;
  116. y = x == 0 ? y + 1 : y;
  117. image.data[ i + 2 ] = Math.floor( x ^ y );
  118. image.data[ i + 3 ] = 255;
  119. }
  120. context.putImageData( image, 0, 0 );
  121. return canvas;
  122. }
  123. //
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. var timer = new Date().getTime() * 0.0001;
  131. camera.position.x = Math.cos( timer ) * 1000;
  132. camera.position.z = Math.sin( timer ) * 1000;
  133. for ( var i = 0, l = objects.length; i < l; i++ ) {
  134. var object = objects[ i ];
  135. object.rotation.x += 0.01;
  136. object.rotation.y += 0.005;
  137. }
  138. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  139. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  140. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  141. pointLight.position.x = particleLight.position.x;
  142. pointLight.position.y = particleLight.position.y;
  143. pointLight.position.z = particleLight.position.z;
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>