webgl_materials.html 6.2 KB

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