Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent shadows #3078

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion Kha
Submodule Kha updated from 47ef84 to 133a36
2 changes: 1 addition & 1 deletion Krom
Submodule Krom updated 2 files
+ Krom
+ Krom.exe
50 changes: 32 additions & 18 deletions armory/Shaders/deferred_light/deferred_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ uniform sampler2D voxels_specular;
uniform sampler2D voxels_ao;
#endif
#ifdef _VoxelShadow
uniform sampler2D voxels_shadows;
uniform sampler3D voxels;
uniform sampler3D voxelsSDF;
uniform float clipmaps[10 * voxelgiClipmapCount];
#endif

uniform float envmapStrength;
Expand Down Expand Up @@ -83,10 +85,12 @@ uniform mat4 invVP;
#ifdef _ShadowMap
#ifdef _SinglePoint
//!uniform sampler2DShadow shadowMapSpot[1];
//!uniform sampler2D shadowMapSpotTransparent[1];
//!uniform mat4 LWVPSpot[1];
#endif
#ifdef _Clusters
//!uniform sampler2DShadow shadowMapSpot[4];
//!uniform sampler2D shadowMapSpotTransparent[4];
//!uniform mat4 LWVPSpotArray[4];
#endif
#endif
Expand All @@ -109,34 +113,41 @@ uniform vec2 cameraPlane;
#ifdef _SinglePoint
#ifdef _Spot
//!uniform sampler2DShadow shadowMapSpot[1];
//!uniform sampler2D shadowMapSpotTransparent[1];
//!uniform mat4 LWVPSpot[1];
#else
//!uniform samplerCubeShadow shadowMapPoint[1];
//!uniform samplerCube shadowMapPointTransparent[1];
//!uniform vec2 lightProj;
#endif
#endif
#ifdef _Clusters
#ifdef _ShadowMapAtlas
#ifdef _SingleAtlas
uniform sampler2DShadow shadowMapAtlas;
uniform sampler2D shadowMapAtlasTransparent;
#endif
#endif
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
//!uniform sampler2DShadow shadowMapAtlasPoint;
//!uniform sampler2D shadowMapAtlasPointTransparent;
#endif
//!uniform vec4 pointLightDataArray[maxLightsCluster * 6];
//!uniform vec4 pointLightDataArray[4];
#else
//!uniform samplerCubeShadow shadowMapPoint[4];
//!uniform samplerCube shadowMapPointTransparent[4];
#endif
//!uniform vec2 lightProj;
#ifdef _Spot
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
//!uniform sampler2DShadow shadowMapAtlasSpot;
//!uniform sampler2D shadowMapAtlasSpotTransparent;
#endif
#else
//!uniform sampler2DShadow shadowMapSpot[4];
//!uniform sampler2D shadowMapSpotTransparent[4];
#endif
//!uniform mat4 LWVPSpotArray[maxLightsCluster];
#endif
Expand All @@ -150,9 +161,11 @@ uniform vec3 sunCol;
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
uniform sampler2DShadow shadowMapAtlasSun;
uniform sampler2D shadowMapAtlasSunTransparent;
#endif
#else
uniform sampler2DShadow shadowMap;
uniform sampler2D shadowMapTransparent;
#endif
uniform float shadowsBias;
#ifdef _CSM
Expand Down Expand Up @@ -272,7 +285,8 @@ void main() {
envl.rgb *= envmapStrength * occspec.x;

#ifdef _VoxelGI
fragColor.rgb = textureLod(voxels_diffuse, texCoord, 0.0).rgb * albedo * voxelgiDiff;
vec4 indirect_diffuse = textureLod(voxels_diffuse, texCoord, 0.0);
fragColor.rgb = (indirect_diffuse.rgb * albedo + envl.rgb * (1.0 - indirect_diffuse.a)) * voxelgiDiff;
if(roughness < 1.0 && occspec.y > 0.0)
fragColor.rgb += textureLod(voxels_specular, texCoord, 0.0).rgb * occspec.y * voxelgiRefl;
#endif
Expand Down Expand Up @@ -328,45 +342,45 @@ void main() {
float sdotNH = max(0.0, dot(n, sh));
float sdotVH = max(0.0, dot(v, sh));
float sdotNL = max(0.0, dot(n, sunDir));
float svisibility = 1.0;
vec3 sdirect = lambertDiffuseBRDF(albedo, sdotNL) +
vec3 svisibility = vec3(1.0);
vec3 sdirect = lambertDiffuseBRDF(albedo, sdotNL) +
specularBRDF(f0, roughness, sdotNL, sdotNH, dotNV, sdotVH) * occspec.y;

#ifdef _ShadowMap
#ifdef _CSM
svisibility = shadowTestCascade(
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
shadowMapAtlasSun
shadowMapAtlasSun, shadowMapAtlasSunTransparent
#else
shadowMapAtlas
shadowMapAtlas, shadowMapAtlasTransparent
#endif
#else
shadowMap
shadowMap, shadowMapTransparent
#endif
, eye, p + n * shadowsBias * 10, shadowsBias
, eye, p + n * shadowsBias * 10, shadowsBias, false
);
#else
vec4 lPos = LWVP * vec4(p + n * shadowsBias * 100, 1.0);
if (lPos.w > 0.0) {
svisibility = shadowTest(
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
shadowMapAtlasSun
shadowMapAtlasSun, shadowMapAtlasSunTransparent
#else
shadowMapAtlas
shadowMapAtlas, shadowMapAtlasTransparent
#endif
#else
shadowMap
shadowMap, shadowMapTransparent
#endif
, lPos.xyz / lPos.w, shadowsBias
, lPos.xyz / lPos.w, shadowsBias, false
);
}
#endif
#endif

#ifdef _VoxelShadow
svisibility *= textureLod(voxels_shadows, texCoord, 0.0).r * voxelgiShad;
svisibility *= (1.0 - traceShadow(p, n, voxels, voxelsSDF, sunDir, clipmaps, gl_FragCoord.xy).r) * voxelgiShad;
#endif

#ifdef _SSRS
Expand Down Expand Up @@ -425,13 +439,13 @@ void main() {
fragColor.rgb += sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, roughness, occspec.y, f0
#ifdef _ShadowMap
, 0, pointBias, true
, 0, pointBias, true, false
#endif
#ifdef _Spot
, true, spotData.x, spotData.y, spotDir, spotData.zw, spotRight
#endif
#ifdef _VoxelShadow
, texCoord
, voxels, voxelsSDF, clipmaps
#endif
#ifdef _MicroShadowing
, occspec.x
Expand Down Expand Up @@ -478,7 +492,7 @@ void main() {
f0
#ifdef _ShadowMap
// light index, shadow bias, cast_shadows
, li, lightsArray[li * 3 + 2].x, lightsArray[li * 3 + 2].z != 0.0
, li, lightsArray[li * 3 + 2].x, lightsArray[li * 3 + 2].z != 0.0, false
#endif
#ifdef _Spot
, lightsArray[li * 3 + 2].y != 0.0
Expand All @@ -489,7 +503,7 @@ void main() {
, lightsArraySpot[li * 2 + 1].xyz // right
#endif
#ifdef _VoxelShadow
, texCoord
, voxels, voxelsSDF, clipmaps
#endif
#ifdef _MicroShadowing
, occspec.x
Expand Down
2 changes: 1 addition & 1 deletion armory/Shaders/deferred_light/deferred_light.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{
"name": "clipmaps",
"link": "_clipmaps",
"ifdef": ["VoxelShadow"]
"ifdef": ["_VoxelShadow"]
},
{
"name": "invVP",
Expand Down
28 changes: 11 additions & 17 deletions armory/Shaders/ssrefr_pass/ssrefr_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uniform sampler2D tex1;
uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform sampler2D gbufferD1;

uniform sampler2D gbuffer_refraction; // ior\opacity
uniform mat4 P;
uniform mat3 V3;
Expand All @@ -38,7 +39,7 @@ vec2 getProjectedCoord(const vec3 hit) {
}

float getDeltaDepth(const vec3 hit) {
float depth = textureLod(gbufferD1, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
depth = textureLod(gbufferD1, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
vec3 viewPos = getPosView(viewRay, depth, cameraProj);
return viewPos.z - hit.z;
}
Expand All @@ -63,7 +64,7 @@ vec4 rayCast(vec3 dir) {
ddepth = getDeltaDepth(hitCoord);
if (ddepth > 0.0) return binarySearch(dir);
}
return vec4(0.0);
return vec4(texCoord, 0.0, 1.0);
}

void main() {
Expand All @@ -72,19 +73,12 @@ void main() {
vec4 gr = textureLod(gbuffer_refraction, texCoord, 0.0);
float ior = gr.x;
float opac = gr.y;

float d = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;

if (opac == 1.0) {
fragColor.rgb = textureLod(tex, texCoord, 0.0).rgb;
return;
}
if (d == 0.0) {
if (d == 0.0 || d == 1.0 || opac == 1.0 || ior == 1.0) {
fragColor.rgb = textureLod(tex1, texCoord, 0.0).rgb;
return;
}

vec2 enc = g0.rg;
vec2 enc = g0.rg;
vec3 n;
n.z = 1.0 - abs(enc.x) - abs(enc.y);
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
Expand All @@ -97,16 +91,16 @@ void main() {

vec3 dir = refracted * (1.0 - rand(texCoord) * ss_refractionJitter * roughness) * 2.0;
vec4 coords = rayCast(dir);

vec2 deltaCoords = abs(vec2(0.5, 0.5) - coords.xy);
float screenEdgeFactor = clamp(1.0 - (deltaCoords.x + deltaCoords.y), 0.0, 1.0);

float refractivity = 1.0 - roughness;
float intensity = pow(refractivity, ss_refractionFalloffExp) * screenEdgeFactor * clamp(-refracted.z, 0.0, 1.0) * clamp((length(viewPos - hitCoord)), 0.0, 1.0) * coords.w;

float intensity = pow(refractivity, ss_refractionFalloffExp) * screenEdgeFactor * \
clamp(-refracted.z, 0.0, 1.0) * clamp((length(viewPos - hitCoord)), 0.0, 1.0) * coords.w;
intensity = clamp(intensity, 0.0, 1.0);

vec3 refractionCol = textureLod(tex1, coords.xy, 0.0).rgb;
vec3 refractionCol = textureLod(tex1, coords.xy, 0.0).rgb;
refractionCol *= intensity;
vec3 color = textureLod(tex, texCoord.xy, 0.0).rgb;
fragColor.rgb = mix(refractionCol * intensity, color, opac);

fragColor.rgb = mix(refractionCol, color, opac);
}
31 changes: 14 additions & 17 deletions armory/Shaders/std/conetrace.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ float sampleVoxel(sampler3D voxels, vec3 P, const float clipmaps[voxelgiClipmapC

#ifdef _VoxelGI
vec4 traceCone(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 origin, const vec3 n, const vec3 dir, const int precomputed_direction, const bool use_sdf, const float aperture, const float step_size, const float clipmaps[voxelgiClipmapCount * 10]) {
vec3 color = vec3(0.0);
float alpha = 0.0;
vec4 sampleCol = vec4(0.0);
float voxelSize0 = float(clipmaps[0]) * 2.0;
float dist = voxelSize0;
float step_dist = dist;
Expand All @@ -98,15 +97,15 @@ vec4 traceCone(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 ori

vec3 aniso_direction = -dir;
vec3 face_offset = vec3(
aniso_direction.x > 0 ? 0 : 1,
aniso_direction.y > 0 ? 2 : 3,
aniso_direction.z > 0 ? 4 : 5
aniso_direction.x > 0.0 ? 0 : 1,
aniso_direction.y > 0.0 ? 2 : 3,
aniso_direction.z > 0.0 ? 4 : 5
) / (6 + DIFFUSE_CONE_COUNT);
vec3 direction_weight = abs(dir);

float coneCoefficient = 2.0 * tan(aperture * 0.5);

while (alpha < 1.0 && dist < MAX_DISTANCE && clipmap_index0 < voxelgiClipmapCount) {
while (sampleCol.a < 1.0 && dist < MAX_DISTANCE && clipmap_index0 < voxelgiClipmapCount) {
vec4 mipSample = vec4(0.0);
float diam = max(voxelSize0, dist * coneCoefficient);
float lod = clamp(log2(diam / voxelSize0), clipmap_index0, voxelgiClipmapCount - 1);
Expand All @@ -126,12 +125,10 @@ vec4 traceCone(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 ori

if(clipmap_blend > 0.0 && clipmap_index < voxelgiClipmapCount - 1) {
vec4 mipSampleNext = sampleVoxel(voxels, p0, clipmaps, clipmap_index + 1.0, step_dist, precomputed_direction, face_offset, direction_weight);
mipSample = mix(mipSample, mipSampleNext, clipmap_blend);
mipSample = mix(mipSample, mipSampleNext, smoothstep(0.0, 1.0, clipmap_blend));
}

float a = 1.0 - alpha;
color += a * mipSample.rgb;
alpha += a * mipSample.a;
sampleCol += (1.0 - sampleCol.a) * mipSample;

float stepSizeCurrent = step_size;
if (use_sdf) {
Expand All @@ -145,7 +142,7 @@ vec4 traceCone(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 ori
step_dist = diam * stepSizeCurrent;
dist += step_dist;
}
return vec4(color, alpha);
return sampleCol;
}

vec4 traceDiffuse(const vec3 origin, const vec3 normal, const sampler3D voxels, const float clipmaps[voxelgiClipmapCount * 10]) {
Expand All @@ -162,15 +159,15 @@ vec4 traceDiffuse(const vec3 origin, const vec3 normal, const sampler3D voxels,
}

amount /= sum;
amount.rgb = max(vec3(0.0), amount.rgb);
amount.rgb = max(amount.rgb, vec3(0.0));
amount.a = clamp(amount.a, 0.0, 1.0);

return amount * voxelgiOcc;
}

vec4 traceSpecular(const vec3 origin, const vec3 normal, const sampler3D voxels, const sampler3D voxelsSDF, const vec3 viewDir, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel) {
vec4 traceSpecular(const vec3 origin, const vec3 normal, const sampler3D voxels, const sampler3D voxelsSDF, const vec3 viewDir, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel, const vec2 velocity) {
vec3 specularDir = reflect(-viewDir, normal);
vec3 P = origin + specularDir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
vec3 P = origin + specularDir * ((BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) + sqrt(5.0 * length(velocity))) * voxelgiStep;
vec4 amount = traceCone(voxels, voxelsSDF, P, normal, specularDir, 0, true, roughness, voxelgiStep, clipmaps);

amount.rgb = max(vec3(0.0), amount.rgb);
Expand All @@ -179,10 +176,10 @@ vec4 traceSpecular(const vec3 origin, const vec3 normal, const sampler3D voxels,
return amount * voxelgiOcc;
}

vec4 traceRefraction(const vec3 origin, const vec3 normal, sampler3D voxels, sampler3D voxelsSDF, const vec3 viewDir, const float ior, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel) {
vec4 traceRefraction(const vec3 origin, const vec3 normal, sampler3D voxels, sampler3D voxelsSDF, const vec3 viewDir, const float ior, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel, const vec2 velocity) {
const float transmittance = 1.0;
vec3 refractionDir = refract(-viewDir, normal, 1.0 / ior);
vec3 P = origin + refractionDir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
vec3 P = origin + refractionDir * ((BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) + sqrt(5.0 * length(velocity))) * voxelgiStep;
vec4 amount = transmittance * traceCone(voxels, voxelsSDF, P, normal, refractionDir, 0, true, roughness, voxelgiStep, clipmaps);

amount.rgb = max(vec3(0.0), amount.rgb);
Expand Down Expand Up @@ -253,7 +250,7 @@ float traceAO(const vec3 origin, const vec3 normal, const sampler3D voxels, cons
const float cosTheta = dot(normal, coneDir);
if (cosTheta <= 0)
continue;
amount += traceConeAO(voxels, origin, normal, coneDir, precomputed_direction, DIFFUSE_CONE_APERTURE, voxelgiStep, clipmaps) * cosTheta;
amount += traceConeAO(voxels, origin, normal, coneDir, precomputed_direction, DIFFUSE_CONE_APERTURE, 1.0, clipmaps) * cosTheta;
sum += cosTheta;
}
amount /= sum;
Expand Down
Loading