webgl_materials.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.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: 0xffffff, opacity: 0.2 } ),
  36. geometry = new THREE.Geometry(),
  37. floor = -75, step = 25;
  38. for ( var i = 0; i <= 40; i ++ ) {
  39. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, floor, i * step - 500 ) ) );
  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( i * step - 500, floor, -500 ) ) );
  42. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( i * step - 500, floor, 500 ) ) );
  43. }
  44. var line = new THREE.Line( geometry, line_material, THREE.LinePieces );
  45. scene.add( line );
  46. // Materials
  47. var generatedTexture = new THREE.Texture( generateTexture() );
  48. generatedTexture.needsUpdate = true;
  49. var materials = [];
  50. materials.push( new THREE.MeshLambertMaterial( { map: generatedTexture } ) );
  51. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  52. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  53. materials.push( new THREE.MeshNormalMaterial( ) );
  54. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true, blending: THREE.AdditiveBlending } ) );
  55. //materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  56. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  57. 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" ) } ) );
  58. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  59. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  60. materials.push( new THREE.MeshDepthMaterial() );
  61. materials.push( new THREE.MeshBasicMaterial( { map: generatedTexture } ) );
  62. // Spheres geometry
  63. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  64. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  65. var geometry_pieces = new THREE.SphereGeometry( 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. materials.push( new THREE.MeshFaceMaterial() );
  71. objects = [];
  72. var sphere, geometry, material;
  73. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  74. material = materials[ i ];
  75. geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces :
  76. ( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth );
  77. sphere = new THREE.Mesh( geometry, material );
  78. sphere.position.x = ( i % 4 ) * 200 - 400;
  79. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  80. sphere.rotation.x = Math.random() * 200 - 100;
  81. sphere.rotation.y = Math.random() * 200 - 100;
  82. sphere.rotation.z = Math.random() * 200 - 100;
  83. objects.push( sphere );
  84. scene.add( sphere );
  85. }
  86. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  87. scene.add( particleLight );
  88. // Lights
  89. scene.add( new THREE.AmbientLight( 0x202020 ) );
  90. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  91. directionalLight.position.x = Math.random() - 0.5;
  92. directionalLight.position.y = Math.random() - 0.5;
  93. directionalLight.position.z = Math.random() - 0.5;
  94. directionalLight.position.normalize();
  95. scene.add( directionalLight );
  96. pointLight = new THREE.PointLight( 0xffffff, 1 );
  97. scene.add( pointLight );
  98. //
  99. renderer = new THREE.WebGLRenderer();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. //
  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. camera.lookAt( scene.position );
  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>