diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/fx')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/fx/blur.js | 196 | ||||
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/fx/ssao.js | 116 |
2 files changed, 312 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/fx/blur.js b/js/helper-classes/RDGE/src/core/script/fx/blur.js new file mode 100644 index 00000000..7fa24712 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/fx/blur.js | |||
@@ -0,0 +1,196 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | |||
8 | /** | ||
9 | * Implements a 5x5 blur. | ||
10 | * See http://prideout.net/archive/bloom/ | ||
11 | * @param mipSizes - up to three pow2 mip sizes to be separately blurred | ||
12 | * and combined with the src image, e.g. [256,128,64] | ||
13 | * @param enAuxTexture - true to enable an extra texture to be added to | ||
14 | * the weighted blur mips (see doBlur) | ||
15 | */ | ||
16 | function fxBlur(mipSizes, enAuxTexture) | ||
17 | { | ||
18 | var separableBlurCombine_fshader = [ | ||
19 | "#ifdef GL_ES", | ||
20 | "precision highp float;", | ||
21 | "#endif", | ||
22 | |||
23 | "varying vec2 vTexcoord;", // base tex coord | ||
24 | "uniform vec4 vWeights;", // blend weights | ||
25 | |||
26 | enAuxTexture ? "uniform sampler2D sTextureAux;" : "", // aux image (unweighted) | ||
27 | "uniform sampler2D sTexture1;", // source texture #1 | ||
28 | mipSizes[0] ? "uniform sampler2D sTexture2;" : "", // source texture #2 | ||
29 | mipSizes[1] ? "uniform sampler2D sTexture3;" : "", // source texture #3 | ||
30 | mipSizes[2] ? "uniform sampler2D sTexture4;" : "", // source texture #4 | ||
31 | |||
32 | "void main()", | ||
33 | "{", | ||
34 | "vec4 blurCol = vWeights.x * texture2D(sTexture1, vTexcoord, -32.0);", | ||
35 | mipSizes[0] ? "blurCol += vWeights.y * texture2D(sTexture2, vTexcoord, -32.0);" : "", | ||
36 | mipSizes[1] ? "blurCol += vWeights.z * texture2D(sTexture3, vTexcoord, -32.0);" : "", | ||
37 | mipSizes[2] ? "blurCol += vWeights.w * texture2D(sTexture4, vTexcoord, -32.0);" : "", | ||
38 | |||
39 | enAuxTexture ? "gl_FragColor = texture2D(sTextureAux, vTexcoord, -32.0) + blurCol;" : "gl_FragColor = blurCol;", | ||
40 | "}" | ||
41 | |||
42 | ].join("\n"); | ||
43 | |||
44 | |||
45 | function renderInitBlur(quad) | ||
46 | { | ||
47 | quad.shader = createShader(gl, 'separableBlur_vshader', 'separableBlur_fshader', [ "vert", "texcoord"]); | ||
48 | quad.renderObj = new RenderObject(quad.shader); | ||
49 | |||
50 | quad.vertBuffer = quadBuf.vertexObject; | ||
51 | quad.uvBuffer = quadBuf.texCoordObject; | ||
52 | |||
53 | quad.renderObj.addTexture("sTexture", 0, UNIFORMTYPE.TEXTURE2D); | ||
54 | |||
55 | quad.renderObj.addBuffers(quad.vertBuffer, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); | ||
56 | quad.renderObj.addBuffers(quad.uvBuffer, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); | ||
57 | } | ||
58 | |||
59 | function renderInitCombine(quad) | ||
60 | { | ||
61 | quad.shader = createShader(gl, 'separableBlur_vshader', separableBlurCombine_fshader, [ "vert", "texcoord"]); | ||
62 | quad.renderObj = new RenderObject(quad.shader); | ||
63 | |||
64 | quad.vertBuffer = quadBuf.vertexObject; | ||
65 | quad.uvBuffer = quadBuf.texCoordObject; | ||
66 | |||
67 | quad.renderObj.addTexture("sTexture1", 0, UNIFORMTYPE.TEXTURE2D); | ||
68 | quad.renderObj.addTexture("sTexture2", 1, UNIFORMTYPE.TEXTURE2D); | ||
69 | quad.renderObj.addTexture("sTexture3", 2, UNIFORMTYPE.TEXTURE2D); | ||
70 | quad.renderObj.addTexture("sTexture4", 3, UNIFORMTYPE.TEXTURE2D); | ||
71 | quad.renderObj.addTexture("sTextureAux", 4, UNIFORMTYPE.TEXTURE2D); | ||
72 | |||
73 | quad.renderObj.addBuffers(quad.vertBuffer, gl.ARRAY_BUFFER, 3, 0, gl.FLOAT); | ||
74 | quad.renderObj.addBuffers(quad.uvBuffer, gl.ARRAY_BUFFER, 2, 2, gl.FLOAT); | ||
75 | } | ||
76 | |||
77 | // Screen aligned quad geometry | ||
78 | var quadBuf = getScreenAlignedQuad(); | ||
79 | |||
80 | // Fbos for each mip level; two sets are used to pingpong horizontal and vertical blur | ||
81 | mipSizes = mipSizes || [128, 64, 32]; | ||
82 | |||
83 | this.fboSet1 = []; | ||
84 | this.fboSet2 = []; | ||
85 | |||
86 | for(var i in mipSizes) | ||
87 | { | ||
88 | this.fboSet1.push(createRenderTargetTexture(mipSizes[i], mipSizes[i])); | ||
89 | this.fboSet2.push(createRenderTargetTexture(mipSizes[i], mipSizes[i])); | ||
90 | } | ||
91 | |||
92 | // Blitter for downsampling | ||
93 | this.blitQuad = new ScreenQuad(null); | ||
94 | this.blitQuad.initialize(renderInitScreenQuad); | ||
95 | |||
96 | // Blur shader | ||
97 | this.blurQuad = new ScreenQuad(null); | ||
98 | this.blurQuad.initialize(renderInitBlur); | ||
99 | |||
100 | this.v3Kernel = [ 5.0 / 16.0, 6.0 / 16.0, 5.0 / 16.0 ]; | ||
101 | this.blurQuad.renderObj.addUniform("vCoeffs", this.v3Kernel, UNIFORMTYPE.FLOAT3); | ||
102 | |||
103 | this.v2Offset = vec2.zero(); | ||
104 | this.blurQuad.renderObj.addUniform("vOffset", this.v2Offset, UNIFORMTYPE.FLOAT2); | ||
105 | |||
106 | // Combine/blend shader | ||
107 | this.combineQuad = new ScreenQuad(null); | ||
108 | this.combineQuad.initialize(renderInitCombine); | ||
109 | |||
110 | this.v4Weights = [0.25, 0.25, 0.25, 0.25]; | ||
111 | this.combineQuad.renderObj.addUniform("vWeights", this.v4Weights, UNIFORMTYPE.FLOAT4); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Blurs the passed render target. | ||
116 | * See http://prideout.net/archive/bloom/ | ||
117 | * @param srcTexture - source image to blur | ||
118 | * @param dstRenderTarget - where to put the result of the blur | ||
119 | * @param weights - array of 4 blend weights for the blurred mip levels | ||
120 | * levels in the form [srcTexture-weight, mip0-weight, | ||
121 | * mip1-weight, mip2-weight] | ||
122 | * @param auxTexture - null, else an extra texture to be added to the | ||
123 | * weighted blur mips | ||
124 | */ | ||
125 | fxBlur.prototype.doBlur = function(srcTexture, dstRenderTarget, weights, auxTexture) | ||
126 | { | ||
127 | // Set weights | ||
128 | this.v4Weights[0] = (weights == undefined) ? 0.25 : weights[0]; | ||
129 | this.v4Weights[1] = (weights == undefined) ? 0.25 : weights[1]; | ||
130 | this.v4Weights[2] = (weights == undefined) ? 0.25 : weights[2]; | ||
131 | this.v4Weights[3] = (weights == undefined) ? 0.25 : weights[3]; | ||
132 | |||
133 | // Do horizontal blur of fbo set 1 into fbo set 2 | ||
134 | for (var i = 0, fboSrc, fboDst; (fboSrc = srcTexture) && (fboDst = this.fboSet2[i]); i++) | ||
135 | { | ||
136 | gl.bindFramebuffer(gl.FRAMEBUFFER, fboDst.frameBuffer); | ||
137 | gl.viewport(0, 0, fboDst.frameBuffer.width, fboDst.frameBuffer.height); | ||
138 | gl.clear(gl.COLOR_BUFFER_BIT); | ||
139 | gl.disable(gl.DEPTH_TEST); | ||
140 | |||
141 | this.v2Offset[0] = 0.0; | ||
142 | this.v2Offset[1] = 1.2 / fboDst.frameBuffer.width; | ||
143 | |||
144 | this.blurQuad.texture = fboSrc; | ||
145 | |||
146 | renderProcScreenQuad(this.blurQuad); | ||
147 | } | ||
148 | |||
149 | // Do vertical blur of fbo set 2 into fbo set 1 | ||
150 | for (var i = 0, fboSrc, fboDst; (fboSrc = this.fboSet2[i]) && (fboDst = this.fboSet1[i]); i++) | ||
151 | { | ||
152 | gl.bindFramebuffer(gl.FRAMEBUFFER, fboDst.frameBuffer); | ||
153 | gl.viewport(0, 0, fboDst.frameBuffer.width, fboDst.frameBuffer.height); | ||
154 | gl.clear(gl.COLOR_BUFFER_BIT); | ||
155 | gl.disable(gl.DEPTH_TEST); | ||
156 | |||
157 | this.v2Offset[0] = 1.2 / fboDst.frameBuffer.width; | ||
158 | this.v2Offset[1] = 0.0; | ||
159 | |||
160 | this.blurQuad.texture = fboSrc; | ||
161 | |||
162 | renderProcScreenQuad(this.blurQuad); | ||
163 | } | ||
164 | |||
165 | // Do a weighted combine of the textures in fbo set 1 | ||
166 | gl.bindFramebuffer(gl.FRAMEBUFFER, dstRenderTarget ? dstRenderTarget.frameBuffer : null); | ||
167 | gl.viewport(0, 0, g_width, g_height); | ||
168 | gl.clear(gl.COLOR_BUFFER_BIT); | ||
169 | |||
170 | gl.disable(gl.DEPTH_TEST); | ||
171 | |||
172 | gl.useProgram(this.combineQuad.shader); | ||
173 | |||
174 | this.combineQuad.renderObj.bindBuffers(); | ||
175 | this.combineQuad.renderObj.bindTextures(); | ||
176 | this.combineQuad.renderObj.bindUniforms(); | ||
177 | |||
178 | gl.activeTexture(gl.TEXTURE0); | ||
179 | gl.bindTexture(gl.TEXTURE_2D, srcTexture); | ||
180 | gl.activeTexture(gl.TEXTURE1); | ||
181 | gl.bindTexture(gl.TEXTURE_2D, this.fboSet1[0]); | ||
182 | gl.activeTexture(gl.TEXTURE2); | ||
183 | gl.bindTexture(gl.TEXTURE_2D, this.fboSet1[1]); | ||
184 | gl.activeTexture(gl.TEXTURE3); | ||
185 | gl.bindTexture(gl.TEXTURE_2D, this.fboSet1[2]); | ||
186 | gl.activeTexture(gl.TEXTURE4); | ||
187 | gl.bindTexture(gl.TEXTURE_2D, auxTexture); | ||
188 | gl.activeTexture(gl.TEXTURE0); | ||
189 | |||
190 | gl.drawArrays(gl.TRIANGLES, 0, 6); | ||
191 | |||
192 | gl.enable(gl.DEPTH_TEST); | ||
193 | gl.useProgram(null); | ||
194 | |||
195 | return dstRenderTarget; | ||
196 | } | ||
diff --git a/js/helper-classes/RDGE/src/core/script/fx/ssao.js b/js/helper-classes/RDGE/src/core/script/fx/ssao.js new file mode 100644 index 00000000..05793594 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/fx/ssao.js | |||
@@ -0,0 +1,116 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | |||
8 | /** | ||
9 | * Implements SSAO. | ||
10 | * See http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753 | ||
11 | * @param v2ScreenSize - size of the viewport in window coordinates | ||
12 | */ | ||
13 | function fxSSAO(enHRDepth) | ||
14 | { | ||
15 | function renderInitSSAO(quad) | ||