├── README.md └── sources ├── RenderManager.js ├── Shaders.js └── Utils.js /README.md: -------------------------------------------------------------------------------- 1 | # Extensions 2 | * **THREE.Extras.RenderManager** : [Examples and explanations](http://bkcore.com/blog/3d/webgl-three-js-extension-rendermanager.html) 3 | * **THREE.Extras.Shaders** : [Godrays example and explanations](http://bkcore.com/blog/3d/webgl-three-js-volumetric-light-godrays.html) 4 | * **THREE.Extras.Utils** : Screen space projection. 5 | 6 | # License 7 | These extensions are released under the MIT License. 8 | 9 | Copyright (C) 2010-2012 Thibaut Despoulain 10 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy of 12 | this software and associated documentation files (the "Software"), to deal in 13 | the Software without restriction, including without limitation the rights to 14 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 15 | of the Software, and to permit persons to whom the Software is furnished to do 16 | so, subject to the following conditions: 17 | 18 | The above copyright notice and this permission notice shall be included in all 19 | copies or substantial portions of the Software. 20 | 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 | SOFTWARE. -------------------------------------------------------------------------------- /sources/RenderManager.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * THREE.Extras.RenderManager helps handling multiple scenes, cameras and render loops. 3 | * 4 | * @author Thibaut 'BKcore' Despoulain 5 | * 6 | * Initialize the a RenderManager by passing a Renderer object: 7 | * var renderManager = new THREE.Extras.RenderManager(new THREE.WebGLRenderer()); 8 | * 9 | * A render setup structure : 10 | * { 11 | * id : render setup ID, 12 | * scene : main scene, 13 | * camera : main camera, 14 | * render : render loop called when render setup is active (current), 15 | * objects : object references accessible in the render loop via this.objects 16 | * } 17 | * 18 | * The render method's context will be the render setup's object, so in your render loop: 19 | * function(delta, renderer) 20 | * { 21 | * this.scene; 22 | * this.camera; 23 | * this.id; 24 | * this.objects; 25 | * renderer.render(...); 26 | * } 27 | * 28 | * Use the "objects" attribute to store useful references and variables like time, geometries, materials, etc. 29 | * Example: 30 | * renderManager.add('mySetup', scene, camera, function(delta, renderer) 31 | * { 32 | * this.objects.timer += delta; 33 | * this.objects.torus.rotation.z = Math.PI * Math.cos(this.objects.timer); 34 | * renderer.render(this.scene, this.camera); 35 | * }, 36 | * { 37 | * timer: 0, 38 | * torus: torusMesh 39 | * }); 40 | */ 41 | 42 | THREE = THREE || {}; 43 | THREE.Extras = THREE.Extras || {}; 44 | 45 | THREE.Extras.RenderManager = function(renderer) 46 | { 47 | this.renderer = renderer; 48 | this.time = Date.now()/1000; 49 | 50 | this.renders = {}; 51 | this.current = {}; 52 | this.size = 0; 53 | 54 | this.defaultRenderMethod = function(delta, renderer) 55 | { 56 | renderer.render(this.scene, this.camera); 57 | }; 58 | }; 59 | 60 | THREE.Extras.RenderManager.prototype.add = function(id, scene, camera, render, objects) 61 | { 62 | render = render || this.defaultRenderMethod; 63 | objects = objects || {}; 64 | 65 | this.renders[id] = { 66 | id: id, 67 | scene: scene, 68 | camera: camera, 69 | render: render, 70 | objects: objects 71 | }; 72 | 73 | if(this.size == 0) this.current = this.renders[id]; 74 | 75 | this.size++; 76 | }; 77 | 78 | THREE.Extras.RenderManager.prototype.get = function(id) 79 | { 80 | return this.renders[id]; 81 | }; 82 | 83 | THREE.Extras.RenderManager.prototype.remove = function(id) 84 | { 85 | if(id in this.renders) 86 | { 87 | delete this.renders[id]; 88 | this.size--; 89 | } 90 | }; 91 | 92 | THREE.Extras.RenderManager.prototype.renderCurrent = function() 93 | { 94 | if(this.current && this.current.render) 95 | { 96 | var now = Date.now()/1000; 97 | var delta = now - this.time; 98 | this.time = now; 99 | 100 | this.current.render.call(this.current, delta, this.renderer); 101 | } 102 | else console.warn('RenderManager: No current render defined.'); 103 | }; 104 | 105 | THREE.Extras.RenderManager.prototype.setCurrent = function(id) 106 | { 107 | if(id in this.renders) 108 | { 109 | this.current = this.renders[id]; 110 | } 111 | else console.warn('RenderManager: Render "'+id+'" not found.'); 112 | }; -------------------------------------------------------------------------------- /sources/Shaders.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * THREE.Extras.Shaders contains extra Fx shaders like godrays 3 | * 4 | * @author Thibaut 'BKcore' Despoulain 5 | * 6 | */ 7 | 8 | THREE = THREE || {}; 9 | THREE.Extras = THREE.Extras || {}; 10 | 11 | THREE.Extras.Shaders = { 12 | // Volumetric Light Approximation (Godrays) 13 | Godrays: { 14 | uniforms: { 15 | tDiffuse: {type: "t", value:0, texture:null}, 16 | fX: {type: "f", value: 0.5}, 17 | fY: {type: "f", value: 0.5}, 18 | fExposure: {type: "f", value: 0.6}, 19 | fDecay: {type: "f", value: 0.93}, 20 | fDensity: {type: "f", value: 0.96}, 21 | fWeight: {type: "f", value: 0.4}, 22 | fClamp: {type: "f", value: 1.0} 23 | }, 24 | 25 | vertexShader: [ 26 | "varying vec2 vUv;", 27 | 28 | "void main() {", 29 | 30 | "vUv = uv;", 31 | "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", 32 | 33 | "}" 34 | ].join("\n"), 35 | 36 | fragmentShader: [ 37 | "varying vec2 vUv;", 38 | "uniform sampler2D tDiffuse;", 39 | 40 | "uniform float fX;", 41 | "uniform float fY;", 42 | "uniform float fExposure;", 43 | "uniform float fDecay;", 44 | "uniform float fDensity;", 45 | "uniform float fWeight;", 46 | "uniform float fClamp;", 47 | 48 | "const int iSamples = 20;", 49 | 50 | "void main()", 51 | "{", 52 | "vec2 deltaTextCoord = vec2(vUv - vec2(fX,fY));", 53 | "deltaTextCoord *= 1.0 / float(iSamples) * fDensity;", 54 | "vec2 coord = vUv;", 55 | "float illuminationDecay = 1.0;", 56 | "vec4 FragColor = vec4(0.0);", 57 | 58 | "for(int i=0; i < iSamples ; i++)", 59 | "{", 60 | "coord -= deltaTextCoord;", 61 | "vec4 texel = texture2D(tDiffuse, coord);", 62 | "texel *= illuminationDecay * fWeight;", 63 | 64 | "FragColor += texel;", 65 | 66 | "illuminationDecay *= fDecay;", 67 | "}", 68 | "FragColor *= fExposure;", 69 | "FragColor = clamp(FragColor, 0.0, fClamp);", 70 | "gl_FragColor = FragColor;", 71 | "}" 72 | ].join("\n") 73 | }, 74 | 75 | // Coeff'd additive buffer blending 76 | Additive: { 77 | uniforms: { 78 | tDiffuse: { type: "t", value: 0, texture: null }, 79 | tAdd: { type: "t", value: 1, texture: null }, 80 | fCoeff: { type: "f", value: 1.0 } 81 | }, 82 | 83 | vertexShader: [ 84 | "varying vec2 vUv;", 85 | 86 | "void main() {", 87 | 88 | "vUv = uv;", 89 | "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", 90 | 91 | "}" 92 | ].join("\n"), 93 | 94 | fragmentShader: [ 95 | "uniform sampler2D tDiffuse;", 96 | "uniform sampler2D tAdd;", 97 | "uniform float fCoeff;", 98 | 99 | "varying vec2 vUv;", 100 | 101 | "void main() {", 102 | 103 | "vec4 texel = texture2D( tDiffuse, vUv );", 104 | "vec4 add = texture2D( tAdd, vUv );", 105 | "gl_FragColor = texel + add * fCoeff;", 106 | 107 | "}" 108 | ].join("\n") 109 | } 110 | }; -------------------------------------------------------------------------------- /sources/Utils.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * THREE.Extras.UTils contains extra useful methods 3 | * 4 | * @author Thibaut 'BKcore' Despoulain 5 | * 6 | */ 7 | 8 | THREE = THREE || {}; 9 | THREE.Extras = THREE.Extras || {}; 10 | THREE.Extras.Utils = THREE.Extras.Utils || {}; 11 | 12 | /*! Projects object origin into screen space coordinates using provided camera */ 13 | THREE.Extras.Utils.projectOnScreen = function(object, camera) 14 | { 15 | var mat = new THREE.Matrix4(); 16 | mat.multiply( camera.matrixWorldInverse, object.matrixWorld); 17 | mat.multiply( camera.projectionMatrix , mat); 18 | 19 | var c = mat.n44; 20 | var lPos = new THREE.Vector3(mat.n14/c, mat.n24/c, mat.n34/c); 21 | lPos.multiplyScalar(0.5); 22 | lPos.addScalar(0.5); 23 | return lPos; 24 | } --------------------------------------------------------------------------------