mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-12-04 21:31:11 -05:00
146 lines
5.3 KiB
Text
146 lines
5.3 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 {
|
||
|
highp vec4 pos;
|
||
|
highp vec4 uv;
|
||
|
highp vec3 ray;
|
||
|
};
|
||
|
uniform sampler2D _CameraDepthTexture;
|
||
|
uniform sampler2D _CameraNormalsTexture;
|
||
|
uniform highp mat4 _CameraToWorld;
|
||
|
uniform highp vec4 _LightColor;
|
||
|
uniform highp mat4 _LightMatrix0;
|
||
|
uniform highp vec4 _LightPos;
|
||
|
uniform highp vec4 _LightPositionRange;
|
||
|
uniform highp vec4 _LightShadowData;
|
||
|
uniform samplerCube _LightTexture0;
|
||
|
uniform sampler2D _LightTextureB0;
|
||
|
uniform highp vec4 _ProjectionParams;
|
||
|
uniform samplerCube _ShadowMapTexture;
|
||
|
uniform highp vec3 _WorldSpaceCameraPos;
|
||
|
uniform highp vec4 _ZBufferParams;
|
||
|
uniform highp vec4 unity_LightmapFade;
|
||
|
mediump float Luminance( in mediump vec3 c );
|
||
|
highp float Linear01Depth( in highp float z );
|
||
|
highp float DecodeFloatRGBA( in highp vec4 enc );
|
||
|
highp float SampleCubeDistance( in highp vec3 vec );
|
||
|
mediump float unitySampleShadow( in highp vec3 vec, in highp float mydist );
|
||
|
mediump float ComputeShadow( in highp vec3 vec, in highp float z, in highp vec2 uv );
|
||
|
mediump vec4 xlat_main( in v2f i );
|
||
|
mediump float Luminance( in mediump vec3 c ) {
|
||
|
return dot( c, mediump vec3( 0.220000, 0.707000, 0.0710000));
|
||
|
}
|
||
|
highp float Linear01Depth( in highp float z ) {
|
||
|
return (1.00000 / ((_ZBufferParams.x * z) + _ZBufferParams.y ));
|
||
|
}
|
||
|
highp float DecodeFloatRGBA( in highp vec4 enc ) {
|
||
|
highp vec4 kDecodeDot = vec4( 1.00000, 0.00392157, 1.53787e-005, 6.22737e-009);
|
||
|
return dot( enc, kDecodeDot);
|
||
|
}
|
||
|
highp float SampleCubeDistance( in highp vec3 vec ) {
|
||
|
highp vec4 packDist;
|
||
|
packDist = textureCube( _ShadowMapTexture, vec);
|
||
|
return DecodeFloatRGBA( packDist);
|
||
|
}
|
||
|
mediump float unitySampleShadow( in highp vec3 vec, in highp float mydist ) {
|
||
|
highp float z = 0.00781250;
|
||
|
highp vec4 shadowVals;
|
||
|
mediump 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));
|
||
|
}
|
||
|
mediump float ComputeShadow( in highp vec3 vec, in highp float z, in highp vec2 uv ) {
|
||
|
highp float fade;
|
||
|
highp 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;
|
||
|
}
|
||
|
mediump vec4 xlat_main( in v2f i ) {
|
||
|
highp vec2 uv;
|
||
|
mediump vec4 nspec;
|
||
|
mediump vec3 normal;
|
||
|
highp float depth;
|
||
|
highp vec4 vpos;
|
||
|
highp vec3 wpos;
|
||
|
highp vec3 tolight;
|
||
|
mediump vec3 lightDir;
|
||
|
highp float att;
|
||
|
highp float atten;
|
||
|
mediump float diff;
|
||
|
mediump vec3 h;
|
||
|
highp float spec;
|
||
|
mediump vec4 res;
|
||
|
highp 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 highp vec4 xlv_TEXCOORD0;
|
||
|
varying highp vec3 xlv_TEXCOORD1;
|
||
|
void main() {
|
||
|
mediump vec4 xl_retval;
|
||
|
v2f xlt_i;
|
||
|
xlt_i.pos = highp vec4(0.0);
|
||
|
xlt_i.uv = highp vec4( xlv_TEXCOORD0);
|
||
|
xlt_i.ray = highp vec3( xlv_TEXCOORD1);
|
||
|
xl_retval = xlat_main( xlt_i);
|
||
|
gl_FragData[0] = mediump vec4( xl_retval);
|
||
|
}
|