spoonedit/Shaders/ForwardPass.fsh

239 lines
6.4 KiB
Text
Raw Permalink Normal View History

2024-12-21 17:09:23 +01:00
#version 460
in vec2 TexCoord;
in vec4 WldPos;
in mat3 TBN;
in vec3 aanormal;
layout(location = 0) out vec4 FragColor;
layout(location = 1) out int ObjId;
layout(location = 2) out vec4 WorldPos;
uniform sampler2D TextureSampler;
uniform sampler2D NormalMapSampler;
uniform sampler2D EmmissionMapSampler;
uniform sampler2D SpecularMapSampler;
uniform sampler2D TeamcolorMap;
uniform sampler2D ShadowMapSampler;
uniform int ObjIdIn;
uniform vec4 ObjColor;
uniform vec3 LightDirection;
uniform vec3 CameraPosition;
uniform float distribution;
uniform float shadowHardness;
in vec3 shadowSpacePos;
const float pixel = 1.0f / 8192.0f;
vec4 TeamIdColors[3] = vec4[3](vec4(0.41568628f, 0.74901962f, 0.04705882f, 1.00000000f ),
vec4(1.00000000f, 0.89999998f, 0.00000000f, 1.00000000f ),
vec4(0.55000000f, 0.05000000f, 0.50000000f, 1.00000000f ));
vec2 ShadowOffsets[81] = vec2[81](
vec2(-4.0f*pixel,-4.0f*pixel),
vec2(-4.0f*pixel,-3.0f*pixel),
vec2(-4.0f*pixel,-2.0f*pixel),
vec2(-4.0f*pixel,-1.0f*pixel),
vec2(-4.0f*pixel,0.0f*pixel),
vec2(-4.0f*pixel,1.0f*pixel),
vec2(-4.0f*pixel,2.0f*pixel),
vec2(-4.0f*pixel,3.0f*pixel),
vec2(-4.0f*pixel,4.0f*pixel),
vec2(-3.0f*pixel,-4.0f*pixel),
vec2(-3.0f*pixel,-3.0f*pixel),
vec2(-3.0f*pixel,-2.0f*pixel),
vec2(-3.0f*pixel,-1.0f*pixel),
vec2(-3.0f*pixel,0.0f*pixel),
vec2(-3.0f*pixel,1.0f*pixel),
vec2(-3.0f*pixel,2.0f*pixel),
vec2(-3.0f*pixel,3.0f*pixel),
vec2(-3.0f*pixel,4.0f*pixel),
vec2(-2.0f*pixel,-4.0f*pixel),
vec2(-2.0f*pixel,-3.0f*pixel),
vec2(-2.0f*pixel,-2.0f*pixel),
vec2(-2.0f*pixel,-1.0f*pixel),
vec2(-2.0f*pixel,0.0f*pixel),
vec2(-2.0f*pixel,1.0f*pixel),
vec2(-2.0f*pixel,2.0f*pixel),
vec2(-2.0f*pixel,3.0f*pixel),
vec2(-2.0f*pixel,4.0f*pixel),
vec2(-1.0f*pixel,-4.0f*pixel),
vec2(-1.0f*pixel,-3.0f*pixel),
vec2(-1.0f*pixel,-2.0f*pixel),
vec2(-1.0f*pixel,-1.0f*pixel),
vec2(-1.0f*pixel,0.0f*pixel),
vec2(-1.0f*pixel,1.0f*pixel),
vec2(-1.0f*pixel,2.0f*pixel),
vec2(-1.0f*pixel,3.0f*pixel),
vec2(-1.0f*pixel,4.0f*pixel),
vec2(0.0f*pixel,-4.0f*pixel),
vec2(0.0f*pixel,-3.0f*pixel),
vec2(0.0f*pixel,-2.0f*pixel),
vec2(0.0f*pixel,-1.0f*pixel),
vec2(0.0f*pixel,0.0f*pixel),
vec2(0.0f*pixel,1.0f*pixel),
vec2(0.0f*pixel,2.0f*pixel),
vec2(0.0f*pixel,3.0f*pixel),
vec2(0.0f*pixel,4.0f*pixel),
vec2(1.0f*pixel,-4.0f*pixel),
vec2(1.0f*pixel,-3.0f*pixel),
vec2(1.0f*pixel,-2.0f*pixel),
vec2(1.0f*pixel,-1.0f*pixel),
vec2(1.0f*pixel,0.0f*pixel),
vec2(1.0f*pixel,1.0f*pixel),
vec2(1.0f*pixel,2.0f*pixel),
vec2(1.0f*pixel,3.0f*pixel),
vec2(1.0f*pixel,4.0f*pixel),
vec2(2.0f*pixel,-4.0f*pixel),
vec2(2.0f*pixel,-3.0f*pixel),
vec2(2.0f*pixel,-2.0f*pixel),
vec2(2.0f*pixel,-1.0f*pixel),
vec2(2.0f*pixel,0.0f*pixel),
vec2(2.0f*pixel,1.0f*pixel),
vec2(2.0f*pixel,2.0f*pixel),
vec2(2.0f*pixel,3.0f*pixel),
vec2(2.0f*pixel,4.0f*pixel),
vec2(3.0f*pixel,-4.0f*pixel),
vec2(3.0f*pixel,-3.0f*pixel),
vec2(3.0f*pixel,-2.0f*pixel),
vec2(3.0f*pixel,-1.0f*pixel),
vec2(3.0f*pixel,0.0f*pixel),
vec2(3.0f*pixel,1.0f*pixel),
vec2(3.0f*pixel,2.0f*pixel),
vec2(3.0f*pixel,3.0f*pixel),
vec2(3.0f*pixel,4.0f*pixel),
vec2(4.0f*pixel,-4.0f*pixel),
vec2(4.0f*pixel,-3.0f*pixel),
vec2(4.0f*pixel,-2.0f*pixel),
vec2(4.0f*pixel,-1.0f*pixel),
vec2(4.0f*pixel,0.0f*pixel),
vec2(4.0f*pixel,1.0f*pixel),
vec2(4.0f*pixel,2.0f*pixel),
vec2(4.0f*pixel,3.0f*pixel),
vec2(4.0f*pixel,4.0f*pixel)
);
uniform int TeamId;
float specPow = 128.0f;
//float specPow = 16;
float euler = 2.71828182845904523536f;
float gaussian(float x){
return pow(euler,
-(pow(x,2)
/
pow(2.0f*distribution,2)));
}
float calcMissing(vec3 normal){
return abs(sqrt(1-(normal.x*normal.x+normal.y*normal.y)));
}
vec4 transition(vec4 v1,vec4 v2,float val){
return v1 * val + v2 * (1-val);
}
void main(){
vec3 lightDir = normalize(LightDirection);
WorldPos = vec4(WldPos.xyz,1);
vec3 CamDir = normalize(CameraPosition - WldPos.xyz);
vec4 textel = texture(TextureSampler,TexCoord);
//vec4 textel = texture(NormalMapSampler,TexCoord);
vec3 normal = vec3(texture(NormalMapSampler, TexCoord));
normal = normal * 2.0f - 1.0f;
normal.y = -normal.y;
normal.z = calcMissing(normal);
normal = normalize(TBN * normal);
vec3 ReflectionVec = normalize(reflect(-lightDir, normal));
float spec = 0.0f;
if(textel.a < 0.5f) discard;
//textel.a = (CamPos.z / CamPos.w) / 100.0f;
float reflect = dot(normal,lightDir);
float lightness = pow(clamp((reflect),0.0f,1.0f),1.25f);
float darkness = clamp(reflect,-1.0f,0.0f) * 0.1f;
float shadowness = 0.0f;
if(length(shadowSpacePos.xy) < 1){
vec3 ShadowScreenSpace = shadowSpacePos * 0.5f + 0.5f;
float shadowDiv = 0.0f;
vec2 ShadowHardCenter = vec2(float(int(ShadowScreenSpace.x / pixel)*pixel), float(int(ShadowScreenSpace.y / pixel)*pixel));
for(int i = 0; i < 81; i++){
vec2 SamplePos = ShadowHardCenter.xy + ShadowOffsets[i].xy;
float ShadowDepth = texture(ShadowMapSampler, SamplePos).r;
float gaussDiff = gaussian(distance(SamplePos,ShadowScreenSpace.xy) / pixel);
shadowDiv += gaussDiff;
shadowness += gaussDiff * clamp(((ShadowScreenSpace.z - ShadowDepth) * 750.0f),0.0f,1.0f);
}
//shadowness = clamp(shadowness,0.0f,1.0f);
shadowness /= shadowDiv;
shadowness = clamp(((shadowness - 0.5f) * shadowHardness) + 0.5f, 0.0f, 1.0f);
lightness *= (1.0f - shadowness);
if(shadowness >= 0.9f)
lightness = clamp((reflect),0.0f,1.0f) * 0.1f;
}
if(lightness != 0.0f){
spec = pow(clamp(dot(CamDir,ReflectionVec),0.0f,1.0f), specPow);
spec *= texture(SpecularMapSampler,TexCoord).a;
}
spec *= lightness;
//lightness += darkness;
lightness += 0.35f;
vec4 emmission = texture(EmmissionMapSampler, TexCoord);
vec4 diffuseColor = vec4((textel.rgb * ObjColor.rgb * lightness),textel.a * ObjColor.a);
FragColor = vec4((diffuseColor.rgb * (1.0f - emmission.a * emmission.r)) + (textel.rgb * emmission.a * emmission.r) * 1.5f,textel.a);
FragColor *= transition(TeamIdColors[TeamId],vec4(1.0f,1.0f,1.0f,1.0f), texture(TeamcolorMap, TexCoord).a);
FragColor += vec4(spec,spec,spec,0.0f);
ObjId = ObjIdIn;
//FragColor = vec4(normal,1.0f);
//FragColor = vec4(vec3(1.0f, 1.0f, 1.0f) * shadowness,1.0f);
//FragColor = vec4(spec,spec,spec,1.0f);
};