major shader lighting rework
This commit is contained in:
parent
2fc8283bd5
commit
51f2627621
@ -31,35 +31,22 @@ void main(){
|
|||||||
}
|
}
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
||||||
|
|
||||||
//grab light intensity
|
|
||||||
vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
|
|
||||||
|
|
||||||
//get color of base texture
|
// Calculate the light to apply
|
||||||
vec3 textureColor = texture(material.diffuse, TexCoord).rgb;
|
vec3 light = getTotalLight(
|
||||||
|
material,
|
||||||
//shadow
|
TexCoord,
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), -norm);
|
vec3(viewPos.xyz),
|
||||||
|
FragPosLightSpace,
|
||||||
//
|
ViewFragPos,
|
||||||
//point light calculations
|
FragPos,
|
||||||
vec3 lightAmount = vec3(0);
|
norm,
|
||||||
uint clusterIndex = findCluster(ViewFragPos, zNear, zFar);
|
viewDir
|
||||||
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,0,1.0f);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//calculate final color
|
//calculate final color
|
||||||
vec3 finalColor = textureColor * lightIntensity * max(shadow,0.4);
|
vec3 finalColor = light;
|
||||||
|
|
||||||
//this final calculation is for transparency
|
//this final calculation is for transparency
|
||||||
FragColor = vec4(finalColor, texture(material.diffuse, TexCoord).a);//texture(ourTexture, TexCoord);//vec4(result, 1.0);
|
FragColor = vec4(finalColor, texture(material.diffuse, TexCoord).a);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,7 +63,7 @@ void main() {
|
|||||||
cluster.count = 0;
|
cluster.count = 0;
|
||||||
|
|
||||||
for (uint i = 0; i < lightCount; ++i){
|
for (uint i = 0; i < lightCount; ++i){
|
||||||
if (testSphereAABB(i, cluster) && cluster.count < 100){
|
if (testSphereAABB(i, cluster) && cluster.count < MAX_LIGHTS_PER_CLUSTER){
|
||||||
cluster.lightIndices[cluster.count] = i;
|
cluster.lightIndices[cluster.count] = i;
|
||||||
cluster.count++;
|
cluster.count++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,36 +37,24 @@ void main(){
|
|||||||
|
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
||||||
|
|
||||||
//grab light intensity
|
|
||||||
vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
|
|
||||||
|
|
||||||
//get color of base texture
|
//get color of base texture
|
||||||
vec4 textureColor = texture(material.diffuse, TexCoord);
|
vec4 textureColor = texture(material.diffuse, TexCoord);
|
||||||
vec3 textureRGB = textureColor.rgb;
|
|
||||||
|
|
||||||
//shadow
|
// Calculate the light to apply
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), norm);
|
vec3 light = getTotalLight(
|
||||||
|
material,
|
||||||
//
|
TexCoord,
|
||||||
//point light calculations
|
vec3(viewPos.xyz),
|
||||||
uint clusterIndex = findCluster(ViewFragPos, zNear, zFar);
|
FragPosLightSpace,
|
||||||
uint pointLightCount = clusters[clusterIndex].count;
|
ViewFragPos,
|
||||||
for(int i = 0; i < pointLightCount; i++){
|
FragPos,
|
||||||
uint pointLightIndex = clusters[clusterIndex].lightIndices[i];
|
norm,
|
||||||
PointLight pointLight = pointLight[pointLightIndex];
|
viewDir
|
||||||
lightIntensity = lightIntensity + CalcPointLight(pointLight, norm, FragPos, viewDir);
|
);
|
||||||
}
|
|
||||||
//error checking on light clusters
|
|
||||||
if(pointLightCount > MAX_LIGHTS_PER_CLUSTER){
|
|
||||||
accum = vec4(1.0f,0.0f,0.0f,1);
|
|
||||||
reveal = textureColor.a;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//calculate final color
|
//calculate final color
|
||||||
vec3 shadowModifiedColor = textureRGB * lightIntensity * max(shadow, 0.4);
|
vec4 finalColor = vec4(light,textureColor.a);
|
||||||
vec4 finalColor = vec4(shadowModifiedColor,textureColor.a);
|
|
||||||
|
|
||||||
//calculate weight function
|
//calculate weight function
|
||||||
float weight = clamp(pow(min(1.0, finalColor.a * 10.0) + 0.01, 3.0) * 1e8 *
|
float weight = clamp(pow(min(1.0, finalColor.a * 10.0) + 0.01, 3.0) * 1e8 *
|
||||||
|
|||||||
@ -34,41 +34,32 @@ out vec4 FragColor;
|
|||||||
|
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
vec4 getColor(vec2 uv, vec3 normal, int samplerIndexVec, Material material);
|
vec2 getColor(vec2 uv, vec3 normal, int samplerIndexVec);
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 viewDir = normalize(viewPos - FragPos);
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
|
|
||||||
//grab light intensity
|
|
||||||
vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
|
|
||||||
|
|
||||||
//get color of base texture
|
//get color of base texture
|
||||||
vec4 textureColor = getColor(uv, norm, samplerIndexVec, material);
|
vec2 TexCoord = getColor(uv, norm, samplerIndexVec);
|
||||||
|
|
||||||
//shadow
|
// Calculate the light to apply
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), -norm);
|
vec3 light = getTotalLight(
|
||||||
|
material,
|
||||||
//
|
TexCoord,
|
||||||
//point light calculations
|
vec3(viewPos.xyz),
|
||||||
uint clusterIndex = findCluster(ViewFragPos, zNear, zFar);
|
FragPosLightSpace,
|
||||||
uint pointLightCount = clusters[clusterIndex].count;
|
ViewFragPos,
|
||||||
for(int i = 0; i < pointLightCount; i++){
|
FragPos,
|
||||||
uint pointLightIndex = clusters[clusterIndex].lightIndices[i];
|
norm,
|
||||||
PointLight pointLight = pointLight[pointLightIndex];
|
viewDir
|
||||||
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 final color
|
//calculate final color
|
||||||
vec3 finalColor = textureColor.rgb * lightIntensity * max(shadow,0.4);
|
vec3 finalColor = light;
|
||||||
|
|
||||||
//this final calculation is for transparency
|
//this final calculation is for transparency
|
||||||
FragColor = vec4(finalColor, textureColor.a);
|
FragColor = vec4(finalColor, texture(material.diffuse, TexCoord).a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -76,16 +67,14 @@ void main(){
|
|||||||
* The function that gets the texture color based on the triplanar texture mapping and the voxel type at each point along the vert.
|
* The function that gets the texture color based on the triplanar texture mapping and the voxel type at each point along the vert.
|
||||||
* See the triplanar mapping wiki article for an explanation of math involved.
|
* See the triplanar mapping wiki article for an explanation of math involved.
|
||||||
*/
|
*/
|
||||||
vec4 getColor(vec2 uv, vec3 normal, int samplerIndexVec, Material material){
|
vec2 getColor(vec2 uv, vec3 normal, int samplerIndexVec){
|
||||||
|
|
||||||
//the uv of the texture clamped within the atlas
|
//the uv of the texture clamped within the atlas
|
||||||
vec2 actualUv = vec2(
|
vec2 actualUv = vec2(
|
||||||
(fract(uv.x) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (mod(samplerIndexVec,ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL),
|
(fract(uv.x) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (mod(samplerIndexVec,ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL),
|
||||||
(fract(uv.y) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (round(samplerIndexVec / ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL)
|
(fract(uv.y) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (round(samplerIndexVec / ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL)
|
||||||
);
|
);
|
||||||
//albedo for the X texture
|
|
||||||
vec4 color = texture(material.diffuse, actualUv);
|
|
||||||
|
|
||||||
|
|
||||||
return color;
|
return actualUv;
|
||||||
}
|
}
|
||||||
@ -34,41 +34,32 @@ out vec4 FragColor;
|
|||||||
|
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
vec4 getColor(vec2 uv, vec3 normal, int blockIndex, Material material);
|
vec2 getColor(vec2 uv, vec3 normal, int samplerIndexVec);
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 viewDir = normalize(viewPos - FragPos);
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
|
|
||||||
//grab light intensity
|
|
||||||
vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
|
|
||||||
|
|
||||||
//get color of base texture
|
//get color of base texture
|
||||||
vec4 textureColor = getColor(uv, norm, blockAtlasIndex, material);
|
vec2 TexCoord = getColor(uv, norm, blockAtlasIndex);
|
||||||
|
|
||||||
//shadow
|
// Calculate the light to apply
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), -norm);
|
vec3 light = getTotalLight(
|
||||||
|
material,
|
||||||
//
|
TexCoord,
|
||||||
//point light calculations
|
vec3(viewPos.xyz),
|
||||||
uint clusterIndex = findCluster(ViewFragPos, zNear, zFar);
|
FragPosLightSpace,
|
||||||
uint pointLightCount = clusters[clusterIndex].count;
|
ViewFragPos,
|
||||||
for(int i = 0; i < pointLightCount; i++){
|
FragPos,
|
||||||
uint pointLightIndex = clusters[clusterIndex].lightIndices[i];
|
norm,
|
||||||
PointLight pointLight = pointLight[pointLightIndex];
|
viewDir
|
||||||
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 final color
|
//calculate final color
|
||||||
vec3 finalColor = textureColor.rgb * lightIntensity * max(shadow,0.4);
|
vec3 finalColor = light;
|
||||||
|
|
||||||
//this final calculation is for transparency
|
//this final calculation is for transparency
|
||||||
FragColor = vec4(finalColor, textureColor.a);
|
FragColor = vec4(finalColor, texture(material.diffuse, TexCoord).a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -76,16 +67,14 @@ void main(){
|
|||||||
* The function that gets the texture color based on the triplanar texture mapping and the voxel type at each point along the vert.
|
* The function that gets the texture color based on the triplanar texture mapping and the voxel type at each point along the vert.
|
||||||
* See the triplanar mapping wiki article for an explanation of math involved.
|
* See the triplanar mapping wiki article for an explanation of math involved.
|
||||||
*/
|
*/
|
||||||
vec4 getColor(vec2 uv, vec3 normal, int blockIndex, Material material){
|
vec2 getColor(vec2 uv, vec3 normal, int samplerIndexVec){
|
||||||
|
|
||||||
//the uv of the texture clamped within the atlas
|
//the uv of the texture clamped within the atlas
|
||||||
vec2 actualUv = vec2(
|
vec2 actualUv = vec2(
|
||||||
(fract(uv.x) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (mod(blockIndex,ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL),
|
(fract(uv.x) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (mod(samplerIndexVec,ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL),
|
||||||
(fract(uv.y) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (round(blockIndex / ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL)
|
(fract(uv.y) * ATLAS_NORMALIZED_ELEMENT_WIDTH) + (round(samplerIndexVec / ATLAS_EL_PER_ROW) * ATLAS_NORMALIZED_ELEMENT_WIDTH_FULL)
|
||||||
);
|
);
|
||||||
//albedo for the X texture
|
|
||||||
vec4 color = texture(material.diffuse, actualUv);
|
|
||||||
|
|
||||||
|
|
||||||
return color;
|
return actualUv;
|
||||||
}
|
}
|
||||||
@ -3,10 +3,11 @@
|
|||||||
#include "../../lib/lights.fs"
|
#include "../../lib/lights.fs"
|
||||||
#include "../../lib/material.fs"
|
#include "../../lib/material.fs"
|
||||||
|
|
||||||
|
#define FLUID_TRANSPARENCY 0.2f
|
||||||
|
|
||||||
|
|
||||||
in vec3 FragPos;
|
in vec3 FragPos;
|
||||||
|
in vec3 ViewFragPos;
|
||||||
in vec3 Normal;
|
in vec3 Normal;
|
||||||
in vec2 texPlane1;
|
in vec2 texPlane1;
|
||||||
in vec2 texPlane2;
|
in vec2 texPlane2;
|
||||||
@ -27,20 +28,23 @@ void main(){
|
|||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
vec3 viewDir = normalize(vec3(viewPos) - FragPos);
|
||||||
|
|
||||||
//grab light intensity
|
// Calculate the light to apply
|
||||||
float lightIntensity = calcLightIntensityTotal(norm);
|
vec3 light = getTotalLight(
|
||||||
|
material,
|
||||||
//get color of base texture
|
vec2(0),
|
||||||
vec3 textureColor = vec3(0.6, 0.92, 0.92);//getColor(texPlane1, texPlane2, texPlane3, norm, material);
|
vec3(viewPos.xyz),
|
||||||
|
FragPosLightSpace,
|
||||||
//shadow
|
ViewFragPos,
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), norm);
|
FragPos,
|
||||||
|
norm,
|
||||||
|
viewDir
|
||||||
|
);
|
||||||
|
|
||||||
//calculate final color
|
//calculate final color
|
||||||
vec3 finalColor = textureColor * lightIntensity * max(shadow,0.4);
|
vec3 finalColor = light;
|
||||||
|
|
||||||
//this final calculation is for transparency
|
//this final calculation is for transparency
|
||||||
FragColor = vec4(finalColor, 0.2);
|
FragColor = vec4(finalColor, FLUID_TRANSPARENCY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ uniform mat4 lightSpaceMatrix;
|
|||||||
//output buffers
|
//output buffers
|
||||||
out vec3 Normal;
|
out vec3 Normal;
|
||||||
out vec3 FragPos;
|
out vec3 FragPos;
|
||||||
|
out vec3 ViewFragPos;
|
||||||
out vec2 texPlane1;
|
out vec2 texPlane1;
|
||||||
out vec2 texPlane2;
|
out vec2 texPlane2;
|
||||||
out vec2 texPlane3;
|
out vec2 texPlane3;
|
||||||
@ -39,6 +40,7 @@ void main() {
|
|||||||
|
|
||||||
//push frag, normal, and texture positions to fragment shader
|
//push frag, normal, and texture positions to fragment shader
|
||||||
FragPos = vec3(model * FinalVertex);
|
FragPos = vec3(model * FinalVertex);
|
||||||
|
ViewFragPos = vec3(view * model * FinalVertex);
|
||||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||||
|
|
||||||
//reference https://catlikecoding.com/unity/tutorials/advanced-rendering/triplanar-mapping/
|
//reference https://catlikecoding.com/unity/tutorials/advanced-rendering/triplanar-mapping/
|
||||||
|
|||||||
@ -64,7 +64,7 @@ void main(){
|
|||||||
// vec3 textureColor = vec3(0.17647,0.4,0.09411);//texture(material.diffuse, TexCoord).rgb;
|
// vec3 textureColor = vec3(0.17647,0.4,0.09411);//texture(material.diffuse, TexCoord).rgb;
|
||||||
|
|
||||||
//shadow
|
//shadow
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), norm);
|
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction.xyz), norm);
|
||||||
|
|
||||||
//
|
//
|
||||||
//point light calculations
|
//point light calculations
|
||||||
|
|||||||
@ -19,6 +19,7 @@ The direct global light
|
|||||||
struct DirectLight {
|
struct DirectLight {
|
||||||
vec3 direction;
|
vec3 direction;
|
||||||
vec3 color;
|
vec3 color;
|
||||||
|
vec3 ambientColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -51,7 +51,7 @@ void main(){
|
|||||||
vec3 textureColor = getColor(texPlane1, texPlane2, texPlane3, norm, samplerIndexVec, samplerRatioVec, material);
|
vec3 textureColor = getColor(texPlane1, texPlane2, texPlane3, norm, samplerIndexVec, samplerRatioVec, material);
|
||||||
|
|
||||||
//shadow
|
//shadow
|
||||||
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), -norm);
|
float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction.xyz), -norm);
|
||||||
|
|
||||||
//
|
//
|
||||||
//point light calculations
|
//point light calculations
|
||||||
|
|||||||
@ -24,9 +24,9 @@
|
|||||||
* The direct global light
|
* The direct global light
|
||||||
*/
|
*/
|
||||||
struct DirectLight {
|
struct DirectLight {
|
||||||
vec3 ambientColor;
|
vec4 direction;
|
||||||
vec3 direction;
|
vec4 color;
|
||||||
vec3 color;
|
vec4 ambientColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,6 +69,11 @@ layout(std430, binding = DIRECT_LIGHT_SSBO_BIND_POINT) restrict buffer dirLightS
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum multiplier that shadow can apply to the fragment (ie how dark can it make the fragment)
|
||||||
|
*/
|
||||||
|
#define SHADOW_MIN_MULTIPLIER 0.4f
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -116,39 +121,56 @@ vec3 getTotalLight(Material mat, vec3 norm, vec3 viewDir);
|
|||||||
*/
|
*/
|
||||||
float calcLightIntensityAmbient(){
|
float calcLightIntensityAmbient(){
|
||||||
//calculate average of ambient light
|
//calculate average of ambient light
|
||||||
float avg = (directLight.ambientColor.x + directLight.ambientColor.y + directLight.ambientColor.z)/3.0;
|
float avg = (directLight.color.x + directLight.color.y + directLight.color.z)/3.0;
|
||||||
return avg;
|
return avg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the ambient light
|
||||||
|
*/
|
||||||
|
vec3 calcAmbientLight(vec3 diffuseVal){
|
||||||
|
return directLight.ambientColor.rgb * diffuseVal;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
/**
|
/**
|
||||||
* Calculates the direct light applied to this fragment
|
* Calculates the direct light applied to this fragment
|
||||||
*/
|
*/
|
||||||
float calcLightIntensityDir(vec3 normal){
|
float calcLightIntensityDir(vec3 normal){
|
||||||
vec3 lightDir = normalize(-directLight.direction);
|
vec3 lightDir = normalize(-directLight.direction.xyz);
|
||||||
// diffuse shading
|
// diffuse shading
|
||||||
float diff = max(dot(normal, lightDir), 0.0);
|
float diff = max(dot(normal, lightDir), 0.0);
|
||||||
|
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the direct light
|
||||||
|
*/
|
||||||
|
vec3 calcDiffuseLight(vec3 normal, vec3 diffuseVal){
|
||||||
|
vec3 lightDir = normalize(-directLight.direction.xyz);
|
||||||
|
float diff = max(dot(normal, lightDir), 0.0);
|
||||||
|
vec3 fullColor = directLight.color.rgb * (diff * diffuseVal);
|
||||||
|
vec3 colorClamp = vec3(max(fullColor.x,0),max(fullColor.y,0),max(fullColor.z,0));
|
||||||
|
return colorClamp;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the direct light applied to this fragment
|
* Calculates the direct light applied to this fragment
|
||||||
*/
|
*/
|
||||||
vec3 calcLightIntensitySpec(
|
vec3 calcSpecLight(
|
||||||
DirectLight directLight,
|
vec3 viewPos,
|
||||||
vec3 viewPos,
|
vec3 fragPos,
|
||||||
vec3 fragPos,
|
vec3 norm,
|
||||||
vec3 norm,
|
float shininess,
|
||||||
float shininess,
|
vec3 specularVal
|
||||||
float specularVal
|
|
||||||
){
|
){
|
||||||
vec3 viewDir = normalize(viewPos - fragPos);
|
vec3 viewDir = normalize(viewPos - fragPos);
|
||||||
vec3 reflectDir = reflect(-directLight.direction, norm);
|
vec3 reflectDir = reflect(-directLight.direction.xyz, norm);
|
||||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
|
||||||
vec3 specular = directLight.color * (spec * specularVal);
|
vec3 specular = directLight.color.rgb * (spec * specularVal);
|
||||||
|
vec3 specClamp = vec3(max(specular.x,0),max(specular.y,0),max(specular.z,0));
|
||||||
return specular;
|
return specClamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -169,7 +191,7 @@ float calcLightIntensityTotal(vec3 normal){
|
|||||||
//
|
//
|
||||||
vec3 getTotalLightColor(vec3 normal){
|
vec3 getTotalLightColor(vec3 normal){
|
||||||
//get the direct light color adjusted for intensity
|
//get the direct light color adjusted for intensity
|
||||||
vec3 diffuseLightColor = directLight.color * calcLightIntensityDir(normal);
|
vec3 diffuseLightColor = directLight.color.rgb * calcLightIntensityDir(normal);
|
||||||
|
|
||||||
//sum light colors
|
//sum light colors
|
||||||
vec3 totalLightColor = diffuseLightColor;
|
vec3 totalLightColor = diffuseLightColor;
|
||||||
@ -264,39 +286,47 @@ float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal){
|
|||||||
* Gets the total light to apply to the fragment
|
* Gets the total light to apply to the fragment
|
||||||
*/
|
*/
|
||||||
vec3 getTotalLight(
|
vec3 getTotalLight(
|
||||||
Material mat,
|
Material mat,
|
||||||
vec3 viewPos,
|
vec2 texCoord,
|
||||||
vec4 fragPosLightSpace,
|
vec3 viewPos,
|
||||||
vec3 fragPosView,
|
vec4 fragPosLightSpace,
|
||||||
vec3 fragPos,
|
vec3 fragPosView,
|
||||||
vec3 norm,
|
vec3 fragPos,
|
||||||
vec3 viewDir
|
vec3 norm,
|
||||||
|
vec3 viewDir
|
||||||
){
|
){
|
||||||
float specular = 1.0f;
|
vec3 diffuseVal = texture(mat.diffuse, texCoord).rgb;
|
||||||
|
vec3 specularVal = vec3(0);
|
||||||
|
|
||||||
//grab light intensity
|
//
|
||||||
float ambientLightIntensity = calcLightIntensityAmbient();
|
//Global light calculations
|
||||||
float directLightIntensity = calcLightIntensityDir(norm);
|
vec3 ambientLight = calcAmbientLight(diffuseVal);
|
||||||
vec3 specularLightIntensity = calcLightIntensitySpec(directLight,viewPos,fragPos,norm,mat.shininess,specular);
|
vec3 diffuseLight = calcDiffuseLight(norm, diffuseVal);
|
||||||
|
vec3 specLight = calcSpecLight(viewPos,fragPos,norm,mat.shininess,specularVal);
|
||||||
|
vec3 lightSum = ambientLight + diffuseLight + specLight;
|
||||||
|
|
||||||
vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
|
//
|
||||||
|
//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
|
//
|
||||||
float shadow = ShadowCalculation(fragPosLightSpace, normalize(-directLight.direction), -norm);
|
//shadow calculations
|
||||||
|
float shadow = ShadowCalculation(fragPosLightSpace, normalize(-directLight.direction.xyz), -norm);
|
||||||
|
float shadowMultiplier = max(shadow,SHADOW_MIN_MULTIPLIER);
|
||||||
|
lightSum = lightSum * shadowMultiplier;
|
||||||
|
|
||||||
//
|
//clamp
|
||||||
//point light calculations
|
vec3 lightClamp = vec3(min(lightSum.x,1),min(lightSum.y,1),min(lightSum.z,1));
|
||||||
vec3 lightAmount = vec3(0);
|
|
||||||
uint clusterIndex = findCluster(fragPosView, zNear, zFar);
|
return lightClamp;
|
||||||
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){
|
|
||||||
return vec3(1.0,0.0,0.0);
|
|
||||||
}
|
|
||||||
return lightIntensity;
|
|
||||||
}
|
}
|
||||||
@ -1,3 +1,3 @@
|
|||||||
#maven.buildNumber.plugin properties file
|
#maven.buildNumber.plugin properties file
|
||||||
#Fri May 16 13:50:39 EDT 2025
|
#Mon May 19 14:18:34 EDT 2025
|
||||||
buildNumber=625
|
buildNumber=626
|
||||||
|
|||||||
@ -1902,6 +1902,7 @@ Break out shader material into dedicated library file
|
|||||||
Materials read some properties from assimp data
|
Materials read some properties from assimp data
|
||||||
OpenGLState VAO caching
|
OpenGLState VAO caching
|
||||||
Ambient light sent in dedicated variable to shader
|
Ambient light sent in dedicated variable to shader
|
||||||
|
Major lighting shader organization rework
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -383,6 +383,11 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
fillInDefaultContent(scrollable);
|
fillInDefaultContent(scrollable);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
||||||
|
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
||||||
|
|
||||||
|
Label dirLabel = Label.createLabel("" + directionalLight.getDirection());
|
||||||
|
|
||||||
//global light direction
|
//global light direction
|
||||||
scrollable.addChild(Label.createLabel("Global Light Direction"));
|
scrollable.addChild(Label.createLabel("Global Light Direction"));
|
||||||
Div xDiv = Div.createDiv();
|
Div xDiv = Div.createDiv();
|
||||||
@ -390,11 +395,11 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
xDiv.setFlexDirection(YogaFlexDirection.Row);
|
xDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
xDiv.addChild(Label.createLabel("X: "));
|
xDiv.addChild(Label.createLabel("X: "));
|
||||||
xDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
xDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f direction = directionalLight.getDirection();
|
Vector3f direction = directionalLight.getDirection();
|
||||||
direction.x = event.getAsFloat() * 2 - 1;
|
direction.x = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setDirection(direction);
|
directionalLight.setDirection(direction);
|
||||||
|
dirLabel.setText("" + directionalLight.getDirection());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(xDiv);
|
scrollable.addChild(xDiv);
|
||||||
|
|
||||||
@ -403,11 +408,11 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
yDiv.setFlexDirection(YogaFlexDirection.Row);
|
yDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
yDiv.addChild(Label.createLabel("Y: "));
|
yDiv.addChild(Label.createLabel("Y: "));
|
||||||
yDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
yDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f direction = directionalLight.getDirection();
|
Vector3f direction = directionalLight.getDirection();
|
||||||
direction.y = event.getAsFloat() * 2 - 1;
|
direction.y = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setDirection(direction);
|
directionalLight.setDirection(direction);
|
||||||
|
dirLabel.setText("" + directionalLight.getDirection());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(yDiv);
|
scrollable.addChild(yDiv);
|
||||||
|
|
||||||
@ -416,24 +421,29 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
zDiv.setFlexDirection(YogaFlexDirection.Row);
|
zDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
zDiv.addChild(Label.createLabel("Z: "));
|
zDiv.addChild(Label.createLabel("Z: "));
|
||||||
zDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
zDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f direction = directionalLight.getDirection();
|
Vector3f direction = directionalLight.getDirection();
|
||||||
direction.z = event.getAsFloat() * 2 - 1;
|
direction.z = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setDirection(direction);
|
directionalLight.setDirection(direction);
|
||||||
|
dirLabel.setText("" + directionalLight.getDirection());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(zDiv);
|
scrollable.addChild(zDiv);
|
||||||
|
|
||||||
|
scrollable.addChild(dirLabel);
|
||||||
|
|
||||||
|
|
||||||
|
Label colorLabel = Label.createLabel("" + directionalLight.getColor());
|
||||||
|
|
||||||
Div rDiv = Div.createDiv();
|
Div rDiv = Div.createDiv();
|
||||||
rDiv.setMaxHeight(50);
|
rDiv.setMaxHeight(50);
|
||||||
rDiv.setFlexDirection(YogaFlexDirection.Row);
|
rDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
rDiv.addChild(Label.createLabel("R: "));
|
rDiv.addChild(Label.createLabel("R: "));
|
||||||
rDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
rDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f color = directionalLight.getColor();
|
Vector3f color = directionalLight.getColor();
|
||||||
color.x = event.getAsFloat() * 2 - 1;
|
color.x = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setColor(color);
|
directionalLight.setColor(color);
|
||||||
|
colorLabel.setText("" + directionalLight.getColor());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(rDiv);
|
scrollable.addChild(rDiv);
|
||||||
|
|
||||||
@ -442,11 +452,11 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
gDiv.setFlexDirection(YogaFlexDirection.Row);
|
gDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
gDiv.addChild(Label.createLabel("G: "));
|
gDiv.addChild(Label.createLabel("G: "));
|
||||||
gDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
gDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f color = directionalLight.getColor();
|
Vector3f color = directionalLight.getColor();
|
||||||
color.y = event.getAsFloat() * 2 - 1;
|
color.y = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setColor(color);
|
directionalLight.setColor(color);
|
||||||
|
colorLabel.setText("" + directionalLight.getColor());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(gDiv);
|
scrollable.addChild(gDiv);
|
||||||
|
|
||||||
@ -455,13 +465,15 @@ public class MenuGeneratorsLevelEditor {
|
|||||||
bDiv.setFlexDirection(YogaFlexDirection.Row);
|
bDiv.setFlexDirection(YogaFlexDirection.Row);
|
||||||
bDiv.addChild(Label.createLabel("B: "));
|
bDiv.addChild(Label.createLabel("B: "));
|
||||||
bDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
bDiv.addChild(Slider.createSlider((ValueChangeEvent event) -> {
|
||||||
LightManager lightManager = Globals.renderingEngine.getLightManager();
|
|
||||||
DirectionalLight directionalLight = lightManager.getDirectionalLight();
|
|
||||||
Vector3f color = directionalLight.getColor();
|
Vector3f color = directionalLight.getColor();
|
||||||
color.z = event.getAsFloat() * 2 - 1;
|
color.z = event.getAsFloat() * 2 - 1;
|
||||||
directionalLight.setColor(color);
|
directionalLight.setColor(color);
|
||||||
|
colorLabel.setText("" + directionalLight.getColor());
|
||||||
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
}));
|
}));
|
||||||
scrollable.addChild(bDiv);
|
scrollable.addChild(bDiv);
|
||||||
|
|
||||||
|
scrollable.addChild(colorLabel);
|
||||||
|
|
||||||
|
|
||||||
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
Globals.engineState.signalSystem.post(SignalType.YOGA_APPLY,mainSidePanel);
|
||||||
|
|||||||
@ -83,7 +83,7 @@ public class LightManager {
|
|||||||
/**
|
/**
|
||||||
* Size of the direct light buffer
|
* Size of the direct light buffer
|
||||||
*/
|
*/
|
||||||
static final int DIRECT_LIGHT_BUFFER_SIZE = 12 + 12 + 12;
|
static final int DIRECT_LIGHT_BUFFER_SIZE = 16 + 16 + 16;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bind point for the cluster ssbo
|
* Bind point for the cluster ssbo
|
||||||
@ -140,9 +140,9 @@ public class LightManager {
|
|||||||
* The direct light ssbo
|
* The direct light ssbo
|
||||||
*
|
*
|
||||||
struct DirectLight {
|
struct DirectLight {
|
||||||
vec3 ambientColor; //12 bytes
|
vec4 direction; //16 bytes
|
||||||
vec3 direction; //12 bytes
|
vec4 color; //16 bytes
|
||||||
vec3 color; //12 bytes
|
vec4 ambientColor; //16 bytes
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
private ShaderStorageBuffer dirLightSSBO;
|
private ShaderStorageBuffer dirLightSSBO;
|
||||||
@ -155,7 +155,7 @@ public class LightManager {
|
|||||||
/**
|
/**
|
||||||
* Color of the ambient light
|
* Color of the ambient light
|
||||||
*/
|
*/
|
||||||
private Vector3f ambientLightColor = new Vector3f(0.8f);
|
private Vector3f ambientLightColor = new Vector3f(0.3f);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
@ -276,23 +276,26 @@ public class LightManager {
|
|||||||
//push direct light
|
//push direct light
|
||||||
ByteBuffer directLightBuffer = dirLightSSBO.getBuffer();
|
ByteBuffer directLightBuffer = dirLightSSBO.getBuffer();
|
||||||
{
|
{
|
||||||
//
|
|
||||||
//ambient light color
|
|
||||||
directLightBuffer.putFloat(ambientLightColor.x);
|
|
||||||
directLightBuffer.putFloat(ambientLightColor.y);
|
|
||||||
directLightBuffer.putFloat(ambientLightColor.z);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
//direction
|
//direction
|
||||||
directLightBuffer.putFloat(directionalLight.getDirection().x);
|
directLightBuffer.putFloat(directionalLight.getDirection().x);
|
||||||
directLightBuffer.putFloat(directionalLight.getDirection().y);
|
directLightBuffer.putFloat(directionalLight.getDirection().y);
|
||||||
directLightBuffer.putFloat(directionalLight.getDirection().z);
|
directLightBuffer.putFloat(directionalLight.getDirection().z);
|
||||||
|
directLightBuffer.putFloat(0);
|
||||||
|
|
||||||
//
|
//
|
||||||
//color
|
//color
|
||||||
directLightBuffer.putFloat(directionalLight.getColor().x);
|
directLightBuffer.putFloat(directionalLight.getColor().x);
|
||||||
directLightBuffer.putFloat(directionalLight.getColor().y);
|
directLightBuffer.putFloat(directionalLight.getColor().y);
|
||||||
directLightBuffer.putFloat(directionalLight.getColor().z);
|
directLightBuffer.putFloat(directionalLight.getColor().z);
|
||||||
|
directLightBuffer.putFloat(0);
|
||||||
|
|
||||||
|
//
|
||||||
|
//ambient light color
|
||||||
|
directLightBuffer.putFloat(ambientLightColor.x);
|
||||||
|
directLightBuffer.putFloat(ambientLightColor.y);
|
||||||
|
directLightBuffer.putFloat(ambientLightColor.z);
|
||||||
|
directLightBuffer.putFloat(0);
|
||||||
}
|
}
|
||||||
directLightBuffer.flip();
|
directLightBuffer.flip();
|
||||||
dirLightSSBO.upload(openGLState);
|
dirLightSSBO.upload(openGLState);
|
||||||
|
|||||||
@ -30,7 +30,7 @@ public class Material {
|
|||||||
/**
|
/**
|
||||||
* Default shininess
|
* Default shininess
|
||||||
*/
|
*/
|
||||||
public static final double DEFAULT_SHININESS = 0.0f;
|
public static final double DEFAULT_SHININESS = 1.0f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default reflectivity
|
* The default reflectivity
|
||||||
@ -350,9 +350,9 @@ public class Material {
|
|||||||
Globals.renderingEngine.checkError();
|
Globals.renderingEngine.checkError();
|
||||||
}
|
}
|
||||||
//send physical properties
|
//send physical properties
|
||||||
openGLState.getActiveShader().setUniform(openGLState, "material.shininess", this.shininess);
|
openGLState.getActiveShader().setUniform(openGLState, "material.shininess", (float)this.shininess);
|
||||||
Globals.renderingEngine.checkError();
|
Globals.renderingEngine.checkError();
|
||||||
openGLState.getActiveShader().setUniform(openGLState, "material.reflectivity", this.reflectivity);
|
openGLState.getActiveShader().setUniform(openGLState, "material.reflectivity", (float)this.reflectivity);
|
||||||
Globals.renderingEngine.checkError();
|
Globals.renderingEngine.checkError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -113,8 +113,15 @@ public class ShaderUtils {
|
|||||||
Globals.renderingEngine.checkError();
|
Globals.renderingEngine.checkError();
|
||||||
uniformMap.put(uniformLocation,(Float)value);
|
uniformMap.put(uniformLocation,(Float)value);
|
||||||
|
|
||||||
|
//
|
||||||
|
//double
|
||||||
|
} else if(value instanceof Double){
|
||||||
|
GL40.glUniform1d(uniformLocation, (Double)value);
|
||||||
|
Globals.renderingEngine.checkError();
|
||||||
|
uniformMap.put(uniformLocation,(Double)value);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedOperationException("Tried to set uniform with unsupported type!");
|
throw new UnsupportedOperationException("Tried to set uniform with unsupported type! " + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user