webgl_postprocessing_godrays.html 12 KB

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