载入中...
搜索中...
未找到
CinemaDirector.cpp
浏览该文件的文档.
2
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8#include <algorithm>
9#include <cmath>
10
11namespace eve::camera {
12namespace {
13
14Result<void> fail(const char* message, const char* where) {
16}
17
18bool finite3(const glm::vec3& v) {
19 return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z);
20}
21
22float smoothstep01(float t) {
23 t = glm::clamp(t, 0.f, 1.f);
24 return t * t * (3.f - 2.f * t);
25}
26
27} // namespace
28
30 if (!controller) return fail("controller is required", "camera.cinema.bind");
31 if (isActive()) return fail("unbind is required before rebinding an active director", "camera.cinema.bind");
32 controller_ = controller;
33 cancelMove();
34 return Result<void>::success();
35}
36
37Result<void> CinemaDirector::defineOverview(const std::string& name, float eyeX, float eyeY, float eyeZ,
38 float targetX, float targetY, float targetZ, float fov) {
39 if (name.empty()) return fail("overview name is required", "camera.cinema.defineOverview");
40 const glm::vec3 eye(eyeX, eyeY, eyeZ);
41 const glm::vec3 target(targetX, targetY, targetZ);
42 if (!finite3(eye) || !finite3(target) || !std::isfinite(fov))
43 return fail("overview pose must be finite", "camera.cinema.defineOverview");
44 if (fov <= 0.f || fov >= 180.f) return fail("overview fov must be in (0,180)", "camera.cinema.defineOverview");
45 if (glm::length(target - eye) < 1e-5f)
46 return fail("overview eye and target must differ", "camera.cinema.defineOverview");
47 for (auto& shot : overviews_) {
48 if (shot.name == name) {
49 shot.eye = eye;
50 shot.target = target;
51 shot.fov = fov;
52 return Result<void>::success();
53 }
54 }
55 overviews_.push_back(OverviewShot{name, eye, target, fov});
56 return Result<void>::success();
57}
58
59Result<void> CinemaDirector::requireBound() const {
60 if (!controller_) return fail("CinemaDirector is not bound", "camera.cinema");
61 return Result<void>::success();
62}
63
64const CinemaDirector::OverviewShot* CinemaDirector::findOverview(const std::string& name) const {
65 for (const auto& shot : overviews_)
66 if (shot.name == name) return &shot;
67 return nullptr;
68}
69
70Result<void> CinemaDirector::sampleLivePose(glm::vec3& eye, glm::vec3& target, float& fov) const {
71 auto bound = requireBound();
72 if (!bound) return bound;
73 graphics::Camera3D* cam = controller_->getCamera();
74 if (!cam) return fail("bound controller has no Camera3D", "camera.cinema.pose");
75 eye = glm::vec3(cam->getEyeX(), cam->getEyeY(), cam->getEyeZ());
76 target = glm::vec3(cam->getTargetX(), cam->getTargetY(), cam->getTargetZ());
77 fov = cam->getFov();
78 if (!finite3(eye) || !finite3(target) || !std::isfinite(fov))
79 return fail("live camera pose is not finite", "camera.cinema.pose");
80 return Result<void>::success();
81}
82
83Result<void> CinemaDirector::writeActivePose(const glm::vec3& eye, const glm::vec3& target, float fov) {
84 if (stack_.empty()) return fail("no active overview to write", "camera.cinema.writePose");
85 const std::string& name = stack_.back().overview;
86 auto written = controller_->setView(name, eye.x, eye.y, eye.z, target.x, target.y, target.z, fov);
87 if (!written) return written;
88 // Keep the active cinematic index on this view without restarting a blend.
89 if (!controller_->switchTo(name, 0.f))
90 return fail("active overview view is missing", "camera.cinema.writePose");
91 // Film moves author the pose each frame; snap so damping does not lag the track.
92 controller_->snap();
93 return Result<void>::success();
94}
95
96Result<void> CinemaDirector::applyOverviewPose(const OverviewShot& shot, float blendTime) {
97 auto written =
98 controller_->setView(shot.name, shot.eye.x, shot.eye.y, shot.eye.z, shot.target.x, shot.target.y,
99 shot.target.z, shot.fov);
100 if (!written) return written;
101 controller_->setMode("cinematic");
102 controller_->stopSequence();
103 controller_->setFov(shot.fov);
104 if (!controller_->switchTo(shot.name, blendTime))
105 return fail("failed to switch to overview view", "camera.cinema.pushOverview");
106 if (blendTime <= 0.f) controller_->snap();
107 return Result<void>::success();
108}
109
110Result<void> CinemaDirector::pushOverview(const std::string& name, float blendTime) {
111 auto bound = requireBound();
112 if (!bound) return bound;
113 if (!std::isfinite(blendTime) || blendTime < 0.f)
114 return fail("blendTime must be finite and >= 0", "camera.cinema.pushOverview");
115 const OverviewShot* shot = findOverview(name);
116 if (!shot) return fail("overview is not defined", "camera.cinema.pushOverview");
117
118 StackEntry entry;
119 entry.overview = name;
120 if (stack_.empty()) {
121 entry.gameplay = controller_->captureFraming();
122 entry.ownsGameplay = true;
123 if (controller_->isTimelinePlaying()) controller_->pauseTimeline();
124 controller_->setDirectorEnabled(false);
125 controller_->stopSequence();
126 }
127 cancelMove();
128 stack_.push_back(entry);
129 return applyOverviewPose(*shot, blendTime);
130}
131
132void CinemaDirector::cancelMove() { move_ = MoveState{}; }
133
134Result<void> CinemaDirector::beginMove(MoveKind kind, float duration, float param) {
135 auto bound = requireBound();
136 if (!bound) return bound;
137 if (!isActive()) return fail("push an overview before playing a film move", "camera.cinema.move");
138 if (!std::isfinite(duration) || duration <= 0.f)
139 return fail("move duration must be finite and > 0", "camera.cinema.move");
140 if (!std::isfinite(param)) return fail("move parameter must be finite", "camera.cinema.move");
141
142 glm::vec3 eye, target;
143 float fov = 60.f;
144 auto sampled = sampleLivePose(eye, target, fov);
145 if (!sampled) return sampled;
146
147 MoveState next;
148 next.active = true;
149 next.kind = kind;
150 next.duration = duration;
151 next.param = param;
152 next.fromEye = eye;
153 next.fromTarget = target;
154 next.fromFov = fov;
155 next.toTarget = target;
156 next.toFov = fov;
157
158 const glm::vec3 forward = target - eye;
159 const float length = glm::length(forward);
160 if (length < 1e-5f) return fail("camera look direction is degenerate", "camera.cinema.move");
161 const glm::vec3 dir = forward / length;
162
163 if (kind == MoveKind::Dolly) {
164 next.toEye = eye + dir * param;
165 if (glm::length(target - next.toEye) < 1e-4f)
166 return fail("dolly would collapse eye onto target", "camera.cinema.dolly");
167 } else if (kind == MoveKind::Crane) {
168 next.toEye = eye + glm::vec3(0.f, param, 0.f);
169 } else if (kind == MoveKind::OrbitReveal) {
170 next.orbitCenter = target;
171 const glm::vec3 rel = eye - target;
172 next.orbitRadius = glm::length(glm::vec2(rel.x, rel.z));
173 if (next.orbitRadius < 1e-4f) return fail("orbit reveal needs horizontal radius", "camera.cinema.orbitReveal");
174 next.orbitElevation = rel.y;
175 next.orbitAzimuth0 = std::atan2(rel.x, rel.z);
176 const float endAz = next.orbitAzimuth0 + glm::radians(param);
177 next.toEye =
178 target + glm::vec3(std::sin(endAz) * next.orbitRadius, next.orbitElevation, std::cos(endAz) * next.orbitRadius);
179 } else {
180 return fail("unsupported move kind", "camera.cinema.move");
181 }
182
183 move_ = next;
184 return Result<void>::success();
185}
186
188 return beginMove(MoveKind::Dolly, duration, distance);
189}
190
192 return beginMove(MoveKind::Crane, duration, deltaY);
193}
194
196 return beginMove(MoveKind::OrbitReveal, duration, degrees);
197}
198
200 auto bound = requireBound();
201 if (!bound) return bound;
202 if (stack_.empty()) return fail("story camera stack is empty", "camera.cinema.pop");
203 if (!std::isfinite(blendTime) || blendTime < 0.f)
204 return fail("blendTime must be finite and >= 0", "camera.cinema.pop");
205
206 cancelMove();
207 const StackEntry top = stack_.back();
208 stack_.pop_back();
209
210 if (stack_.empty()) {
211 auto restored = controller_->restoreFraming(top.gameplay, blendTime);
212 if (!restored) {
213 stack_.push_back(top);
214 return restored;
215 }
216 return Result<void>::success();
217 }
218
219 const OverviewShot* previous = findOverview(stack_.back().overview);
220 if (!previous) {
221 stack_.push_back(top);
222 return fail("previous overview is missing", "camera.cinema.pop");
223 }
224 return applyOverviewPose(*previous, blendTime);
225}
226
228 auto bound = requireBound();
229 if (!bound) return bound;
230 if (!std::isfinite(dt) || dt < 0.f) return fail("dt must be finite and >= 0", "camera.cinema.update");
231 if (!move_.active) return Result<void>::success();
232
233 move_.age += dt;
234 const float t = smoothstep01(move_.duration > 0.f ? move_.age / move_.duration : 1.f);
235 glm::vec3 eye = glm::mix(move_.fromEye, move_.toEye, t);
236 glm::vec3 target = glm::mix(move_.fromTarget, move_.toTarget, t);
237 float fov = glm::mix(move_.fromFov, move_.toFov, t);
238
239 if (move_.kind == MoveKind::OrbitReveal) {
240 const float az = move_.orbitAzimuth0 + glm::radians(move_.param) * t;
241 eye = move_.orbitCenter +
242 glm::vec3(std::sin(az) * move_.orbitRadius, move_.orbitElevation, std::cos(az) * move_.orbitRadius);
243 target = move_.orbitCenter;
244 fov = move_.fromFov;
245 }
246
247 auto written = writeActivePose(eye, target, fov);
248 if (!written) return written;
249
250 if (move_.age >= move_.duration) {
251 auto finalWrite = writeActivePose(move_.kind == MoveKind::OrbitReveal ? eye : move_.toEye,
252 move_.kind == MoveKind::OrbitReveal ? target : move_.toTarget,
253 move_.kind == MoveKind::OrbitReveal ? fov : move_.toFov);
254 cancelMove();
255 return finalWrite;
256 }
257 return Result<void>::success();
258}
259
261 return stack_.empty() ? std::string() : stack_.back().overview;
262}
263
264void exposeCinemaDirectorBindings(ssq::Table& table) {
265 auto cls = table.addClass<CinemaDirector>(
266 "CinemaDirector", std::function<CinemaDirector*()>([]() { return new CinemaDirector(); }), true);
267 auto* vm = table.getHandle();
268 cls.addFunc("bind", [vm](CinemaDirector* self, CameraController* controller) {
269 return eve::script::projectResult(vm, self->bind(controller));
270 });
271 cls.addFunc("defineOverview",
272 [vm](CinemaDirector* self, const std::string& name, float ex, float ey, float ez, float tx, float ty,
273 float tz, float fov) {
274 return eve::script::projectResult(vm, self->defineOverview(name, ex, ey, ez, tx, ty, tz, fov));
275 });
276 cls.addFunc("pushOverview", [vm](CinemaDirector* self, const std::string& name, float blend) {
278 });
279 cls.addFunc("dolly", [vm](CinemaDirector* self, float distance, float duration) {
281 });
282 cls.addFunc("crane", [vm](CinemaDirector* self, float deltaY, float duration) {
283 return eve::script::projectResult(vm, self->crane(deltaY, duration));
284 });
285 cls.addFunc("orbitReveal", [vm](CinemaDirector* self, float degrees, float duration) {
287 });
288 cls.addFunc("pop", [vm](CinemaDirector* self, float blend) {
289 return eve::script::projectResult(vm, self->pop(blend));
290 });
291 cls.addFunc("update", [vm](CinemaDirector* self, float dt) {
292 return eve::script::projectResult(vm, self->update(dt));
293 });
294 cls.addFunc("isActive", [](const CinemaDirector* self) { return self->isActive(); });
295 cls.addFunc("depth", [](const CinemaDirector* self) { return self->depth(); });
296 cls.addFunc("isMovePlaying", [](const CinemaDirector* self) { return self->isMovePlaying(); });
297 cls.addFunc("getActiveOverview", [](const CinemaDirector* self) { return self->getActiveOverview(); });
298}
299
300} // namespace eve::camera
LogicalId target
float duration
float degrees
Definition CardTypes.cpp:38
HSQUIRRELVM vm
Definition ECS.cpp:38
HSQOBJECT cls
Definition ECS.cpp:39
std::string name
Definition ECS.cpp:154
std::string message
float v
float blend
float length
float distance
double dt
std::shared_ptr< const OceanSprayFrame > previous
float t
glm::vec3 eye
std::string kind
The single Squirrel projection for common Result, Status and Value.
float targetX
V3 dir
Definition TreeMesh.cpp:151
V forward
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
3D 摄像机控制器(eve.Camera / eve.CameraController)。
void stopSequence()
Stops sequence.
graphics::Camera3D * getCamera() const
Returns the camera.
CameraFramingSnapshot captureFraming() const
Snapshot gameplay framing so a story director can restore it later. @cost Constant copy of scalar fra...
bool isTimelinePlaying() const
True when timeline playing.
Result< void > restoreFraming(const CameraFramingSnapshot &snapshot, float blendTime)
Restore a previously captured framing, optionally blending from the live pose. @cost One pose evaluat...
void setMode(const std::string &mode)
Sets the mode.
bool switchTo(const std::string &name, float blendTime)
Switch to.
Result< void > setView(const std::string &name, float ex, float ey, float ez, float tx, float ty, float tz, float fov)
Insert or replace a named cinematic view including FOV. @cost Constant; amortised over the next blend...
void setFov(float degrees)
Sets the fov.
void pauseTimeline()
Pause timeline.
void setDirectorEnabled(bool enabled)
Pause or resume priority-rig director selection (story cameras need this off).
Story-scene cinema director that push/pops overview shots and film moves on a CameraController.
Result< void > orbitReveal(float degrees, float duration)
Orbit the eye around the current look target by a finite yaw arc. @cost Constant setup; amortized acr...
Result< void > bind(CameraController *controller)
Bind the gameplay camera controller this director will temporarily override. @ownership Borrowed; the...
bool isActive() const noexcept
True while at least one story shot is on the stack.
bool isMovePlaying() const noexcept
True while a dolly/crane/orbitReveal is in progress.
Result< void > dolly(float distance, float duration)
Dolly the eye along the look axis (positive toward subject). @cost Constant setup; amortized across u...
std::string getActiveOverview() const
Name of the top overview, or empty when inactive.
Result< void > defineOverview(const std::string &name, float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float fov)
Register or replace a named establishing / overview shot.
Result< void > pushOverview(const std::string &name, float blendTime)
Push an overview shot onto the story stack, blending from the live pose. @cost One framing capture (f...
Result< void > update(float dt)
Advance the active film move; call before CameraController::update. @cost Constant per frame while a ...
Result< void > crane(float deltaY, float duration)
Raise or lower the eye while keeping the look target fixed. @cost Constant setup; amortized across up...
Result< void > pop(float blendTime)
Pop the top story shot and restore the previous overview or gameplay framing. @cost One framing resto...
int depth() const noexcept
Number of stacked story shots.
SQInteger top
Definition Snapshot.cpp:115
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
void exposeCinemaDirectorBindings(ssq::Table &table)
Register CinemaDirector EveScript bindings on the camera module table.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.