mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-25 09:08:22 -05:00
79 lines
3.1 KiB
Text
79 lines
3.1 KiB
Text
#version 300 es
|
|
#define gl_FragData _glesFragData
|
|
layout(location = 0) out mediump vec4 _glesFragData[4];
|
|
highp float xll_saturate_f( highp float x) {
|
|
return clamp( x, 0.0, 1.0);
|
|
}
|
|
struct v2f_ao {
|
|
highp vec4 pos;
|
|
highp vec2 uv;
|
|
highp vec2 uvr;
|
|
};
|
|
uniform highp vec4 _ProjectionParams;
|
|
uniform highp vec2 _NoiseScale;
|
|
uniform highp vec4 _CameraDepthNormalsTexture_ST;
|
|
uniform highp sampler2D _CameraDepthNormalsTexture;
|
|
uniform sampler2D _RandomTexture;
|
|
uniform highp vec4 _Params;
|
|
highp float DecodeFloatRG( in highp vec2 enc ) {
|
|
highp vec2 kDecodeDot = vec2( 1.0, 0.00392157);
|
|
return dot( enc, kDecodeDot);
|
|
}
|
|
highp vec3 DecodeViewNormalStereo( in highp vec4 enc4 ) {
|
|
highp float kScale = 1.7777;
|
|
highp vec3 nn = ((enc4.xyz * vec3( (2.0 * kScale), (2.0 * kScale), 0.0)) + vec3( (-kScale), (-kScale), 1.0));
|
|
highp float g = (2.0 / dot( nn.xyz, nn.xyz));
|
|
highp vec3 n;
|
|
n.xy = (g * nn.xy);
|
|
n.z = (g - 1.0);
|
|
return n;
|
|
}
|
|
void DecodeDepthNormal( in highp vec4 enc, out highp float depth, out highp vec3 normal ) {
|
|
depth = DecodeFloatRG( enc.zw);
|
|
normal = DecodeViewNormalStereo( enc);
|
|
}
|
|
mediump float frag_ao( in v2f_ao i, in highp int sampleCount, in highp vec3 samples[8] ) {
|
|
mediump vec3 randN = ((texture( _RandomTexture, i.uvr).xyz * 2.0) - 1.0);
|
|
highp vec4 depthnormal = texture( _CameraDepthNormalsTexture, i.uv);
|
|
highp vec3 viewNorm;
|
|
highp float depth;
|
|
DecodeDepthNormal( depthnormal, depth, viewNorm);
|
|
depth *= _ProjectionParams.z;
|
|
highp float scale = (_Params.x / depth);
|
|
highp float occ = 0.0;
|
|
highp int s = 0;
|
|
for ( ; (s < sampleCount); (++s)) {
|
|
mediump vec3 randomDir = reflect( samples[s], randN);
|
|
mediump float flip = (( (dot( viewNorm, randomDir) < 0.0) ) ? ( 1.0 ) : ( -1.0 ));
|
|
randomDir *= (-flip);
|
|
randomDir += (viewNorm * 0.3);
|
|
highp vec2 offset = (randomDir.xy * scale);
|
|
highp float sD = (depth - (randomDir.z * _Params.x));
|
|
highp vec4 sampleND = texture( _CameraDepthNormalsTexture, (i.uv + offset));
|
|
highp float sampleD;
|
|
highp vec3 sampleN;
|
|
DecodeDepthNormal( sampleND, sampleD, sampleN);
|
|
sampleD *= _ProjectionParams.z;
|
|
highp float zd = xll_saturate_f((sD - sampleD));
|
|
if ((zd > _Params.y)){
|
|
occ += pow( (1.0 - zd), _Params.z);
|
|
}
|
|
}
|
|
occ /= float(sampleCount);
|
|
return (1.0 - occ);
|
|
}
|
|
mediump vec4 frag( in v2f_ao i ) {
|
|
const highp vec3[8] RAND_SAMPLES = vec3[8]( vec3( 0.0130572, 0.587232, -0.119337), vec3( 0.323078, 0.0220727, -0.418873), vec3( -0.310725, -0.191367, 0.0561369), vec3( -0.479646, 0.0939877, -0.580265), vec3( 0.139999, -0.33577, 0.559679), vec3( -0.248458, 0.255532, 0.348944), vec3( 0.18719, -0.702764, -0.231748), vec3( 0.884915, 0.284208, 0.368524));
|
|
return vec4( frag_ao( i, 8, RAND_SAMPLES));
|
|
}
|
|
in highp vec2 xlv_TEXCOORD0;
|
|
in highp vec2 xlv_TEXCOORD1;
|
|
void main() {
|
|
mediump vec4 xl_retval;
|
|
v2f_ao xlt_i;
|
|
xlt_i.pos = vec4(0.0);
|
|
xlt_i.uv = vec2(xlv_TEXCOORD0);
|
|
xlt_i.uvr = vec2(xlv_TEXCOORD1);
|
|
xl_retval = frag( xlt_i);
|
|
gl_FragData[0] = vec4(xl_retval);
|
|
}
|