webgl_postprocessing_godrays.html 12 KB

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