webgl_hdr.html 5.5 KB

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