webgl_hdr.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - HDR texture</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: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. font-weight: bold;
  14. background-color: #fff;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. color:#000;
  20. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  23. }
  24. a { color: red; }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info">
  30. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl HDR texture example -
  31. based on <a href="http://spidergl.org/example.php?id=13" target="_blank">SpiderGL</a>
  32. </div>
  33. <script src="../build/Three.js"></script>
  34. <script src="js/Detector.js"></script>
  35. <script src="js/RequestAnimationFrame.js"></script>
  36. <script src="js/Stats.js"></script>
  37. <!-- HDR fragment shader -->
  38. <script id="fs-hdr" type="x-shader/x-fragment">
  39. uniform sampler2D tDiffuse;
  40. uniform float exposure;
  41. uniform float brightMax;
  42. varying vec2 vUv;
  43. vec3 decode_pnghdr( const in vec4 color ) {
  44. // remove gamma correction
  45. vec4 res = color * color;
  46. // decoded RI
  47. float ri = pow( 2.0, res.w * 32.0 - 16.0 );
  48. // decoded HDR pixel
  49. res.xyz = res.xyz * ri;
  50. return res.xyz;
  51. }
  52. void main() {
  53. vec4 color = texture2D( tDiffuse, vUv );
  54. color.xyz = decode_pnghdr( color );
  55. // apply gamma correction and exposure
  56. //gl_FragColor = vec4( pow( exposure * color.xyz, vec3( 0.474 ) ), 1.0 );
  57. // Perform tone-mapping
  58. float Y = dot(vec4(0.30, 0.59, 0.11, 0.0), color);
  59. float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0);
  60. color *= YD;
  61. gl_FragColor = vec4( color.xyz, 1.0 );
  62. }
  63. </script>
  64. <!-- HDR vertex shader -->
  65. <script id="vs-hdr" type="x-shader/x-vertex">
  66. varying vec2 vUv;
  67. void main() {
  68. vUv = uv;
  69. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  70. }
  71. </script>
  72. <script>
  73. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  74. var container, stats;
  75. var camera, scene, renderer, mesh, directionalLight;
  76. var windowHalfX = window.innerWidth / 2;
  77. var windowHalfY = window.innerHeight / 2;
  78. var materialHDR, quad;
  79. var delta, current, old = 0, sign = 1, rate = 1;
  80. init();
  81. animate();
  82. function init() {
  83. container = document.getElementById( 'container' );
  84. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  85. camera.position.z = 900;
  86. scene = new THREE.Scene();
  87. directionalLight = new THREE.DirectionalLight( 0xffffff );
  88. directionalLight.position.set( 0, 0, 1 ).normalize();
  89. scene.add( directionalLight );
  90. var texture = THREE.ImageUtils.loadTexture( "textures/memorial.png" );
  91. texture.minFilter = THREE.LinearFilter;
  92. texture.magFilter = THREE.NearestFilter;
  93. materialHDR = new THREE.ShaderMaterial( {
  94. uniforms: {
  95. tDiffuse: { type: "t", value: 0, texture: texture },
  96. exposure: { type: "f", value: 0.125 },
  97. brightMax: { type: "f", value: 0.5 }
  98. },
  99. vertexShader: getText( 'vs-hdr' ),
  100. fragmentShader: getText( 'fs-hdr' )
  101. } );
  102. var plane = new THREE.PlaneGeometry( 512, 768 );
  103. quad = new THREE.Mesh( plane, materialHDR );
  104. quad.position.z = -100;
  105. scene.add( quad );
  106. renderer = new THREE.WebGLRenderer();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. container.appendChild( renderer.domElement );
  109. stats = new Stats();
  110. stats.domElement.style.position = 'absolute';
  111. stats.domElement.style.top = '0px';
  112. container.appendChild( stats.domElement );
  113. }
  114. function getText( id ) {
  115. return document.getElementById( id ).textContent;
  116. }
  117. //
  118. function animate() {
  119. requestAnimationFrame( animate );
  120. render();
  121. stats.update();
  122. }
  123. function render() {
  124. if ( ! old ) old = new Date().getTime();
  125. current = new Date().getTime();
  126. delta = (current - old ) * 0.005;
  127. old = current;
  128. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  129. rate = 0.25;
  130. } else {
  131. rate = 1;
  132. }
  133. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  134. sign *= -1;
  135. }
  136. materialHDR.uniforms.exposure.value += sign * rate * delta;
  137. // Render final scene to the screen with film shader
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>