Implement shadow offsets for the new SM distortion function (#12191)
* Move shadow position calculation to vertex shaders * Animate entire scene before rendering shadows to prevent lagging of shadows * Remove unnecessary use of PolygonOffsetFactor * Apply normal offset to both nodes and objects * Rename getPerspectiveFactor -> applyPerspectiveDistortion * Remove perspective distortion from fragment shaders
This commit is contained in:
		
							parent
							
								
									9aabd911eb
								
							
						
					
					
						commit
						a5d29fa1d4
					
				@ -18,10 +18,13 @@ uniform float animationTimer;
 | 
			
		||||
	uniform float f_shadowfar;
 | 
			
		||||
	uniform float f_shadow_strength;
 | 
			
		||||
	uniform vec4 CameraPos;
 | 
			
		||||
	varying float normalOffsetScale;
 | 
			
		||||
	uniform float xyPerspectiveBias0;
 | 
			
		||||
	uniform float xyPerspectiveBias1;
 | 
			
		||||
	
 | 
			
		||||
	varying float adj_shadow_strength;
 | 
			
		||||
	varying float cosLight;
 | 
			
		||||
	varying float f_normal_length;
 | 
			
		||||
	varying vec3 shadow_position;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -45,24 +48,7 @@ varying float nightRatio;
 | 
			
		||||
const float fogStart = FOG_START;
 | 
			
		||||
const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
vec4 getPerspectiveFactor(in vec4 shadowPosition)
 | 
			
		||||
{
 | 
			
		||||
	vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
 | 
			
		||||
	vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
 | 
			
		||||
	float pDistance = length(l);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	l /= pFactor;
 | 
			
		||||
	shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
 | 
			
		||||
	shadowPosition.z *= zPerspectiveBias;
 | 
			
		||||
	return shadowPosition;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// assuming near is always 1.0
 | 
			
		||||
float getLinearDepth()
 | 
			
		||||
@ -72,15 +58,7 @@ float getLinearDepth()
 | 
			
		||||
 | 
			
		||||
vec3 getLightSpacePosition()
 | 
			
		||||
{
 | 
			
		||||
	vec4 pLightSpace;
 | 
			
		||||
	// some drawtypes have zero normals, so we need to handle it :(
 | 
			
		||||
	#if DRAW_TYPE == NDT_PLANTLIKE
 | 
			
		||||
	pLightSpace = m_ShadowViewProj * vec4(worldPosition, 1.0);
 | 
			
		||||
	#else
 | 
			
		||||
	pLightSpace = m_ShadowViewProj * vec4(worldPosition + normalOffsetScale * normalize(vNormal), 1.0);
 | 
			
		||||
	#endif
 | 
			
		||||
	pLightSpace = getPerspectiveFactor(pLightSpace);
 | 
			
		||||
	return pLightSpace.xyz * 0.5 + 0.5;
 | 
			
		||||
	return shadow_position * 0.5 + 0.5;
 | 
			
		||||
}
 | 
			
		||||
// custom smoothstep implementation because it's not defined in glsl1.2
 | 
			
		||||
// https://docs.gl/sl4/smoothstep
 | 
			
		||||
@ -499,14 +477,14 @@ void main(void)
 | 
			
		||||
 | 
			
		||||
#ifdef COLORED_SHADOWS
 | 
			
		||||
			vec4 visibility;
 | 
			
		||||
			if (cosLight > 0.0)
 | 
			
		||||
			if (cosLight > 0.0 || f_normal_length < 1e-3)
 | 
			
		||||
				visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
 | 
			
		||||
			else
 | 
			
		||||
				visibility = vec4(1.0, 0.0, 0.0, 0.0);
 | 
			
		||||
			shadow_int = visibility.r;
 | 
			
		||||
			shadow_color = visibility.gba;
 | 
			
		||||
#else
 | 
			
		||||
			if (cosLight > 0.0)
 | 
			
		||||
			if (cosLight > 0.0 || f_normal_length < 1e-3)
 | 
			
		||||
				shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
 | 
			
		||||
			else
 | 
			
		||||
				shadow_int = 1.0;
 | 
			
		||||
 | 
			
		||||
@ -32,10 +32,13 @@ centroid varying vec2 varTexCoord;
 | 
			
		||||
	uniform float f_shadowfar;
 | 
			
		||||
	uniform float f_shadow_strength;
 | 
			
		||||
	uniform float f_timeofday;
 | 
			
		||||
	uniform vec4 CameraPos;
 | 
			
		||||
 | 
			
		||||
	varying float cosLight;
 | 
			
		||||
	varying float normalOffsetScale;
 | 
			
		||||
	varying float adj_shadow_strength;
 | 
			
		||||
	varying float f_normal_length;
 | 
			
		||||
	varying vec3 shadow_position;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -47,8 +50,36 @@ const float e = 2.718281828459;
 | 
			
		||||
const float BS = 10.0;
 | 
			
		||||
uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
 | 
			
		||||
vec4 getRelativePosition(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec2 l = position.xy - CameraPos.xy;
 | 
			
		||||
	vec2 s = l / abs(l);
 | 
			
		||||
	s = (1.0 - s * CameraPos.xy);
 | 
			
		||||
	l /= s;
 | 
			
		||||
	return vec4(l, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float getPerspectiveFactor(in vec4 relativePosition)
 | 
			
		||||
{
 | 
			
		||||
	float pDistance = length(relativePosition.xy);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	return pFactor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vec4 applyPerspectiveDistortion(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec4 l = getRelativePosition(position);
 | 
			
		||||
	float pFactor = getPerspectiveFactor(l);
 | 
			
		||||
	l.xy /= pFactor;
 | 
			
		||||
	position.xy = l.xy * l.zw + CameraPos.xy;
 | 
			
		||||
	position.z *= zPerspectiveBias;
 | 
			
		||||
	return position;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// custom smoothstep implementation because it's not defined in glsl1.2
 | 
			
		||||
// https://docs.gl/sl4/smoothstep
 | 
			
		||||
float mtsmoothstep(in float edge0, in float edge1, in float x)
 | 
			
		||||
@ -196,21 +227,32 @@ void main(void)
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
	if (f_shadow_strength > 0.0) {
 | 
			
		||||
		vec3 nNormal = normalize(vNormal);
 | 
			
		||||
		cosLight = dot(nNormal, -v_LightDirection);
 | 
			
		||||
		
 | 
			
		||||
		// Calculate normal offset scale based on the texel size adjusted for
 | 
			
		||||
		// curvature of the SM texture. This code must be change together with
 | 
			
		||||
		// getPerspectiveFactor or any light-space transformation.
 | 
			
		||||
		vec3 eyeToVertex = worldPosition - eyePosition + cameraOffset;
 | 
			
		||||
		// Distance from the vertex to the player
 | 
			
		||||
		float distanceToPlayer = length(eyeToVertex - v_LightDirection * dot(eyeToVertex, v_LightDirection)) / f_shadowfar;
 | 
			
		||||
		// perspective factor estimation according to the
 | 
			
		||||
		float perspectiveFactor = distanceToPlayer * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
		float texelSize = f_shadowfar * perspectiveFactor * perspectiveFactor /
 | 
			
		||||
				(f_textureresolution * xyPerspectiveBias1  - perspectiveFactor * xyPerspectiveBias0);
 | 
			
		||||
		float slopeScale = clamp(pow(1.0 - cosLight*cosLight, 0.5), 0.0, 1.0);
 | 
			
		||||
		normalOffsetScale = texelSize * slopeScale;
 | 
			
		||||
		vec3 nNormal;
 | 
			
		||||
		f_normal_length = length(vNormal);
 | 
			
		||||
 | 
			
		||||
		/* normalOffsetScale is in world coordinates (1/10th of a meter)
 | 
			
		||||
		   z_bias is in light space coordinates */
 | 
			
		||||
		float normalOffsetScale, z_bias;
 | 
			
		||||
		float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
 | 
			
		||||
		if (f_normal_length > 0.0) {
 | 
			
		||||
			nNormal = normalize(vNormal);
 | 
			
		||||
			cosLight = dot(nNormal, -v_LightDirection);
 | 
			
		||||
			float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
 | 
			
		||||
			normalOffsetScale = 2.0 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) / 
 | 
			
		||||
					xyPerspectiveBias1 / f_textureresolution;
 | 
			
		||||
			z_bias = 1.0 * sinLight / cosLight;
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			nNormal = vec3(0.0);
 | 
			
		||||
			cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
 | 
			
		||||
			float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
 | 
			
		||||
			normalOffsetScale = 0.0;
 | 
			
		||||
			z_bias = 3.6e3 * sinLight / cosLight;
 | 
			
		||||
		}
 | 
			
		||||
		z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
 | 
			
		||||
 | 
			
		||||
		shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
 | 
			
		||||
		shadow_position.z -= z_bias;
 | 
			
		||||
 | 
			
		||||
		if (f_timeofday < 0.2) {
 | 
			
		||||
			adj_shadow_strength = f_shadow_strength * 0.5 *
 | 
			
		||||
@ -223,7 +265,6 @@ void main(void)
 | 
			
		||||
				mtsmoothstep(0.20, 0.25, f_timeofday) *
 | 
			
		||||
				(1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
 | 
			
		||||
		}
 | 
			
		||||
		f_normal_length = length(vNormal);
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -19,10 +19,13 @@ uniform float animationTimer;
 | 
			
		||||
	uniform float f_shadowfar;
 | 
			
		||||
	uniform float f_shadow_strength;
 | 
			
		||||
	uniform vec4 CameraPos;
 | 
			
		||||
	varying float normalOffsetScale;
 | 
			
		||||
	uniform float xyPerspectiveBias0;
 | 
			
		||||
	uniform float xyPerspectiveBias1;
 | 
			
		||||
	
 | 
			
		||||
	varying float adj_shadow_strength;
 | 
			
		||||
	varying float cosLight;
 | 
			
		||||
	varying float f_normal_length;
 | 
			
		||||
	varying vec3 shadow_position;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -48,24 +51,7 @@ varying float vIDiff;
 | 
			
		||||
const float fogStart = FOG_START;
 | 
			
		||||
const float fogShadingParameter = 1.0 / (1.0 - fogStart);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
vec4 getPerspectiveFactor(in vec4 shadowPosition)
 | 
			
		||||
{
 | 
			
		||||
	vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
 | 
			
		||||
	vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
 | 
			
		||||
	float pDistance = length(l);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	l /= pFactor;
 | 
			
		||||
	shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
 | 
			
		||||
	shadowPosition.z *= zPerspectiveBias;
 | 
			
		||||
	return shadowPosition;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// assuming near is always 1.0
 | 
			
		||||
float getLinearDepth()
 | 
			
		||||
@ -75,15 +61,7 @@ float getLinearDepth()
 | 
			
		||||
 | 
			
		||||
vec3 getLightSpacePosition()
 | 
			
		||||
{
 | 
			
		||||
	vec4 pLightSpace;
 | 
			
		||||
	// some drawtypes have zero normals, so we need to handle it :(
 | 
			
		||||
	#if DRAW_TYPE == NDT_PLANTLIKE
 | 
			
		||||
	pLightSpace = m_ShadowViewProj * vec4(worldPosition, 1.0);
 | 
			
		||||
	#else
 | 
			
		||||
	pLightSpace = m_ShadowViewProj * vec4(worldPosition + normalOffsetScale * normalize(vNormal), 1.0);
 | 
			
		||||
	#endif
 | 
			
		||||
	pLightSpace = getPerspectiveFactor(pLightSpace);
 | 
			
		||||
	return pLightSpace.xyz * 0.5 + 0.5;
 | 
			
		||||
	return shadow_position * 0.5 + 0.5;
 | 
			
		||||
}
 | 
			
		||||
// custom smoothstep implementation because it's not defined in glsl1.2
 | 
			
		||||
// https://docs.gl/sl4/smoothstep
 | 
			
		||||
@ -503,13 +481,14 @@ void main(void)
 | 
			
		||||
 | 
			
		||||
#ifdef COLORED_SHADOWS
 | 
			
		||||
			vec4 visibility;
 | 
			
		||||
			if (cosLight > 0.0)
 | 
			
		||||
			if (cosLight > 0.0 || f_normal_length < 1e-3)
 | 
			
		||||
				visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
 | 
			
		||||
			else
 | 
			
		||||
				visibility = vec4(1.0, 0.0, 0.0, 0.0);
 | 
			
		||||
			shadow_int = visibility.r;
 | 
			
		||||
			shadow_color = visibility.gba;
 | 
			
		||||
#else
 | 
			
		||||
			if (cosLight > 0.0 || f_normal_length < 1e-3)
 | 
			
		||||
			if (cosLight > 0.0)
 | 
			
		||||
				shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
 | 
			
		||||
			else
 | 
			
		||||
 | 
			
		||||
@ -24,10 +24,12 @@ centroid varying vec2 varTexCoord;
 | 
			
		||||
	uniform float f_shadowfar;
 | 
			
		||||
	uniform float f_shadow_strength;
 | 
			
		||||
	uniform float f_timeofday;
 | 
			
		||||
	uniform vec4 CameraPos;
 | 
			
		||||
 | 
			
		||||
	varying float cosLight;
 | 
			
		||||
	varying float normalOffsetScale;
 | 
			
		||||
	varying float adj_shadow_strength;
 | 
			
		||||
	varying float f_normal_length;
 | 
			
		||||
	varying vec3 shadow_position;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
varying vec3 eyeVec;
 | 
			
		||||
@ -39,8 +41,36 @@ const float e = 2.718281828459;
 | 
			
		||||
const float BS = 10.0;
 | 
			
		||||
uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
 | 
			
		||||
vec4 getRelativePosition(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec2 l = position.xy - CameraPos.xy;
 | 
			
		||||
	vec2 s = l / abs(l);
 | 
			
		||||
	s = (1.0 - s * CameraPos.xy);
 | 
			
		||||
	l /= s;
 | 
			
		||||
	return vec4(l, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float getPerspectiveFactor(in vec4 relativePosition)
 | 
			
		||||
{
 | 
			
		||||
	float pDistance = length(relativePosition.xy);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	return pFactor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vec4 applyPerspectiveDistortion(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec4 l = getRelativePosition(position);
 | 
			
		||||
	float pFactor = getPerspectiveFactor(l);
 | 
			
		||||
	l.xy /= pFactor;
 | 
			
		||||
	position.xy = l.xy * l.zw + CameraPos.xy;
 | 
			
		||||
	position.z *= zPerspectiveBias;
 | 
			
		||||
	return position;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// custom smoothstep implementation because it's not defined in glsl1.2
 | 
			
		||||
// https://docs.gl/sl4/smoothstep
 | 
			
		||||
float mtsmoothstep(in float edge0, in float edge1, in float x)
 | 
			
		||||
@ -107,20 +137,31 @@ void main(void)
 | 
			
		||||
#ifdef ENABLE_DYNAMIC_SHADOWS
 | 
			
		||||
	if (f_shadow_strength > 0.0) {
 | 
			
		||||
		vec3 nNormal = normalize(vNormal);
 | 
			
		||||
		cosLight = dot(nNormal, -v_LightDirection);
 | 
			
		||||
		f_normal_length = length(vNormal);
 | 
			
		||||
 | 
			
		||||
		// Calculate normal offset scale based on the texel size adjusted for 
 | 
			
		||||
		// curvature of the SM texture. This code must be change together with
 | 
			
		||||
		// getPerspectiveFactor or any light-space transformation.
 | 
			
		||||
		vec3 eyeToVertex = worldPosition - eyePosition + cameraOffset;
 | 
			
		||||
		// Distance from the vertex to the player
 | 
			
		||||
		float distanceToPlayer = length(eyeToVertex - v_LightDirection * dot(eyeToVertex, v_LightDirection)) / f_shadowfar;
 | 
			
		||||
		// perspective factor estimation according to the
 | 
			
		||||
		float perspectiveFactor = distanceToPlayer * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
		float texelSize = f_shadowfar * perspectiveFactor * perspectiveFactor /
 | 
			
		||||
				(f_textureresolution * xyPerspectiveBias1  - perspectiveFactor * xyPerspectiveBias0);
 | 
			
		||||
		float slopeScale = clamp(pow(1.0 - cosLight*cosLight, 0.5), 0.0, 1.0);
 | 
			
		||||
		normalOffsetScale = texelSize * slopeScale;
 | 
			
		||||
		/* normalOffsetScale is in world coordinates (1/10th of a meter)
 | 
			
		||||
		   z_bias is in light space coordinates */
 | 
			
		||||
		float normalOffsetScale, z_bias;
 | 
			
		||||
		float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
 | 
			
		||||
		if (f_normal_length > 0.0) {
 | 
			
		||||
			nNormal = normalize(vNormal);
 | 
			
		||||
			cosLight = dot(nNormal, -v_LightDirection);
 | 
			
		||||
			float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
 | 
			
		||||
			normalOffsetScale = 0.1 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) / 
 | 
			
		||||
					xyPerspectiveBias1 / f_textureresolution;
 | 
			
		||||
			z_bias = 1e3 * sinLight / cosLight * (0.5 + f_textureresolution / 1024.0);
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			nNormal = vec3(0.0);
 | 
			
		||||
			cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
 | 
			
		||||
			float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
 | 
			
		||||
			normalOffsetScale = 0.0;
 | 
			
		||||
			z_bias = 3.6e3 * sinLight / cosLight;
 | 
			
		||||
		}
 | 
			
		||||
		z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
 | 
			
		||||
 | 
			
		||||
		shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
 | 
			
		||||
		shadow_position.z -= z_bias;
 | 
			
		||||
 | 
			
		||||
		if (f_timeofday < 0.2) {
 | 
			
		||||
			adj_shadow_strength = f_shadow_strength * 0.5 *
 | 
			
		||||
@ -133,7 +174,6 @@ void main(void)
 | 
			
		||||
				mtsmoothstep(0.20, 0.25, f_timeofday) *
 | 
			
		||||
				(1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
 | 
			
		||||
		}
 | 
			
		||||
		f_normal_length = length(vNormal);
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -9,24 +9,37 @@ uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
vec4 getPerspectiveFactor(in vec4 shadowPosition)
 | 
			
		||||
vec4 getRelativePosition(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
 | 
			
		||||
	vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
 | 
			
		||||
	float pDistance = length(l);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	l /= pFactor;
 | 
			
		||||
	shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
 | 
			
		||||
	shadowPosition.z *= zPerspectiveBias;
 | 
			
		||||
	return shadowPosition;
 | 
			
		||||
	vec2 l = position.xy - CameraPos.xy;
 | 
			
		||||
	vec2 s = l / abs(l);
 | 
			
		||||
	s = (1.0 - s * CameraPos.xy);
 | 
			
		||||
	l /= s;
 | 
			
		||||
	return vec4(l, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float getPerspectiveFactor(in vec4 relativePosition)
 | 
			
		||||
{
 | 
			
		||||
	float pDistance = length(relativePosition.xy);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	return pFactor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vec4 applyPerspectiveDistortion(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec4 l = getRelativePosition(position);
 | 
			
		||||
	float pFactor = getPerspectiveFactor(l);
 | 
			
		||||
	l.xy /= pFactor;
 | 
			
		||||
	position.xy = l.xy * l.zw + CameraPos.xy;
 | 
			
		||||
	position.z *= zPerspectiveBias;
 | 
			
		||||
	return position;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void main()
 | 
			
		||||
{
 | 
			
		||||
	vec4 pos = LightMVP * gl_Vertex;
 | 
			
		||||
 | 
			
		||||
	tPos = getPerspectiveFactor(LightMVP * gl_Vertex);
 | 
			
		||||
	tPos = applyPerspectiveDistortion(LightMVP * gl_Vertex);
 | 
			
		||||
 | 
			
		||||
	gl_Position = vec4(tPos.xyz, 1.0);
 | 
			
		||||
	gl_TexCoord[0].st = gl_MultiTexCoord0.st;
 | 
			
		||||
 | 
			
		||||
@ -6,24 +6,37 @@ uniform float xyPerspectiveBias0;
 | 
			
		||||
uniform float xyPerspectiveBias1;
 | 
			
		||||
uniform float zPerspectiveBias;
 | 
			
		||||
 | 
			
		||||
vec4 getPerspectiveFactor(in vec4 shadowPosition)
 | 
			
		||||
vec4 getRelativePosition(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
 | 
			
		||||
	vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
 | 
			
		||||
	float pDistance = length(l);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	l /= pFactor;
 | 
			
		||||
	shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
 | 
			
		||||
	shadowPosition.z *= zPerspectiveBias;
 | 
			
		||||
	return shadowPosition;
 | 
			
		||||
	vec2 l = position.xy - CameraPos.xy;
 | 
			
		||||
	vec2 s = l / abs(l);
 | 
			
		||||
	s = (1.0 - s * CameraPos.xy);
 | 
			
		||||
	l /= s;
 | 
			
		||||
	return vec4(l, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float getPerspectiveFactor(in vec4 relativePosition)
 | 
			
		||||
{
 | 
			
		||||
	float pDistance = length(relativePosition.xy);
 | 
			
		||||
	float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
 | 
			
		||||
	return pFactor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vec4 applyPerspectiveDistortion(in vec4 position)
 | 
			
		||||
{
 | 
			
		||||
	vec4 l = getRelativePosition(position);
 | 
			
		||||
	float pFactor = getPerspectiveFactor(l);
 | 
			
		||||
	l.xy /= pFactor;
 | 
			
		||||
	position.xy = l.xy * l.zw + CameraPos.xy;
 | 
			
		||||
	position.z *= zPerspectiveBias;
 | 
			
		||||
	return position;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void main()
 | 
			
		||||
{
 | 
			
		||||
	vec4 pos = LightMVP * gl_Vertex;
 | 
			
		||||
 | 
			
		||||
	tPos = getPerspectiveFactor(pos);
 | 
			
		||||
	tPos = applyPerspectiveDistortion(pos);
 | 
			
		||||
 | 
			
		||||
	gl_Position = vec4(tPos.xyz, 1.0);
 | 
			
		||||
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
 | 
			
		||||
 | 
			
		||||
@ -76,8 +76,11 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min
 | 
			
		||||
	draw_wield_tool = _draw_wield_tool;
 | 
			
		||||
	draw_crosshair = _draw_crosshair;
 | 
			
		||||
 | 
			
		||||
	if (shadow_renderer)
 | 
			
		||||
	if (shadow_renderer) {
 | 
			
		||||
		// This is necessary to render shadows for animations correctly
 | 
			
		||||
		smgr->getRootSceneNode()->OnAnimate(device->getTimer()->getTime());
 | 
			
		||||
		shadow_renderer->update();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	beforeDraw();
 | 
			
		||||
	drawAll();
 | 
			
		||||
 | 
			
		||||
@ -69,12 +69,18 @@ public:
 | 
			
		||||
	const core::matrix4 &getFutureProjectionMatrix() const;
 | 
			
		||||
	core::matrix4 getViewProjMatrix();
 | 
			
		||||
 | 
			
		||||
	/// Gets the light's far value.
 | 
			
		||||
	/// Gets the light's maximum far value, i.e. the shadow boundary
 | 
			
		||||
	f32 getMaxFarValue() const
 | 
			
		||||
	{
 | 
			
		||||
		return farPlane * BS;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Gets the current far value of the light
 | 
			
		||||
	f32 getFarValue() const
 | 
			
		||||
	{
 | 
			
		||||
		return shadow_frustum.zFar;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/// Gets the light's color.
 | 
			
		||||
	const video::SColorf &getLightColor() const
 | 
			
		||||
 | 
			
		||||
@ -143,7 +143,7 @@ size_t ShadowRenderer::getDirectionalLightCount() const
 | 
			
		||||
f32 ShadowRenderer::getMaxShadowFar() const
 | 
			
		||||
{
 | 
			
		||||
	if (!m_light_list.empty()) {
 | 
			
		||||
		float zMax = m_light_list[0].getMaxFarValue();
 | 
			
		||||
		float zMax = m_light_list[0].getFarValue();
 | 
			
		||||
		return zMax;
 | 
			
		||||
	}
 | 
			
		||||
	return 0.0f;
 | 
			
		||||
@ -418,10 +418,6 @@ void ShadowRenderer::renderShadowMap(video::ITexture *target,
 | 
			
		||||
 | 
			
		||||
		material.BackfaceCulling = false;
 | 
			
		||||
		material.FrontfaceCulling = true;
 | 
			
		||||
		material.PolygonOffsetFactor = 4.0f;
 | 
			
		||||
		material.PolygonOffsetDirection = video::EPO_BACK;
 | 
			
		||||
		//material.PolygonOffsetDepthBias = 1.0f/4.0f;
 | 
			
		||||
		//material.PolygonOffsetSlopeScale = -1.f;
 | 
			
		||||
 | 
			
		||||
		if (m_shadow_map_colored && pass != scene::ESNRP_SOLID) {
 | 
			
		||||
			material.MaterialType = (video::E_MATERIAL_TYPE) depth_shader_trans;
 | 
			
		||||
@ -431,9 +427,6 @@ void ShadowRenderer::renderShadowMap(video::ITexture *target,
 | 
			
		||||
			material.BlendOperation = video::EBO_MIN;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// FIXME: I don't think this is needed here
 | 
			
		||||
		map_node->OnAnimate(m_device->getTimer()->getTime());
 | 
			
		||||
 | 
			
		||||
		m_driver->setTransform(video::ETS_WORLD,
 | 
			
		||||
				map_node->getAbsoluteTransformation());
 | 
			
		||||
 | 
			
		||||
@ -479,10 +472,6 @@ void ShadowRenderer::renderShadowObjects(
 | 
			
		||||
 | 
			
		||||
			current_mat.BackfaceCulling = true;
 | 
			
		||||
			current_mat.FrontfaceCulling = false;
 | 
			
		||||
			current_mat.PolygonOffsetFactor = 1.0f/2048.0f;
 | 
			
		||||
			current_mat.PolygonOffsetDirection = video::EPO_BACK;
 | 
			
		||||
			//current_mat.PolygonOffsetDepthBias = 1.0 * 2.8e-6;
 | 
			
		||||
			//current_mat.PolygonOffsetSlopeScale = -1.f;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		m_driver->setTransform(video::ETS_WORLD,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user