123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <!doctype html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing - godrays</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- background-color: #000000;
- margin: 0px;
- overflow: hidden;
- font-family:Monospace;
- font-size:13px;
- text-align:center;
- font-weight: bold;
- text-align:center;
- }
- a {
- color:#0078ff;
- }
- #info {
- color:#fff;
- position: absolute;
- top: 0px; width: 100%;
- padding: 5px;
- z-index:100;
- }
- </style>
- </head>
- <body>
- <script src="../build/Three.js"></script>
- <script src="js/Detector.js"></script>
- <script src="js/ShaderGodRays.js"></script>
- <script src="js/Stats.js"></script>
- <script src='js/DAT.GUI.min.js'></script>
- <div id="info">
- <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl god-rays example - tree from turbosquid</a>
- </div>
- <script>
- if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
- var container, stats;
- var camera, scene, renderer,
- materials = [], objects = [],
- singleMaterial, zmaterial = [],
- parameters, i, j, k, h, color, x, y, z, s, n, nobjects,
- material_depth, cubeMaterial;
- var tree_mesh, sphere_mesh;
-
- var proj = new THREE.Projector();
- var sunPos = new THREE.Vector3(0,1000,-1000);
-
- var mouseX = 0, mouseY = 0;
-
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var height = window.innerHeight-100;
- var postprocessing = { enabled : true };
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / height, 1, 3000 );
- camera.position.z = 200;
- scene.add( camera );
- renderer = new THREE.WebGLRenderer( { antialias: false } );
- renderer.setSize( window.innerWidth, height );
- container.appendChild( renderer.domElement );
- renderer.sortObjects = false;
- // todo - try with fog?
- //scene.fog = new THREE.Fog( 0xffaa55, 1000, FAR );
- //THREE.ColorUtils.adjustHSV( scene.fog.color, 0.02, -0.15, -0.65 );
-
- material_depth = new THREE.MeshDepthMaterial();
- var path = "textures/cube/SwedishRoyalCastle/";
- var format = '.jpg';
- var urls = [
- path + 'px' + format, path + 'nx' + format,
- path + 'py' + format, path + 'ny' + format,
- path + 'pz' + format, path + 'nz' + format
- ];
- var textureCube = THREE.ImageUtils.loadTextureCube( urls );
- // was 0x003300
- // then 0x050500
- parameters = { color: 0x000000, /*envMap: textureCube,*/ shading: THREE.FlatShading };
- cubeMaterial = new THREE.MeshBasicMaterial( parameters );
- singleMaterial = false;
- if( singleMaterial ) zmaterial = [ cubeMaterial ];
- var start = new Date().getTime();
- renderer.initMaterial( cubeMaterial, scene.__lights, scene.fog );
- var xgrid = 1, //14,
- ygrid = 1, //9,
- zgrid = 1; //14;
- nobjects = xgrid * ygrid * zgrid;
- c = 0;
- var zmat = new THREE.MeshBasicMaterial( parameters );
- //renderer.initMaterial( materials[ c ], scene.__lights, scene.fog, mesh );
-
- // tree mesh
- var jsonLoader = new THREE.JSONLoader();
- jsonLoader.load( "obj/tree/tree.js",
- function( geometry ) {
- var m = new THREE.Mesh( geometry, zmat );
- m.position.set( 0, -150, -150 );
- var sc = 400;
- m.scale.set( sc, sc, sc );
- m.matrixAutoUpdate = false;
- m.updateMatrix();
- scene.add( m );
- objects.push( m );
- tree_mesh = m;
- }
- );
- /*
- // cog mesh
- var jsonLoader = new THREE.JSONLoader();
- jsonLoader.load( "obj/cog/cog1.js",
- function( geometry ) {
- var m = new THREE.Mesh( geometry, zmat );
- m.position.set( -150, -150, -150 );
- var sc = 400;
- m.scale.set( sc, sc, sc );
- m.matrixAutoUpdate = false;
- m.updateMatrix();
- scene.add( m );
- objects.push( m );
- //tree_mesh = m;
- }
- );
- */
- var geo = new THREE.SphereGeometry( 1, 20, 10 );
- mesh = new THREE.Mesh( geo, zmat );
- var sc = 20;
- mesh.scale.set( sc, sc, sc );
- mesh.matrixAutoUpdate = false;
- mesh.updateMatrix();
- scene.add( mesh );
- objects.push( mesh );
- sphere_mesh = mesh;
-
- scene.matrixAutoUpdate = false;
- initPostprocessing();
- renderer.autoClear = false;
- renderer.setClearColorHex( 0x000033, 1);
- renderer.domElement.style.position = 'absolute';
- renderer.domElement.style.top = "50px";
- renderer.domElement.style.left = "0px";
- stats = new Stats();
- stats.domElement.style.position = 'absolute';
- stats.domElement.style.top = '0px';
- container.appendChild( stats.domElement );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- /*
- var effectController = {
-
- intensity: 0.69,
- patchSize: 0.3,
- };
- var matChanger = function( ) {
- postprocessing.godray_combine_uniforms[ "fGodRayIntensity" ].value = effectController.intensity;
- patchSize = effectController.patchSize;
- };
- var gui = new DAT.GUI();
- gui.add( effectController, "intensity", 0.0, 1.0, 0.001 ).onChange( matChanger );
- gui.add( effectController, "patchSize", 0.0, 1.0, 0.001 ).onChange( matChanger );
- gui.close();
- */
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- mouseY = event.clientY - windowHalfY;
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- function initPostprocessing() {
- postprocessing.scene = new THREE.Scene();
- postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
- postprocessing.camera.position.z = 100;
- postprocessing.scene.add( postprocessing.camera );
- var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
- postprocessing.rtTextureColors = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
-
- // Switching the depth formats to luminance from rgb doesn't seem to work. I didn't
- // investigate further for now.
- //pars.format = THREE.LuminanceFormat;
-
- // I would have this quarter size and use it as one of the ping-pong render
- // targets but the aliasing causes some temporal flickering
- postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
-
- // Aggressive downsizing to minimize cost of postprocessing passes. God rays
- // are low frequency so aliasing is less noticeable.
- var w = window.innerWidth / 4.0;
- var h = height / 4.0;
- postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget( w, h, pars );
- postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget( w, h, pars );
-
- // GOD RAY POST PROC
- var godray_gen_shader = THREE.ShaderGodRays[ "godrays_generate" ];
- postprocessing.godray_gen_uniforms = THREE.UniformsUtils.clone( godray_gen_shader.uniforms );
- postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial( {
- uniforms: postprocessing.godray_gen_uniforms,
- vertexShader: godray_gen_shader.vertexShader,
- fragmentShader: godray_gen_shader.fragmentShader
- } );
-
- var godrays_combine_shader = THREE.ShaderGodRays[ "godrays_combine" ];
- postprocessing.godray_combine_uniforms = THREE.UniformsUtils.clone( godrays_combine_shader.uniforms );
- postprocessing.materialGodraysCombine = new THREE.ShaderMaterial( {
- uniforms: postprocessing.godray_combine_uniforms,
- vertexShader: godrays_combine_shader.vertexShader,
- fragmentShader: godrays_combine_shader.fragmentShader
- } );
-
- var godrays_fake_sun_shader = THREE.ShaderGodRays[ "godrays_fake_sun" ];
- postprocessing.godrays_fake_sun_uniforms = THREE.UniformsUtils.clone( godrays_fake_sun_shader.uniforms );
- postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial( {
- uniforms: postprocessing.godrays_fake_sun_uniforms,
- vertexShader: godrays_fake_sun_shader.vertexShader,
- fragmentShader: godrays_fake_sun_shader.fragmentShader
- } );
-
- postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialGodraysGenerate );
- postprocessing.quad.position.z = -9900;
- postprocessing.scene.add( postprocessing.quad );
- }
- function animate() {
- requestAnimationFrame( animate, renderer.domElement );
-
- if( sphere_mesh ) {
- var radius = 100;
- sphere_mesh.position.x = 200*Math.cos(Date.now()/4000);
- sphere_mesh.position.z = 200*Math.sin(Date.now()/4000)-100;
- sphere_mesh.updateMatrix();
- }
-
- render();
- stats.update();
- }
- function render() {
- var time = Date.now() * 0.00005;
- camera.position.x += ( mouseX - camera.position.x ) * 0.036;
- camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
- camera.lookAt( scene.position );
-
- if ( postprocessing.enabled ) {
- var sspos = new THREE.Vector3( sunPos.x, sunPos.y, sunPos.z); //new THREE.Vector3();
- //var sspos = new THREE.Vector3( sphere_mesh.position.x, sphere_mesh.position.y, sphere_mesh.position.z); //new THREE.Vector3();
- proj.projectVector(sspos,camera);
- sspos.x = (sspos.x+1)/2;
- sspos.y = (sspos.y+1)/2;
- postprocessing.godray_gen_uniforms[ "vSunPositionScreenSpace" ].value =
- new THREE.Vector2(sspos.x,sspos.y);
- postprocessing.godrays_fake_sun_uniforms[ "vSunPositionScreenSpace" ].value =
- new THREE.Vector2(sspos.x,sspos.y);
-
- // Draw sun and sky ----------------------------------
-
- // Clear colors and depths, will clear to sky color
- renderer.clearTarget(postprocessing.rtTextureColors,true,true,false);
-
- // My poor excuse for rendering a sun. Runs a shader that gives
- // a brightness based on the screen space distance to the sun. Not very
- // efficient, so i make a scissor rect around the suns position to
- // avoid rendering surrounding pixels
- var sunsqH = 0.74 * height; // .74 depends on extent of sun from shader
- var sunsqW = 0.74 * height; // both dep on height because sun is aspect-corrected
- sspos.x *= window.innerWidth;
- sspos.y *= height;
- renderer.setScissor( sspos.x-sunsqW/2,sspos.y-sunsqH/2,sunsqW,sunsqH );
- renderer.enableScissorTest ( true );
-
- postprocessing.godrays_fake_sun_uniforms[ "fAspect" ].value = window.innerWidth / height;
-
- postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun;
- renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureColors );
-
- renderer.enableScissorTest ( false );
-
- // draw scene objects (e.g. tree)
- scene.overrideMaterial = null;
- renderer.render( scene, camera, postprocessing.rtTextureColors );
-
- // Render scene objects depth into texture
- scene.overrideMaterial = material_depth;
- renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
-
-
- // Render godray composite
-
- var filterLen = 1.0;
- var TAPS_PER_PASS = 6.0;
-
- // pass order could equivalently be 3,2,1 (instead of 1,2,3), which
- // would start with a small filter support and grow to large. however
- // the large-to-small order produces less objectionable aliasing artifacts that
- // appear as a glimmer along the length of the beams
-
- // pass 1 - render into first ping-pong target
- var pass = 1.0;
- var stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
- postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
- postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureDepth;
- postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate;
- var onepass = 0;
-
- if( onepass ) {
- renderer.render( postprocessing.scene, postprocessing.camera );
- } else {
- renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureGodRays2 );
-
- // pass 2 - render into second ping-pong target
- pass = 2.0;
- stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
- postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
- postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureGodRays2;
- renderer.render( postprocessing.scene, postprocessing.camera , postprocessing.rtTextureGodRays1 );
-
- // pass 3 - RT
- pass = 3.0;
- stepLen = filterLen*Math.pow(TAPS_PER_PASS, -pass);
- postprocessing.godray_gen_uniforms[ "fStepSize" ].value = stepLen;
- postprocessing.godray_gen_uniforms[ "tInput" ].texture = postprocessing.rtTextureGodRays1;
- renderer.render( postprocessing.scene, postprocessing.camera , postprocessing.rtTextureGodRays2 );
-
- postprocessing.godray_combine_uniforms["tColors"].texture = postprocessing.rtTextureColors;
- postprocessing.godray_combine_uniforms["tGodRays"].texture = postprocessing.rtTextureGodRays2;
- postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine;
- renderer.render( postprocessing.scene, postprocessing.camera );
- postprocessing.scene.overrideMaterial = null;
-
- }
- } else {
- renderer.clear();
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|