webgl_hdr.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - HDR texture</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. a { color: red; }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="container"></div>
  28. <div id="info">
  29. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl HDR texture example -
  30. based on <a href="http://spidergl.org/example.php?id=13" target="_blank">SpiderGL</a>
  31. </div>
  32. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  33. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  34. <script type="text/javascript" src="js/Stats.js"></script>
  35. <!-- HDR fragment shader -->
  36. <script id="fs-hdr" type="x-shader/x-fragment">
  37. uniform sampler2D tDiffuse;
  38. uniform float exposure;
  39. uniform float brightMax;
  40. varying vec2 vUv;
  41. vec3 decode_pnghdr( const in vec4 color ) {
  42. // remove gamma correction
  43. vec4 res = color * color;
  44. // decoded RI
  45. float ri = pow( 2.0, res.w * 32.0 - 16.0 );
  46. // decoded HDR pixel
  47. res.xyz = res.xyz * ri;
  48. return res.xyz;
  49. }
  50. void main(void)
  51. {
  52. vec4 color = texture2D( tDiffuse, vUv );
  53. color.xyz = decode_pnghdr( color );
  54. // apply gamma correction and exposure
  55. //gl_FragColor = vec4( pow( exposure * color.xyz, vec3( 0.474 ) ), 1.0 );
  56. // Perform tone-mapping
  57. float Y = dot(vec4(0.30, 0.59, 0.11, 0.0), color);
  58. float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0);
  59. color *= YD;
  60. gl_FragColor = vec4( color.xyz, 1.0 );
  61. }
  62. </script>
  63. <!-- HDR vertex shader -->
  64. <script id="vs-hdr" type="x-shader/x-vertex">
  65. varying vec2 vUv;
  66. void main(void)
  67. {
  68. vUv = uv;
  69. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  70. }
  71. </script>
  72. <script type="text/javascript">
  73. if ( ! THREE.Detector.webgl ) THREE.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.Camera( 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.x = 0;
  89. directionalLight.position.y = 0;
  90. directionalLight.position.z = 1;
  91. directionalLight.position.normalize();
  92. scene.addLight( directionalLight );
  93. var texture = ImageUtils.loadTexture( "textures/memorial.png" );
  94. texture.min_filter = THREE.LinearFilter;
  95. texture.mag_filter = THREE.NearestFilter;
  96. materialHDR = new THREE.MeshShaderMaterial( {
  97. uniforms: {
  98. tDiffuse: { type: "t", value: 0, texture: texture },
  99. exposure: { type: "f", value: 0.125 },
  100. brightMax: { type: "f", value: 0.5 }
  101. },
  102. vertex_shader: getText( 'vs-hdr' ),
  103. fragment_shader: getText( 'fs-hdr' )
  104. } );
  105. var plane = new Plane( 512, 768 );
  106. quad = new THREE.Mesh( plane, materialHDR );
  107. quad.position.z = -100;
  108. scene.addObject( quad );
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. container.appendChild( renderer.domElement );
  112. stats = new Stats();
  113. stats.domElement.style.position = 'absolute';
  114. stats.domElement.style.top = '0px';
  115. container.appendChild( stats.domElement );
  116. }
  117. function getText( id ) {
  118. return document.getElementById( id ).textContent;
  119. }
  120. //
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. stats.update();
  125. }
  126. function render() {
  127. if ( ! old ) old = new Date().getTime();
  128. current = new Date().getTime();
  129. delta = (current - old ) * 0.005;
  130. old = current;
  131. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  132. rate = 0.25;
  133. } else {
  134. rate = 1;
  135. }
  136. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  137. sign *= -1;
  138. }
  139. materialHDR.uniforms.exposure.value += sign * rate * delta;
  140. // Render final scene to the screen with film shader
  141. renderer.render( scene, camera );
  142. }
  143. function is_browser_compatible() {
  144. // WebGL support
  145. try { var test = new Float32Array(1); } catch(e) { return false; }
  146. // Web workers
  147. return !!window.Worker;
  148. }
  149. </script>
  150. </body>
  151. </html>