aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/fx/ssao.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/fx/ssao.js')
-rwxr-xr-xjs/helper-classes/RDGE/src/core/script/fx/ssao.js279
1 files changed, 140 insertions, 139 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/fx/ssao.js b/js/helper-classes/RDGE/src/core/script/fx/ssao.js
index e88beef5..08b9bb3b 100755
--- a/js/helper-classes/RDGE/src/core/script/fx/ssao.js
+++ b/js/helper-classes/RDGE/src/core/script/fx/ssao.js
@@ -1,139 +1,140 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License. 4
5 5Redistribution and use in source and binary forms, with or without
6Redistribution and use in source and binary forms, with or without 6modification, are permitted provided that the following conditions are met:
7modification, are permitted provided that the following conditions are met: 7
8 8* Redistributions of source code must retain the above copyright notice,
9 - Redistributions of source code must retain the above copyright notice, 9 this list of conditions and the following disclaimer.
10 this list of conditions and the following disclaimer. 10
11 - Redistributions in binary form must reproduce the above copyright 11* Redistributions in binary form must reproduce the above copyright notice,
12 notice, this list of conditions and the following disclaimer in the 12 this list of conditions and the following disclaimer in the documentation
13 documentation and/or other materials provided with the distribution. 13 and/or other materials provided with the distribution.
14 - Neither the name of Motorola Mobility nor the names of its contributors 14
15 may be used to endorse or promote products derived from this software 15* Neither the name of Motorola Mobility LLC nor the names of its
16 without specific prior written permission. 16 contributors may be used to endorse or promote products derived from this
17 17 software without specific prior written permission.
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28POSSIBILITY OF SUCH DAMAGE. 28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29</copyright> */ 29POSSIBILITY OF SUCH DAMAGE.
30 30</copyright> */
31var RDGE = RDGE || {}; 31
32RDGE.fx = RDGE.fx || {}; 32var RDGE = RDGE || {};
33 33RDGE.fx = RDGE.fx || {};
34/** 34
35* Implements SSAO. 35/**
36* See http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753 36* Implements SSAO.
37* @param v2ScreenSize - size of the viewport in window coordinates 37* See http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753
38*/ 38* @param v2ScreenSize - size of the viewport in window coordinates
39RDGE.fx.fxSSAO = function (enHRDepth) { 39*/
40 function renderInitSSAO(quad) { 40RDGE.fx.fxSSAO = function (enHRDepth) {
41 quad.shader = RDGE.createShader(RDGE.globals.gl, 'ssao_vshader', enHRDepth ? 'ssaohr_fshader' : 'ssao_fshader', ["vert", "texcoord"]); 41 function renderInitSSAO(quad) {
42 quad.renderObj = new RDGE.RenderObject(quad.shader); 42 quad.shader = RDGE.createShader(RDGE.globals.gl, 'ssao_vshader', enHRDepth ? 'ssaohr_fshader' : 'ssao_fshader', ["vert", "texcoord"]);
43 43 quad.renderObj = new RDGE.RenderObject(quad.shader);
44 var quadBuf = getScreenAlignedQuad(); 44
45 quad.vertBuffer = quadBuf.vertexObject; 45 var quadBuf = getScreenAlignedQuad();
46 quad.uvBuffer = quadBuf.texCoordObject; 46 quad.vertBuffer = quadBuf.vertexObject;
47 47 quad.uvBuffer = quadBuf.texCoordObject;
48 quad.renderObj.addTexture("sColMap", 0, RDGE.UNIFORMTYPE.TEXTURE2D); 48
49 quad.renderObj.addTexture("sNormDepthMap", 1, RDGE.UNIFORMTYPE.TEXTURE2D); 49 quad.renderObj.addTexture("sColMap", 0, RDGE.UNIFORMTYPE.TEXTURE2D);
50 quad.renderObj.addTexture("sRandMap", 2, RDGE.UNIFORMTYPE.TEXTURE2D); 50 quad.renderObj.addTexture("sNormDepthMap", 1, RDGE.UNIFORMTYPE.TEXTURE2D);
51 if (enHRDepth) 51 quad.renderObj.addTexture("sRandMap", 2, RDGE.UNIFORMTYPE.TEXTURE2D);
52 quad.renderObj.addTexture("sHRDepthMap", 3, RDGE.UNIFORMTYPE.TEXTURE2D); 52 if (enHRDepth)
53 53 quad.renderObj.addTexture("sHRDepthMap", 3, RDGE.UNIFORMTYPE.TEXTURE2D);
54 quad.renderObj.addBuffers(quad.vertBuffer, RDGE.globals.gl.ARRAY_BUFFER, 3, 0, RDGE.globals.gl.FLOAT); 54
55 quad.renderObj.addBuffers(quad.uvBuffer, RDGE.globals.gl.ARRAY_BUFFER, 2, 2, RDGE.globals.gl.FLOAT); 55 quad.renderObj.addBuffers(quad.vertBuffer, RDGE.globals.gl.ARRAY_BUFFER, 3, 0, RDGE.globals.gl.FLOAT);
56 }; 56 quad.renderObj.addBuffers(quad.uvBuffer, RDGE.globals.gl.ARRAY_BUFFER, 2, 2, RDGE.globals.gl.FLOAT);
57 57 };
58 // Load random normal texture 58
59 this.randTexture = createTexture(RDGE.globals.gl, RDGE.globals.engine._assetPath + "images/random_normal.png"); 59 // Load random normal texture
60 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, this.randTexture); 60 this.randTexture = createTexture(RDGE.globals.gl, RDGE.globals.engine._assetPath + "images/random_normal.png");
61 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_MIN_FILTER, RDGE.globals.gl.LINEAR); 61 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, this.randTexture);
62 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_WRAP_S, RDGE.globals.gl.REPEAT); 62 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_MIN_FILTER, RDGE.globals.gl.LINEAR);
63 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_WRAP_T, RDGE.globals.gl.REPEAT); 63 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_WRAP_S, RDGE.globals.gl.REPEAT);
64 64 RDGE.globals.gl.texParameteri(RDGE.globals.gl.TEXTURE_2D, RDGE.globals.gl.TEXTURE_WRAP_T, RDGE.globals.gl.REPEAT);
65 // Whether or not to use a high res depth texture 65
66 this.enHRDepth = enHRDepth; 66 // Whether or not to use a high res depth texture
67 67 this.enHRDepth = enHRDepth;
68 // Quad for full screen pass 68
69 this.ssaoQuad = new RDGE.ScreenQuad(null); 69 // Quad for full screen pass
70 this.ssaoQuad.initialize(renderInitSSAO); 70 this.ssaoQuad = new RDGE.ScreenQuad(null);
71 71 this.ssaoQuad.initialize(renderInitSSAO);
72 // Set up uniforms 72
73 var activeCam = g_cameraManager.getActiveCamera(); 73 // Set up uniforms
74 this.v3FrustumFLT = activeCam.getFTR(); 74 var activeCam = g_cameraManager.getActiveCamera();
75 this.ssaoQuad.renderObj.addUniform("u_frustumFLT", this.v3FrustumFLT, RDGE.UNIFORMTYPE.FLOAT3); 75 this.v3FrustumFLT = activeCam.getFTR();
76 76 this.ssaoQuad.renderObj.addUniform("u_frustumFLT", this.v3FrustumFLT, RDGE.UNIFORMTYPE.FLOAT3);
77 this.v4ArtVals = [1.0, 1.0, 1.0, 1.0]; 77
78 this.ssaoQuad.renderObj.addUniform("u_artVals", this.v4ArtVals, RDGE.UNIFORMTYPE.FLOAT4); 78 this.v4ArtVals = [1.0, 1.0, 1.0, 1.0];
79 79 this.ssaoQuad.renderObj.addUniform("u_artVals", this.v4ArtVals, RDGE.UNIFORMTYPE.FLOAT4);
80 this.fRandMapSize = 64.0; 80
81 this.ssaoQuad.renderObj.addUniform("u_randMapSize", this.fRandMapSize, RDGE.UNIFORMTYPE.FLOAT); 81 this.fRandMapSize = 64.0;
82 82 this.ssaoQuad.renderObj.addUniform("u_randMapSize", this.fRandMapSize, RDGE.UNIFORMTYPE.FLOAT);
83 this.v2ScreenSize = [1024, 1024]; 83
84 this.ssaoQuad.renderObj.addUniform("u_screenSize", this.v2ScreenSize, RDGE.UNIFORMTYPE.FLOAT2); 84 this.v2ScreenSize = [1024, 1024];
85}; 85 this.ssaoQuad.renderObj.addUniform("u_screenSize", this.v2ScreenSize, RDGE.UNIFORMTYPE.FLOAT2);
86 86};
87/** 87
88* Contributes SSAO to the passed offscreen surface, rendering to another surface. 88/**
89* See http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753 89* Contributes SSAO to the passed offscreen surface, rendering to another surface.
90* @param srcTexColor - color surface of rendered scene 90* See http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753
91* @param srcTexNormDepth - screenspace normal+depth surface for scene; {nx, ny, nz, depth} 91* @param srcTexColor - color surface of rendered scene
92* @param dstRenderTarget - where to put the result of SSAO 92* @param srcTexNormDepth - screenspace normal+depth surface for scene; {nx, ny, nz, depth}
93* @param sampleRadius - 93* @param dstRenderTarget - where to put the result of SSAO
94* @param intensity - 94* @param sampleRadius -
95* @param distScale - 95* @param intensity -
96* @param bias - 96* @param distScale -
97*/ 97* @param bias -
98RDGE.fx.fxSSAO.prototype.doSSAO = function (srcTexColor, srcTexNormDepth, srcTexHRDepth, dstRenderTarget, sampleRadius, intensity, distScale, bias) { 98*/
99 // Set art params and other uniforms 99RDGE.fx.fxSSAO.prototype.doSSAO = function (srcTexColor, srcTexNormDepth, srcTexHRDepth, dstRenderTarget, sampleRadius, intensity, distScale, bias) {
100 this.v4ArtVals[0] = sampleRadius; 100 // Set art params and other uniforms
101 this.v4ArtVals[1] = intensity; 101 this.v4ArtVals[0] = sampleRadius;
102 this.v4ArtVals[2] = distScale; 102 this.v4ArtVals[1] = intensity;
103 this.v4ArtVals[3] = bias; 103 this.v4ArtVals[2] = distScale;
104 104 this.v4ArtVals[3] = bias;
105 this.v2ScreenSize[0] = dstRenderTarget ? dstRenderTarget.frameBuffer.width : RDGE.globals.width; 105
106 this.v2ScreenSize[1] = dstRenderTarget ? dstRenderTarget.frameBuffer.height : RDGE.globals.height; 106 this.v2ScreenSize[0] = dstRenderTarget ? dstRenderTarget.frameBuffer.width : RDGE.globals.width;
107 107 this.v2ScreenSize[1] = dstRenderTarget ? dstRenderTarget.frameBuffer.height : RDGE.globals.height;
108 // Do ssao 108
109 RDGE.globals.gl.bindFramebuffer(RDGE.globals.gl.FRAMEBUFFER, dstRenderTarget ? dstRenderTarget.frameBuffer : null); 109 // Do ssao
110 // gl.viewport(0, 0, 99999, 99999); 110 RDGE.globals.gl.bindFramebuffer(RDGE.globals.gl.FRAMEBUFFER, dstRenderTarget ? dstRenderTarget.frameBuffer : null);
111 RDGE.globals.gl.clear(RDGE.globals.gl.COLOR_BUFFER_BIT); 111 // gl.viewport(0, 0, 99999, 99999);
112 112 RDGE.globals.gl.clear(RDGE.globals.gl.COLOR_BUFFER_BIT);
113 RDGE.globals.gl.disable(RDGE.globals.gl.DEPTH_TEST); 113
114 114 RDGE.globals.gl.disable(RDGE.globals.gl.DEPTH_TEST);
115 RDGE.globals.gl.useProgram(this.ssaoQuad.shader); 115
116 116 RDGE.globals.gl.useProgram(this.ssaoQuad.shader);
117 this.ssaoQuad.renderObj.bindBuffers(); 117
118 this.ssaoQuad.renderObj.bindTextures(); 118 this.ssaoQuad.renderObj.bindBuffers();
119 this.ssaoQuad.renderObj.bindUniforms(); 119 this.ssaoQuad.renderObj.bindTextures();
120 120 this.ssaoQuad.renderObj.bindUniforms();
121 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE0); 121
122 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, srcTexColor); 122 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE0);
123 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE1); 123 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, srcTexColor);
124 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, srcTexNormDepth); 124 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE1);
125 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE2); 125 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, srcTexNormDepth);
126 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, this.randTexture); 126 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE2);
127 if (this.enHRDepth) { 127 RDGE.globals.gl.bindTexture(RDGE.globals.gl.TEXTURE_2D, this.randTexture);
128 RDGE.globals.gl.activeTexture(RDGE.globals.gl.TEXTURE3); 128 if