webgl_materials_envmaps_hdr.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs 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. background-image:url("../examples/textures/roughness_map.jpg");
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="container"></div>
  27. <div id="info"><a href="http://threejs.org" target="_blank">threejs</a> - PMREM Generator by Prashant Sharma and <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>
  28. <script src="../build/three.min.js"></script>
  29. <script src="../examples/js/controls/OrbitControls.js"></script>
  30. <script src="../src/loaders/BinaryTextureLoader.js"></script>
  31. <script src="../examples/js/loaders/RGBELoader.js"></script>
  32. <script src="../examples/js/loaders/HDRCubeMapLoader.js"></script>
  33. <script src="../examples/js/Detector.js"></script>
  34. <script src="../examples/js/libs/stats.min.js"></script>
  35. <script src="../examples/js/Half.js"></script>
  36. <script src="../examples/js/Encodings.js"></script>
  37. <script src="../examples/js/pmrem/PMREMGenerator.js"></script>
  38. <script src="../examples/js/pmrem/PMREM_CubeUVPacker.js"></script>
  39. <script src="../examples/js/libs/dat.gui.min.js"></script>
  40. <script src="../examples/js/postprocessing/EffectComposer.js"></script>
  41. <script src="../examples/js/postprocessing/RenderPass.js"></script>
  42. <script src="../examples/js/postprocessing/MaskPass.js"></script>
  43. <script src="../examples/js/postprocessing/ShaderPass.js"></script>
  44. <script src="../examples/js/shaders/CopyShader.js"></script>
  45. <script src="../examples/js/shaders/FXAAShader.js"></script>
  46. <script src="../examples/js/postprocessing/BloomPass.js"></script>
  47. <script src="../examples/js/shaders/ConvolutionShader.js"></script>
  48. <script>
  49. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  50. var container, stats;
  51. var params = {
  52. projection: 'normal',
  53. roughnessFactor: 0,
  54. background: true,
  55. };
  56. var camera, scene, renderer, controls, objects = [];
  57. var hdrCubeMap;
  58. var composer;
  59. var particleLight;
  60. var standardMaterial;
  61. var torusGeometry = new THREE.TorusGeometry( 15, 6, 16, 100 );
  62. //var torusGeometry = new THREE.SphereGeometry( 15, 32, 32 );
  63. var loader = new THREE.FontLoader();
  64. loader.load( '../examples/fonts/gentilis_regular.typeface.js', function ( font ) {
  65. init( font );
  66. animate();
  67. } );
  68. function init( font ) {
  69. container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  72. camera.position.set( 0.0, 40, 40 * 3.5 );
  73. scene = new THREE.Scene();
  74. // Materials
  75. var hdrpath = "../examples/textures/cube/hdrPisa/";
  76. var hdrformat = '.hdr';
  77. var hdrurls = [
  78. hdrpath + 'px' + hdrformat, hdrpath + 'nx' + hdrformat,
  79. hdrpath + 'py' + hdrformat, hdrpath + 'ny' + hdrformat,
  80. hdrpath + 'pz' + hdrformat, hdrpath + 'nz' + hdrformat
  81. ];
  82. var hdrCubeMap = new THREE.HDRCubeMapLoader().load( THREE.HalfFloatType, hdrurls, function ( hdrCubeMap ) {
  83. var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
  84. pmremGenerator.update(renderer);
  85. var pmremCubeUVPacker = new THREE.PMREM_CubeUVPacker(pmremGenerator.cubeLods);
  86. pmremCubeUVPacker.update(renderer);
  87. var roughnessTexture = THREE.ImageUtils.loadTexture( "../examples/textures/roughness_map.jpg" );
  88. roughnessTexture.wrapS = THREE.RepeatWrapping;
  89. roughnessTexture.wrapT = THREE.RepeatWrapping;
  90. roughnessTexture.repeat.set( 3, 3 );
  91. var diffuseColor = new THREE.Color( 1, 1.0, 1.0 ).multiplyScalar( 1 - 0.08 );
  92. standardMaterial = new THREE.MeshStandardMaterial( { map: null, bumpMap: null, roughnessMap: null, bumpScale: 1.0, color: diffuseColor, metalness: 1.0, roughness: 0.5, shading: THREE.SmoothShading, envMap: pmremCubeUVPacker.CubeUVRenderTarget } )
  93. var torusMesh1 = new THREE.Mesh( torusGeometry, standardMaterial );
  94. torusMesh1.position.x = 0.0;
  95. scene.add( torusMesh1 );
  96. } );
  97. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  98. // Lights
  99. scene.add( new THREE.AmbientLight( 0x222222 ) );
  100. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  101. directionalLight.position.set( 1, 1, 1 ).normalize();
  102. //scene.add( directionalLight );
  103. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  104. particleLight.add( pointLight );
  105. var spotLight = new THREE.SpotLight( 0xffffff );
  106. spotLight.position.set( 10, 100, 10 );
  107. spotLight.angle = Math.PI/10;
  108. scene.add( spotLight );
  109. var spotLightHelper = new THREE.SpotLightHelper( spotLight );
  110. scene.add( spotLightHelper );
  111. renderer = new THREE.WebGLRenderer( { alpha:true, antialias: false } );
  112. renderer.setClearColor( 0x0a0a0a, 0 );
  113. renderer.setPixelRatio( window.devicePixelRatio );
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. renderer.sortObjects = true;
  116. container.appendChild( renderer.domElement );
  117. renderer.gammaInput = true;
  118. renderer.gammaOutput = true;
  119. var renderScene = new THREE.RenderPass(scene, camera);
  120. composer = new THREE.EffectComposer(renderer);
  121. composer.setSize(window.innerWidth, window.innerHeight);
  122. composer.addPass(renderScene);
  123. bloomPass = new THREE.BloomPass(1.0, 9, 0.25, 1024);
  124. composer.addPass(bloomPass);
  125. //
  126. effectFXAA = new THREE.ShaderPass(THREE.FXAAShader);
  127. effectFXAA.uniforms['resolution'].value.set(1 / (window.innerWidth), 1 / (window.innerHeight));
  128. effectFXAA.renderToScreen = true;
  129. composer.addPass(effectFXAA);
  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. var gui = new dat.GUI();
  139. //gui.add( params, 'projection', { 'From cam to mesh': 'camera', 'Normal to mesh': 'normal' } );
  140. gui.add( params, 'roughnessFactor', 0, 1 );
  141. gui.add( params, 'background' );
  142. gui.open();
  143. }
  144. function onWindowResize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. //
  150. function animate() {
  151. requestAnimationFrame( animate );
  152. render();
  153. stats.update();
  154. }
  155. function render() {
  156. if(standardMaterial !== undefined)
  157. standardMaterial.roughness = params.roughnessFactor;
  158. var body = document.getElementsByTagName("BODY")[0];
  159. if(params.background)
  160. body.style.backgroundImage = "url('../examples/textures/roughness_map.jpg')";
  161. else
  162. body.style.backgroundImage = "none";
  163. var timer = Date.now() * 0.00025;
  164. //camera.position.x = Math.cos( timer ) * 800;
  165. //camera.position.z = Math.sin( timer ) * 800;
  166. camera.lookAt( scene.position );
  167. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  168. var object = objects[ i ];
  169. object.rotation.y += 0.005;
  170. }
  171. particleLight.position.x = Math.sin( timer * 7 ) * 100;
  172. particleLight.position.y = Math.cos( timer * 5 ) * 100;
  173. particleLight.position.z = Math.cos( timer * 3 ) * 100;
  174. //renderer.render( scene, camera );
  175. composer.render();
  176. }
  177. </script>
  178. </body>
  179. </html>