webgl_postprocessing_godrays.html 12 KB

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