33#pragma include "../utils/ p3d_light_sources.glsl"
44#pragma include "../utils/ p3d_material.glsl"
55
6+ uniform sampler2D p3d_Texture1;
7+
68// Input from vertex shader
79in vec3 viewspacePos;
810in vec4 fragPos;
@@ -16,6 +18,8 @@ in float specStrength;
1618in vec3 cam_pos;
1719in vec3 cam_dir;
1820
21+ in vec4 [4 ] shadow_uv;
22+
1923out vec4 outputColor;
2024
2125vec3 remap(vec3 iMin, vec3 iMax, vec3 oMin, vec3 oMax, vec3 v) {
@@ -28,6 +32,47 @@ float remap(float iMin, float iMax, float oMin, float oMax, float v) {
2832 return mix (oMin, oMax, t);
2933}
3034
35+
36+ float textureProjSoft(sampler2DShadow tex, vec4 uv, float bias, float blur) {
37+ float result = textureProj(tex, uv, bias);
38+ result += textureProj(tex, vec4 (uv.xy + vec2 ( - 0.326212 , - 0.405805 )* blur, uv.z- bias, uv.w));
39+ result += textureProj(tex, vec4 (uv.xy + vec2 (- 0.840144 , - 0.073580 )* blur, uv.z- bias, uv.w));
40+ result += textureProj(tex, vec4 (uv.xy + vec2 (- 0.695914 , 0.457137 )* blur, uv.z- bias, uv.w));
41+ result += textureProj(tex, vec4 (uv.xy + vec2 (- 0.203345 , 0.620716 )* blur, uv.z- bias, uv.w));
42+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.962340 , - 0.194983 )* blur, uv.z- bias, uv.w));
43+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.473434 , - 0.480026 )* blur, uv.z- bias, uv.w));
44+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.519456 , 0.767022 )* blur, uv.z- bias, uv.w));
45+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.185461 , - 0.893124 )* blur, uv.z- bias, uv.w));
46+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.507431 , 0.064425 )* blur, uv.z- bias, uv.w));
47+ result += textureProj(tex, vec4 (uv.xy + vec2 (0.896420 , 0.412458 )* blur, uv.z- bias, uv.w));
48+ result += textureProj(tex, vec4 (uv.xy + vec2 (- 0.321940 , - 0.932615 )* blur, uv.z- bias, uv.w));
49+ result += textureProj(tex, vec4 (uv.xy + vec2 (- 0.791559 , - 0.597705 )* blur, uv.z- bias, uv.w));
50+ return result/ 13.0 ;
51+ }
52+
53+ // float shadowCoef(vec4 vPos, sampler2DShadow shadowMap, mat4 shadowViewMatrix) {
54+ // int index = 3;
55+ // // find the appropriate depth map to look up in
56+ // // based on the depth of this fragment
57+ // if(gl_FragCoord.z < far_d.x) index = 0;
58+ // else if(gl_FragCoord.z < far_d.y) index = 1;
59+ // else if(gl_FragCoord.z < far_d.z) index = 2;
60+ //
61+ // // transform this fragment's position from view space to
62+ // // scaled light clip space such that the xy coordinates
63+ // // lie in [0;1]. Note that there is no need to divide by w
64+ // // for othogonal light sources
65+ // vec4 shadow_coord = shadowViewMatrix*vPos;
66+ // // set the current depth to compare with
67+ // shadow_coord.w = shadow_coord.z;
68+ //
69+ // // tell glsl in which layer to do the look up
70+ // shadow_coord.z = float(index);
71+ //
72+ // // let the hardware do the comparison for us
73+ // return shadow2DArray(shadowMap, shadow_coord).x;
74+ // }
75+
3176void main() {
3277 vec3 illumLightSum = vec3 (0 );
3378 vec3 normal = normalize (primNormal);
@@ -41,9 +86,11 @@ void main() {
4186
4287 float distance = length (lightDir);
4388 distance = distance * distance ;
44- lightDir = normalize (lightDir);
89+ lightDir = normalize (- lightDir);
4590
4691 float lambertian = max (dot (lightDir, normal), 0.0 );
92+ lambertian = texture(p3d_Texture1, vec2 (lambertian, 0.5 )).x;
93+
4794 float specular = 0.0 ;
4895
4996 if (lambertian > 0.0 ) {
@@ -53,14 +100,17 @@ void main() {
53100 vec3 halfDir = normalize (lightDir + viewDir);
54101 float specAngle = max (dot (halfDir, normal), 0.0 );
55102 specular = pow (specAngle, p3d_Material.shininess);
103+ specular = texture(p3d_Texture1, vec2 (specular, 0.5 )).x;
56104 }
57- vec3 illumDiffuse = diffuseColor * lambertian * p3d_LightSource[i].color.xyz * 1 / distance ;
58- vec3 illumSpecular = diffuseColor * specStrength * specular * p3d_LightSource[i].color.xyz * 1 / distance ;
59-
60- illumLightSum += illumDiffuse+ illumSpecular;
105+ vec3 illumDiffuse = diffuseColor * lambertian * p3d_LightSource[i].color.xyz * 1 / distance ;
106+ vec3 illumSpecular = diffuseColor * specStrength * specular * p3d_LightSource[i].color.xyz * 1 / distance ;
107+ // illumLightSum += (illumDiffuse+illumSpecular)*textureProjSoft(p3d_LightSource[i].shadowMap, shadow_uv[i], 0.0001, 0.2);
108+ illumLightSum += ( illumDiffuse+ illumSpecular) * textureProj(p3d_LightSource[i].shadowMap, shadow_uv[i]) ;
61109 }
62110
63111 vec3 colorGammaCorrected = pow (p3d_LightModel.ambient.xyz* diffuseColor+ illumLightSum, vec3 (0.49504950495 ));
112+ colorGammaCorrected = texture(p3d_Texture1, vec2 (length (colorGammaCorrected.xyz), 0 ), 0 ).x* colorGammaCorrected;
113+
64114 // use the gamma corrected color in the fragment0
65- outputColor = vec4 (colorGammaCorrected, 1.0 );
115+ outputColor = vec4 (colorGammaCorrected, 1 );
66116}
0 commit comments