webgl_postprocessing_godrays.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. <script src='js/DAT.GUI.min.js'></script>
  36. <div id="info">
  37. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl god-rays example - tree from turbosquid</a>
  38. </div>
  39. <script>
  40. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  41. var container, stats;
  42. var camera, scene, renderer,
  43. materials = [], objects = [],
  44. singleMaterial, zmaterial = [],
  45. parameters, i, j, k, h, color, x, y, z, s, n, nobjects,
  46. material_depth, cubeMaterial;
  47. var tree_mesh, sphere_mesh;
  48. var proj = new THREE.Projector();
  49. var sunPos = new THREE.Vector3(0,1000,-1000);
  50. var mouseX = 0, mouseY = 0;
  51. var windowHalfX = window.innerWidth / 2;
  52. var windowHalfY = window.innerHeight / 2;
  53. var height = window.innerHeight-100;
  54. var postprocessing = { enabled : true };
  55. init();
  56. animate();
  57. function init() {
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. scene = new THREE.Scene();
  61. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / height, 1, 3000 );
  62. camera.position.z = 200;
  63. scene.add( camera );
  64. renderer = new THREE.WebGLRenderer( { antialias: false } );
  65. renderer.setSize( window.innerWidth, height );
  66. container.appendChild( renderer.domElement );
  67. renderer.sortObjects = false;
  68. // todo - try with fog?
  69. //scene.fog = new THREE.Fog( 0xffaa55, 1000, FAR );
  70. //THREE.ColorUtils.adjustHSV( scene.fog.color, 0.02, -0.15, -0.65 );
  71. material_depth = new THREE.MeshDepthMaterial();
  72. var path = "textures/cube/SwedishRoyalCastle/";
  73. var format = '.jpg';
  74. var urls = [
  75. path + 'px' + format, path + 'nx' + format,
  76. path + 'py' + format, path + 'ny' + format,
  77. path + 'pz' + format, path + 'nz' + format
  78. ];
  79. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  80. // was 0x003300
  81. // then 0x050500
  82. parameters = { color: 0x000000, /*envMap: textureCube,*/ shading: THREE.FlatShading };
  83. cubeMaterial = new THREE.MeshBasicMaterial( parameters );
  84. singleMaterial = false;
  85. if( singleMaterial ) zmaterial = [ cubeMaterial ];
  86. var start = new Date().getTime();
  87. renderer.initMaterial( cubeMaterial, scene.__lights, scene.fog );
  88. var xgrid = 1, //14,
  89. ygrid = 1, //9,
  90. zgrid = 1; //14;
  91. nobjects = xgrid * ygrid * zgrid;
  92. c = 0;
  93. var zmat = new THREE.MeshBasicMaterial( parameters );
  94. //renderer.initMaterial( materials[ c ], scene.__lights, scene.fog, mesh );
  95. // tree mesh
  96. var jsonLoader = new THREE.JSONLoader();
  97. jsonLoader.load( "obj/tree/tree.js",
  98. function( geometry ) {
  99. var m = new THREE.Mesh( geometry, zmat );
  100. m.position.set( 0, -150, -150 );
  101. var sc = 400;
  102. m.scale.set( sc, sc, sc );
  103. m.matrixAutoUpdate = false;
  104. m.updateMatrix();
  105. scene.add( m );
  106. objects.push( m );
  107. tree_mesh = m;
  108. }
  109. );
  110. /*
  111. // cog mesh
  112. var jsonLoader = new THREE.JSONLoader();
  113. jsonLoader.load( "obj/cog/cog1.js",
  114. function( geometry ) {
  115. var m = new THREE.Mesh( geometry, zmat );
  116. m.position.set( -150, -150, -150 );
  117. var sc = 400;
  118. m.scale.set( sc, sc, sc );
  119. m.matrixAutoUpdate = false;
  120. m.updateMatrix();
  121. scene.add( m );
  122. objects.push( m );
  123. //tree_mesh = m;
  124. }
  125. );
  126. */
  127. var geo = new THREE.SphereGeometry( 1, 20, 10 );
  128. mesh = new THREE.Mesh( geo, zmat );
  129. var sc = 20;
  130. mesh.scale.set( sc, sc, sc );
  131. mesh.matrixAutoUpdate = false;
  132. mesh.updateMatrix();
  133. scene.add( mesh );
  134. objects.push( mesh );
  135. sphere_mesh = mesh;
  136. scene.matrixAutoUpdate = false;
  137. initPostprocessing();
  138. renderer.autoClear = false;
  139. renderer.setClearColorHex( 0x000033, 1);
  140. renderer.domElement.style.position = 'absolute';
  141. renderer.domElement.style.top = "50px";
  142. renderer.domElement.style.left = "0px";
  143. stats = new Stats();
  144. stats.domElement.style.position = 'absolute';
  145. stats.domElement.style.top = '0px';
  146. container.appendChild( stats.domElement );
  147. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  148. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  149. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  150. /*
  151. var effectController = {
  152. intensity: 0.69,
  153. patchSize: 0.3,
  154. };
  155. var matChanger = function( ) {
  156. postprocessing.godray_combine_uniforms[ "fGodRayIntensity" ].value = effectController.intensity;
  157. patchSize = effectController.patchSize;
  158. };
  159. var gui = new DAT.GUI();
  160. gui.add( effectController, "intensity", 0.0, 1.0, 0.001 ).onChange( matChanger );
  161. gui.add( effectController, "patchSize", 0.0, 1.0, 0.001 ).onChange( matChanger );
  162. gui.close();
  163. */
  164. }
  165. function onDocumentMouseMove( event ) {
  166. mouseX = event.clientX - windowHalfX;
  167. mouseY = event.clientY - windowHalfY;
  168. }
  169. function onDocumentTouchStart( event ) {
  170. if ( event.touches.length == 1 ) {
  171. event.preventDefault();
  172. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  173. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  174. }
  175. }
  176. function onDocumentTouchMove( event ) {
  177. if ( event.touches.length == 1 ) {
  178. event.preventDefault();
  179. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  180. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  181. }
  182. }
  183. function initPostprocessing() {
  184. postprocessing.scene = new THREE.Scene();
  185. postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  186. postprocessing.camera.position.z = 100;
  187. postprocessing.scene.add( postprocessing.camera );
  188. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  189. postprocessing.rtTextureColors = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
  190. // Switching the depth formats to luminance from rgb doesn't seem to work. I didn't
  191. // investigate further for now.
  192. //pars.format = THREE.LuminanceFormat;
  193. // I would have this quarter size and use it as one of the ping-pong render
  194. // targets but the aliasing causes some temporal flickering
  195. postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
  196. // Aggressive downsizing to minimize cost of postprocessing passes. God rays
  197. // are low frequency so aliasing is less noticeable.
  198. var w = window.innerWidth / 4.0;
  199. var h = height / 4.0;
  200. postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget( w, h, pars );
  201. postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget( w, h, pars );
  202. // GOD RAY POST PROC
  203. var godray_gen_shader = THREE.ShaderGodRays[ "godrays_generate" ];
  204. postprocessing.godray_gen_uniforms = THREE.UniformsUtils.clone( godray_gen_shader.uniforms );
  205. postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial( {
  206. uniforms: postprocessing.godray_gen_uniforms,
  207. vertexShader: godray_gen_shader.vertexShader,
  208. fragmentShader: godray_gen_shader.fragmentShader
  209. } );
  210. var godrays_combine_shader = THREE.ShaderGodRays[ "godrays_combine" ];
  211. postprocessing.godray_combine_uniforms = THREE.UniformsUtils.clone( godrays_combine_shader.uniforms );
  212. postprocessing.materialGodraysCombine = new THREE.ShaderMaterial( {
  213. uniforms: postprocessing.godray_combine_uniforms,
  214. vertexShader: godrays_combine_shader.vertexShader,
  215. fragmentShader: godrays_combine_shader.fragmentShader
  216. } );
  217. var godrays_fake_sun_shader = THREE.ShaderGodRays[ "godrays_fake_sun" ];
  218. postprocessing.godrays_fake_sun_uniforms = THREE.UniformsUtils.clone( godrays_fake_sun_shader.uniforms );
  219. postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial( {
  220. uniforms: postprocessing.godrays_fake_sun_uniforms,
  221. vertexShader: godrays_fake_sun_shader.vertexShader,
  222. fragmentShader: godrays_fake_sun_shader.fragmentShader
  223. } );
  224. postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialGodraysGenerate );
  225. postprocessing.quad.position.z = -9900;
  226. postprocessing.scene.add( postprocessing.quad );
  227. }
  228. function animate() {
  229. requestAnimationFrame( animate, renderer.domElement );
  230. if( sphere_mesh ) {
  231. var radius = 100;
  232. sphere_mesh.position.x = 200*Math.cos(Date.now()/4000);
  233. sphere_mesh.position.z = 200*Math.sin(Date.now()/4000)-100;
  234. sphere_mesh.updateMatrix();
  235. }
  236. render();
  237. stats.update();
  238. }
  239. function render() {
  240. var time = Date.now() * 0.00005;
  241. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  242. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  243. camera.lookAt( scene.position );
  244. if ( postprocessing.enabled ) {
  245. var sspos = new THREE.Vector3( sunPos.x, sunPos.y, sunPos.z); //new THREE.Vector3();
  246. //var sspos = new THREE.Vector3( sphere_mesh.position.x, sphere_mesh.position.y, sphere_mesh.position.z); //new THREE.Vector3();
  247. proj.projectVector(sspos,camera);
  248. sspos.x = (sspos.x+1)/2;
  249. sspos.y = (sspos.y+1)/2;
  250. postprocessing.godray_gen_uniforms[ "vSunPositionScreenSpace" ].value =
  251. new THREE.Vector2(sspos.x,sspos.y);
  252. postprocessing.godrays_fake_sun_uniforms[ "vSunPositionScreenSpace" ].value =
  253. new THREE.Vector2(sspos.x,sspos.y);
  254. // Draw sun and sky ----------------------------------
  255. // Clear colors and depths, will clear to sky color
  256. renderer.clearTarget(postprocessing.rtTextureColors,true,true,false);
  257. // My poor excuse for rendering a sun. Runs a shader that gives
  258. // a brightness based on the screen space distance to the sun. Not very
  259. // efficient, so i make a scissor rect around the suns position to
  260. // avoid rendering surrounding pixels
  261. var sunsqH = 0.74 * height; // .74 depends on extent of sun from shader
  262. var sunsqW = 0.74 * height; // both dep on height because sun is aspect-corrected
  263. sspos.x *= window.innerWidth;
  264. sspos.y *= height;
  265. renderer.setScissor( sspos.x-sunsqW/2,sspos.y-sunsqH/2,sunsqW,sunsqH );
  266. renderer.enableScissorTest ( true );
  267. postprocessing.godrays_fake_sun_uniforms[ "fAspect" ].value = window.innerWidth / height;
  268. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun;
  269. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureColors );
  270. renderer.enableScissorTest ( false );
  271. // draw scene objects (e.g. tree)
  272. scene.overrideMaterial = null;
  273. renderer.render( scene, camera, postprocessing.rtTextureColors );
  274. // Render scene objects depth into texture
  275. scene.overrideMaterial = material_depth;
  276. renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
  277. // Render godray composite
  278. var filterLen = 1.0;
  279. var TAPS_PER_PASS = 6.0;
  280. // pass order could equivalently be 3,2,1 (instead of 1,2,3), which
  281. // would start with a small filter support and grow to large. however
  282. // the large-to-small order produces less objectionable aliasing artifacts that
  283. // appear as a glimmer along the length of the beams
  284. // pass 1 - render into first ping-pong target
  285. var pass = 1.0;
  286. var stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
  287. postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
  288. postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureDepth;
  289. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate;
  290. var onepass = 0;
  291. if( onepass ) {
  292. renderer.render( postprocessing.scene, postprocessing.camera );
  293. } else {
  294. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureGodRays2 );
  295. // pass 2 - render into second ping-pong target
  296. pass = 2.0;
  297. stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
  298. postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
  299. postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureGodRays2;
  300. renderer.render( postprocessing.scene, postprocessing.camera , postprocessing.rtTextureGodRays1 );
  301. // pass 3 - RT
  302. pass = 3.0;
  303. stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
  304. postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
  305. postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureGodRays1;
  306. renderer.render( postprocessing.scene, postprocessing.camera , postprocessing.rtTextureGodRays2 );
  307. postprocessing.godray_combine_uniforms["tColors"].texture = postprocessing.rtTextureColors;
  308. postprocessing.godray_combine_uniforms["tGodRays"].texture = postprocessing.rtTextureGodRays2;
  309. postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine;
  310. renderer.render( postprocessing.scene, postprocessing.camera );
  311. postprocessing.scene.overrideMaterial = null;
  312. }
  313. } else {
  314. renderer.clear();
  315. renderer.render( scene, camera );
  316. }
  317. }
  318. </script>
  319. </body>
  320. </html>