improve shadow process, now shadow can render according to the camera position

This commit is contained in:
2026-04-07 13:20:02 +08:00
parent 3c64abd77e
commit 230957f728
5 changed files with 309 additions and 274 deletions

View File

@ -1,22 +1,58 @@
Scene: Scene Name
Environment:
AssetHandle: 5211537204242875091
AssetHandle: 10745193190519058183
Entities:
- Entity: 8293051279669100759
- Entity: 4944419254382500800
Parent: 0
Children:
[]
TagComponent:
Tag: Directional Light
TransformComponent:
Position: [0, 0, 0]
Rotation: [-2.2312348, 0, 0]
Scale: [1, 1, 1]
DirectionalLightComponent:
Radiance: [1, 1, 1]
CastShadows: true
SoftShadows: true
LightSize: 0.5
- Entity: 5099152432245948441
Parent: 0
Children:
[]
TagComponent:
Tag: venice_dawn_1_4k
TransformComponent:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
SkyLightComponent:
EnvironmentMap: 10745193190519058183
EnvironmentAssetPath: assets/env/venice_dawn_1_4k.hdr
Intensity: 1
Angle: 0
DynamicSky: false
TurbidityAzimuthInclination: [2, 0, 0]
- Entity: 10732070446010033158
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [1.736814, 1.4724115, -4.2181306]
Position: [0, -2.6466873, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Scale: [100, 1, 100]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
BodyType: 0
Mass: 1
LinearDrag: 0
AngularDrag: 0.05
@ -36,19 +72,50 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 5834225236589765516
- Entity: 9267298328378270409
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
Tag: Player
TransformComponent:
Position: [-2.6417403, 1.4724115, -7.9285727]
Rotation: [0.52199936, 0, 0]
Scale: [1, 1.0000001, 1.0000001]
Position: [0, 0.70693016, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
ScriptComponent:
ModuleName: FPSExample.FPSPlayer
StoredFields:
- Name: m_Radius
Type: 1
Data: 0
- Name: WalkingSpeed
Type: 1
Data: 2
- Name: RunSpeed
Type: 1
Data: 5
- Name: JumpForce
Type: 1
Data: 1
- Name: TorqueStrength
Type: 1
Data: 0
- Name: MouseSensitivity
Type: 1
Data: 10
- Name: CameraForwardOffset
Type: 1
Data: -2
- Name: CameraYOffset
Type: 1
Data: 2
MeshComponent:
AssetID: 18328012085543462741
AssetPath: assets/meshes/Default/Cube.fbx
AssetID: 8763440120556133680
AssetPath: assets/meshes/Default/Capsule.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
@ -57,6 +124,64 @@ Entities:
DisableGravity: false
IsKinematic: false
Layer: 0
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: true
LockRotationY: false
LockRotationZ: true
MeshColliderComponent:
IsConvex: true
IsTrigger: false
OverrideMesh: false
Material: 0
MaterialPath: ""
- Entity: 8114736924719261351
Parent: 0
Children:
[]
TagComponent:
Tag: Camera
TransformComponent:
Position: [0, 0.8097433, 4.573171]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera:
ProjectionType: 0
PerspectiveFOV: 45
PerspectiveNear: 0.01
PerspectiveFar: 10000
OrthographicSize: 10
OrthographicNear: -1
OrthographicFar: 1
Primary: true
- Entity: 4208267561919679628
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [-2.6417403, 1.4724115, -4.8956265]
Rotation: [-0.4034239, 0, 0]
Scale: [1, 0.99999994, 0.99999994]
MeshComponent:
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
LinearDrag: 0
AngularDrag: 0.05
DisableGravity: true
IsKinematic: false
Layer: 0
Constraints:
LockPositionX: false
LockPositionY: false
@ -70,19 +195,23 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 8234256119181302872
- Entity: 3328246672296261054
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [1.736814, 1.4724115, -7.9285727]
Position: [1.736814, 1.4724115, -0.88378817]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
@ -115,8 +244,12 @@ Entities:
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
@ -138,19 +271,23 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 3328246672296261054
- Entity: 8234256119181302872
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [1.736814, 1.4724115, -0.88378817]
Position: [1.736814, 1.4724115, -7.9285727]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
@ -172,25 +309,29 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 4208267561919679628
- Entity: 5834225236589765516
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [-2.6417403, 1.4724115, -4.8956265]
Rotation: [-0.4034239, 0, 0]
Scale: [1, 0.99999994, 0.99999994]
Position: [-2.6417403, 1.4724115, -7.9285727]
Rotation: [0.52199936, 0, 0]
Scale: [1, 1.0000001, 1.0000001]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 1
Mass: 1
LinearDrag: 0
AngularDrag: 0.05
DisableGravity: true
DisableGravity: false
IsKinematic: false
Layer: 0
Constraints:
@ -206,96 +347,25 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 8114736924719261351
Parent: 0
Children:
[]
TagComponent:
Tag: Camera
TransformComponent:
Position: [0, 0.8097433, 4.573171]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera:
ProjectionType: 0
PerspectiveFOV: 45
PerspectiveNear: 0.01
PerspectiveFar: 10000
OrthographicSize: 10
OrthographicNear: -1
OrthographicFar: 1
Primary: true
- Entity: 9267298328378270409
Parent: 0
Children:
[]
TagComponent:
Tag: Player
TransformComponent:
Position: [0, 0.70693016, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
ScriptComponent:
ModuleName: FPSExample.FPSPlayer
StoredFields:
- Name: WalkingSpeed
Type: 1
Data: 2
- Name: RunSpeed
Type: 1
Data: 5
- Name: JumpForce
Type: 1
Data: 1
- Name: MouseSensitivity
Type: 1
Data: 10
- Name: CameraForwardOffset
Type: 1
Data: -2
- Name: CameraYOffset
Type: 1
Data: 2
MeshComponent:
AssetID: 3043502408333723884
AssetPath: assets/meshes/Default/Capsule.fbx
RigidBodyComponent:
BodyType: 1
Mass: 1
LinearDrag: 0
AngularDrag: 0.05
DisableGravity: false
IsKinematic: false
Layer: 0
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: true
LockRotationY: false
LockRotationZ: true
MeshColliderComponent:
IsConvex: true
IsTrigger: false
OverrideMesh: false
Material: 0
MaterialPath: ""
- Entity: 10732070446010033158
- Entity: 8293051279669100759
Parent: 0
Children:
[]
TagComponent:
Tag: Cube
TransformComponent:
Position: [0, -2.6466873, 0]
Position: [1.736814, 1.4724115, -4.2181306]
Rotation: [0, 0, 0]
Scale: [100, 1, 100]
Scale: [1, 1, 1]
MeshComponent:
AssetID: 18328012085543462741
AssetID: 14957733959243172548
AssetPath: assets/meshes/Default/Cube.fbx
Materials:
Slot 0:
AssetHandle: 0
AssetPath: ""
RigidBodyComponent:
BodyType: 0
BodyType: 1
Mass: 1
LinearDrag: 0
AngularDrag: 0.05
@ -315,22 +385,5 @@ Entities:
IsTrigger: false
Material: 0
MaterialPath: ""
- Entity: 5099152432245948441
Parent: 0
Children:
[]
TagComponent:
Tag: venice_dawn_1_4k
TransformComponent:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
SkyLightComponent:
EnvironmentMap: 5211537204242875091
EnvironmentAssetPath: assets/env/venice_dawn_1_4k.hdr
Intensity: 1
Angle: 0
DynamicSky: false
TurbidityAzimuthInclination: [2, 0, 0]
PhysicsLayers:
[]

View File

@ -362,44 +362,38 @@ vec3 IBL(vec3 F0, vec3 Lr)
}
// shadow
float calculateShadow(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir)
{
// Perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// If outside shadow map bounds, assume no shadow
if(projCoords.z > 1.0 || projCoords.x < 0.0 || projCoords.x > 1.0 || projCoords.y < 0.0 || projCoords.y > 1.0)
if (projCoords.z > 1.0 || projCoords.x < 0.0 || projCoords.x > 1.0 ||
projCoords.y < 0.0 || projCoords.y > 1.0)
return 0.0;
// Get closest depth value from light's perspective
float closestDepth = texture(u_ShadowMap, projCoords.xy).r;
float currentDepth = projCoords.z;
// Calculate bias based on surface angle
float bias = max(u_ShadowBias * (1.0 - dot(normal, lightDir)), u_ShadowBias * 0.1);
// PCF (Percentage Closer Filtering) for soft shadows
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(u_ShadowMap, 0);
int pcfRange = int(u_ShadowSoftness);
int sampleCount = 0;
int pcfRange = clamp(int(u_ShadowSoftness), 0, 3);
for(int x = -pcfRange; x <= pcfRange; ++x)
vec2 texelSize = 1.0 / textureSize(u_ShadowMap, 0);
float shadow = 0.0;
int samples = 0;
for (int x = -pcfRange; x <= pcfRange; ++x)
{
for(int y = -pcfRange; y <= pcfRange; ++y)
for (int y = -pcfRange; y <= pcfRange; ++y)
{
float pcfDepth = texture(u_ShadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
sampleCount++;
vec2 offset = vec2(x, y) * texelSize;
float pcfDepth = texture(u_ShadowMap, projCoords.xy + offset).r;
shadow += (currentDepth - bias) > pcfDepth ? 1.0 : 0.0;
samples++;
}
}
shadow /= float(sampleCount);
return shadow;
shadow /= float(samples);
return shadow * u_ShadowIntensity;
}

View File

@ -347,44 +347,38 @@ vec3 IBL(vec3 F0, vec3 Lr)
}
// shadow
float calculateShadow(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir)
{
// Perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// If outside shadow map bounds, assume no shadow
if(projCoords.z > 1.0 || projCoords.x < 0.0 || projCoords.x > 1.0 || projCoords.y < 0.0 || projCoords.y > 1.0)
if (projCoords.z > 1.0 || projCoords.x < 0.0 || projCoords.x > 1.0 ||
projCoords.y < 0.0 || projCoords.y > 1.0)
return 0.0;
// Get closest depth value from light's perspective
float closestDepth = texture(u_ShadowMap, projCoords.xy).r;
float currentDepth = projCoords.z;
// Calculate bias based on surface angle
float bias = max(u_ShadowBias * (1.0 - dot(normal, lightDir)), u_ShadowBias * 0.1);
// PCF (Percentage Closer Filtering) for soft shadows
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(u_ShadowMap, 0);
int pcfRange = int(u_ShadowSoftness);
int sampleCount = 0;
int pcfRange = clamp(int(u_ShadowSoftness), 0, 3);
for(int x = -pcfRange; x <= pcfRange; ++x)
vec2 texelSize = 1.0 / textureSize(u_ShadowMap, 0);
float shadow = 0.0;
int samples = 0;
for (int x = -pcfRange; x <= pcfRange; ++x)
{
for(int y = -pcfRange; y <= pcfRange; ++y)
for (int y = -pcfRange; y <= pcfRange; ++y)
{
float pcfDepth = texture(u_ShadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
sampleCount++;
vec2 offset = vec2(x, y) * texelSize;
float pcfDepth = texture(u_ShadowMap, projCoords.xy + offset).r;
shadow += (currentDepth - bias) > pcfDepth ? 1.0 : 0.0;
samples++;
}
}
shadow /= float(sampleCount);
return shadow;
shadow /= float(samples);
return shadow * u_ShadowIntensity;
}