mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-28 18:45:54 -05:00
145 lines
4.8 KiB
Text
145 lines
4.8 KiB
Text
float xll_saturate( float x) {
|
|
return clamp( x, 0.0, 1.0);
|
|
}
|
|
vec2 xll_saturate( vec2 x) {
|
|
return clamp( x, 0.0, 1.0);
|
|
}
|
|
vec3 xll_saturate( vec3 x) {
|
|
return clamp( x, 0.0, 1.0);
|
|
}
|
|
vec4 xll_saturate( vec4 x) {
|
|
return clamp( x, 0.0, 1.0);
|
|
}
|
|
mat2 xll_saturate(mat2 m) {
|
|
return mat2( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0));
|
|
}
|
|
mat3 xll_saturate(mat3 m) {
|
|
return mat3( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0), clamp(m[2], 0.0, 1.0));
|
|
}
|
|
mat4 xll_saturate(mat4 m) {
|
|
return mat4( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0), clamp(m[2], 0.0, 1.0), clamp(m[3], 0.0, 1.0));
|
|
}
|
|
vec2 xll_vecTSel (bvec2 a, vec2 b, vec2 c) {
|
|
return vec2 (a.x ? b.x : c.x, a.y ? b.y : c.y);
|
|
}
|
|
vec3 xll_vecTSel (bvec3 a, vec3 b, vec3 c) {
|
|
return vec3 (a.x ? b.x : c.x, a.y ? b.y : c.y, a.z ? b.z : c.z);
|
|
}
|
|
vec4 xll_vecTSel (bvec4 a, vec4 b, vec4 c) {
|
|
return vec4 (a.x ? b.x : c.x, a.y ? b.y : c.y, a.z ? b.z : c.z, a.w ? b.w : c.w);
|
|
}
|
|
struct v2f {
|
|
vec4 pos;
|
|
vec4 uv;
|
|
vec3 ray;
|
|
};
|
|
uniform sampler2D _CameraDepthTexture;
|
|
uniform sampler2D _CameraNormalsTexture;
|
|
uniform mat4 _CameraToWorld;
|
|
uniform vec4 _LightColor;
|
|
uniform mat4 _LightMatrix0;
|
|
uniform vec4 _LightPos;
|
|
uniform vec4 _LightPositionRange;
|
|
uniform vec4 _LightShadowData;
|
|
uniform samplerCube _LightTexture0;
|
|
uniform sampler2D _LightTextureB0;
|
|
uniform vec4 _ProjectionParams;
|
|
uniform samplerCube _ShadowMapTexture;
|
|
uniform vec3 _WorldSpaceCameraPos;
|
|
uniform vec4 _ZBufferParams;
|
|
uniform vec4 unity_LightmapFade;
|
|
float Luminance( in vec3 c );
|
|
float Linear01Depth( in float z );
|
|
float DecodeFloatRGBA( in vec4 enc );
|
|
float SampleCubeDistance( in vec3 vec );
|
|
float unitySampleShadow( in vec3 vec, in float mydist );
|
|
float ComputeShadow( in vec3 vec, in float z, in vec2 uv );
|
|
vec4 xlat_main( in v2f i );
|
|
float Luminance( in vec3 c ) {
|
|
return dot( c, vec3( 0.220000, 0.707000, 0.0710000));
|
|
}
|
|
float Linear01Depth( in float z ) {
|
|
return (1.00000 / ((_ZBufferParams.x * z) + _ZBufferParams.y ));
|
|
}
|
|
float DecodeFloatRGBA( in vec4 enc ) {
|
|
vec4 kDecodeDot = vec4( 1.00000, 0.00392157, 1.53787e-005, 6.22737e-009);
|
|
return dot( enc, kDecodeDot);
|
|
}
|
|
float SampleCubeDistance( in vec3 vec ) {
|
|
vec4 packDist;
|
|
packDist = textureCube( _ShadowMapTexture, vec);
|
|
return DecodeFloatRGBA( packDist);
|
|
}
|
|
float unitySampleShadow( in vec3 vec, in float mydist ) {
|
|
float z = 0.00781250;
|
|
vec4 shadowVals;
|
|
vec4 shadows;
|
|
shadowVals.x = SampleCubeDistance( (vec + vec3( z, z, z)));
|
|
shadowVals.y = SampleCubeDistance( (vec + vec3( ( -z ), ( -z ), z)));
|
|
shadowVals.z = SampleCubeDistance( (vec + vec3( ( -z ), z, ( -z ))));
|
|
shadowVals.w = SampleCubeDistance( (vec + vec3( z, ( -z ), ( -z ))));
|
|
shadows = xll_vecTSel (lessThan( shadowVals, vec4( vec4( mydist) )), vec4( _LightShadowData.xxxx ), vec4( 1.00000));
|
|
return dot( shadows, vec4( 0.250000));
|
|
}
|
|
float ComputeShadow( in vec3 vec, in float z, in vec2 uv ) {
|
|
float fade;
|
|
float mydist;
|
|
fade = ((z * _LightShadowData.z ) + _LightShadowData.w );
|
|
fade = xll_saturate( fade );
|
|
mydist = (length( vec ) * _LightPositionRange.w );
|
|
mydist *= 0.970000;
|
|
return unitySampleShadow( vec, mydist);
|
|
return 1.00000;
|
|
}
|
|
vec4 xlat_main( in v2f i ) {
|
|
vec2 uv;
|
|
vec4 nspec;
|
|
vec3 normal;
|
|
float depth;
|
|
vec4 vpos;
|
|
vec3 wpos;
|
|
vec3 tolight;
|
|
vec3 lightDir;
|
|
float att;
|
|
float atten;
|
|
float diff;
|
|
vec3 h;
|
|
float spec;
|
|
vec4 res;
|
|
float fade;
|
|
i.ray = (i.ray * (_ProjectionParams.z / i.ray.z ));
|
|
uv = (i.uv.xy / i.uv.w );
|
|
nspec = texture2D( _CameraNormalsTexture, uv);
|
|
normal = ((nspec.xyz * 2.00000) - 1.00000);
|
|
normal = normalize( normal );
|
|
depth = texture2D( _CameraDepthTexture, uv).x ;
|
|
depth = Linear01Depth( depth);
|
|
vpos = vec4( (i.ray * depth), 1.00000);
|
|
wpos = ( _CameraToWorld * vpos ).xyz ;
|
|
tolight = (wpos - _LightPos.xyz );
|
|
lightDir = ( -normalize( tolight ) );
|
|
att = (dot( tolight, tolight) * _LightPos.w );
|
|
atten = texture2D( _LightTextureB0, vec2( vec2( att))).w ;
|
|
atten *= ComputeShadow( tolight, vpos.z , uv);
|
|
atten *= textureCube( _LightTexture0, ( _LightMatrix0 * vec4( wpos, 1.00000) ).xyz ).w ;
|
|
diff = max( 0.000000, dot( lightDir, normal));
|
|
h = normalize( (lightDir - normalize( (wpos - _WorldSpaceCameraPos) )) );
|
|
spec = pow( max( 0.000000, dot( h, normal)), (nspec.w * 128.000));
|
|
spec *= xll_saturate( atten );
|
|
res.xyz = (_LightColor.xyz * (diff * atten));
|
|
res.w = (spec * Luminance( _LightColor.xyz ));
|
|
fade = ((vpos.z * unity_LightmapFade.z ) + unity_LightmapFade.w );
|
|
res *= xll_saturate( (1.00000 - fade) );
|
|
return exp2( ( -res ) );
|
|
}
|
|
varying vec4 xlv_TEXCOORD0;
|
|
varying vec3 xlv_TEXCOORD1;
|
|
void main() {
|
|
vec4 xl_retval;
|
|
v2f xlt_i;
|
|
xlt_i.pos = vec4(0.0);
|
|
xlt_i.uv = vec4( xlv_TEXCOORD0);
|
|
xlt_i.ray = vec3( xlv_TEXCOORD1);
|
|
xl_retval = xlat_main( xlt_i);
|
|
gl_FragData[0] = vec4( xl_retval);
|
|
}
|