fixed light flickering, and also forgot a thing in my separate script for reference lmfao.
This commit is contained in:
@@ -27,6 +27,18 @@ ControllerState& getState(int id) {
|
|||||||
return g_states[id];
|
return g_states[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void bindSettings(ScriptContext& ctx) {
|
||||||
|
ctx.AutoSetting("moveTuning", moveTuning);
|
||||||
|
ctx.AutoSetting("lookTuning", lookTuning);
|
||||||
|
ctx.AutoSetting("capsuleTuning", capsuleTuning);
|
||||||
|
ctx.AutoSetting("gravityTuning", gravityTuning);
|
||||||
|
ctx.AutoSetting("enableMouseLook", enableMouseLook);
|
||||||
|
ctx.AutoSetting("requireMouseButton", requireMouseButton);
|
||||||
|
ctx.AutoSetting("enforceCollider", enforceCollider);
|
||||||
|
ctx.AutoSetting("enforceRigidbody", enforceRigidbody);
|
||||||
|
ctx.AutoSetting("showDebug", showDebug);
|
||||||
|
}
|
||||||
|
|
||||||
void ensureComponents(ScriptContext& ctx, float height, float radius) {
|
void ensureComponents(ScriptContext& ctx, float height, float radius) {
|
||||||
if (!ctx.object) return;
|
if (!ctx.object) return;
|
||||||
if (enforceCollider) {
|
if (enforceCollider) {
|
||||||
@@ -46,15 +58,7 @@ void ensureComponents(ScriptContext& ctx, float height, float radius) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
extern "C" void Script_OnInspector(ScriptContext& ctx) {
|
extern "C" void Script_OnInspector(ScriptContext& ctx) {
|
||||||
ctx.AutoSetting("moveTuning", moveTuning);
|
bindSettings(ctx);
|
||||||
ctx.AutoSetting("lookTuning", lookTuning);
|
|
||||||
ctx.AutoSetting("capsuleTuning", capsuleTuning);
|
|
||||||
ctx.AutoSetting("gravityTuning", gravityTuning);
|
|
||||||
ctx.AutoSetting("enableMouseLook", enableMouseLook);
|
|
||||||
ctx.AutoSetting("requireMouseButton", requireMouseButton);
|
|
||||||
ctx.AutoSetting("enforceCollider", enforceCollider);
|
|
||||||
ctx.AutoSetting("enforceRigidbody", enforceRigidbody);
|
|
||||||
ctx.AutoSetting("showDebug", showDebug);
|
|
||||||
|
|
||||||
ImGui::TextUnformatted("Standalone Movement Controller");
|
ImGui::TextUnformatted("Standalone Movement Controller");
|
||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
@@ -77,6 +81,7 @@ extern "C" void Script_OnInspector(ScriptContext& ctx) {
|
|||||||
|
|
||||||
void Begin(ScriptContext& ctx, float /*deltaTime*/) {
|
void Begin(ScriptContext& ctx, float /*deltaTime*/) {
|
||||||
if (!ctx.object) return;
|
if (!ctx.object) return;
|
||||||
|
bindSettings(ctx);
|
||||||
ControllerState& state = getState(ctx.object->id);
|
ControllerState& state = getState(ctx.object->id);
|
||||||
if (!state.initialized) {
|
if (!state.initialized) {
|
||||||
state.pitch = ctx.object->rotation.x;
|
state.pitch = ctx.object->rotation.x;
|
||||||
|
|||||||
@@ -1097,71 +1097,86 @@ void Renderer::renderSceneInternal(const Camera& camera, const std::vector<Scene
|
|||||||
return f;
|
return f;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr size_t kMaxLights = 10;
|
||||||
std::vector<LightUniform> lights;
|
std::vector<LightUniform> lights;
|
||||||
lights.reserve(10);
|
lights.reserve(kMaxLights);
|
||||||
|
|
||||||
|
struct LightCandidate {
|
||||||
|
LightUniform light;
|
||||||
|
float distSq = 0.0f;
|
||||||
|
int id = 0;
|
||||||
|
};
|
||||||
|
std::vector<LightCandidate> candidates;
|
||||||
|
candidates.reserve(sceneObjects.size());
|
||||||
|
|
||||||
for (const auto& obj : sceneObjects) {
|
for (const auto& obj : sceneObjects) {
|
||||||
if (!obj.enabled) continue;
|
if (!obj.enabled || !obj.light.enabled) continue;
|
||||||
if (obj.light.enabled && obj.type == ObjectType::DirectionalLight) {
|
if (obj.type == ObjectType::DirectionalLight) {
|
||||||
LightUniform l;
|
LightUniform l;
|
||||||
l.type = 0;
|
l.type = 0;
|
||||||
l.dir = forwardFromRotation(obj);
|
l.dir = forwardFromRotation(obj);
|
||||||
l.color = obj.light.color;
|
l.color = obj.light.color;
|
||||||
l.intensity = obj.light.intensity;
|
l.intensity = obj.light.intensity;
|
||||||
lights.push_back(l);
|
lights.push_back(l);
|
||||||
if (lights.size() >= 10) break;
|
if (lights.size() >= kMaxLights) break;
|
||||||
|
} else if (obj.type == ObjectType::SpotLight) {
|
||||||
|
LightUniform l;
|
||||||
|
l.type = 2;
|
||||||
|
l.pos = obj.position;
|
||||||
|
l.dir = forwardFromRotation(obj);
|
||||||
|
l.color = obj.light.color;
|
||||||
|
l.intensity = obj.light.intensity;
|
||||||
|
l.range = obj.light.range;
|
||||||
|
l.inner = glm::cos(glm::radians(obj.light.innerAngle));
|
||||||
|
l.outer = glm::cos(glm::radians(obj.light.outerAngle));
|
||||||
|
LightCandidate c;
|
||||||
|
c.light = l;
|
||||||
|
glm::vec3 delta = obj.position - camera.position;
|
||||||
|
c.distSq = glm::dot(delta, delta);
|
||||||
|
c.id = obj.id;
|
||||||
|
candidates.push_back(c);
|
||||||
|
} else if (obj.type == ObjectType::PointLight) {
|
||||||
|
LightUniform l;
|
||||||
|
l.type = 1;
|
||||||
|
l.pos = obj.position;
|
||||||
|
l.color = obj.light.color;
|
||||||
|
l.intensity = obj.light.intensity;
|
||||||
|
l.range = obj.light.range;
|
||||||
|
LightCandidate c;
|
||||||
|
c.light = l;
|
||||||
|
glm::vec3 delta = obj.position - camera.position;
|
||||||
|
c.distSq = glm::dot(delta, delta);
|
||||||
|
c.id = obj.id;
|
||||||
|
candidates.push_back(c);
|
||||||
|
} else if (obj.type == ObjectType::AreaLight) {
|
||||||
|
LightUniform l;
|
||||||
|
l.type = 3; // area
|
||||||
|
l.pos = obj.position;
|
||||||
|
l.dir = forwardFromRotation(obj); // plane normal
|
||||||
|
l.color = obj.light.color;
|
||||||
|
l.intensity = obj.light.intensity;
|
||||||
|
float sizeHint = glm::max(obj.light.size.x, obj.light.size.y);
|
||||||
|
l.range = (obj.light.range > 0.0f) ? obj.light.range : glm::max(sizeHint * 2.0f, 1.0f);
|
||||||
|
l.areaSize = obj.light.size;
|
||||||
|
l.areaFade = glm::clamp(obj.light.edgeFade, 0.0f, 1.0f);
|
||||||
|
LightCandidate c;
|
||||||
|
c.light = l;
|
||||||
|
glm::vec3 delta = obj.position - camera.position;
|
||||||
|
c.distSq = glm::dot(delta, delta);
|
||||||
|
c.id = obj.id;
|
||||||
|
candidates.push_back(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lights.size() < 10) {
|
|
||||||
for (const auto& obj : sceneObjects) {
|
if (lights.size() < kMaxLights && !candidates.empty()) {
|
||||||
if (!obj.enabled) continue;
|
std::sort(candidates.begin(), candidates.end(),
|
||||||
if (obj.light.enabled && obj.type == ObjectType::SpotLight) {
|
[](const LightCandidate& a, const LightCandidate& b) {
|
||||||
LightUniform l;
|
if (a.distSq != b.distSq) return a.distSq < b.distSq;
|
||||||
l.type = 2;
|
return a.id < b.id;
|
||||||
l.pos = obj.position;
|
});
|
||||||
l.dir = forwardFromRotation(obj);
|
for (const auto& c : candidates) {
|
||||||
l.color = obj.light.color;
|
if (lights.size() >= kMaxLights) break;
|
||||||
l.intensity = obj.light.intensity;
|
lights.push_back(c.light);
|
||||||
l.range = obj.light.range;
|
|
||||||
l.inner = glm::cos(glm::radians(obj.light.innerAngle));
|
|
||||||
l.outer = glm::cos(glm::radians(obj.light.outerAngle));
|
|
||||||
lights.push_back(l);
|
|
||||||
if (lights.size() >= 10) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lights.size() < 10) {
|
|
||||||
for (const auto& obj : sceneObjects) {
|
|
||||||
if (!obj.enabled) continue;
|
|
||||||
if (obj.light.enabled && obj.type == ObjectType::PointLight) {
|
|
||||||
LightUniform l;
|
|
||||||
l.type = 1;
|
|
||||||
l.pos = obj.position;
|
|
||||||
l.color = obj.light.color;
|
|
||||||
l.intensity = obj.light.intensity;
|
|
||||||
l.range = obj.light.range;
|
|
||||||
lights.push_back(l);
|
|
||||||
if (lights.size() >= 10) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lights.size() < 10) {
|
|
||||||
for (const auto& obj : sceneObjects) {
|
|
||||||
if (!obj.enabled) continue;
|
|
||||||
if (obj.light.enabled && obj.type == ObjectType::AreaLight) {
|
|
||||||
LightUniform l;
|
|
||||||
l.type = 3; // area
|
|
||||||
l.pos = obj.position;
|
|
||||||
l.dir = forwardFromRotation(obj); // plane normal
|
|
||||||
l.color = obj.light.color;
|
|
||||||
l.intensity = obj.light.intensity;
|
|
||||||
float sizeHint = glm::max(obj.light.size.x, obj.light.size.y);
|
|
||||||
l.range = (obj.light.range > 0.0f) ? obj.light.range : glm::max(sizeHint * 2.0f, 1.0f);
|
|
||||||
l.areaSize = obj.light.size;
|
|
||||||
l.areaFade = glm::clamp(obj.light.edgeFade, 0.0f, 1.0f);
|
|
||||||
lights.push_back(l);
|
|
||||||
if (lights.size() >= 10) break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user