-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic lights (directional, point, spot) (#806)
- Loading branch information
1 parent
53dc1c2
commit e3cc906
Showing
58 changed files
with
706 additions
and
101 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
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
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
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,35 @@ | ||
/** | ||
* @file DirectionalLight.h | ||
* @copyright Jaremie Romer and McCallister Romer 2024 | ||
*/ | ||
#pragma once | ||
|
||
#include "ncengine/ecs/Component.h" | ||
|
||
#include "ncmath/Vector.h" | ||
|
||
namespace nc::graphics | ||
{ | ||
/** @brief Component representing an infinite, directional light, like the sun. | ||
* Not implementing shadowing currently due to this type of light's proclivity for artifacts. */ | ||
struct DirectionalLight | ||
{ | ||
explicit DirectionalLight(const Vector3& color_ = Vector3::One()) noexcept | ||
: color{color_} | ||
{ | ||
} | ||
|
||
Vector3 color; | ||
}; | ||
} // namespace nc::graphics | ||
|
||
namespace nc | ||
{ | ||
template<> | ||
struct StoragePolicy<graphics::DirectionalLight> : DefaultStoragePolicy | ||
{ | ||
static constexpr bool EnableOnAddCallbacks = false; | ||
static constexpr bool EnableOnCommitCallbacks = true; | ||
static constexpr bool EnableOnRemoveCallbacks = true; | ||
}; | ||
} // namespace nc |
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
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
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
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,107 @@ | ||
struct DirectionalLightData | ||
{ | ||
float3 color; | ||
float padding; | ||
float3 direction; | ||
float padding2; | ||
}; | ||
|
||
struct PointLightData | ||
{ | ||
float4x4 viewProj; | ||
float3 position; | ||
int castsShadows; | ||
float3 color; | ||
float radius; | ||
}; | ||
|
||
struct SpotLightData | ||
{ | ||
float4x4 viewProj; | ||
float3 position; | ||
int castsShadows; | ||
float3 color; | ||
float innerAngle; | ||
float3 direction; | ||
float outerAngle; | ||
float3 padding; | ||
float radius; | ||
}; | ||
|
||
struct LightInfluence | ||
{ | ||
float3 color; | ||
float specularAmt; | ||
float diffuseAmt; | ||
}; | ||
|
||
LightInfluence DirectionalLightRadiance(DirectionalLightData light, float3 fragWorldPos, float3 cameraPosition, float3 normal) | ||
{ | ||
// Diffuse | ||
float3 lightVec = normalize(-light.direction); // Vector from light to fragment | ||
float normalDotLightVec = saturate(dot(normal, lightVec)); // Light influence is proportional to the angle the light hits the fragment | ||
float diffuseTotal = normalDotLightVec; | ||
|
||
// Specular | ||
float3 cameraVec = normalize(cameraPosition - fragWorldPos); // Vector from camera to fragment | ||
float3 reflectVec = reflect(-lightVec, normal); // Vector of reflected light to normal | ||
float specular = pow(saturate(dot(cameraVec, reflectVec)), 32); | ||
float specularTotal = specular * 0.5f; | ||
|
||
LightInfluence lightInfluence = {light.color, specularTotal, diffuseTotal}; | ||
return lightInfluence; | ||
} | ||
|
||
LightInfluence PointLightRadiance(PointLightData light, float3 fragWorldPos, float3 cameraPosition, float3 normal) | ||
{ | ||
// Diffuse | ||
float3 lightVec = normalize(light.position - fragWorldPos); // Vector from light to fragment | ||
float normalDotLightVec = saturate(dot(normal, lightVec)); // Light influence is proportional to the angle the light hits the fragment | ||
float diffuseTotal = normalDotLightVec; | ||
|
||
// Specular | ||
float3 cameraVec = normalize(cameraPosition - fragWorldPos); // Vector from camera to fragment | ||
float3 reflectVec = reflect(-lightVec, normal); // Vector of reflected light to normal | ||
float specular = pow(saturate(dot(cameraVec, reflectVec)), 32); | ||
float specularTotal = specular * 0.5f; | ||
|
||
// Attenuation | ||
float distance = length(light.position - fragWorldPos); | ||
float attenuation = saturate(1.0 / pow(max(distance, 0.01), 4.0f)) * pow(light.radius, 3); | ||
diffuseTotal *= attenuation; | ||
specularTotal *= attenuation; | ||
|
||
LightInfluence lightInfluence = {light.color, specularTotal, diffuseTotal}; | ||
return lightInfluence; | ||
} | ||
|
||
LightInfluence SpotLightRadiance(SpotLightData light, float3 fragWorldPos, float3 cameraPosition, float3 normal) | ||
{ | ||
// Diffuse | ||
float3 lightVec = normalize(light.position - fragWorldPos); // Vector from light to fragment | ||
float normalDotLightVec = saturate(dot(normal, lightVec)); // Light influence is proportional to the angle the light hits the fragment | ||
float diffuseTotal = normalDotLightVec; | ||
|
||
// Specular | ||
float3 cameraVec = normalize(cameraPosition - fragWorldPos); // Vector from camera to fragment | ||
float3 reflectVec = reflect(-lightVec, normal); // Vector of reflected light to normal | ||
float specular = pow(saturate(dot(cameraVec, reflectVec)), 32); | ||
float specularTotal = specular * 0.5f; | ||
|
||
// Spot Light Cutoff | ||
float theta = dot(lightVec, normalize(-light.direction)); | ||
float epsilon = light.outerAngle - light.innerAngle; | ||
float intensity = saturate((theta - light.innerAngle) / epsilon); | ||
diffuseTotal *= intensity; | ||
specularTotal *= intensity; | ||
|
||
// Attenuation | ||
float distance = length(light.position - fragWorldPos); | ||
float attenuation = saturate(1.0 / pow(max(distance, 0.01), 4.0f)) * pow(light.radius, 3); | ||
|
||
diffuseTotal *= attenuation; | ||
specularTotal *= attenuation; | ||
|
||
LightInfluence lightInfluence = {light.color, specularTotal, diffuseTotal}; | ||
return lightInfluence; | ||
} |
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
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
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
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
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
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
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
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
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
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.