webgl_tonemapping.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. 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">threejs</a> - Inline Tone Mapping (within a Material's fragment shader) without using a pre-processing step or float/half buffers by <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>
  27. <script src="../build/three.min.js"></script>
  28. <script src="../examples/js/controls/OrbitControls.js"></script>
  29. <script src="../src/loaders/BinaryTextureLoader.js"></script>
  30. <script src="../examples/js/Detector.js"></script>
  31. <script src="../examples/js/libs/stats.min.js"></script>
  32. <script src="../examples/js/libs/dat.gui.min.js"></script>
  33. <script src="../src/loaders/BinaryTextureLoader.js"></script>
  34. <script src="../examples/js/loaders/RGBELoader.js"></script>
  35. <script src="../examples/js/loaders/HDRCubeMapLoader.js"></script>
  36. <script src="../examples/js/Half.js"></script>
  37. <script src="../examples/js/Encodings.js"></script>
  38. <script src="../examples/js/pmrem/PMREMGenerator.js"></script>
  39. <script src="../examples/js/pmrem/PMREMCubeUVPacker.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. opacity: 1.0,
  53. roughness: 1.0,
  54. bumpScale: 1.0,
  55. exposure: 3.0,
  56. whitePoint: 1.0,
  57. toneMapping: "Cineon",
  58. renderMode: "Renderer"
  59. };
  60. var toneMappingOptions = {
  61. None: THREE.NoToneMapping,
  62. Linear: THREE.LinearToneMapping,
  63. Reinhard: THREE.ReinhardToneMapping,
  64. Uncharted2: THREE.Uncharted2ToneMapping,
  65. Cineon: THREE.CineonToneMapping
  66. };
  67. var camera, scene, renderer, controls, objects = [];
  68. var composer;
  69. var standardMaterial, standardMaterialPremultiplied, floorMaterial;
  70. init();
  71. animate();
  72. function init() {
  73. container = document.createElement( 'div' );
  74. document.body.appendChild( container );
  75. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  76. camera.position.set( 0.0, 40, 40 * 3.5 );
  77. scene = new THREE.Scene();
  78. renderer = new THREE.WebGLRenderer( { antialias: false } );
  79. standardMaterial = new THREE.MeshStandardMaterial( {
  80. bumpScale: - 0.05,
  81. color: 0xffffff,
  82. metalness: 0.9,
  83. roughness: 0.8,
  84. shading: THREE.SmoothShading,
  85. transparent: true,
  86. blending: THREE.PremultipliedAlphaBlending
  87. } );
  88. var textureLoader = new THREE.TextureLoader();
  89. textureLoader.load( "../examples/textures/brick_diffuse.jpg", function( map ) {
  90. map.wrapS = THREE.RepeatWrapping;
  91. map.wrapT = THREE.RepeatWrapping;
  92. map.repeat.set( 9, 0.5 );
  93. standardMaterial.map = map;
  94. standardMaterial.needsUpdate = true;
  95. } );
  96. textureLoader.load( "../examples/textures/brick_bump.jpg", function( map ) {
  97. map.wrapS = THREE.RepeatWrapping;
  98. map.wrapT = THREE.RepeatWrapping;
  99. map.repeat.set( 9, 0.5 );
  100. standardMaterial.bumpMap = map;
  101. standardMaterial.needsUpdate = true;
  102. } );
  103. textureLoader.load( "../examples/textures/brick_roughness.jpg", function( map ) {
  104. map.wrapS = THREE.RepeatWrapping;
  105. map.wrapT = THREE.RepeatWrapping;
  106. map.repeat.set( 9, 0.5 );
  107. standardMaterial.roughnessMap = map;
  108. standardMaterial.needsUpdate = true;
  109. } );
  110. var geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );;
  111. var torusMesh1 = new THREE.Mesh( geometry, standardMaterial );
  112. torusMesh1.position.x = 0.0;
  113. torusMesh1.castShadow = true;
  114. torusMesh1.receiveShadow = true;
  115. scene.add( torusMesh1 );
  116. objects.push( torusMesh1 );
  117. floorMaterial = new THREE.MeshStandardMaterial( {
  118. map: null,
  119. roughnessMap: null,
  120. color: 0xffffff,
  121. metalness: 0.0,
  122. roughness: 0.0,
  123. shading: THREE.SmoothShading
  124. } );
  125. var planeGeometry = new THREE.PlaneBufferGeometry( 200, 200 );
  126. var planeMesh1 = new THREE.Mesh( planeGeometry, floorMaterial );
  127. planeMesh1.position.y = - 50;
  128. planeMesh1.rotation.x = - Math.PI * 0.5;
  129. planeMesh1.receiveShadow = true;
  130. scene.add( planeMesh1 );
  131. // Materials
  132. var hdrpath = "../examples/textures/cube/hdrPisa/";
  133. var hdrformat = '.hdr';
  134. var hdrurls = [
  135. hdrpath + 'px' + hdrformat, hdrpath + 'nx' + hdrformat,
  136. hdrpath + 'py' + hdrformat, hdrpath + 'ny' + hdrformat,
  137. hdrpath + 'pz' + hdrformat, hdrpath + 'nz' + hdrformat
  138. ];
  139. var hdrCubeMap = new THREE.HDRCubeMapLoader().load( THREE.UnsignedByteType, hdrurls, function ( hdrCubeMap ) {
  140. var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
  141. pmremGenerator.update( renderer );
  142. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  143. pmremCubeUVPacker.update( renderer );
  144. standardMaterial.envMap = pmremCubeUVPacker.CubeUVRenderTarget;
  145. standardMaterial.needsUpdate = true;
  146. } );
  147. // Lights
  148. scene.add( new THREE.AmbientLight( 0x222222 ) );
  149. var spotLight = new THREE.SpotLight( 0xffffff );
  150. spotLight.position.set( 50, 100, 50 );
  151. spotLight.angle = Math.PI / 7;
  152. spotLight.penumbra = 0.8
  153. spotLight.castShadow = true;
  154. scene.add( spotLight );
  155. renderer.setPixelRatio( window.devicePixelRatio );
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. renderer.shadowMap.enabled = true;
  158. container.appendChild( renderer.domElement );
  159. renderer.gammaInput = true;
  160. renderer.gammaOutput = true;
  161. composer = new THREE.EffectComposer( renderer );
  162. composer.setSize( window.innerWidth, window.innerHeight );
  163. var renderScene = new THREE.RenderPass( scene, camera );
  164. composer.addPass( renderScene );
  165. var copyPass = new THREE.ShaderPass( THREE.CopyShader );
  166. copyPass.renderToScreen = true;
  167. composer.addPass( copyPass );
  168. stats = new Stats();
  169. stats.domElement.style.position = 'absolute';
  170. stats.domElement.style.top = '0px';
  171. container.appendChild( stats.domElement );
  172. controls = new THREE.OrbitControls( camera, renderer.domElement );
  173. controls.target.set( 0, 0, 0 );
  174. controls.update();
  175. window.addEventListener( 'resize', onWindowResize, false );
  176. var gui = new dat.GUI();
  177. gui.add( params, 'toneMapping', Object.keys( toneMappingOptions ) );
  178. gui.add( params, 'exposure', 0, 10 );
  179. gui.add( params, 'whitePoint', 0, 10 );
  180. gui.add( params, 'opacity', 0, 1 );
  181. gui.add( params, 'renderMode', [ 'Renderer', 'Composer'] );
  182. gui.open();
  183. }
  184. function onWindowResize() {
  185. var width = window.innerWidth;
  186. var height = window.innerHeight;
  187. camera.aspect = width / height;
  188. camera.updateProjectionMatrix();
  189. renderer.setSize( width, height );
  190. composer.setSize( width, height );
  191. }
  192. //
  193. function animate() {
  194. requestAnimationFrame( animate );
  195. render();
  196. stats.update();
  197. }
  198. function render() {
  199. if ( standardMaterial !== undefined ) {
  200. standardMaterial.roughness = params.roughness;
  201. standardMaterial.bumpScale = - 0.05 * params.bumpScale;
  202. standardMaterial.opacity = params.opacity;
  203. }
  204. if( renderer.toneMapping !== toneMappingOptions[ params.toneMapping ] ) {
  205. renderer.toneMapping = toneMappingOptions[ params.toneMapping ];
  206. if( standardMaterial ) standardMaterial.needsUpdate = true;
  207. if( floorMaterial ) floorMaterial.needsUpdate = true;
  208. }
  209. renderer.toneMappingExposure = params.exposure;
  210. renderer.toneMappingWhitePoint = params.whitePoint;
  211. var timer = Date.now() * 0.00025;
  212. camera.lookAt( scene.position );
  213. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  214. var object = objects[ i ];
  215. object.rotation.y += 0.005;
  216. }
  217. if( params.renderMode === "Composer" ) {
  218. composer.render();
  219. }
  220. else {
  221. renderer.render( scene, camera );
  222. }
  223. }
  224. </script>
  225. </body>
  226. </html>