diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/fx/ssao.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/fx/ssao.js | 279 |
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> |
2 | Copyright (c) 2012, Motorola Mobility, Inc | 2 | Copyright (c) 2012, Motorola Mobility LLC. |
3 | All Rights Reserved. | 3 | All Rights Reserved. |
4 | BSD License. | 4 | |
5 | 5 | Redistribution and use in source and binary forms, with or without | |
6 | Redistribution and use in source and binary forms, with or without | 6 | modification, are permitted provided that the following conditions are met: |
7 | modification, 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. | |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 18 | |
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | POSSIBILITY OF SUCH DAMAGE. | 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | </copyright> */ | 29 | POSSIBILITY OF SUCH DAMAGE. |
30 | 30 | </copyright> */ | |
31 | var RDGE = RDGE || {}; | 31 | |
32 | RDGE.fx = RDGE.fx || {}; | 32 | var RDGE = RDGE || {}; |
33 | 33 | RDGE.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 |
39 | RDGE.fx.fxSSAO = function (enHRDepth) { | 39 | */ |
40 | function renderInitSSAO(quad) { | 40 | RDGE.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 - |
98 | RDGE.fx.fxSSAO.prototype.doSSAO = function (srcTexColor, srcTexNormDepth, srcTexHRDepth, dstRenderTarget, sampleRadius, intensity, distScale, bias) { | 98 | */ |
99 | // Set art params and other uniforms | 99 | RDGE.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.ra |