-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added light casters: point, directional, spotlight
- Loading branch information
1 parent
b04fe80
commit 966a813
Showing
6 changed files
with
317 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Sandbox3D/assets/shaders/textured_cube_with_dir_light_caster.frag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Fragment shader for the cube with texture (LightingSandbox) | ||
#version 450 core | ||
|
||
struct Material { | ||
sampler2D diffuse; | ||
sampler2D specular; | ||
sampler2D emission; | ||
float shininess; | ||
bool enableEmission; | ||
}; | ||
uniform Material u_Material; | ||
|
||
struct Light { | ||
vec3 direction; | ||
|
||
vec3 ambient; | ||
vec3 diffuse; | ||
vec3 specular; | ||
}; | ||
uniform Light u_Light; | ||
|
||
uniform vec3 u_ViewPos; | ||
|
||
in vec3 v_FragPos; | ||
in vec3 v_Normal; | ||
in vec2 v_UV; | ||
|
||
out vec4 FragColor; | ||
|
||
void main() | ||
{ | ||
vec3 normal = normalize(v_Normal); | ||
vec3 lightDir = normalize(-u_Light.direction); // directional light | ||
|
||
vec3 viewDir = normalize(u_ViewPos - v_FragPos); | ||
vec3 reflectDir = reflect(-lightDir, normal); | ||
|
||
// ambient | ||
vec3 diffuseMap = texture(u_Material.diffuse, v_UV).rgb; | ||
vec3 ambient = u_Light.ambient * diffuseMap; | ||
|
||
// diffuse | ||
float diff = max(dot(normal, lightDir), 0.0f); | ||
vec3 diffuse = u_Light.diffuse * diff * texture(u_Material.diffuse, v_UV).rgb; | ||
|
||
// specular | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess); | ||
vec3 specularMap = texture(u_Material.specular, v_UV).rgb; | ||
vec3 specular = u_Light.specular * spec * specularMap; | ||
|
||
// emission | ||
vec3 emission = vec3(0.0f); | ||
|
||
if (u_Material.enableEmission && specularMap == vec3(0.0f)) | ||
{ | ||
emission = texture(u_Material.emission, v_UV).rgb; | ||
} | ||
|
||
// final | ||
vec3 result = ambient + diffuse + specular + emission; | ||
FragColor = vec4(result, 1.0); | ||
} |
74 changes: 74 additions & 0 deletions
74
Sandbox3D/assets/shaders/textured_cube_with_point_light_caster copy.frag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Fragment shader for the cube with texture (LightingSandbox) | ||
#version 450 core | ||
|
||
struct Material { | ||
sampler2D diffuse; | ||
sampler2D specular; | ||
sampler2D emission; | ||
float shininess; | ||
bool enableEmission; | ||
}; | ||
uniform Material u_Material; | ||
|
||
struct Light { | ||
vec3 position; | ||
|
||
vec3 ambient; | ||
vec3 diffuse; | ||
vec3 specular; | ||
|
||
float constant; | ||
float linear; | ||
float quadratic; | ||
}; | ||
uniform Light u_Light; | ||
|
||
uniform vec3 u_ViewPos; | ||
|
||
in vec3 v_FragPos; | ||
in vec3 v_Normal; | ||
in vec2 v_UV; | ||
|
||
out vec4 FragColor; | ||
|
||
void main() | ||
{ | ||
vec3 normal = normalize(v_Normal); | ||
vec3 lightDir = normalize(u_Light.position - v_FragPos); | ||
|
||
vec3 viewDir = normalize(u_ViewPos - v_FragPos); | ||
vec3 reflectDir = reflect(-lightDir, normal); | ||
|
||
// ambient | ||
vec3 diffuseMap = texture(u_Material.diffuse, v_UV).rgb; | ||
vec3 ambient = u_Light.ambient * diffuseMap; | ||
|
||
// diffuse | ||
float diff = max(dot(normal, lightDir), 0.0f); | ||
vec3 diffuse = u_Light.diffuse * diff * texture(u_Material.diffuse, v_UV).rgb; | ||
|
||
// specular | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess); | ||
vec3 specularMap = texture(u_Material.specular, v_UV).rgb; | ||
vec3 specular = u_Light.specular * spec * specularMap; | ||
|
||
// emission | ||
vec3 emission = vec3(0.0f); | ||
|
||
if (u_Material.enableEmission && specularMap == vec3(0.0f)) | ||
{ | ||
emission = texture(u_Material.emission, v_UV).rgb; | ||
} | ||
|
||
// attenuation | ||
float distance = length(u_Light.position - v_FragPos); | ||
float attenuation = 1.0 / (u_Light.constant + u_Light.linear * distance + u_Light.quadratic * (distance * distance)); | ||
|
||
ambient *= attenuation; | ||
diffuse *= attenuation; | ||
specular *= attenuation; | ||
|
||
// final | ||
vec3 result = ambient + diffuse + specular + emission; | ||
FragColor = vec4(result, 1.0); | ||
} |
83 changes: 83 additions & 0 deletions
83
Sandbox3D/assets/shaders/textured_cube_with_spotlight_caster.frag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Fragment shader for the cube with texture (LightingSandbox) | ||
#version 450 core | ||
|
||
struct Material { | ||
sampler2D diffuse; | ||
sampler2D specular; | ||
sampler2D emission; | ||
float shininess; | ||
bool enableEmission; | ||
}; | ||
uniform Material u_Material; | ||
|
||
struct Light { | ||
vec3 position; | ||
vec3 direction; | ||
|
||
vec3 ambient; | ||
vec3 diffuse; | ||
vec3 specular; | ||
|
||
float constant; | ||
float linear; | ||
float quadratic; | ||
float cutOff; // cos of the angle | ||
float outerCutOff; | ||
}; | ||
uniform Light u_Light; | ||
|
||
uniform vec3 u_ViewPos; | ||
|
||
in vec3 v_FragPos; | ||
in vec3 v_Normal; | ||
in vec2 v_UV; | ||
|
||
out vec4 FragColor; | ||
|
||
void main() | ||
{ | ||
vec3 normal = normalize(v_Normal); | ||
vec3 lightDir = normalize(u_Light.position - v_FragPos); | ||
|
||
vec3 viewDir = normalize(u_ViewPos - v_FragPos); | ||
vec3 reflectDir = reflect(-lightDir, normal); | ||
|
||
// ambient | ||
vec3 diffuseMap = texture(u_Material.diffuse, v_UV).rgb; | ||
vec3 ambient = u_Light.ambient * diffuseMap; | ||
|
||
// diffuse | ||
float diff = max(dot(normal, lightDir), 0.0f); | ||
vec3 diffuse = u_Light.diffuse * diff * texture(u_Material.diffuse, v_UV).rgb; | ||
|
||
// specular | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess); | ||
vec3 specularMap = texture(u_Material.specular, v_UV).rgb; | ||
vec3 specular = u_Light.specular * spec * specularMap; | ||
|
||
// emission | ||
vec3 emission = vec3(0.0f); | ||
|
||
if (u_Material.enableEmission && specularMap == vec3(0.0f)) | ||
{ | ||
emission = texture(u_Material.emission, v_UV).rgb; | ||
} | ||
|
||
// spotlight with soft edges | ||
float theta = dot(lightDir, normalize(-u_Light.direction)); | ||
float epsilon = u_Light.cutOff - u_Light.outerCutOff; | ||
float intensity = clamp((theta - u_Light.outerCutOff) / epsilon, 0.0, 1.0); | ||
diffuse *= intensity; | ||
specular *= intensity; | ||
|
||
// attenuation | ||
float distance = length(u_Light.position - v_FragPos); | ||
float attenuation = 1.0 / (u_Light.constant + u_Light.linear * distance + u_Light.quadratic * (distance * distance)); | ||
ambient *= attenuation; | ||
diffuse *= attenuation; | ||
specular *= attenuation; | ||
|
||
// final | ||
vec3 result = ambient + diffuse + specular + emission; | ||
FragColor = vec4(result, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.