From 0c15941efa522632f684786851ff77f18e3969f2 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 19 May 2025 14:53:56 -0400 Subject: [PATCH] terrain uses new lighting flow --- assets/Shaders/entities/terrain2/terrain2.fs | 43 ++++++++--------- assets/Shaders/lib/lights.fs | 49 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/assets/Shaders/entities/terrain2/terrain2.fs b/assets/Shaders/entities/terrain2/terrain2.fs index cfb3f04e..5ad2dbba 100644 --- a/assets/Shaders/entities/terrain2/terrain2.fs +++ b/assets/Shaders/entities/terrain2/terrain2.fs @@ -11,6 +11,12 @@ #define ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL 0.03125 //used to properly shift from texture to texture in the atlas +/** + * Transparency of the terrain + */ +#define TERRAIN_TRANSPARENCY 1.0 + + in vec3 FragPos; @@ -43,36 +49,27 @@ vec3 getColor(vec2 texPlane1, vec2 texPlane2, vec2 texPlane3, vec3 normal, vec3 void main(){ vec3 norm = normalize(Normal); vec3 viewDir = normalize(vec3(viewPos) - FragPos); - - //grab light intensity - vec3 lightIntensity = vec3(calcLightIntensityTotal(norm)); //get color of base texture - vec3 textureColor = getColor(texPlane1, texPlane2, texPlane3, norm, samplerIndexVec, samplerRatioVec, material); + vec3 albedo = getColor(texPlane1, texPlane2, texPlane3, norm, samplerIndexVec, samplerRatioVec, material); - //shadow - float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction.xyz), -norm); - - // - //point light calculations - uint clusterIndex = findCluster(ViewFragPos, zNear, zFar); - uint pointLightCount = clusters[clusterIndex].count; - for(int i = 0; i < pointLightCount; i++){ - uint pointLightIndex = clusters[clusterIndex].lightIndices[i]; - PointLight pointLight = pointLight[pointLightIndex]; - lightIntensity = lightIntensity + CalcPointLight(pointLight, norm, FragPos, viewDir); - } - //error checking on light clusters - if(pointLightCount > MAX_LIGHTS_PER_CLUSTER){ - FragColor = vec4(1.0f,0.0f,0.0f,1); - return; - } + // Calculate the light to apply + vec3 light = getTotalLight( + material, + albedo, + vec3(viewPos.xyz), + FragPosLightSpace, + ViewFragPos, + FragPos, + norm, + viewDir + ); //calculate final color - vec3 finalColor = textureColor * lightIntensity * max(shadow,0.4); + vec3 finalColor = light; //this final calculation is for transparency - FragColor = vec4(finalColor, 1); + FragColor = vec4(finalColor, TERRAIN_TRANSPARENCY); } diff --git a/assets/Shaders/lib/lights.fs b/assets/Shaders/lib/lights.fs index 9b1c133b..40265102 100644 --- a/assets/Shaders/lib/lights.fs +++ b/assets/Shaders/lib/lights.fs @@ -328,5 +328,54 @@ vec3 getTotalLight( //clamp vec3 lightClamp = vec3(min(lightSum.x,1),min(lightSum.y,1),min(lightSum.z,1)); + return lightClamp; +} + +/** + * Gets the total light to apply to the fragment + */ +vec3 getTotalLight( + Material mat, + vec3 albedo, + vec3 viewPos, + vec4 fragPosLightSpace, + vec3 fragPosView, + vec3 fragPos, + vec3 norm, + vec3 viewDir +){ + vec3 diffuseVal = albedo; + vec3 specularVal = vec3(0); + + // + //Global light calculations + vec3 ambientLight = calcAmbientLight(diffuseVal); + vec3 diffuseLight = calcDiffuseLight(norm, diffuseVal); + vec3 specLight = calcSpecLight(viewPos,fragPos,norm,mat.shininess,specularVal); + vec3 lightSum = ambientLight + diffuseLight + specLight; + + // + //point light calculations + uint clusterIndex = findCluster(fragPosView, zNear, zFar); + uint pointLightCount = clusters[clusterIndex].count; + for(int i = 0; i < pointLightCount; i++){ + uint pointLightIndex = clusters[clusterIndex].lightIndices[i]; + PointLight pointLight = pointLight[pointLightIndex]; + lightSum = lightSum + CalcPointLight(pointLight, norm, fragPos, viewDir); + } + //error checking on light clusters + if(pointLightCount > MAX_LIGHTS_PER_CLUSTER){ + return vec3(1.0,0.0,0.0); + } + + // + //shadow calculations + float shadow = ShadowCalculation(fragPosLightSpace, normalize(-directLight.direction.xyz), -norm); + float shadowMultiplier = max(shadow,SHADOW_MIN_MULTIPLIER); + lightSum = lightSum * shadowMultiplier; + + //clamp + vec3 lightClamp = vec3(min(lightSum.x,1),min(lightSum.y,1),min(lightSum.z,1)); + return lightClamp; } \ No newline at end of file