Some checks failed
		
		
	
	studiorailgun/Renderer/pipeline/head There was a failure building this commit
				
			
		
			
				
	
	
		
			312 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
			
		
		
	
	
			312 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
#version 450 core
 | 
						|
 | 
						|
//foliage.fs
 | 
						|
 | 
						|
/**
 | 
						|
Bind points for different SSBOs
 | 
						|
*/
 | 
						|
#define CLUSTER_SSBO_BIND_POINT 1
 | 
						|
#define POINT_LIGHT_SSBO_BIND_POINT 2
 | 
						|
#define DIRECT_LIGHT_SSBO_BIND_POINT 3
 | 
						|
 | 
						|
 | 
						|
/**
 | 
						|
Maximum number of point lights
 | 
						|
*/
 | 
						|
#define MAX_POINT_LIGHTS 512
 | 
						|
 | 
						|
/**
 | 
						|
Maximum number of lights per cluster
 | 
						|
*/
 | 
						|
#define MAX_LIGHTS_PER_CLUSTER 100
 | 
						|
 | 
						|
/**
 | 
						|
The direct global light
 | 
						|
*/
 | 
						|
struct DirectLight {
 | 
						|
    vec3 direction;
 | 
						|
    vec3 color;
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
A point light
 | 
						|
*/
 | 
						|
struct PointLight {
 | 
						|
    vec4 position;
 | 
						|
    vec4 color;
 | 
						|
    float constant;
 | 
						|
    float linear;
 | 
						|
    float quadratic;
 | 
						|
    float radius;
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
A light cluster
 | 
						|
*/
 | 
						|
struct Cluster {
 | 
						|
    vec4 minPoint;
 | 
						|
    vec4 maxPoint;
 | 
						|
    uint count;
 | 
						|
    uint lightIndices[MAX_LIGHTS_PER_CLUSTER];
 | 
						|
};
 | 
						|
 | 
						|
out vec4 FragColor;
 | 
						|
 | 
						|
 | 
						|
layout(std430, binding = CLUSTER_SSBO_BIND_POINT) restrict buffer clusterGridSSBO {
 | 
						|
    Cluster clusters[];
 | 
						|
};
 | 
						|
 | 
						|
layout(std430, binding = POINT_LIGHT_SSBO_BIND_POINT) restrict buffer pointLightSSBO {
 | 
						|
    PointLight pointLight[];
 | 
						|
};
 | 
						|
 | 
						|
layout(std430, binding = DIRECT_LIGHT_SSBO_BIND_POINT) restrict buffer dirLightSSBO {
 | 
						|
    DirectLight directLight;
 | 
						|
};
 | 
						|
 | 
						|
struct Material {
 | 
						|
    sampler2D diffuse;
 | 
						|
    sampler2D specular;
 | 
						|
    float shininess;
 | 
						|
}; 
 | 
						|
 | 
						|
in vec3 FragPos;
 | 
						|
in vec3 ViewFragPos;
 | 
						|
in vec3 Normal;
 | 
						|
in vec2 TexCoord;
 | 
						|
in vec4 FragPosLightSpace;
 | 
						|
in vec3 normalRot1;
 | 
						|
in vec3 normalRot2;
 | 
						|
 | 
						|
 | 
						|
uniform vec3 viewPos;
 | 
						|
// uniform DirLight dirLight;
 | 
						|
// uniform PointLight pointLights[NR_POINT_LIGHTS];
 | 
						|
// uniform SpotLight spotLight;
 | 
						|
uniform Material material;
 | 
						|
 | 
						|
//texture stuff
 | 
						|
// uniform sampler2D ourTexture;
 | 
						|
uniform int hasTransparency;
 | 
						|
// uniform sampler2D specularTexture;
 | 
						|
 | 
						|
//light depth map
 | 
						|
uniform sampler2D shadowMap;
 | 
						|
 | 
						|
/**
 | 
						|
Used for light cluster calculation
 | 
						|
*/
 | 
						|
uniform float zNear;
 | 
						|
uniform float zFar;
 | 
						|
uniform uvec3 gridSize;
 | 
						|
uniform uvec2 screenDimensions;
 | 
						|
uniform mat4 view;
 | 
						|
 | 
						|
 | 
						|
// function prototypes
 | 
						|
uint findCluster(vec3 FragPos, float zNear, float zFar);
 | 
						|
vec3 CalcPointLight(PointLight pointLight, vec3 normal, vec3 fragPos, vec3 viewDir);
 | 
						|
float calcLightIntensityTotal(vec3 normal);
 | 
						|
float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal);
 | 
						|
float easeIn(float interpolator);
 | 
						|
float easeOut(float interpolator);
 | 
						|
 | 
						|
void main(){
 | 
						|
 | 
						|
    //basic vars
 | 
						|
    float heightPercent = TexCoord.y;
 | 
						|
 | 
						|
    //calculate color
 | 
						|
    vec3 baseColor = vec3(0.05,0.2,0.01);
 | 
						|
    vec3 tipColor = vec3(0.25,0.45,0);
 | 
						|
    vec3 textureColor = mix(baseColor,tipColor,easeIn(heightPercent));
 | 
						|
 | 
						|
    //mix normals
 | 
						|
    float normalMix = TexCoord.x;
 | 
						|
    float normalMultiplier = -(1.0 + -2.0 * int(gl_FrontFacing));
 | 
						|
    vec3 norm = normalize(mix(normalRot1,normalRot2,normalMix) * normalMultiplier);
 | 
						|
    // vec3 norm = normalize(Normal * normalMultiplier);
 | 
						|
    vec3 viewDir = normalize(viewPos - FragPos);
 | 
						|
    
 | 
						|
    //grab light intensity
 | 
						|
    vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));
 | 
						|
 | 
						|
    //get color of base texture
 | 
						|
    // vec3 textureColor = vec3((norm.x + 1) / 2.0, norm.y, 1.0 - (norm.x + 1) / 2.0);
 | 
						|
    // vec3 textureColor =  vec3(TexCoord,1.0);
 | 
						|
    // vec3 textureColor = vec3(0.17647,0.4,0.09411);//texture(material.diffuse, TexCoord).rgb;
 | 
						|
 | 
						|
    //shadow
 | 
						|
    float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), 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 final color
 | 
						|
    vec3 finalColor = textureColor * lightIntensity;// * max(shadow,0.4);
 | 
						|
    // vec3 lightAmount = CalcDirLight(norm, viewDir);
 | 
						|
    // for(int i = 0; i < NR_POINT_LIGHTS; i++){
 | 
						|
    //    lightAmount += CalcPointLight(i, norm, FragPos, viewDir);
 | 
						|
    // }
 | 
						|
 | 
						|
    //this final calculation is for transparency
 | 
						|
    FragColor = vec4(finalColor, 1.0);//texture(ourTexture, TexCoord);//vec4(result, 1.0);
 | 
						|
}
 | 
						|
 | 
						|
// calculates the color when using a directional light.
 | 
						|
// vec3 CalcDirLight(vec3 normal, vec3 viewDir){
 | 
						|
//     vec3 lightDir = normalize(-dLDirection);
 | 
						|
//     // diffuse shading
 | 
						|
//     float diff = max(dot(normal, lightDir), 0.0);
 | 
						|
//     // specular shading
 | 
						|
//     // vec3 reflectDir = reflect(-lightDir, normal);
 | 
						|
//     // float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
 | 
						|
//     // combine results
 | 
						|
//     vec3 texColor = texture(material.diffuse, TexCoord).rgb;
 | 
						|
//     vec3 diffuse = dLDiffuse * diff;
 | 
						|
//     //vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord).rgb);
 | 
						|
 | 
						|
 | 
						|
//     float shadow = ShadowCalculation(FragPosLightSpace, lightDir, normal);
 | 
						|
 | 
						|
//     return (  dLAmbient + (1.0-shadow) * diffuse  ) * texColor;// + specular);
 | 
						|
// }
 | 
						|
 | 
						|
//
 | 
						|
float calcLightIntensityAmbient(){
 | 
						|
    //calculate average of ambient light
 | 
						|
    float avg = (directLight.color.x + directLight.color.y + directLight.color.z)/3.0;
 | 
						|
    return avg;
 | 
						|
}
 | 
						|
 | 
						|
//
 | 
						|
float calcLightIntensityDir(vec3 normal){
 | 
						|
    vec3 lightDir = normalize(-directLight.direction);
 | 
						|
    // diffuse shading
 | 
						|
    float diff = max(dot(normal, lightDir), 0.0);
 | 
						|
    
 | 
						|
    return diff;
 | 
						|
}
 | 
						|
 | 
						|
//
 | 
						|
float calcLightIntensityTotal(vec3 normal){
 | 
						|
    //ambient intensity
 | 
						|
    float ambientLightIntensity = calcLightIntensityAmbient();
 | 
						|
 | 
						|
    //get direct intensity
 | 
						|
    float directLightIntensity = calcLightIntensityDir(normal);
 | 
						|
 | 
						|
    //sum
 | 
						|
    float total = ambientLightIntensity + directLightIntensity;
 | 
						|
    return total;
 | 
						|
}
 | 
						|
 | 
						|
//
 | 
						|
vec3 getTotalLightColor(vec3 normal){
 | 
						|
    //get the direct light color adjusted for intensity
 | 
						|
    vec3 diffuseLightColor = directLight.color * calcLightIntensityDir(normal);
 | 
						|
 | 
						|
    //sum light colors
 | 
						|
    vec3 totalLightColor = diffuseLightColor;
 | 
						|
    return totalLightColor;
 | 
						|
}
 | 
						|
 | 
						|
vec3 CalcPointLight(PointLight pointLight, vec3 normal, vec3 fragPos, vec3 viewDir){
 | 
						|
    vec3 lightDir = normalize(pointLight.position.xyz - fragPos);
 | 
						|
    // diffuse shading
 | 
						|
    float diff = max(dot(normal, lightDir), 0.0);
 | 
						|
    // specular shading
 | 
						|
    // vec3 reflectDir = reflect(-lightDir, normal);
 | 
						|
    // float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
 | 
						|
    // attenuation
 | 
						|
    float distance = length(pointLight.position.xyz - fragPos);
 | 
						|
    float attenuation = 1.0 / (pointLight.constant + pointLight.linear * distance + pointLight.quadratic * (distance * distance));
 | 
						|
    if(distance > pointLight.radius){
 | 
						|
        attenuation = 0;
 | 
						|
    }
 | 
						|
    // combine results
 | 
						|
    vec3 ambient = pointLight.color.xyz;// * vec4(texture(material.diffuse, TexCoord)).xyz;
 | 
						|
    vec3 diffuse = pointLight.color.xyz * diff;// * vec4(texture(material.diffuse, TexCoord)).xyz;
 | 
						|
    // vec3 specular = pLspecular[i] * spec;// * vec4(texture(material.specular, TexCoord)).xyz;
 | 
						|
    ambient = ambient * attenuation;
 | 
						|
    diffuse = diffuse * attenuation;
 | 
						|
    // specular *= attenuation;
 | 
						|
    vec3 specular = vec3(0,0,0);
 | 
						|
 | 
						|
    vec3 finalValue = vec3(0);
 | 
						|
    if(distance < pointLight.radius){
 | 
						|
        finalValue = (ambient + diffuse + specular);
 | 
						|
        finalValue = vec3(max(finalValue.x,0),max(finalValue.y,0),max(finalValue.z,0));
 | 
						|
    }
 | 
						|
 | 
						|
    return finalValue;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
Finds the light cluster this fragment belongs to
 | 
						|
*/
 | 
						|
uint findCluster(vec3 viewspaceFragPos, float zNear, float zFar){
 | 
						|
    uint zTile = uint((log(abs(viewspaceFragPos.z) / zNear) * gridSize.z) / log(zFar / zNear));
 | 
						|
    vec2 tileSize = screenDimensions / gridSize.xy;
 | 
						|
    uvec3 tile = uvec3(gl_FragCoord.xy / tileSize, zTile);
 | 
						|
    return tile.x + (tile.y * gridSize.x) + (tile.z * gridSize.x * gridSize.y);
 | 
						|
}
 | 
						|
 | 
						|
float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal){
 | 
						|
 | 
						|
    // perform perspective divide
 | 
						|
    vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
 | 
						|
 | 
						|
    //transform to NDC
 | 
						|
    projCoords = projCoords * 0.5 + 0.5;
 | 
						|
 | 
						|
    //get closest depth from light's POV
 | 
						|
    float closestDepth = texture(shadowMap, projCoords.xy).r;
 | 
						|
 | 
						|
    //get depth of current fragment
 | 
						|
    float currentDepth = projCoords.z;
 | 
						|
    
 | 
						|
    //calculate bias
 | 
						|
    float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
 | 
						|
 | 
						|
    //calculate shadow value
 | 
						|
    float shadow = currentDepth - bias > closestDepth  ? 1.0 : 0.0;
 | 
						|
 | 
						|
    if(projCoords.z > 1.0){
 | 
						|
        shadow = 0.0;
 | 
						|
    }
 | 
						|
 | 
						|
    //calculate dot product, if it is >0 we know they're parallel-ish therefore should disregard the shadow mapping
 | 
						|
    //ie the fragment is already facing away from the light source
 | 
						|
    float dotprod = dot(normalize(lightDir),normalize(normal));
 | 
						|
 | 
						|
    if(dotprod > 0.0){
 | 
						|
        shadow = 0.0;
 | 
						|
    }
 | 
						|
 | 
						|
    // shadow = currentDepth;
 | 
						|
 | 
						|
    return shadow;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
float easeIn(float interpolator){
 | 
						|
    return interpolator * interpolator;
 | 
						|
}
 | 
						|
 | 
						|
float easeOut(float interpolator){
 | 
						|
    return 1 - easeIn(1 - interpolator);
 | 
						|
} |