webgl_shaders_tonemapping.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - adaptive tone-mapping</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. .label {
  10. background-color: black;
  11. position: absolute;
  12. z-index: 100;
  13. padding: 5px
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="d">
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl demo :
  21. Earth diffuse and city lights by <a href="http://seanward.org" target="_blank" rel="noopener">Sean Ward</a> :
  22. </div>
  23. <div class="label" style="position: absolute;left: 10%;bottom: 10%;">
  24. Low Dynamic Range<br/>
  25. Static Tone Mapping
  26. </div>
  27. <div class="label" style="position: absolute;left: 40%;bottom: 10%;">
  28. High Dynamic Range<br/>
  29. Static Tone Mapping
  30. </div>
  31. <div class="label" style="position: absolute;left: 73%;bottom: 10%;">
  32. High Dynamic Range<br/>
  33. Adaptive Tone Mapping
  34. </div>
  35. </div>
  36. <script type="module">
  37. import * as THREE from '../build/three.module.js';
  38. import { GUI } from './jsm/libs/dat.gui.module.js';
  39. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  40. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  41. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  42. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  43. import { AdaptiveToneMappingPass } from './jsm/postprocessing/AdaptiveToneMappingPass.js';
  44. import { BloomPass } from './jsm/postprocessing/BloomPass.js';
  45. import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
  46. var container;
  47. var bloomPass, adaptToneMappingPass, ldrToneMappingPass, hdrToneMappingPass;
  48. var params;
  49. var camera, scene, renderer, dynamicHdrEffectComposer, hdrEffectComposer, ldrEffectComposer;
  50. var cameraCube, sceneCube;
  51. var cameraBG, debugScene;
  52. var adaptiveLuminanceMat, currentLuminanceMat;
  53. var directionalLight;
  54. var orbitControls;
  55. var windowHalfX = window.innerWidth / 2;
  56. var windowHalfY = window.innerHeight / 2;
  57. var windowThirdX = window.innerWidth / 3;
  58. init();
  59. animate();
  60. function init() {
  61. params = {
  62. bloomAmount: 1.0,
  63. sunLight: 4.0,
  64. enabled: true,
  65. avgLuminance: 0.7,
  66. middleGrey: 0.04,
  67. maxLuminance: 16,
  68. adaptionRate: 2.0
  69. };
  70. container = document.createElement( 'div' );
  71. document.body.appendChild( container );
  72. // CAMERAS
  73. camera = new THREE.PerspectiveCamera( 70, windowThirdX / window.innerHeight, 0.1, 100000 );
  74. camera.position.x = 700;
  75. camera.position.y = 400;
  76. camera.position.z = 800;
  77. cameraCube = new THREE.PerspectiveCamera( 70, windowThirdX / window.innerHeight, 1, 100000 );
  78. cameraBG = new THREE.OrthographicCamera( - windowHalfX, windowHalfX, windowHalfY, - windowHalfY, - 10000, 10000 );
  79. cameraBG.position.z = 100;
  80. orbitControls = new OrbitControls( camera, container );
  81. orbitControls.autoRotate = true;
  82. orbitControls.autoRotateSpeed = 1;
  83. // SCENE
  84. scene = new THREE.Scene();
  85. sceneCube = new THREE.Scene();
  86. debugScene = new THREE.Scene();
  87. // LIGHTS
  88. var ambient = new THREE.AmbientLight( 0x050505 );
  89. scene.add( ambient );
  90. directionalLight = new THREE.DirectionalLight( 0xffffff, params.sunLight );
  91. directionalLight.position.set( 2, 0, 10 ).normalize();
  92. scene.add( directionalLight );
  93. var atmoShader = {
  94. side: THREE.BackSide,
  95. // blending: THREE.AdditiveBlending,
  96. transparent: true,
  97. lights: true,
  98. uniforms: THREE.UniformsUtils.merge( [
  99. THREE.UniformsLib[ "common" ],
  100. THREE.UniformsLib[ "lights" ]
  101. ] ),
  102. vertexShader: [
  103. "varying vec3 vViewPosition;",
  104. "varying vec3 vNormal;",
  105. "void main() {",
  106. THREE.ShaderChunk[ "beginnormal_vertex" ],
  107. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  108. " vNormal = normalize( transformedNormal );",
  109. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  110. "vViewPosition = -mvPosition.xyz;",
  111. "gl_Position = projectionMatrix * mvPosition;",
  112. "}"
  113. ].join( "\n" ),
  114. fragmentShader: [
  115. THREE.ShaderChunk[ "common" ],
  116. THREE.ShaderChunk[ "bsdfs" ],
  117. THREE.ShaderChunk[ "lights_pars_begin" ],
  118. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  119. "void main() {",
  120. "vec3 normal = normalize( -vNormal );",
  121. "vec3 viewPosition = normalize( vViewPosition );",
  122. "#if NUM_DIR_LIGHTS > 0",
  123. "vec3 dirDiffuse = vec3( 0.0 );",
  124. "for( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {",
  125. "vec4 lDirection = viewMatrix * vec4( directionalLights[i].direction, 0.0 );",
  126. "vec3 dirVector = normalize( lDirection.xyz );",
  127. "float dotProduct = dot( viewPosition, dirVector );",
  128. "dotProduct = 1.0 * max( dotProduct, 0.0 ) + (1.0 - max( -dot( normal, dirVector ), 0.0 ));",
  129. "dotProduct *= dotProduct;",
  130. "dirDiffuse += max( 0.5 * dotProduct, 0.0 ) * directionalLights[i].color;",
  131. "}",
  132. "#endif",
  133. //Fade out atmosphere at edge
  134. "float viewDot = abs(dot( normal, viewPosition ));",
  135. "viewDot = clamp( pow( viewDot + 0.6, 10.0 ), 0.0, 1.0);",
  136. "vec3 color = vec3( 0.05, 0.09, 0.13 ) * dirDiffuse;",
  137. "gl_FragColor = vec4( color, viewDot );",
  138. "}"
  139. ].join( "\n" )
  140. };
  141. var earthAtmoMat = new THREE.ShaderMaterial( atmoShader );
  142. var earthMat = new THREE.MeshPhongMaterial( {
  143. color: 0xffffff,
  144. shininess: 200
  145. } );
  146. var textureLoader = new THREE.TextureLoader();
  147. textureLoader.load( 'textures/planets/earth_atmos_4096.jpg', function ( tex ) {
  148. earthMat.map = tex;
  149. earthMat.map.encoding = THREE.sRGBEncoding;
  150. earthMat.needsUpdate = true;
  151. } );
  152. textureLoader.load( 'textures/planets/earth_specular_2048.jpg', function ( tex ) {
  153. earthMat.specularMap = tex;
  154. earthMat.specularMap.encoding = THREE.sRGBEncoding;
  155. earthMat.needsUpdate = true;
  156. } );
  157. // var earthNormal = textureLoader.load( 'textures/planets/earth-new-normal-2048.jpg', function( tex ) {
  158. // earthMat.normalMap = tex;
  159. // earthMat.needsUpdate = true;
  160. // } );
  161. var earthLights = textureLoader.load( 'textures/planets/earth_lights_2048.png' );
  162. earthLights.encoding = THREE.sRGBEncoding;
  163. var earthLightsMat = new THREE.MeshBasicMaterial( {
  164. color: 0xffffff,
  165. blending: THREE.AdditiveBlending,
  166. transparent: true,
  167. depthTest: false,
  168. map: earthLights,
  169. } );
  170. var clouds = textureLoader.load( 'textures/planets/earth_clouds_2048.png' );
  171. clouds.encoding = THREE.sRGBEncoding;
  172. var earthCloudsMat = new THREE.MeshLambertMaterial( {
  173. color: 0xffffff,
  174. blending: THREE.NormalBlending,
  175. transparent: true,
  176. depthTest: false,
  177. map: clouds
  178. } );
  179. var earthGeo = new THREE.SphereBufferGeometry( 600, 24, 24 );
  180. var sphereMesh = new THREE.Mesh( earthGeo, earthMat );
  181. scene.add( sphereMesh );
  182. var sphereLightsMesh = new THREE.Mesh( earthGeo, earthLightsMat );
  183. scene.add( sphereLightsMesh );
  184. var sphereCloudsMesh = new THREE.Mesh( earthGeo, earthCloudsMat );
  185. scene.add( sphereCloudsMesh );
  186. var sphereAtmoMesh = new THREE.Mesh( earthGeo, earthAtmoMat );
  187. sphereAtmoMesh.scale.set( 1.05, 1.05, 1.05 );
  188. scene.add( sphereAtmoMesh );
  189. var vBGShader = [
  190. // "attribute vec2 uv;",
  191. "varying vec2 vUv;",
  192. "void main() {",
  193. "vUv = uv;",
  194. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  195. "}"
  196. ].join( "\n" );
  197. var pBGShader = [
  198. "uniform sampler2D map;",
  199. "varying vec2 vUv;",
  200. "void main() {",
  201. "vec2 sampleUV = vUv;",
  202. "vec4 color = texture2D( map, sampleUV, 0.0 );",
  203. "gl_FragColor = vec4( color.xyz, 1.0 );",
  204. "}"
  205. ].join( "\n" );
  206. // Skybox
  207. adaptiveLuminanceMat = new THREE.ShaderMaterial( {
  208. uniforms: {
  209. "map": { value: null }
  210. },
  211. vertexShader: vBGShader,
  212. fragmentShader: pBGShader,
  213. depthTest: false,
  214. // color: 0xffffff
  215. blending: THREE.NoBlending
  216. } );
  217. currentLuminanceMat = new THREE.ShaderMaterial( {
  218. uniforms: {
  219. "map": { value: null }
  220. },
  221. vertexShader: vBGShader,
  222. fragmentShader: pBGShader,
  223. depthTest: false
  224. // color: 0xffffff
  225. // blending: THREE.NoBlending
  226. } );
  227. var quadBG = new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.1, 0.1 ), currentLuminanceMat );
  228. quadBG.position.z = - 500;
  229. quadBG.position.x = - window.innerWidth * 0.5 + window.innerWidth * 0.05;
  230. quadBG.scale.set( window.innerWidth, window.innerHeight, 1 );
  231. debugScene.add( quadBG );
  232. quadBG = new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.1, 0.1 ), adaptiveLuminanceMat );
  233. quadBG.position.z = - 500;
  234. quadBG.position.x = - window.innerWidth * 0.5 + window.innerWidth * 0.15;
  235. quadBG.scale.set( window.innerWidth, window.innerHeight, 1 );
  236. debugScene.add( quadBG );
  237. var r = "textures/cube/MilkyWay/";
  238. var urls = [ r + "dark-s_px.jpg", r + "dark-s_nx.jpg",
  239. r + "dark-s_py.jpg", r + "dark-s_ny.jpg",
  240. r + "dark-s_pz.jpg", r + "dark-s_nz.jpg" ];
  241. var textureCube = new THREE.CubeTextureLoader().load( urls );
  242. textureCube.format = THREE.RGBFormat;
  243. textureCube.encoding = THREE.sRGBEncoding;
  244. sceneCube.background = textureCube;
  245. renderer = new THREE.WebGLRenderer();
  246. renderer.setPixelRatio( window.devicePixelRatio );
  247. renderer.setSize( window.innerWidth, window.innerHeight );
  248. renderer.autoClear = false;
  249. container.appendChild( renderer.domElement );
  250. // var width = window.innerWidth || 1;
  251. var height = window.innerHeight || 1;
  252. var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: false };
  253. var regularRenderTarget = new THREE.WebGLRenderTarget( windowThirdX, height, parameters );
  254. ldrEffectComposer = new EffectComposer( renderer, regularRenderTarget );
  255. if ( renderer.extensions.get( 'OES_texture_half_float_linear' ) ) {
  256. parameters.type = THREE.FloatType;
  257. }
  258. var hdrRenderTarget = new THREE.WebGLRenderTarget( windowThirdX, height, parameters );
  259. dynamicHdrEffectComposer = new EffectComposer( renderer, hdrRenderTarget );
  260. dynamicHdrEffectComposer.setSize( window.innerWidth, window.innerHeight );
  261. hdrEffectComposer = new EffectComposer( renderer, hdrRenderTarget );
  262. var debugPass = new RenderPass( debugScene, cameraBG );
  263. debugPass.clear = false;
  264. var scenePass = new RenderPass( scene, camera, undefined, undefined, undefined );
  265. var skyboxPass = new RenderPass( sceneCube, cameraCube );
  266. scenePass.clear = false;
  267. adaptToneMappingPass = new AdaptiveToneMappingPass( true, 256 );
  268. adaptToneMappingPass.needsSwap = true;
  269. ldrToneMappingPass = new AdaptiveToneMappingPass( false, 256 );
  270. hdrToneMappingPass = new AdaptiveToneMappingPass( false, 256 );
  271. bloomPass = new BloomPass();
  272. var gammaCorrectionPass = new ShaderPass( GammaCorrectionShader );
  273. dynamicHdrEffectComposer.addPass( skyboxPass );
  274. dynamicHdrEffectComposer.addPass( scenePass );
  275. dynamicHdrEffectComposer.addPass( adaptToneMappingPass );
  276. dynamicHdrEffectComposer.addPass( bloomPass );
  277. dynamicHdrEffectComposer.addPass( gammaCorrectionPass );
  278. hdrEffectComposer.addPass( skyboxPass );
  279. hdrEffectComposer.addPass( scenePass );
  280. hdrEffectComposer.addPass( hdrToneMappingPass );
  281. hdrEffectComposer.addPass( bloomPass );
  282. hdrEffectComposer.addPass( gammaCorrectionPass );
  283. ldrEffectComposer.addPass( skyboxPass );
  284. ldrEffectComposer.addPass( scenePass );
  285. ldrEffectComposer.addPass( ldrToneMappingPass );
  286. ldrEffectComposer.addPass( bloomPass );
  287. ldrEffectComposer.addPass( gammaCorrectionPass );
  288. var gui = new GUI();
  289. var sceneGui = gui.addFolder( 'Scenes' );
  290. var toneMappingGui = gui.addFolder( 'ToneMapping' );
  291. var staticToneMappingGui = gui.addFolder( 'StaticOnly' );
  292. var adaptiveToneMappingGui = gui.addFolder( 'AdaptiveOnly' );
  293. sceneGui.add( params, 'bloomAmount', 0.0, 10.0 );
  294. sceneGui.add( params, 'sunLight', 0.1, 12.0 );
  295. toneMappingGui.add( params, 'enabled' );
  296. toneMappingGui.add( params, 'middleGrey', 0, 12 );
  297. toneMappingGui.add( params, 'maxLuminance', 1, 30 );
  298. staticToneMappingGui.add( params, 'avgLuminance', 0.001, 2.0 );
  299. adaptiveToneMappingGui.add( params, 'adaptionRate', 0.0, 10.0 );
  300. gui.open();
  301. window.addEventListener( 'resize', onWindowResize, false );
  302. }
  303. function onWindowResize() {
  304. windowHalfX = window.innerWidth / 2;
  305. windowHalfY = window.innerHeight / 2;
  306. windowThirdX = window.innerWidth / 3;
  307. camera.aspect = windowThirdX / window.innerHeight;
  308. camera.updateProjectionMatrix();
  309. cameraCube.aspect = windowThirdX / window.innerHeight;
  310. cameraCube.updateProjectionMatrix();
  311. renderer.setSize( window.innerWidth, window.innerHeight );
  312. }
  313. function animate() {
  314. requestAnimationFrame( animate );
  315. if ( bloomPass ) {
  316. bloomPass.copyUniforms[ "opacity" ].value = params.bloomAmount;
  317. }
  318. if ( adaptToneMappingPass ) {
  319. adaptToneMappingPass.setAdaptionRate( params.adaptionRate );
  320. adaptiveLuminanceMat.uniforms[ "map" ].value = adaptToneMappingPass.luminanceRT;
  321. currentLuminanceMat.uniforms[ "map" ].value = adaptToneMappingPass.currentLuminanceRT;
  322. adaptToneMappingPass.enabled = params.enabled;
  323. adaptToneMappingPass.setMaxLuminance( params.maxLuminance );
  324. adaptToneMappingPass.setMiddleGrey( params.middleGrey );
  325. hdrToneMappingPass.enabled = params.enabled;
  326. hdrToneMappingPass.setMaxLuminance( params.maxLuminance );
  327. hdrToneMappingPass.setMiddleGrey( params.middleGrey );
  328. if ( hdrToneMappingPass.setAverageLuminance ) {
  329. hdrToneMappingPass.setAverageLuminance( params.avgLuminance );
  330. }
  331. ldrToneMappingPass.enabled = params.enabled;
  332. ldrToneMappingPass.setMaxLuminance( params.maxLuminance );
  333. ldrToneMappingPass.setMiddleGrey( params.middleGrey );
  334. if ( ldrToneMappingPass.setAverageLuminance ) {
  335. ldrToneMappingPass.setAverageLuminance( params.avgLuminance );
  336. }
  337. }
  338. directionalLight.intensity = params.sunLight;
  339. orbitControls.update();
  340. render();
  341. }
  342. function render() {
  343. camera.lookAt( scene.position );
  344. cameraCube.rotation.copy( camera.rotation );
  345. renderer.setViewport( 0, 0, windowThirdX, window.innerHeight );
  346. ldrEffectComposer.render( 0.017 );
  347. renderer.setViewport( windowThirdX, 0, windowThirdX, window.innerHeight );
  348. hdrEffectComposer.render( 0.017 );
  349. renderer.setViewport( windowThirdX * 2, 0, windowThirdX, window.innerHeight );
  350. dynamicHdrEffectComposer.render( 0.017 );
  351. }
  352. </script>
  353. </body>
  354. </html>