webgl_postprocessing_godrays.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - godrays</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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a { color:#0078ff; }
  19. #info {
  20. color:#fff;
  21. position: absolute;
  22. top: 0px; width: 100%;
  23. padding: 5px;
  24. z-index:100;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <script src="../build/three.js"></script>
  30. <script src="js/loaders/OBJLoader.js"></script>
  31. <script src="js/ShaderGodRays.js"></script>
  32. <script src="js/WebGL.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <div id="info">
  35. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl god-rays example - tree by <a href="http://www.turbosquid.com/3d-models/free-tree-3d-model/592617" target="_blank" rel="noopener">stanloshka</a>
  36. </div>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var container, stats;
  42. var camera, scene, renderer, materialDepth;
  43. var sphereMesh;
  44. var sunPosition = new THREE.Vector3( 0, 1000, - 1000 );
  45. var screenSpacePosition = new THREE.Vector3();
  46. var mouseX = 0, mouseY = 0;
  47. var postprocessing = { enabled: true };
  48. var orbitRadius = 200;
  49. var bgColor = 0x000511;
  50. var sunColor = 0xffee00;
  51. // Use a smaller size for some of the god-ray render targets for better performance.
  52. var godrayRenderTargetResolutionMultiplier = 1.0 / 4.0;
  53. init();
  54. animate();
  55. function init() {
  56. container = document.createElement( 'div' );
  57. document.body.appendChild( container );
  58. //
  59. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  60. camera.position.z = 200;
  61. scene = new THREE.Scene();
  62. //
  63. materialDepth = new THREE.MeshDepthMaterial();
  64. var materialScene = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  65. // tree
  66. var loader = new THREE.OBJLoader();
  67. loader.load( 'models/obj/tree.obj', function ( object ) {
  68. object.material = materialScene;
  69. object.position.set( 0, - 150, - 150 );
  70. object.scale.multiplyScalar( 400 );
  71. scene.add( object );
  72. } );
  73. // sphere
  74. var geo = new THREE.SphereBufferGeometry( 1, 20, 10 );
  75. sphereMesh = new THREE.Mesh( geo, materialScene );
  76. sphereMesh.scale.multiplyScalar( 20 );
  77. scene.add( sphereMesh );
  78. //
  79. renderer = new THREE.WebGLRenderer();
  80. renderer.setClearColor( 0xffffff );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. container.appendChild( renderer.domElement );
  84. renderer.autoClear = false;
  85. //
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. //
  89. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  90. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  91. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. //
  94. initPostprocessing( window.innerWidth, window.innerHeight );
  95. }
  96. //
  97. function onDocumentMouseMove( event ) {
  98. mouseX = event.clientX - window.innerWidth / 2;
  99. mouseY = event.clientY - window.innerHeight / 2;
  100. }
  101. function onDocumentTouchStart( event ) {
  102. if ( event.touches.length === 1 ) {
  103. event.preventDefault();
  104. mouseX = event.touches[ 0 ].pageX - window.innerWidth / 2;
  105. mouseY = event.touches[ 0 ].pageY - window.innerHeight / 2;
  106. }
  107. }
  108. function onDocumentTouchMove( event ) {
  109. if ( event.touches.length === 1 ) {
  110. event.preventDefault();
  111. mouseX = event.touches[ 0 ].pageX - window.innerWidth / 2;
  112. mouseY = event.touches[ 0 ].pageY - window.innerHeight / 2;
  113. }
  114. }
  115. //
  116. function onWindowResize() {
  117. var renderTargetWidth = window.innerWidth;
  118. var renderTargetHeight = window.innerHeight;
  119. camera.aspect = renderTargetWidth / renderTargetHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( renderTargetWidth, renderTargetHeight );
  122. postprocessing.rtTextureColors.setSize( renderTargetWidth, renderTargetHeight );
  123. postprocessing.rtTextureDepth.setSize( renderTargetWidth, renderTargetHeight );
  124. postprocessing.rtTextureDepthMask.setSize( renderTargetWidth, renderTargetHeight );
  125. var adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier;
  126. var adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier;
  127. postprocessing.rtTextureGodRays1.setSize( adjustedWidth, adjustedHeight );
  128. postprocessing.rtTextureGodRays2.setSize( adjustedWidth, adjustedHeight );
  129. }
  130. function initPostprocessing( renderTargetWidth, renderTargetHeight ) {
  131. postprocessing.scene = new THREE.Scene();
  132. postprocessing.camera = new THREE.OrthographicCamera( - 0.5, 0.5, 0.5, - 0.5, - 10000, 10000 );
  133. postprocessing.camera.position.z = 100;
  134. postprocessing.scene.add( postprocessing.camera );
  135. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  136. postprocessing.rtTextureColors = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
  137. // Switching the depth formats to luminance from rgb doesn't seem to work. I didn't
  138. // investigate further for now.
  139. // pars.format = THREE.LuminanceFormat;
  140. // I would have this quarter size and use it as one of the ping-pong render
  141. // targets but the aliasing causes some temporal flickering
  142. postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
  143. postprocessing.rtTextureDepthMask = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
  144. // The ping-pong render targets can use an adjusted resolution to minimize cost
  145. var adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier;
  146. var adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier;
  147. postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget( adjustedWidth, adjustedHeight, pars );
  148. postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget( adjustedWidth, adjustedHeight, pars );
  149. // god-ray shaders
  150. var godraysMaskShader = THREE.ShaderGodRays[ "godrays_depthMask" ];
  151. postprocessing.godrayMaskUniforms = THREE.UniformsUtils.clone( godraysMaskShader.uniforms );
  152. postprocessing.materialGodraysDepthMask = new THREE.ShaderMaterial( {
  153. uniforms: postprocessing.godrayMaskUniforms,
  154. vertexShader: godraysMaskShader.vertexShader,
  155. fragmentShader: godraysMaskShader.fragmentShader
  156. } );
  157. var godraysGenShader = THREE.ShaderGodRays[ "godrays_generate" ];
  158. postprocessing.godrayGenUniforms = THREE.UniformsUtils.clone( godraysGenShader.uniforms );
  159. postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial( {
  160. uniforms: postprocessing.godrayGenUniforms,
  161. vertexShader: godraysGenShader.vertexShader,
  162. fragmentShader: godraysGenShader.fragmentShader
  163. } );
  164. var godraysCombineShader = THREE.ShaderGodRays[ "godrays_combine" ];
  165. postprocessing.godrayCombineUniforms = THREE.UniformsUtils.clone( godraysCombineShader.uniforms );
  166. postprocessing.materialGodraysCombine = new THREE.ShaderMaterial( {
  167. uniforms: postprocessing.godrayCombineUniforms,
  168. vertexShader: godraysCombineShader.vertexShader,
  169. fragmentShader: godraysCombineShader.fragmentShader
  170. } );
  171. var godraysFakeSunShader = THREE.ShaderGodRays[ "godrays_fake_sun" ];
  172. postprocessing.godraysFakeSunUniforms = THREE.UniformsUtils.clone( godraysFakeSunShader.uniforms );
  173. postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial( {
  174. uniforms: postprocessing.godraysFakeSunUniforms,
  175. vertexShader: godraysFakeSunShader.vertexShader,
  176. fragmentShader: godraysFakeSunShader.fragmentShader
  177. } );
  178. postprocessing.godraysFakeSunUniforms.bgColor.value.setHex( bgColor );
  179. postprocessing.godraysFakeSunUniforms.sunColor.value.setHex( sunColor );
  180. postprocessing.godrayCombineUniforms.fGodRayIntensity.value = 0.75;
  181. postprocessing.quad = new THREE.Mesh(
  182. new THREE.PlaneBufferGeometry( 1.0, 1.0 ),
  183. postprocessing.materialGodraysGenerate
  184. );
  185. postprocessing.quad.position.z = - 9900;
  186. postprocessing.scene.add( postprocessing.quad );
  187. }
  188. function animate() {
  189. requestAnimationFrame( animate, renderer.domElement );
  190. stats.begin();
  191. render();
  192. stats.end();
  193. }
  194. function getStepSize( filterLen, tapsPerPass, pass ) {
  195. return filterLen * Math.pow( tapsPerPass, - pass );
  196. }
  197. function filterGodRays( inputTex, renderTarget, stepSize ) {
  198. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate;
  199. postprocessing.godrayGenUniforms[ "fStepSize" ].value = stepSize;
  200. postprocessing.godrayGenUniforms[ "tInput" ].value = inputTex;
  201. renderer.setRenderTarget( renderTarget );
  202. renderer.render( postprocessing.scene, postprocessing.camera );
  203. postprocessing.scene.overrideMaterial = null;
  204. }
  205. function render() {
  206. var time = Date.now() / 4000;
  207. sphereMesh.position.x = orbitRadius * Math.cos( time );
  208. sphereMesh.position.z = orbitRadius * Math.sin( time ) - 100;
  209. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  210. camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
  211. camera.lookAt( scene.position );
  212. if ( postprocessing.enabled ) {
  213. // Find the screenspace position of the sun
  214. screenSpacePosition.copy( sunPosition ).project( camera );
  215. screenSpacePosition.x = ( screenSpacePosition.x + 1 ) / 2;
  216. screenSpacePosition.y = ( screenSpacePosition.y + 1 ) / 2;
  217. // Give it to the god-ray and sun shaders
  218. postprocessing.godrayGenUniforms[ "vSunPositionScreenSpace" ].value.x = screenSpacePosition.x;
  219. postprocessing.godrayGenUniforms[ "vSunPositionScreenSpace" ].value.y = screenSpacePosition.y;
  220. postprocessing.godraysFakeSunUniforms[ "vSunPositionScreenSpace" ].value.x = screenSpacePosition.x;
  221. postprocessing.godraysFakeSunUniforms[ "vSunPositionScreenSpace" ].value.y = screenSpacePosition.y;
  222. // -- Draw sky and sun --
  223. // Clear colors and depths, will clear to sky color
  224. renderer.setRenderTarget( postprocessing.rtTextureColors );
  225. renderer.clear( true, true, false );
  226. // Sun render. Runs a shader that gives a brightness based on the screen
  227. // space distance to the sun. Not very efficient, so i make a scissor
  228. // rectangle around the suns position to avoid rendering surrounding pixels.
  229. var sunsqH = 0.74 * window.innerHeight; // 0.74 depends on extent of sun from shader
  230. var sunsqW = 0.74 * window.innerHeight; // both depend on height because sun is aspect-corrected
  231. screenSpacePosition.x *= window.innerWidth;
  232. screenSpacePosition.y *= window.innerHeight;
  233. renderer.setScissor( screenSpacePosition.x - sunsqW / 2, screenSpacePosition.y - sunsqH / 2, sunsqW, sunsqH );
  234. renderer.setScissorTest( true );
  235. postprocessing.godraysFakeSunUniforms[ "fAspect" ].value = window.innerWidth / window.innerHeight;
  236. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun;
  237. renderer.setRenderTarget( postprocessing.rtTextureColors );
  238. renderer.render( postprocessing.scene, postprocessing.camera );
  239. renderer.setScissorTest( false );
  240. // -- Draw scene objects --
  241. // Colors
  242. scene.overrideMaterial = null;
  243. renderer.setRenderTarget( postprocessing.rtTextureColors );
  244. renderer.render( scene, camera );
  245. // Depth
  246. scene.overrideMaterial = materialDepth;
  247. renderer.setRenderTarget( postprocessing.rtTextureDepth );
  248. renderer.clear();
  249. renderer.render( scene, camera );
  250. //
  251. postprocessing.godrayMaskUniforms[ "tInput" ].value = postprocessing.rtTextureDepth.texture;
  252. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysDepthMask;
  253. renderer.setRenderTarget( postprocessing.rtTextureDepthMask );
  254. renderer.render( postprocessing.scene, postprocessing.camera );
  255. // -- Render god-rays --
  256. // Maximum length of god-rays (in texture space [0,1]X[0,1])
  257. var filterLen = 1.0;
  258. // Samples taken by filter
  259. var TAPS_PER_PASS = 6.0;
  260. // Pass order could equivalently be 3,2,1 (instead of 1,2,3), which
  261. // would start with a small filter support and grow to large. however
  262. // the large-to-small order produces less objectionable aliasing artifacts that
  263. // appear as a glimmer along the length of the beams
  264. // pass 1 - render into first ping-pong target
  265. filterGodRays( postprocessing.rtTextureDepthMask.texture, postprocessing.rtTextureGodRays2, getStepSize( filterLen, TAPS_PER_PASS, 1.0 ) );
  266. // pass 2 - render into second ping-pong target
  267. filterGodRays( postprocessing.rtTextureGodRays2.texture, postprocessing.rtTextureGodRays1, getStepSize( filterLen, TAPS_PER_PASS, 2.0 ) );
  268. // pass 3 - 1st RT
  269. filterGodRays( postprocessing.rtTextureGodRays1.texture, postprocessing.rtTextureGodRays2, getStepSize( filterLen, TAPS_PER_PASS, 3.0 ) );
  270. // final pass - composite god-rays onto colors
  271. postprocessing.godrayCombineUniforms[ "tColors" ].value = postprocessing.rtTextureColors.texture;
  272. postprocessing.godrayCombineUniforms[ "tGodRays" ].value = postprocessing.rtTextureGodRays2.texture;
  273. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine;
  274. renderer.setRenderTarget( null );
  275. renderer.render( postprocessing.scene, postprocessing.camera );
  276. postprocessing.scene.overrideMaterial = null;
  277. } else {
  278. renderer.setRenderTarget( null );
  279. renderer.clear();
  280. renderer.render( scene, camera );
  281. }
  282. }
  283. </script>
  284. </body>
  285. </html>