webgl_hdr.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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() {
  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() {
  67. vUv = uv;
  68. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  69. }
  70. </script>
  71. <script type="text/javascript">
  72. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  73. var container, stats;
  74. var camera, scene, renderer, mesh, directionalLight;
  75. var windowHalfX = window.innerWidth / 2;
  76. var windowHalfY = window.innerHeight / 2;
  77. var materialHDR, quad;
  78. var delta, current, old = 0, sign = 1, rate = 1;
  79. init();
  80. animate();
  81. function init() {
  82. container = document.getElementById( 'container' );
  83. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  84. camera.position.z = 900;
  85. scene = new THREE.Scene();
  86. directionalLight = new THREE.DirectionalLight( 0xffffff );
  87. directionalLight.position.x = 0;
  88. directionalLight.position.y = 0;
  89. directionalLight.position.z = 1;
  90. directionalLight.position.normalize();
  91. scene.add( directionalLight );
  92. var texture = THREE.ImageUtils.loadTexture( "textures/memorial.png" );
  93. texture.minFilter = THREE.LinearFilter;
  94. texture.magFilter = THREE.NearestFilter;
  95. materialHDR = new THREE.ShaderMaterial( {
  96. uniforms: {
  97. tDiffuse: { type: "t", value: 0, texture: texture },
  98. exposure: { type: "f", value: 0.125 },
  99. brightMax: { type: "f", value: 0.5 }
  100. },
  101. vertexShader: getText( 'vs-hdr' ),
  102. fragmentShader: getText( 'fs-hdr' )
  103. } );
  104. var plane = new THREE.PlaneGeometry( 512, 768 );
  105. quad = new THREE.Mesh( plane, materialHDR );
  106. quad.position.z = -100;
  107. scene.add( quad );
  108. renderer = new THREE.WebGLRenderer();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. container.appendChild( renderer.domElement );
  111. stats = new Stats();
  112. stats.domElement.style.position = 'absolute';
  113. stats.domElement.style.top = '0px';
  114. container.appendChild( stats.domElement );
  115. }
  116. function getText( id ) {
  117. return document.getElementById( id ).textContent;
  118. }
  119. //
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. stats.update();
  124. }
  125. function render() {
  126. if ( ! old ) old = new Date().getTime();
  127. current = new Date().getTime();
  128. delta = (current - old ) * 0.005;
  129. old = current;
  130. if ( materialHDR.uniforms.exposure.value > 0 || materialHDR.uniforms.exposure.value < 1 ) {
  131. rate = 0.25;
  132. } else {
  133. rate = 1;
  134. }
  135. if ( materialHDR.uniforms.exposure.value > 5 || materialHDR.uniforms.exposure.value <= 0 ) {
  136. sign *= -1;
  137. }
  138. materialHDR.uniforms.exposure.value += sign * rate * delta;
  139. // Render final scene to the screen with film shader
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>