webgl_materials.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/libs/stats.min.js"></script>
  20. <script>
  21. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var pointLight;
  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 helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  36. helper.position.y = - 75;
  37. scene.add( helper );
  38. // Materials
  39. var texture = new THREE.Texture( generateTexture() );
  40. texture.needsUpdate = true;
  41. materials.push( new THREE.MeshLambertMaterial( { map: texture, transparent: true } ) );
  42. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  43. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  44. materials.push( new THREE.MeshNormalMaterial( ) );
  45. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true, blending: THREE.AdditiveBlending } ) );
  46. //materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  47. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  48. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading, map: texture, transparent: true } ) );
  49. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  50. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  51. materials.push( new THREE.MeshDepthMaterial() );
  52. materials.push( new THREE.MeshLambertMaterial( { color: 0x666666, emissive: 0xff0000, shading: THREE.SmoothShading } ) );
  53. materials.push( new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0xff0000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.9, transparent: true } ) );
  54. materials.push( new THREE.MeshBasicMaterial( { map: texture, transparent: true } ) );
  55. // Spheres geometry
  56. var geometry = new THREE.SphereGeometry( 70, 32, 16 );
  57. for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) {
  58. var face = geometry.faces[ i ];
  59. face.materialIndex = Math.floor( Math.random() * materials.length );
  60. }
  61. geometry.sortFacesByMaterialIndex();
  62. objects = [];
  63. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  64. addMesh( geometry, materials[ i ] );
  65. }
  66. addMesh( geometry, materials );
  67. // Lights
  68. scene.add( new THREE.AmbientLight( 0x111111 ) );
  69. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  70. directionalLight.position.x = Math.random() - 0.5;
  71. directionalLight.position.y = Math.random() - 0.5;
  72. directionalLight.position.z = Math.random() - 0.5;
  73. directionalLight.position.normalize();
  74. scene.add( directionalLight );
  75. pointLight = new THREE.PointLight( 0xffffff, 1 );
  76. scene.add( pointLight );
  77. pointLight.add( new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  78. //
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. //
  84. stats = new Stats();
  85. container.appendChild( stats.dom );
  86. //
  87. window.addEventListener( 'resize', onWindowResize, false );
  88. }
  89. function addMesh( geometry, material ) {
  90. var mesh = new THREE.Mesh( geometry, material );
  91. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  92. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  93. mesh.rotation.x = Math.random() * 200 - 100;
  94. mesh.rotation.y = Math.random() * 200 - 100;
  95. mesh.rotation.z = Math.random() * 200 - 100;
  96. objects.push( mesh );
  97. scene.add( mesh );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function generateTexture() {
  105. var canvas = document.createElement( 'canvas' );
  106. canvas.width = 256;
  107. canvas.height = 256;
  108. var context = canvas.getContext( '2d' );
  109. var image = context.getImageData( 0, 0, 256, 256 );
  110. var x = 0, y = 0;
  111. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  112. x = j % 256;
  113. y = x == 0 ? y + 1 : y;
  114. image.data[ i ] = 255;
  115. image.data[ i + 1 ] = 255;
  116. image.data[ i + 2 ] = 255;
  117. image.data[ i + 3 ] = Math.floor( x ^ y );
  118. }
  119. context.putImageData( image, 0, 0 );
  120. return canvas;
  121. }
  122. //
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. render();
  126. stats.update();
  127. }
  128. function render() {
  129. var timer = 0.0001 * Date.now();
  130. camera.position.x = Math.cos( timer ) * 1000;
  131. camera.position.z = Math.sin( timer ) * 1000;
  132. camera.lookAt( scene.position );
  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. materials[ materials.length - 2 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) );
  139. materials[ materials.length - 3 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) );
  140. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  141. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  142. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  143. renderer.render( scene, camera );
  144. }
  145. </script>
  146. </body>
  147. </html>