Skip to content

Commit

Permalink
Merge branch 'CatDogEngine:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
t0xxxic authored Jan 28, 2024
2 parents 4ecf838 + 8104906 commit 1177fbf
Show file tree
Hide file tree
Showing 20 changed files with 962 additions and 58 deletions.
14 changes: 12 additions & 2 deletions Engine/BuiltInShaders/UniformDefines/U_Light.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
#define RECTANGLE_LIGHT 5
#define TUBE_LIGHT 6

#define LIGHT_LENGTH 320
#define LIGHT_LENGTH 21
#define LIGHT_TRANSFORM_LENGTH 12

/*
LIGHT_LENGTH = num of lights(3) * num of total vec4 in one light(7)
LIGHT_TRANSFORM_LENGTH = num of lights(3) * max num of total mat4 of light transform for different kind of light(4)
*/
struct U_Light {
// vec4 * 5
// vec4 * 7
float type;
vec3 position; // 1
float intensity;
Expand All @@ -23,4 +28,9 @@ struct U_Light {
float height;
float lightAngleScale;
float lightAngleOffeset; // 5
int shadowType;
int lightViewProjOffset;
int cascadeNum;
float shadowBias; // 6
vec4 frustumClips; // 7
};
3 changes: 3 additions & 0 deletions Engine/BuiltInShaders/UniformDefines/U_Shadow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SHADOW_MAP_CUBE_FIRST_SLOT 11
#define SHADOW_MAP_CUBE_SECOND_SLOT 12
#define SHADOW_MAP_CUBE_THIRD_SLOT 13
182 changes: 158 additions & 24 deletions Engine/BuiltInShaders/common/LightSource.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
//-----------------------------------------------------------------------------------------//

#include "../UniformDefines/U_Light.sh"
#include "../UniformDefines/U_Shadow.sh"

uniform vec4 u_lightCountAndStride;
uniform vec4 u_lightParams[LIGHT_LENGTH];
uniform mat4 u_lightViewProjs[4*3];
uniform vec4 u_clipFrustumDepth;
uniform vec4 u_bias[3];//[LIGHT_NUM]

SAMPLERCUBE(s_texCubeShadowMap_1, SHADOW_MAP_CUBE_FIRST_SLOT);
SAMPLERCUBE(s_texCubeShadowMap_2, SHADOW_MAP_CUBE_SECOND_SLOT);
SAMPLERCUBE(s_texCubeShadowMap_3, SHADOW_MAP_CUBE_THIRD_SLOT);

U_Light GetLightParams(int pointer) {
// struct {
Expand All @@ -16,21 +24,28 @@ U_Light GetLightParams(int pointer) {
// /*2*/ struct { float range; vec3 direction; };
// /*3*/ struct { float radius; vec3 up; };
// /*4*/ struct { float width, height, lightAngleScale, lightAngleOffeset; };
// /*5*/ struct { int shadowType, lightViewProjOffset, cascadeNum; float shadowBias; };
// /*6*/ struct { vec4 frustumClips; };
// }

U_Light light;
light.type = u_lightParams[pointer + 0].x;
light.position = u_lightParams[pointer + 0].yzw;
light.intensity = u_lightParams[pointer + 1].x;
light.color = u_lightParams[pointer + 1].yzw;
light.range = u_lightParams[pointer + 2].x;
light.direction = u_lightParams[pointer + 2].yzw;
light.radius = u_lightParams[pointer + 3].x;
light.up = u_lightParams[pointer + 3].yzw;
light.width = u_lightParams[pointer + 4].x;
light.height = u_lightParams[pointer + 4].y;
light.lightAngleScale = u_lightParams[pointer + 4].z;
light.lightAngleOffeset = u_lightParams[pointer + 4].w;
light.type = u_lightParams[pointer + 0].x;
light.position = u_lightParams[pointer + 0].yzw;
light.intensity = u_lightParams[pointer + 1].x;
light.color = u_lightParams[pointer + 1].yzw;
light.range = u_lightParams[pointer + 2].x;
light.direction = u_lightParams[pointer + 2].yzw;
light.radius = u_lightParams[pointer + 3].x;
light.up = u_lightParams[pointer + 3].yzw;
light.width = u_lightParams[pointer + 4].x;
light.height = u_lightParams[pointer + 4].y;
light.lightAngleScale = u_lightParams[pointer + 4].z;
light.lightAngleOffeset = u_lightParams[pointer + 4].w;
light.shadowType = asint(u_lightParams[pointer + 5].x);
light.lightViewProjOffset = asint(u_lightParams[pointer + 5].y);
light.cascadeNum = asint(u_lightParams[pointer + 5].z);
light.shadowBias = u_lightParams[pointer + 5].z;
light.frustumClips = u_lightParams[pointer + 6];
return light;
}

Expand Down Expand Up @@ -98,9 +113,50 @@ vec3 closestPointOnSegment(vec3 a, vec3 b, vec3 c) {
return a + saturate(t) * ab;
}

// Return shadow map sampler according to light index
float textureIndex(int lightIndex, vec3 sampleVec){
float closestDepth = 1.0;
if(0 == lightIndex){
closestDepth = textureCube(s_texCubeShadowMap_1, sampleVec).r;
}else if(1 == lightIndex){
closestDepth = textureCube(s_texCubeShadowMap_2, sampleVec).r;
}else if(2 == lightIndex){
closestDepth = textureCube(s_texCubeShadowMap_3, sampleVec).r;
}
return closestDepth;
}


// -------------------- Point -------------------- //

vec3 CalculatePointLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF) {
float CalculatePointShadow(vec3 fragPosWorldSpace, vec3 lightPosWorldSpace, float far_plane, int lightIndex) {
vec3 lightToFrag = fragPosWorldSpace - lightPosWorldSpace;
float currentDepth = length(lightToFrag);
float bias = 0.05;

// PCF shadow | Filter Size : 3x3
float shadow = 0.0;
float samples = 3.0;
float totalOffset = 0.03;
float stepOffset = totalOffset / ((samples-1) * 0.5);
for(float x = -totalOffset; x <= totalOffset; x += stepOffset)
{
for(float y = -totalOffset; y <= totalOffset; y += stepOffset)
{
for(float z = -totalOffset; z <= totalOffset; z += stepOffset)
{
float closestDepth = textureIndex(lightIndex, lightToFrag + vec3(x, y, z));
closestDepth *= far_plane;
shadow += step(closestDepth, currentDepth - bias);
}
}
}
shadow /= (samples * samples * samples);

return shadow;
}

vec3 CalculatePointLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, int lightIndex) {
vec3 lightDir = normalize(light.position - worldPos);
vec3 harfDir = normalize(lightDir + viewDir);

Expand All @@ -119,12 +175,38 @@ vec3 CalculatePointLight(U_Light light, Material material, vec3 worldPos, vec3 v
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
return (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex);
return (1 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

// -------------------- Spot -------------------- //

vec3 CalculateSpotLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF) {
float CalculateSpotShadow(vec3 fragPosWorldSpace, vec3 normal, vec3 lightDir,int lightViewProjOffset, int lightIndex){
vec4 fragPosLightSpace = mul(u_lightViewProjs[lightViewProjOffset], vec4(fragPosWorldSpace, 1.0));
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
vec3 sampleVec = vec3(1.0, projCoords.y, -projCoords.x);
float fragDepth = projCoords.z;

// Calculate bias (based on depth map resolution and slope)
float bias = max(0.001 * (1.0 - dot(normal, lightDir)), 0.00001);

// PCF Filter Size : 3x3
float shadow = 0.0;
float samples = 3.0;
float totalOffset = 0.0015;
float stepOffset = totalOffset / ((samples-1) * 0.5);
for(float x = -totalOffset; x <= totalOffset; x += stepOffset){
for(float y = -totalOffset; y <= totalOffset; y += stepOffset){
float closestDepth = textureIndex(lightIndex, sampleVec + vec3(0, y, x));
shadow += step(closestDepth, fragDepth - bias);
}
}
shadow /= (samples * samples);

return shadow;
}

vec3 CalculateSpotLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, int lightIndex) {
vec3 lightDir = normalize(light.position - worldPos);
vec3 harfDir = normalize(lightDir + viewDir);

Expand All @@ -145,12 +227,63 @@ vec3 CalculateSpotLight(U_Light light, Material material, vec3 worldPos, vec3 vi
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
return (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
float shadow = CalculateSpotShadow(worldPos, material.normal, lightDir, light.lightViewProjOffset, lightIndex);
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

// -------------------- Directional -------------------- //

vec3 CalculateDirectionalLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF) {
float CalculateDirectionalShadow(vec3 fragPosWorldSpace, vec3 normal, vec3 lightDir, mat4 lightViewProj, int lightIndex, float num){
vec4 fragPosLightSpace = mul(lightViewProj, vec4(fragPosWorldSpace, 1.0));
vec3 projCoords = fragPosLightSpace.xyz/fragPosLightSpace.w;
float currentDepth = projCoords.z;
float bias = max(0.02 * (1.0 - dot(normal, lightDir)), 0.002);

// PCF shadow | Filter Size : 3x3
vec3 sampleVec;
if(num < 0.5) sampleVec = vec3(1.0, projCoords.y, -projCoords.x);
else if(num > 0.5 && num < 1.5) sampleVec = vec3(-1.0, projCoords.y, projCoords.x);
else if(num > 1.5 && num < 2.5) sampleVec = vec3(projCoords.x, 1.0, projCoords.y);
else if(num > 2.5 && num < 3.5) sampleVec = vec3(projCoords.x, -1.0, projCoords.y);
float shadow = 0.0;
float samples = 3.0;
float totalOffset = 0.0015;
float stepOffset = totalOffset / ((samples-1) * 0.5);
if(num < 1.5){
for(float x = -totalOffset; x <= totalOffset; x += stepOffset){
for(float y = -totalOffset; y <= totalOffset; y += stepOffset){
float closestDepth = textureIndex(lightIndex, sampleVec + vec3(0, y, x));
shadow += step(closestDepth, currentDepth - bias);
}
}
}
else{
for(float x = -totalOffset; x <= totalOffset; x += stepOffset){
for(float y = -totalOffset; y <= totalOffset; y += stepOffset){
float closestDepth = textureIndex(lightIndex, sampleVec + vec3(x, 0, y));
shadow += step(closestDepth, currentDepth - bias);
}
}
}
shadow /= (samples * samples);

return shadow;
}

float CalculateCascadedDirectionalShadow(vec3 fragPosWorldSpace, vec3 normal, vec3 lightDir, float csmDepth, int lightViewProjOffset, int lightIndex){
if(csmDepth > 0 && csmDepth <= u_clipFrustumDepth.x)
return CalculateDirectionalShadow(fragPosWorldSpace, normal, lightDir, u_lightViewProjs[lightViewProjOffset], lightIndex, 0.0);
else if(csmDepth > u_clipFrustumDepth.x && csmDepth <= u_clipFrustumDepth.y)
return CalculateDirectionalShadow(fragPosWorldSpace, normal, lightDir, u_lightViewProjs[lightViewProjOffset+1], lightIndex, 1.0);
else if(csmDepth > u_clipFrustumDepth.y && csmDepth <= u_clipFrustumDepth.z)
return CalculateDirectionalShadow(fragPosWorldSpace, normal, lightDir, u_lightViewProjs[lightViewProjOffset+2], lightIndex, 2.0);
else if(csmDepth > u_clipFrustumDepth.z && csmDepth <= 1)
return CalculateDirectionalShadow(fragPosWorldSpace, normal, lightDir, u_lightViewProjs[lightViewProjOffset+3], lightIndex, 3.0);
else
return 1.0;
}

vec3 CalculateDirectionalLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, float csmDepth, int lightIndex) {
// TODO : Remove this normalize in the future.
vec3 lightDir = normalize(-light.direction);
vec3 harfDir = normalize(lightDir + viewDir);
Expand All @@ -167,7 +300,8 @@ vec3 CalculateDirectionalLight(U_Light light, Material material, vec3 worldPos,

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
vec3 irradiance = light.color * light.intensity;
return (KD * diffuseBRDF + specularBRDF) * irradiance * NdotL;
float shadow = CalculateCascadedDirectionalShadow(worldPos, material.normal, lightDir, csmDepth, light.lightViewProjOffset, lightIndex);
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * irradiance * NdotL;
}

// -------------------- Sphere -------------------- //
Expand Down Expand Up @@ -505,19 +639,19 @@ vec3 CalculateTubeLight(U_Light light, Material material, vec3 worldPos, vec3 vi

// -------------------- Calculate each light -------------------- //

vec3 CalculateLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF) {
vec3 CalculateLight(U_Light light, Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, float csmDepth, int lightIndex) {
vec3 color = vec3_splat(0.0);
if (light.type == POINT_LIGHT)
{
color = CalculatePointLight(light, material, worldPos, viewDir, diffuseBRDF);
color = CalculatePointLight(light, material, worldPos, viewDir, diffuseBRDF, lightIndex);
}
else if (light.type == SPOT_LIGHT)
{
color = CalculateSpotLight(light, material, worldPos, viewDir, diffuseBRDF);
color = CalculateSpotLight(light, material, worldPos, viewDir, diffuseBRDF, lightIndex);
}
else if (light.type == DIRECTIONAL_LIGHT)
{
color = CalculateDirectionalLight(light, material, worldPos, viewDir, diffuseBRDF);
color = CalculateDirectionalLight(light, material, worldPos, viewDir, diffuseBRDF, csmDepth, lightIndex);
}
else if (light.type == SPHERE_LIGHT)
{
Expand All @@ -542,12 +676,12 @@ vec3 CalculateLight(U_Light light, Material material, vec3 worldPos, vec3 viewDi
return color;
}

vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF) {
vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, float csmDepth) {
vec3 color = vec3_splat(0.0);
for(int lightIndex = 0; lightIndex < int(u_lightCountAndStride.x); ++lightIndex) {
int pointer = int(lightIndex * u_lightCountAndStride.y);
U_Light light = GetLightParams(pointer);
color += CalculateLight(light, material, worldPos, viewDir, diffuseBRDF);
color += CalculateLight(light, material, worldPos, viewDir, diffuseBRDF, csmDepth, lightIndex);
}
return color;
}
12 changes: 7 additions & 5 deletions Engine/BuiltInShaders/shaders/fs_PBR.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$input v_worldPos, v_normal, v_texcoord0, v_TBN
$input v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0

#include "../common/common.sh"
#include "../common/BRDF.sh"
Expand All @@ -9,10 +9,11 @@ $input v_worldPos, v_normal, v_texcoord0, v_TBN
#include "../common/Envirnoment.sh"

uniform vec4 u_emissiveColorAndFactor;
uniform vec4 u_cameraNearFarPlane;

vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir) {
vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir, float csmDepth) {
vec3 diffuseBRDF = material.albedo * CD_INV_PI;
return CalculateLights(material, worldPos, viewDir, diffuseBRDF);
return CalculateLights(material, worldPos, viewDir, diffuseBRDF, csmDepth);
}

vec3 GetEnvironment(Material material, vec3 worldPos, vec3 viewDir, vec3 normal) {
Expand All @@ -29,8 +30,9 @@ void main()
vec3 cameraPos = GetCamera().position.xyz;
vec3 viewDir = normalize(cameraPos - v_worldPos);

// Directional Light
vec3 dirColor = GetDirectional(material, v_worldPos, viewDir);
// Directional Light
float csmDepth = (v_color0.z - u_cameraNearFarPlane.x)/(u_cameraNearFarPlane.y - u_cameraNearFarPlane.x);
vec3 dirColor = GetDirectional(material, v_worldPos, viewDir, csmDepth);

// Environment Light
vec3 envColor = GetEnvironment(material, v_worldPos, viewDir, v_normal);
Expand Down
8 changes: 8 additions & 0 deletions Engine/BuiltInShaders/shaders/fs_shadowMap.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$input v_worldPos

#include "../common/common.sh"

void main()
{

}
11 changes: 11 additions & 0 deletions Engine/BuiltInShaders/shaders/fs_shadowMap_linear.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$input v_worldPos

#include "../common/common.sh"

uniform vec4 u_lightWorldPos_farPlane;

void main()
{
float diatance = length(v_worldPos - u_lightWorldPos_farPlane.xyz);
gl_FragColor= vec4(diatance/u_lightWorldPos_farPlane.w, 0.0, 0.0, 1.0);
}
4 changes: 2 additions & 2 deletions Engine/BuiltInShaders/shaders/vs_PBR.sc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
$input a_position, a_normal, a_tangent, a_texcoord0
$output v_worldPos, v_normal, v_texcoord0, v_TBN
$output v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0

#include "../common/common.sh"

void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));

v_worldPos = mul(u_model[0], vec4(a_position, 1.0)).xyz;
v_color0 = mul(u_modelView, vec4(a_position, 1.0));

v_normal = normalize(mul(u_modelInvTrans, vec4(a_normal, 0.0)).xyz);
vec3 tangent = normalize(mul(u_modelInvTrans, vec4(a_tangent, 0.0)).xyz);
Expand Down
10 changes: 10 additions & 0 deletions Engine/BuiltInShaders/shaders/vs_shadowMap.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$input a_position
$output v_worldPos

#include "../common/common.sh"

void main()
{
v_worldPos = mul(u_model[0], vec4(a_position, 1.0)).xyz;
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
}
Loading

0 comments on commit 1177fbf

Please sign in to comment.