webgl_hdr.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/Three.js"></script>
  33. <script type="text/javascript" src="js/Detector.js"></script>
  34. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  35. <script type="text/javascript" src="js/Stats.js"></script>
  36. <!-- HDR fragment shader -->
  37. <script id="fs-hdr" type="x-shader/x-fragment">
  38. uniform sampler2D tDiffuse;
  39. uniform float exposure;
  40. uniform float brightMax;
  41. varying vec2 vUv;
  42. vec3 decode_pnghdr( const in vec4 color ) {
  43. // remove gamma correction
  44. vec4 res = color * color;
  45. // decoded RI
  46. float ri = pow( 2.0, res.w * 32.0 - 16.0 );
  47. // decoded HDR pixel
  48. res.xyz = res.xyz * ri;
  49. return res.xyz;
  50. }
  51. void main(void)
  52. {
  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(void)
  68. {
  69. vUv = uv;
  70. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  71. }
  72. </script>
  73. <script type="text/javascript">
  74. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  75. var container, stats;
  76. var camera, scene, renderer, mesh, directionalLight;
  77. var windowHalfX = window.innerWidth / 2;
  78. var windowHalfY = window.innerHeight / 2;
  79. var materialHDR, quad;
  80. var delta, current, old = 0, sign = 1, rate = 1;
  81. init();
  82. animate();
  83. function init() {
  84. container = document.getElementById( 'container' );
  85. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  86. camera.position.z = 900;
  87. scene = new THREE.Scene();
  88. directionalLight = new THREE.DirectionalLight( 0xffffff );
  89. directionalLight.position.x = 0;
  90. directionalLight.position.y = 0;
  91. directionalLight.position.z = 1;
  92. directionalLight.position.normalize();
  93. scene.addLight( directionalLight );
  94. var texture = ImageUtils.loadTexture( "textures/memorial.png" );
  95. texture.min_filter = THREE.LinearFilter;
  96. texture.mag_filter = THREE.NearestFilter;
  97. materialHDR = new THREE.MeshShaderMaterial( {
  98. uniforms: {
  99. tDiffuse: { type: "t", value: 0, texture: texture },
  100. exposure: { type: "f", value: 0.125 },
  101. brightMax: { type: "f", value: 0.5 }
  102. },
  103. vertex_shader: getText( 'vs-hdr' ),
  104. fragment_shader: getText( 'fs-hdr' )
  105. } );
  106. var plane = new Plane( 512, 768 );
  107. quad = new THREE.Mesh( plane, materialHDR );
  108. quad.position.z = -100;
  109. scene.addObject( quad );
  110. renderer = new THREE.WebGLRenderer();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. container.appendChild( renderer.domElement );
  113. stats = new Stats();
  114. stats.domElement.style.position = 'absolute';
  115. stats.domElement.style.top = '0px';
  116. container.appendChild( stats.domElement );
  117. }
  118. function getText( id ) {
  119. return document.getElementById( id ).textContent;
  120. }
  121. //
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. render();
  125. stats.update();
  126. }
  127. function render() {
  128. if ( ! old ) old = new Date().getTime();
  129. current = new Date().getTime();
  130. delta = (current - old ) * 0.005;
  131. old = current;
  132. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  133. rate = 0.25;
  134. } else {
  135. rate = 1;
  136. }
  137. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  138. sign *= -1;
  139. }
  140. materialHDR.uniforms.exposure.value += sign * rate * delta;
  141. // Render final scene to the screen with film shader
  142. renderer.render( scene, camera );
  143. }
  144. function is_browser_compatible() {
  145. // WebGL support
  146. try { var test = new Float32Array(1); } catch(e) { return false; }
  147. // Web workers
  148. return !!window.Worker;
  149. }
  150. </script>
  151. </body>
  152. </html>