Skip to content

Commit

Permalink
correct lighting, pointColor is giving issue in sampleLight (uniform …
Browse files Browse the repository at this point in the history
…not included ?)
  • Loading branch information
e2002e committed Feb 17, 2023
1 parent 8b63840 commit a5e34eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
28 changes: 14 additions & 14 deletions Shaders/deferred_light/deferred_light.frag.glsl
Expand Up @@ -290,6 +290,9 @@ void main() {

envl.rgb *= envmapStrength * occspec.x;

vec3 diffuse = vec3(0.0);
vec3 reflection = vec3(0.0);

#ifdef _VoxelAOvar
#ifdef _VoxelGICam
vec3 voxpos = (p - eyeSnap) / voxelgiHalfExtents;
Expand All @@ -299,16 +302,13 @@ void main() {

#ifndef _VoxelAONoTrace
#ifdef _VoxelGITemporal
envl *= 1.0 - (traceAO(voxpos, n, voxels) * voxelBlend +
diffuse += 1.0 - (traceAO(voxpos, n, voxels) * voxelBlend +
traceAO(voxpos, n, voxelsLast) * (1.0 - voxelBlend));
#else
envl *= 1.0 - traceAO(voxpos, n, voxels);
diffuse += 1.0 - traceAO(voxpos, n, voxels);
#endif
#endif
#endif

vec3 diffuse = vec3(0.0);
vec3 reflection = vec3(0.0);

#ifdef _VoxelGI
#ifdef _VoxelGICam
Expand All @@ -335,7 +335,7 @@ reflection = traceReflection(voxels, voxpos, n, -v, roughness).rgb * voxelgiRefl
fragColor.rgb = (diffuse + reflection) + envl * voxelgiEnv;
#else
#ifdef _VoxelAOvar
fragColor.rgb = envl * voxelgiEnv;
fragColor.rgb = diffuse + envl * voxelgiEnv;
#else
fragColor.rgb = envl;
#endif
Expand Down Expand Up @@ -379,6 +379,7 @@ fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r;
float sdotNL = max(0.0, dot(n, sunDir));
float svisibility = 1.0;
vec3 sdirect = lambertDiffuseBRDF(albedo, sdotNL) + diffuse + (specularBRDF(f0, roughness, sdotNL, sdotNH, dotNV, sdotVH) + reflection) * occspec.y;

#ifdef _ShadowMap
#ifdef _CSM
svisibility = shadowTestCascade(
Expand Down Expand Up @@ -425,7 +426,7 @@ fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r;
// See https://advances.realtimerendering.com/other/2016/naughty_dog/NaughtyDog_TechArt_Final.pdf
svisibility *= clamp(sdotNL + 2.0 * occspec.x * occspec.x - 1.0, 0.0, 1.0);
#endif

fragColor.rgb *= svisibility;
fragColor.rgb += sdirect * sunCol;

Expand Down Expand Up @@ -463,8 +464,7 @@ fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r;
#endif // _Sun

#ifdef _SinglePoint
vec4 lightData;
lightData = sampleLight(
vec4 lightData = sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, roughness, occspec.y, f0, false, diffuse, reflection
#ifdef _ShadowMap
, 0, pointBias, true
Expand All @@ -489,15 +489,15 @@ fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r;
, gbufferD, invVP, eye
#endif
);
fragColor.rgb *= lightData.a;
fragColor.rgb += lightData.rgb;


#ifdef _Spot
#ifdef _SSS
if (matid == 2) fragColor.rgb += fragColor.rgb * SSSSTransmittance(LWVPSpot0, p, n, normalize(pointPos - p), lightPlane.y, shadowMapSpot[0]);
if (matid == 2) fragColor.rgb *= fragColor.rgb * SSSSTransmittance(LWVPSpot0, p, n, normalize(pointPos - p), lightPlane.y, shadowMapSpot[0]);
#endif
#endif

fragColor.rgb *= lightData.a;
fragColor.rgb += lightData.rgb;

#endif

Expand Down Expand Up @@ -529,7 +529,7 @@ fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r;
roughness,
occspec.y,
f0,
false,
true,
diffuse,
reflection
#ifdef _ShadowMap
Expand Down
36 changes: 14 additions & 22 deletions Shaders/std/light.glsl
Expand Up @@ -125,48 +125,40 @@ vec4 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
vec3(1.0, 0.0, t.y),
vec3(0.0, t.z, 0.0),
vec3(t.w, 0.0, t.x));
float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3);
float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3) + reflection;
ltcspec *= textureLod(sltcMag, tuv, 0.0).a;
float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3);
direct = albedo * ltcdiff + (ltcspec + reflection) * spec * 0.05 + diffuse;
float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3) + diffuse;
direct = albedo * ltcdiff + (ltcspec) * spec * 0.05;
#else
direct = lambertDiffuseBRDF(albedo, dotNL) + diffuse +
(specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) + reflection) * spec;
direct = lambertDiffuseBRDF(albedo, dotNL) + diffuse + (specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) + reflection) * spec;
#endif

direct = albedo;
direct *= lightCol;
direct *= attenuate(distance(p, lp));

float svisibility = 1.0;

#ifdef _MicroShadowing
direct *= clamp(dotNL + 2.0 * occ * occ - 1.0, 0.0, 1.0);
svisibility *= clamp(dotNL + 2.0 * occ * occ - 1.0, 0.0, 1.0);
#endif

#ifdef _SSRS
direct *= traceShadowSS(l, p, gbufferD, invVP, eye);
svisibility *= traceShadowSS(l, p, gbufferD, invVP, eye);
#endif

if(!vox) {
#ifdef _VoxelAOvar
#ifdef _VoxelShadow
direct *= 1.0 - traceShadow(voxels, voxpos, l);
svisibility *= 1.0 - traceShadow(voxels, voxpos, l);
#endif
#endif

#ifdef _VoxelAOvar
#ifdef _VoxelShadow
direct *= 1.0 - traceShadow(voxels, voxpos, l);
#endif
#endif

#ifdef _VoxelGI
#ifdef _VoxelShadow
direct *= 1.0 - traceShadow(voxels, voxpos, l);
svisibility *= 1.0 - traceShadow(voxels, voxpos, l);
#endif
#endif
}

vec3 svisibility = vec3(1.0, 1.0, 1.0);

#ifdef _LTC
#ifdef _ShadowMap
Expand Down Expand Up @@ -200,7 +192,7 @@ vec4 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co

#ifdef _Spot
if (isSpot) {
direct *= spotlightMask(l, spotDir, right, scale, spotSize, spotBlend);
svisibility *= spotlightMask(l, spotDir, right, scale, spotSize, spotBlend);

#ifdef _ShadowMap
if (receiveShadow) {
Expand All @@ -211,7 +203,7 @@ vec4 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
#ifdef _Clusters
vec4 lPos = LWVPSpotArray[index] * vec4(p + n * bias * 10, 1.0);
#ifdef _ShadowMapAtlas
direct *= shadowTest(
svisibility *= shadowTest(
#ifndef _SingleAtlas
shadowMapAtlasSpot
#else
Expand All @@ -233,7 +225,7 @@ vec4 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
#endif

#ifdef _LightIES
direct *= iesAttenuation(-l);
svisibility *= iesAttenuation(-l);
#endif

#ifdef _ShadowMap
Expand All @@ -245,7 +237,7 @@ vec4 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
#endif
#ifdef _Clusters
#ifdef _ShadowMapAtlas
direct *= PCFFakeCube(
svisibility *= PCFFakeCube(
#ifndef _SingleAtlas
shadowMapAtlasPoint
#else
Expand Down
8 changes: 3 additions & 5 deletions blender/arm/material/make_mesh.py
Expand Up @@ -662,9 +662,9 @@ def make_forward_base(con_mesh, parse_opacity=False, transluc_pass=False):
if '_VoxelGITemporal' in wrd.world_defs:
frag.add_uniform('float voxelBlend', link='_voxelBlend')
frag.add_uniform('sampler3D voxelsLast')
frag.write('indirect *= vec3(1.0 - (traceAO(voxpos, n, voxels) * voxelBlend + traceAO(voxpos, n, voxelsLast) * (1.0 - voxelBlend)));')
frag.write('diffuse += vec3(1.0 - (traceAO(voxpos, n, voxels) * voxelBlend + traceAO(voxpos, n, voxelsLast) * (1.0 - voxelBlend)));')
else:
frag.write('indirect *= vec3(1.0 - traceAO(voxpos, n, voxels));')
frag.write('diffuse += vec3(1.0 - traceAO(voxpos, n, voxels));')

elif '_VoxelGI' in wrd.world_defs:
frag.add_include('std/conetrace.glsl')
Expand All @@ -688,10 +688,8 @@ def make_forward_base(con_mesh, parse_opacity=False, transluc_pass=False):
else:
frag.write(' reflection = traceReflection(voxels, voxpos, n, -vVec, roughness).rgb * voxelgiRefl;')

if '_VoxelGI' in wrd.world_defs:
if '_VoxelGI' in wrd.world_defs or 'VoxelAOvar' in wrd.world_defs:
frag.write('vec3 final = (diffuse + reflection) + indirect * voxelgiEnv;')
elif 'VoxelAOvar' in wrd.world_defs:
frag.write('vec3 final = indirect * voxelgiEnv;')
else:
frag.write('vec3 final = indirect;')

Expand Down
5 changes: 2 additions & 3 deletions blender/arm/material/make_voxel.py
Expand Up @@ -322,7 +322,6 @@ def make_gi(context_id):
frag.write(', d, m, e')
frag.write(');')
frag.write('basecol *= lightData.a;')
frag.write('basecol += lightData.rgb;')

if '_Clusters' in wrd.world_defs:
frag.add_uniform('vec4 lightsArray[maxLights * 3]', link='_lightsArray')
Expand Down Expand Up @@ -372,7 +371,7 @@ def make_gi(context_id):
frag.write('vec4 lightData = vec4(0.0);')
frag.write('for (int i = 0; i < min(numLights, maxLightsCluster); i++) {')
frag.write(' int li = int(texelFetch(clustersData, ivec2(clusterI, i + 1), 0).r * 255);')
frag.write(' lightData = sampleLight(')
frag.write(' lightData += sampleLight(')
frag.write(' wposition,')
frag.write(' n,')
frag.write(' vVec,')
Expand Down Expand Up @@ -412,11 +411,11 @@ def make_gi(context_id):
frag.write(' );')
frag.write('};')
frag.write('basecol *= lightData.a;')
frag.write('basecol += lightData.rgb;')

frag.write('basecol += emissionCol;')
frag.write('vec3 voxel = voxposition * 0.5 + 0.5;')
frag.write('imageStore(voxels, ivec3(voxelgiResolution * voxel), vec4(basecol, 1.0));')

frag.write('#ifdef _VoxelsBounce')
frag.write('imageStore(voxelsNor, ivec3(voxelgiResolution * voxel), vec4(wnormal, 1.0));')
frag.write('imageStore(voxelsVr, ivec3(voxelgiResolution * voxel), vec4(vVec, roughness));')
Expand Down

0 comments on commit a5e34eb

Please sign in to comment.