webgl_postprocessing_godrays.html 12 KB

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