Revision 6e7c8721
Added by Leszek Koltunski about 7 years ago
src/main/res/raw/main_vertex_shader.glsl | ||
---|---|---|
38 | 38 |
uniform vec3 u_objD; // half of object width x half of object height X half the depth; |
39 | 39 |
// point (0,0,0) is the center of the object |
40 | 40 |
|
41 |
uniform float u_Depth; // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes. |
|
42 |
// I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n, |
|
43 |
// .far = f, .diff = f-n so maybe u_Depth is redundant |
|
44 |
// Update: this struct is only available in fragment shaders |
|
45 |
|
|
46 | 41 |
uniform mat4 u_MVPMatrix; // the combined model/view/projection matrix. |
47 | 42 |
uniform mat4 u_MVMatrix; // the combined model/view matrix. |
48 | 43 |
|
... | ... | |
147 | 142 |
return min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT)),E); |
148 | 143 |
} |
149 | 144 |
|
150 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
151 |
// Clamp v.z to (-u_Depth,u_Depth) with the following function: |
|
152 |
// define h to be, say, 0.7; let H=u_Depth |
|
153 |
// if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H) |
|
154 |
// else if v.z > hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H) |
|
155 |
// else v.z = v.z |
|
156 |
|
|
157 |
void restrictZ(inout float v) |
|
158 |
{ |
|
159 |
const float h = 0.7; |
|
160 |
float signV = 2.0*max(0.0,sign(v))-1.0; |
|
161 |
float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth; |
|
162 |
float b = max(0.0,sign(abs(v)-h*u_Depth)); |
|
163 |
|
|
164 |
v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v. |
|
165 |
} |
|
166 |
|
|
167 | 145 |
////////////////////////////////////////////////////////////////////////////////////////////// |
168 | 146 |
// DEFORM EFFECT |
169 | 147 |
// |
... | ... | |
575 | 553 |
|
576 | 554 |
j+=3; |
577 | 555 |
} |
578 |
|
|
579 |
restrictZ(v.z); |
|
580 | 556 |
#endif |
581 | 557 |
|
582 | 558 |
v_Position = v; |
Also available in: Unified diff
Simplifications.