webgl_materials.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.MeshLambertMaterial( { color: 0xdddddd } ) );
  49. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, map: texture, transparent: true } ) );
  50. materials.push( new THREE.MeshNormalMaterial( { flatShading: true } ) );
  51. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  52. materials.push( new THREE.MeshDepthMaterial() );
  53. materials.push( new THREE.MeshLambertMaterial( { color: 0x666666, emissive: 0xff0000 } ) );
  54. materials.push( new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0xff0000, shininess: 10, opacity: 0.9, transparent: true } ) );
  55. materials.push( new THREE.MeshBasicMaterial( { map: texture, transparent: true } ) );
  56. // Spheres geometry
  57. var geometry = new THREE.SphereBufferGeometry( 70, 32, 16 );
  58. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  59. addMesh( geometry, materials[ i ] );
  60. }
  61. // Lights
  62. scene.add( new THREE.AmbientLight( 0x111111 ) );
  63. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  64. directionalLight.position.x = Math.random() - 0.5;
  65. directionalLight.position.y = Math.random() - 0.5;
  66. directionalLight.position.z = Math.random() - 0.5;
  67. directionalLight.position.normalize();
  68. scene.add( directionalLight );
  69. pointLight = new THREE.PointLight( 0xffffff, 1 );
  70. scene.add( pointLight );
  71. pointLight.add( new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  72. //
  73. renderer = new THREE.WebGLRenderer( { antialias: true } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. container.appendChild( renderer.domElement );
  77. //
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. //
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. }
  83. function addMesh( geometry, material ) {
  84. var mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  86. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  87. mesh.rotation.x = Math.random() * 200 - 100;
  88. mesh.rotation.y = Math.random() * 200 - 100;
  89. mesh.rotation.z = Math.random() * 200 - 100;
  90. objects.push( mesh );
  91. scene.add( mesh );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function generateTexture() {
  99. var canvas = document.createElement( 'canvas' );
  100. canvas.width = 256;
  101. canvas.height = 256;
  102. var context = canvas.getContext( '2d' );
  103. var image = context.getImageData( 0, 0, 256, 256 );
  104. var x = 0, y = 0;
  105. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  106. x = j % 256;
  107. y = x == 0 ? y + 1 : y;
  108. image.data[ i ] = 255;
  109. image.data[ i + 1 ] = 255;
  110. image.data[ i + 2 ] = 255;
  111. image.data[ i + 3 ] = Math.floor( x ^ y );
  112. }
  113. context.putImageData( image, 0, 0 );
  114. return canvas;
  115. }
  116. //
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. var timer = 0.0001 * Date.now();
  124. camera.position.x = Math.cos( timer ) * 1000;
  125. camera.position.z = Math.sin( timer ) * 1000;
  126. camera.lookAt( scene.position );
  127. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  128. var object = objects[ i ];
  129. object.rotation.x += 0.01;
  130. object.rotation.y += 0.005;
  131. }
  132. materials[ materials.length - 2 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) );
  133. materials[ materials.length - 3 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) );
  134. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  135. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  136. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>