Directional Works
This commit is contained in:
@@ -89,43 +89,44 @@ void HydraDirectionalShadowSourceSystem::_UpdateDirectionalLight(HydraID entity_
|
||||
HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
|
||||
|
||||
float view_size = 20.0f;
|
||||
float view_near = -10.0f;
|
||||
float view_far = 10.0f;
|
||||
float view_near = 0.1f;
|
||||
float view_far = 20.0f;
|
||||
|
||||
float PI = 3.1415927;
|
||||
float TWO_PI = 6.2831854;
|
||||
|
||||
glm::mat4 light_rot = glm::eulerAngleXYZ(pos_comp.orientation.x, pos_comp.orientation.y, pos_comp.orientation.z);
|
||||
glm::vec3 light_dir = glm::normalize(glm::vec3(light_rot * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
|
||||
|
||||
glm::vec3 light_dir = glm::vec3(0, 0, -1);
|
||||
glm::mat3 light_rot = glm::mat3(1);
|
||||
glm::vec3 light_euler = pos_comp.orientation * glm::vec3(-1,-1,-1);
|
||||
light_rot = glm::eulerAngleXYZ(light_euler.x, light_euler.y, light_euler.z);
|
||||
|
||||
light_dir = light_rot * light_dir;
|
||||
light_dir = glm::normalize(light_dir);
|
||||
glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition();
|
||||
float light_distance = 2;
|
||||
|
||||
for (uint32_t i = 0; i < 6; i++)
|
||||
{
|
||||
glm::vec3 light_pos = camera_pos - (light_dir * light_distance);
|
||||
glm::vec3 light_pos = camera_pos - (light_dir * 2.0f);
|
||||
glm::vec3 target_pos = light_pos + light_dir;
|
||||
glm::vec3 up_vector = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
glm::mat4 light_trans = glm::translate(light_pos * glm::vec3(-1, 1, -1));
|
||||
glm::mat4 light_orient = glm::eulerAngleXYZ(light_euler.x, light_euler.y, light_euler.z);
|
||||
|
||||
glm::mat4 view = light_orient * light_trans;
|
||||
glm::mat4 proj = glm::ortho<float>(-view_size, view_size, view_size, -view_size, view_near, view_far);
|
||||
if (glm::abs(glm::dot(light_dir, up_vector)) > 0.99f)
|
||||
{
|
||||
up_vector = glm::vec3(1.0f, 0.0f, 0.0f); // Prevent gimbal lock
|
||||
}
|
||||
|
||||
shad_comp.far_plane[i] = view_far;
|
||||
shad_comp.near_plane[i] = view_near;
|
||||
shad_comp.light_space_proj[i] = proj;
|
||||
shad_comp.light_space_view[i] = view;
|
||||
glm::mat4 view = glm::lookAt(light_pos, target_pos, up_vector);
|
||||
glm::mat4 proj = glm::ortho(-view_size, view_size, -view_size, view_size, view_near, view_far);
|
||||
|
||||
view_far *= 3.0f;
|
||||
view_near = -view_far;
|
||||
view_size *= 3.0f;
|
||||
// Save data to component
|
||||
shad_comp.far_plane[i] = view_far;
|
||||
shad_comp.near_plane[i] = view_near;
|
||||
shad_comp.light_space_proj[i] = proj;
|
||||
shad_comp.light_space_view[i] = view;
|
||||
|
||||
// Scale bounds exponentially for the next cascade tier
|
||||
view_size *= 3.5f;
|
||||
view_far *= 3.5f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HydraDirectionalShadowSourceSystem::_InitialiseDirectionalLight(HydraID entity_id)
|
||||
{
|
||||
HydraEngine *engine = GetEngine();
|
||||
@@ -134,7 +135,7 @@ void HydraDirectionalShadowSourceSystem::_InitialiseDirectionalLight(HydraID ent
|
||||
HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
|
||||
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
|
||||
|
||||
light_comp.shadow_caster = 1;
|
||||
|
||||
shad_comp.entity_id = entity_id;
|
||||
shad_comp.cascade_count = 6;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user