Extract bounded additional scene lights
This commit is contained in:
@@ -21,11 +21,95 @@ bool IsUsableCamera(const Components::CameraComponent* camera) {
|
||||
camera->GetGameObject()->IsActiveInHierarchy();
|
||||
}
|
||||
|
||||
bool IsUsableLight(const Components::LightComponent* light) {
|
||||
return light != nullptr &&
|
||||
light->IsEnabled() &&
|
||||
light->GetGameObject() != nullptr &&
|
||||
light->GetGameObject()->IsActiveInHierarchy();
|
||||
RenderLightType ToRenderLightType(Components::LightType lightType) {
|
||||
switch (lightType) {
|
||||
case Components::LightType::Directional:
|
||||
return RenderLightType::Directional;
|
||||
case Components::LightType::Point:
|
||||
return RenderLightType::Point;
|
||||
case Components::LightType::Spot:
|
||||
return RenderLightType::Spot;
|
||||
}
|
||||
|
||||
return RenderLightType::Point;
|
||||
}
|
||||
|
||||
RenderDirectionalLightData BuildDirectionalLightData(
|
||||
const Components::LightComponent& light) {
|
||||
RenderDirectionalLightData lightData;
|
||||
lightData.enabled = true;
|
||||
lightData.castsShadows = light.GetCastsShadows();
|
||||
lightData.intensity = light.GetIntensity();
|
||||
lightData.color = light.GetColor();
|
||||
lightData.direction = BuildRenderLightDirection(light);
|
||||
return lightData;
|
||||
}
|
||||
|
||||
RenderAdditionalLightData BuildAdditionalLightData(
|
||||
const Components::LightComponent& light) {
|
||||
RenderAdditionalLightData lightData;
|
||||
lightData.type = ToRenderLightType(light.GetLightType());
|
||||
lightData.enabled = true;
|
||||
lightData.castsShadows = light.GetCastsShadows();
|
||||
lightData.color = light.GetColor();
|
||||
lightData.intensity = light.GetIntensity();
|
||||
|
||||
switch (light.GetLightType()) {
|
||||
case Components::LightType::Directional:
|
||||
lightData.direction = BuildRenderLightDirection(light);
|
||||
break;
|
||||
case Components::LightType::Point:
|
||||
lightData.position = light.transform().GetPosition();
|
||||
lightData.range = light.GetRange();
|
||||
break;
|
||||
case Components::LightType::Spot:
|
||||
lightData.position = light.transform().GetPosition();
|
||||
lightData.direction = BuildRenderLightDirection(light);
|
||||
lightData.range = light.GetRange();
|
||||
lightData.spotAngle = light.GetSpotAngle();
|
||||
break;
|
||||
}
|
||||
|
||||
return lightData;
|
||||
}
|
||||
|
||||
float ComputeAdditionalLightInfluence(
|
||||
const Components::LightComponent& light,
|
||||
const Math::Vector3& cameraPosition) {
|
||||
if (light.GetIntensity() <= Math::EPSILON) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
if (light.GetLightType() == Components::LightType::Directional) {
|
||||
return light.GetIntensity();
|
||||
}
|
||||
|
||||
const float range = std::max(light.GetRange(), 0.001f);
|
||||
const float rangeSq = range * range;
|
||||
const Math::Vector3 lightPosition = light.transform().GetPosition();
|
||||
const float distanceSq = (lightPosition - cameraPosition).SqrMagnitude();
|
||||
if (distanceSq >= rangeSq) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
return light.GetIntensity() / std::max(distanceSq, Math::EPSILON);
|
||||
}
|
||||
|
||||
uint32_t GetAdditionalLightGroupRank(const Components::LightComponent& light) {
|
||||
return light.GetLightType() == Components::LightType::Directional ? 0u : 1u;
|
||||
}
|
||||
|
||||
uint32_t GetAdditionalLightTypeRank(const Components::LightComponent& light) {
|
||||
switch (light.GetLightType()) {
|
||||
case Components::LightType::Directional:
|
||||
return 0u;
|
||||
case Components::LightType::Point:
|
||||
return 1u;
|
||||
case Components::LightType::Spot:
|
||||
return 2u;
|
||||
}
|
||||
|
||||
return 3u;
|
||||
}
|
||||
|
||||
bool CompareVisibleItems(const VisibleRenderItem& lhs, const VisibleRenderItem& rhs) {
|
||||
@@ -73,7 +157,7 @@ RenderSceneData RenderSceneExtractor::Extract(
|
||||
sceneData.visibleItems.begin(),
|
||||
sceneData.visibleItems.end(),
|
||||
CompareVisibleItems);
|
||||
ExtractLighting(scene, sceneData.lighting);
|
||||
ExtractLighting(scene, cameraPosition, cullingMask, sceneData.lighting);
|
||||
|
||||
return sceneData;
|
||||
}
|
||||
@@ -102,7 +186,7 @@ RenderSceneData RenderSceneExtractor::ExtractForCamera(
|
||||
sceneData.visibleItems.begin(),
|
||||
sceneData.visibleItems.end(),
|
||||
CompareVisibleItems);
|
||||
ExtractLighting(scene, sceneData.lighting);
|
||||
ExtractLighting(scene, cameraPosition, cullingMask, sceneData.lighting);
|
||||
|
||||
return sceneData;
|
||||
}
|
||||
@@ -144,43 +228,73 @@ Components::CameraComponent* RenderSceneExtractor::SelectCamera(
|
||||
|
||||
void RenderSceneExtractor::ExtractLighting(
|
||||
const Components::Scene& scene,
|
||||
const Math::Vector3& cameraPosition,
|
||||
uint32_t cullingMask,
|
||||
RenderLightingData& lightingData) const {
|
||||
lightingData = {};
|
||||
|
||||
const std::vector<Components::LightComponent*> lights =
|
||||
scene.FindObjectsOfType<Components::LightComponent>();
|
||||
|
||||
Components::LightComponent* mainDirectionalLight = nullptr;
|
||||
Components::LightComponent* mainDirectionalLight =
|
||||
FindMainDirectionalLight(scene, cullingMask);
|
||||
if (mainDirectionalLight != nullptr) {
|
||||
lightingData.mainDirectionalLight = BuildDirectionalLightData(*mainDirectionalLight);
|
||||
}
|
||||
|
||||
struct AdditionalLightCandidate {
|
||||
RenderAdditionalLightData data = {};
|
||||
float influence = 0.0f;
|
||||
uint32_t groupRank = 0u;
|
||||
uint32_t typeRank = 0u;
|
||||
Components::GameObject::ID gameObjectId = Components::GameObject::INVALID_ID;
|
||||
};
|
||||
|
||||
std::vector<AdditionalLightCandidate> candidates;
|
||||
candidates.reserve(lights.size());
|
||||
for (Components::LightComponent* light : lights) {
|
||||
if (!IsUsableLight(light) ||
|
||||
light->GetLightType() != Components::LightType::Directional) {
|
||||
if (light == nullptr ||
|
||||
!IsLightVisibleForCullingMask(*light, cullingMask) ||
|
||||
light == mainDirectionalLight) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mainDirectionalLight == nullptr ||
|
||||
light->GetIntensity() > mainDirectionalLight->GetIntensity()) {
|
||||
mainDirectionalLight = light;
|
||||
}
|
||||
AdditionalLightCandidate candidate;
|
||||
candidate.data = BuildAdditionalLightData(*light);
|
||||
candidate.influence = ComputeAdditionalLightInfluence(*light, cameraPosition);
|
||||
candidate.groupRank = GetAdditionalLightGroupRank(*light);
|
||||
candidate.typeRank = GetAdditionalLightTypeRank(*light);
|
||||
candidate.gameObjectId = light->GetGameObject() != nullptr
|
||||
? light->GetGameObject()->GetID()
|
||||
: Components::GameObject::INVALID_ID;
|
||||
candidates.push_back(candidate);
|
||||
}
|
||||
|
||||
if (mainDirectionalLight == nullptr) {
|
||||
lightingData = {};
|
||||
return;
|
||||
std::sort(
|
||||
candidates.begin(),
|
||||
candidates.end(),
|
||||
[](const AdditionalLightCandidate& lhs, const AdditionalLightCandidate& rhs) {
|
||||
if (lhs.groupRank != rhs.groupRank) {
|
||||
return lhs.groupRank < rhs.groupRank;
|
||||
}
|
||||
|
||||
if (lhs.influence != rhs.influence) {
|
||||
return lhs.influence > rhs.influence;
|
||||
}
|
||||
|
||||
if (lhs.typeRank != rhs.typeRank) {
|
||||
return lhs.typeRank < rhs.typeRank;
|
||||
}
|
||||
|
||||
return lhs.gameObjectId < rhs.gameObjectId;
|
||||
});
|
||||
|
||||
const uint32_t additionalLightCount = static_cast<uint32_t>(
|
||||
std::min(candidates.size(), static_cast<size_t>(RenderLightingData::kMaxAdditionalLightCount)));
|
||||
lightingData.additionalLightCount = additionalLightCount;
|
||||
for (uint32_t index = 0; index < additionalLightCount; ++index) {
|
||||
lightingData.additionalLights[index] = candidates[index].data;
|
||||
}
|
||||
|
||||
RenderDirectionalLightData lightData;
|
||||
lightData.enabled = true;
|
||||
lightData.castsShadows = mainDirectionalLight->GetCastsShadows();
|
||||
lightData.intensity = mainDirectionalLight->GetIntensity();
|
||||
lightData.color = mainDirectionalLight->GetColor();
|
||||
|
||||
Math::Vector3 direction = mainDirectionalLight->transform().GetForward() * -1.0f;
|
||||
if (direction.SqrMagnitude() <= Math::EPSILON) {
|
||||
direction = Math::Vector3::Back();
|
||||
} else {
|
||||
direction = direction.Normalized();
|
||||
}
|
||||
lightData.direction = direction;
|
||||
|
||||
lightingData.mainDirectionalLight = lightData;
|
||||
}
|
||||
|
||||
void RenderSceneExtractor::ExtractVisibleItems(
|
||||
|
||||
Reference in New Issue
Block a user