webgl_postprocessing_godrays.html 12 KB

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