terrain uses new lighting flow

This commit is contained in:
austin 2025-05-19 14:53:56 -04:00
parent 51f2627621
commit 0c15941efa
2 changed files with 69 additions and 23 deletions

View File

@ -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);
}

View File

@ -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;
}