webgl_materials_variations_phong.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. color: #fff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - Phong Material Variantions by <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>
  27. <script src="../build/three.js"></script>
  28. <script src="js/controls/OrbitControls.js"></script>
  29. <script src="js/Detector.js"></script>
  30. <script src="js/libs/stats.min.js"></script>
  31. <script>
  32. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  33. var container, stats;
  34. var camera, scene, renderer, controls, objects = [];
  35. var particleLight;
  36. var loader = new THREE.FontLoader();
  37. loader.load( 'fonts/gentilis_regular.typeface.js', function ( font ) {
  38. init( font );
  39. animate();
  40. } );
  41. function init( font ) {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  45. camera.position.set( 0.0, 400, 400 * 3.5 );
  46. scene = new THREE.Scene();
  47. // Materials
  48. var imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  49. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  50. imgTexture.anisotropy = 16;
  51. imgTexture = null;
  52. var shininess = 50, specular = 0x333333, bumpScale = 1, shading = THREE.SmoothShading;
  53. var materials = [];
  54. var path = "textures/cube/SwedishRoyalCastle/";
  55. var format = '.jpg';
  56. var urls = [
  57. path + 'px' + format, path + 'nx' + format,
  58. path + 'py' + format, path + 'ny' + format,
  59. path + 'pz' + format, path + 'nz' + format
  60. ];
  61. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  62. reflectionCube.format = THREE.RGBFormat;
  63. var cubeWidth = 400;
  64. var numberOfSphersPerSide = 5;
  65. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  66. var stepSize = 1.0 / numberOfSphersPerSide;
  67. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  68. var localReflectionCube;
  69. for( var alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  70. if( alphaIndex % 2 === 0 ) {
  71. localReflectionCube = null;
  72. }
  73. else {
  74. localReflectionCube = reflectionCube;
  75. }
  76. var specularShininess = Math.pow( 2, alpha * 10 );
  77. for( var beta = 0; beta <= 1.0; beta += stepSize ) {
  78. var specularColor = new THREE.Color( beta * 0.2, beta * 0.2, beta * 0.2 );
  79. var reflectivity = beta;
  80. for( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  81. // basic monochromatic energy preservation
  82. var diffuseColor = new THREE.Color( 0, 0, gamma ).multiplyScalar( 1 - beta * 0.2 );
  83. var material = new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: diffuseColor, specular: specularColor, reflectivity: reflectivity, shininess: specularShininess, shading: THREE.SmoothShading, envMap: localReflectionCube } )
  84. var mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.x = alpha * 400 - 200;
  86. mesh.position.y = beta * 400 - 200;
  87. mesh.position.z = gamma * 400 - 200;
  88. objects.push( mesh );
  89. scene.add( mesh );
  90. }
  91. }
  92. }
  93. function addLabel( name, location ) {
  94. var textGeo = new THREE.TextGeometry( name, {
  95. font: font,
  96. size: 20,
  97. height: 1,
  98. curveSegments: 1
  99. });
  100. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  101. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  102. textMesh.position.copy( location );
  103. scene.add( textMesh );
  104. }
  105. addLabel( "-shininess", new THREE.Vector3( -350, 0, 0 ) );
  106. addLabel( "+shininess", new THREE.Vector3( 350, 0, 0 ) );
  107. addLabel( "-specular, -reflectivity", new THREE.Vector3( 0, -300, 0 ) );
  108. addLabel( "+specular, +reflectivity", new THREE.Vector3( 0, 300, 0 ) );
  109. addLabel( "-diffuse", new THREE.Vector3( 0, 0, -300 ) );
  110. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  111. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  112. scene.add( particleLight );
  113. // Lights
  114. scene.add( new THREE.AmbientLight( 0x222222 ) );
  115. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  116. directionalLight.position.set( 1, 1, 1 ).normalize();
  117. scene.add( directionalLight );
  118. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  119. particleLight.add( pointLight );
  120. //
  121. renderer = new THREE.WebGLRenderer( { antialias: true } );
  122. renderer.setClearColor( 0x0a0a0a );
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. renderer.sortObjects = true;
  126. container.appendChild( renderer.domElement );
  127. renderer.gammaInput = true;
  128. renderer.gammaOutput = true;
  129. //
  130. stats = new Stats();
  131. stats.domElement.style.position = 'absolute';
  132. stats.domElement.style.top = '0px';
  133. container.appendChild( stats.domElement );
  134. controls = new THREE.OrbitControls( camera );
  135. controls.target.set( 0, 0, 0 );
  136. controls.update();
  137. window.addEventListener( 'resize', onWindowResize, false );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. stats.begin();
  148. render();
  149. stats.end();
  150. }
  151. function render() {
  152. var timer = Date.now() * 0.00025;
  153. //camera.position.x = Math.cos( timer ) * 800;
  154. //camera.position.z = Math.sin( timer ) * 800;
  155. camera.lookAt( scene.position );
  156. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  157. var object = objects[ i ];
  158. object.rotation.y += 0.005;
  159. }
  160. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  161. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  162. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>