hdr.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. varying vec2 vUv;
  61. vec3 decode_pnghdr( const in vec4 color ) {
  62. // remove gamma correction
  63. vec4 res = color * color;
  64. // decoded RI
  65. float ri = pow( 2.0, res.w * 32.0 - 16.0 );
  66. // decoded HDR pixel
  67. res.xyz = res.xyz * ri;
  68. return res.xyz;
  69. }
  70. void main(void)
  71. {
  72. vec4 color = texture2D( tDiffuse, vUv );
  73. color.xyz = decode_pnghdr( color );
  74. // apply gamma correction and exposure
  75. gl_FragData[0] = vec4( pow( exposure * color.xyz, vec3( 0.474 ) ), 1.0 );
  76. }
  77. </script>
  78. <!-- HDR vertex shader -->
  79. <script id="vs-hdr" type="x-shader/x-vertex">
  80. varying vec2 vUv;
  81. void main(void)
  82. {
  83. vUv = uv;
  84. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  85. }
  86. </script>
  87. <script type="text/javascript">
  88. if ( !is_browser_compatible() ) {
  89. document.getElementById( "oldie" ).style.display = "block";
  90. }
  91. var container, stats;
  92. var camera, scene, renderer, mesh, directionalLight;
  93. var windowHalfX = window.innerWidth / 2;
  94. var windowHalfY = window.innerHeight / 2;
  95. var materialHDR, quad;
  96. init();
  97. setInterval( loop, 1000 / 60 );
  98. function init() {
  99. container = document.getElementById( 'container' );
  100. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  101. camera.position.z = 900;
  102. scene = new THREE.Scene();
  103. directionalLight = new THREE.DirectionalLight( 0xffffff );
  104. directionalLight.position.x = 0;
  105. directionalLight.position.y = 0;
  106. directionalLight.position.z = 1;
  107. directionalLight.position.normalize();
  108. scene.addLight( directionalLight );
  109. var texture = ImageUtils.loadTexture( "textures/memorial.png" );
  110. texture.min_filter = THREE.LinearFilter;
  111. texture.mag_filter = THREE.NearestFilter;
  112. materialHDR = new THREE.MeshShaderMaterial( {
  113. uniforms: { tDiffuse: { type: "t", value: 0, texture: texture },
  114. exposure: { type: "f", value: 0.125 }
  115. },
  116. vertex_shader: getText( 'vs-hdr' ),
  117. fragment_shader: getText( 'fs-hdr' )
  118. } );
  119. var plane = new Plane( 512, 768 );
  120. quad = new THREE.Mesh( plane, materialHDR );
  121. quad.position.z = -100;
  122. scene.addObject( quad );
  123. renderer = new THREE.WebGLRenderer();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. container.appendChild( renderer.domElement );
  126. stats = new Stats();
  127. stats.domElement.style.position = 'absolute';
  128. stats.domElement.style.top = '0px';
  129. container.appendChild( stats.domElement );
  130. }
  131. function getText( id ) {
  132. return document.getElementById( id ).textContent;
  133. }
  134. var delta, current, old = 0, sign = 1, rate = 1;
  135. function loop() {
  136. if ( ! old ) old = new Date().getTime();
  137. current = new Date().getTime();
  138. delta = (current - old ) * 0.005;
  139. old = current;
  140. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  141. rate = 0.25;
  142. } else {
  143. rate = 1;
  144. }
  145. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  146. sign *= -1;
  147. }
  148. materialHDR.uniforms.exposure.value += sign * rate * delta;
  149. // Render final scene to the screen with film shader
  150. renderer.render( scene, camera );
  151. stats.update();
  152. }
  153. function is_browser_compatible() {
  154. // WebGL support
  155. try { var test = new Float32Array(1); } catch(e) { return false; }
  156. // Web workers
  157. return !!window.Worker;
  158. }
  159. </script>
  160. </body>
  161. </html>