hdr.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - HDR texture - webgl</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #000;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. font-weight: bold;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color:#000;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. #oldie {
  24. font-family:monospace;
  25. font-size:13px;
  26. text-align:center;
  27. background:#eee;
  28. color:#000;
  29. padding:1em;
  30. width:475px;
  31. margin:5em auto 0;
  32. display:none;
  33. }
  34. a { color: red; }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="container"></div>
  39. <div id="info">
  40. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl HDR texture example -
  41. based on <a href="http://spidergl.org/example.php?id=13" target="_blank">SpiderGL</a>
  42. </div>
  43. <center>
  44. <div id="oldie">
  45. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
  46. and <a href="http://www.whatwg.org/specs/web-workers/current-work/">Web Workers</a>.<br/>
  47. <br/>
  48. Please try in
  49. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  50. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  51. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  52. </div>
  53. </center>
  54. <script type="text/javascript" src="js/Stats.js"></script>
  55. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  56. <!-- HDR fragment shader -->
  57. <script id="fs-hdr" type="x-shader/x-fragment">
  58. uniform sampler2D tDiffuse;
  59. uniform float exposure;
  60. uniform float brightMax;
  61. varying vec2 vUv;
  62. vec3 decode_pnghdr( const in vec4 color ) {
  63. // remove gamma correction
  64. vec4 res = color * color;
  65. // decoded RI
  66. float ri = pow( 2.0, res.w * 32.0 - 16.0 );
  67. // decoded HDR pixel
  68. res.xyz = res.xyz * ri;
  69. return res.xyz;
  70. }
  71. void main(void)
  72. {
  73. vec4 color = texture2D( tDiffuse, vUv );
  74. color.xyz = decode_pnghdr( color );
  75. // apply gamma correction and exposure
  76. //gl_FragColor = vec4( pow( exposure * color.xyz, vec3( 0.474 ) ), 1.0 );
  77. // Perform tone-mapping
  78. float Y = dot(vec4(0.30, 0.59, 0.11, 0.0), color);
  79. float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0);
  80. color *= YD;
  81. gl_FragColor = vec4( color.xyz, 1.0 );
  82. }
  83. </script>
  84. <!-- HDR vertex shader -->
  85. <script id="vs-hdr" type="x-shader/x-vertex">
  86. varying vec2 vUv;
  87. void main(void)
  88. {
  89. vUv = uv;
  90. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  91. }
  92. </script>
  93. <script type="text/javascript">
  94. if ( !is_browser_compatible() ) {
  95. document.getElementById( "oldie" ).style.display = "block";
  96. }
  97. var container, stats;
  98. var camera, scene, renderer, mesh, directionalLight;
  99. var windowHalfX = window.innerWidth / 2;
  100. var windowHalfY = window.innerHeight / 2;
  101. var materialHDR, quad;
  102. init();
  103. setInterval( loop, 1000 / 60 );
  104. function init() {
  105. container = document.getElementById( 'container' );
  106. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  107. camera.position.z = 900;
  108. scene = new THREE.Scene();
  109. directionalLight = new THREE.DirectionalLight( 0xffffff );
  110. directionalLight.position.x = 0;
  111. directionalLight.position.y = 0;
  112. directionalLight.position.z = 1;
  113. directionalLight.position.normalize();
  114. scene.addLight( directionalLight );
  115. var texture = ImageUtils.loadTexture( "textures/memorial.png" );
  116. texture.min_filter = THREE.LinearFilter;
  117. texture.mag_filter = THREE.NearestFilter;
  118. materialHDR = new THREE.MeshShaderMaterial( {
  119. uniforms: { tDiffuse: { type: "t", value: 0, texture: texture },
  120. exposure: { type: "f", value: 0.125 },
  121. brightMax: { type: "f", value: 0.5 }
  122. },
  123. vertex_shader: getText( 'vs-hdr' ),
  124. fragment_shader: getText( 'fs-hdr' )
  125. } );
  126. var plane = new Plane( 512, 768 );
  127. quad = new THREE.Mesh( plane, materialHDR );
  128. quad.position.z = -100;
  129. scene.addObject( quad );
  130. renderer = new THREE.WebGLRenderer();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. container.appendChild( renderer.domElement );
  133. stats = new Stats();
  134. stats.domElement.style.position = 'absolute';
  135. stats.domElement.style.top = '0px';
  136. container.appendChild( stats.domElement );
  137. }
  138. function getText( id ) {
  139. return document.getElementById( id ).textContent;
  140. }
  141. var delta, current, old = 0, sign = 1, rate = 1;
  142. function loop() {
  143. if ( ! old ) old = new Date().getTime();
  144. current = new Date().getTime();
  145. delta = (current - old ) * 0.005;
  146. old = current;
  147. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  148. rate = 0.25;
  149. } else {
  150. rate = 1;
  151. }
  152. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  153. sign *= -1;
  154. }
  155. materialHDR.uniforms.exposure.value += sign * rate * delta;
  156. // Render final scene to the screen with film shader
  157. renderer.render( scene, camera );
  158. stats.update();
  159. }
  160. function is_browser_compatible() {
  161. // WebGL support
  162. try { var test = new Float32Array(1); } catch(e) { return false; }
  163. // Web workers
  164. return !!window.Worker;
  165. }
  166. </script>
  167. </body>
  168. </html>