@quenty/camera 14.58.1 → 14.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [14.59.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/camera@14.58.2...@quenty/camera@14.59.0) (2026-11-24)
7
+
8
+ ### Features
9
+
10
+ - Add CameraUtils API to fit cameras to subrects of the camera ([4bdc953](https://github.com/Quenty/NevermoreEngine/commit/4bdc95304b8c001241d1154d82bd39071fac7c6b))
11
+
12
+ ## [14.58.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/camera@14.58.1...@quenty/camera@14.58.2) (2026-11-18)
13
+
14
+ **Note:** Version bump only for package @quenty/camera
15
+
6
16
  ## [14.58.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/camera@14.58.0...@quenty/camera@14.58.1) (2026-10-20)
7
17
 
8
18
  ### Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/camera",
3
- "version": "14.58.1",
3
+ "version": "14.59.0",
4
4
  "description": "Quenty's camera system for Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -29,34 +29,35 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@quenty/acceltween": "2.8.0",
32
- "@quenty/baseobject": "10.16.3",
32
+ "@quenty/baseobject": "10.16.4",
33
33
  "@quenty/cframeutils": "5.5.6",
34
34
  "@quenty/cubicspline": "10.13.3",
35
- "@quenty/draw": "7.17.3",
35
+ "@quenty/draw": "7.17.4",
36
36
  "@quenty/ducktype": "5.11.3",
37
- "@quenty/inputobjectutils": "4.43.1",
38
- "@quenty/jestutils": "1.1.3",
37
+ "@quenty/inputobjectutils": "4.43.2",
38
+ "@quenty/jestutils": "1.1.4",
39
39
  "@quenty/loader": "10.11.3",
40
- "@quenty/maid": "3.12.3",
40
+ "@quenty/maid": "3.12.4",
41
41
  "@quenty/math": "2.7.5",
42
- "@quenty/nevermore-test-runner": "1.6.3",
43
- "@quenty/playermock": "1.11.1",
44
- "@quenty/promise": "10.25.1",
45
- "@quenty/qframe": "10.37.1",
46
- "@quenty/rx": "13.35.1",
47
- "@quenty/servicebag": "11.21.3",
48
- "@quenty/spring": "10.12.3",
49
- "@quenty/steputils": "3.9.3",
50
- "@quenty/transitionmodel": "7.53.1",
51
- "@quenty/valueobject": "13.39.1",
52
- "@quenty/vector3utils": "10.12.3",
53
- "@quentystudios/jest-lua": "3.10.0-quenty.2"
42
+ "@quenty/nevermore-test-runner": "1.6.4",
43
+ "@quenty/playermock": "1.11.2",
44
+ "@quenty/promise": "10.25.2",
45
+ "@quenty/qframe": "10.38.0",
46
+ "@quenty/rx": "13.35.2",
47
+ "@quenty/servicebag": "11.21.4",
48
+ "@quenty/signal": "7.13.5",
49
+ "@quenty/spring": "10.13.0",
50
+ "@quenty/steputils": "3.9.4",
51
+ "@quenty/transitionmodel": "7.54.0",
52
+ "@quenty/valueobject": "13.39.2",
53
+ "@quenty/vector3utils": "10.12.4",
54
+ "@quentystudios/jest-lua": "3.10.0-quenty.3"
54
55
  },
55
56
  "devDependencies": {
56
- "@quenty/camerastoryutils": "10.36.1"
57
+ "@quenty/camerastoryutils": "10.37.0"
57
58
  },
58
59
  "publishConfig": {
59
60
  "access": "public"
60
61
  },
61
- "gitHead": "533798d87896e367ee481abe66b7f22b86965e1e"
62
+ "gitHead": "191a328ce40545ac6fb0282d33dde5b0576494e6"
62
63
  }
@@ -0,0 +1,78 @@
1
+ --!nonstrict
2
+ --[[
3
+ Frames a sphere in a draggable sub-rect of the viewport. The sphere stays centered in
4
+ the outlined rect and touches its limiting edge.
5
+
6
+ @class CameraUtils.SubRect.story
7
+ ]]
8
+
9
+ local require = (require :: any)(
10
+ game:GetService("ServerScriptService"):FindFirstChild("LoaderUtils", true).Parent
11
+ ).bootstrapStory(script) :: typeof(require(script.Parent.loader).load(script))
12
+
13
+ local Maid = require("Maid")
14
+
15
+ local CameraUtils = require("CameraUtils")
16
+
17
+ return function(target: Instance)
18
+ local maid = Maid.new()
19
+
20
+ local viewportFrame = maid:Add(Instance.new("ViewportFrame"))
21
+ viewportFrame.BorderSizePixel = 0
22
+ viewportFrame.BackgroundColor3 = Color3.new(0.7, 0.7, 0.7)
23
+ viewportFrame.Size = UDim2.fromScale(1, 1)
24
+
25
+ local camera: Camera = maid:Add(Instance.new("Camera"))
26
+ camera.FieldOfView = 70
27
+ viewportFrame.CurrentCamera = camera
28
+
29
+ local radius = 3
30
+
31
+ local ball: Part = maid:Add(Instance.new("Part"))
32
+ ball.Color = Color3.new(1, 0.5, 0.5)
33
+ ball.Size = Vector3.new(2 * radius, 2 * radius, 2 * radius)
34
+ ball.Shape = Enum.PartType.Ball
35
+ ball.TopSurface = Enum.SurfaceType.Smooth
36
+ ball.BottomSurface = Enum.SurfaceType.Smooth
37
+ ball.CFrame = CFrame.new()
38
+ ball.Anchored = true
39
+ ball.Parent = viewportFrame
40
+
41
+ local subrectFrame = Instance.new("Frame")
42
+ subrectFrame.BackgroundColor3 = Color3.new(0.3, 1, 0.5)
43
+ subrectFrame.BackgroundTransparency = 0.8
44
+ subrectFrame.BorderSizePixel = 0
45
+ subrectFrame.Position = UDim2.fromScale(0.1, 0.1)
46
+ subrectFrame.Size = UDim2.fromScale(0.5, 0.55)
47
+ subrectFrame.Parent = viewportFrame
48
+
49
+ local uiAspectRatioConstraint = Instance.new("UIAspectRatioConstraint")
50
+ uiAspectRatioConstraint.AspectRatio = 1
51
+ uiAspectRatioConstraint.Parent = subrectFrame
52
+
53
+ local dragDetector = Instance.new("UIDragDetector")
54
+ dragDetector.BoundingUI = viewportFrame
55
+ dragDetector.Parent = subrectFrame
56
+
57
+ local function update()
58
+ local absSize = viewportFrame.AbsoluteSize
59
+ if absSize.X > 0 and absSize.Y > 0 then
60
+ local min = (subrectFrame.AbsolutePosition - viewportFrame.AbsolutePosition) / absSize
61
+ local subrect = Rect.new(min, min + subrectFrame.AbsoluteSize / absSize)
62
+
63
+ local offset = CameraUtils.fitSphereToCameraRect(radius, camera.FieldOfView, absSize.X / absSize.Y, subrect)
64
+ camera.CFrame = CFrame.new(offset)
65
+ end
66
+ end
67
+ maid:GiveTask(camera:GetPropertyChangedSignal("FieldOfView"):Connect(update))
68
+ maid:GiveTask(viewportFrame:GetPropertyChangedSignal("AbsoluteSize"):Connect(update))
69
+ maid:GiveTask(subrectFrame:GetPropertyChangedSignal("AbsolutePosition"):Connect(update))
70
+ maid:GiveTask(subrectFrame:GetPropertyChangedSignal("AbsoluteSize"):Connect(update))
71
+ update()
72
+
73
+ viewportFrame.Parent = target
74
+
75
+ return function()
76
+ maid:DoCleaning()
77
+ end
78
+ end
@@ -0,0 +1,80 @@
1
+ --!nonstrict
2
+ --[[
3
+ Frames a rotated, non-cube box in a draggable sub-rect of the viewport, from a camera angled
4
+ down. The box's corners touch both edges of the rect on the limiting axis.
5
+
6
+ @class CameraUtils.SubRectBB.story
7
+ ]]
8
+
9
+ local require = (require :: any)(
10
+ game:GetService("ServerScriptService"):FindFirstChild("LoaderUtils", true).Parent
11
+ ).bootstrapStory(script) :: typeof(require(script.Parent.loader).load(script))
12
+
13
+ local Maid = require("Maid")
14
+
15
+ local CameraUtils = require("CameraUtils")
16
+
17
+ return function(target: Instance)
18
+ local maid = Maid.new()
19
+
20
+ local viewportFrame = maid:Add(Instance.new("ViewportFrame"))
21
+ viewportFrame.BorderSizePixel = 0
22
+ viewportFrame.BackgroundColor3 = Color3.new(0.7, 0.7, 0.7)
23
+ viewportFrame.Size = UDim2.fromScale(1, 1)
24
+
25
+ local camera: Camera = maid:Add(Instance.new("Camera"))
26
+ camera.FieldOfView = 70
27
+ viewportFrame.CurrentCamera = camera
28
+
29
+ local box: Part = maid:Add(Instance.new("Part"))
30
+ box.Color = Color3.new(0.5, 0.6, 1)
31
+ box.Size = Vector3.new(8, 3, 2)
32
+ box.TopSurface = Enum.SurfaceType.Smooth
33
+ box.BottomSurface = Enum.SurfaceType.Smooth
34
+ box.CFrame = CFrame.Angles(0, math.rad(20), 0)
35
+ box.Anchored = true
36
+ box.Parent = viewportFrame
37
+
38
+ local rotation = CFrame.Angles(0, math.rad(35), 0) * CFrame.Angles(math.rad(-30), 0, 0)
39
+
40
+ local subrectFrame = Instance.new("Frame")
41
+ subrectFrame.BackgroundColor3 = Color3.new(0.3, 1, 0.5)
42
+ subrectFrame.BackgroundTransparency = 0.8
43
+ subrectFrame.BorderSizePixel = 0
44
+ subrectFrame.Position = UDim2.fromScale(0.1, 0.1)
45
+ subrectFrame.Size = UDim2.fromScale(0.5, 0.55)
46
+ subrectFrame.Parent = viewportFrame
47
+
48
+ local dragDetector = Instance.new("UIDragDetector")
49
+ dragDetector.BoundingUI = viewportFrame
50
+ dragDetector.Parent = subrectFrame
51
+
52
+ local function update()
53
+ local absSize = viewportFrame.AbsoluteSize
54
+ if absSize.X > 0 and absSize.Y > 0 then
55
+ local min = (subrectFrame.AbsolutePosition - viewportFrame.AbsolutePosition) / absSize
56
+ local subrect = Rect.new(min, min + subrectFrame.AbsoluteSize / absSize)
57
+
58
+ local offset = CameraUtils.fitBoundingBoxToCameraRectExact(
59
+ box.CFrame,
60
+ box.Size,
61
+ rotation,
62
+ camera.FieldOfView,
63
+ absSize.X / absSize.Y,
64
+ subrect
65
+ )
66
+ camera.CFrame = CFrame.new(box.Position) * rotation * CFrame.new(offset)
67
+ end
68
+ end
69
+ maid:GiveTask(camera:GetPropertyChangedSignal("FieldOfView"):Connect(update))
70
+ maid:GiveTask(viewportFrame:GetPropertyChangedSignal("AbsoluteSize"):Connect(update))
71
+ maid:GiveTask(subrectFrame:GetPropertyChangedSignal("AbsolutePosition"):Connect(update))
72
+ maid:GiveTask(subrectFrame:GetPropertyChangedSignal("AbsoluteSize"):Connect(update))
73
+ update()
74
+
75
+ viewportFrame.Parent = target
76
+
77
+ return function()
78
+ maid:DoCleaning()
79
+ end
80
+ end
@@ -11,7 +11,7 @@
11
11
  RunService.RenderStepped:Connect(function()
12
12
  local cframe, size = model:GetBoundingBox()
13
13
  local size = viewportFrame.AbsoluteSize
14
- local aspectRatio = size.x/size.y
14
+ local aspectRatio = size.X/size.Y
15
15
  local dist = CameraUtils.fitBoundingBoxToCamera(size, camera.FieldOfView, aspectRatio)
16
16
  camera.CFrame = cframe.Position + CFrame.Angles(0, math.pi*os.clock() % math.pi, -math.pi/8)
17
17
  :vectorToWorldSpace(Vector3.new(0, 0, -dist))
@@ -23,6 +23,25 @@
23
23
 
24
24
  local CameraUtils = {}
25
25
 
26
+ type PointsSubRectBounds = {
27
+ lowEdges: Vector2,
28
+ highEdges: Vector2,
29
+ minSums: Vector2,
30
+ maxSums: Vector2,
31
+ nearestZ: number,
32
+ }
33
+
34
+ local BOX_CORNERS = {
35
+ Vector3.new(-1, -1, -1),
36
+ Vector3.new(-1, -1, 1),
37
+ Vector3.new(-1, 1, -1),
38
+ Vector3.new(-1, 1, 1),
39
+ Vector3.new(1, -1, -1),
40
+ Vector3.new(1, -1, 1),
41
+ Vector3.new(1, 1, -1),
42
+ Vector3.new(1, 1, 1),
43
+ }
44
+
26
45
  --[=[
27
46
  Computes the diameter of a cubeid
28
47
 
@@ -51,6 +70,67 @@ function CameraUtils.fitBoundingBoxToCamera(size: Vector3, fovDeg: number, aspec
51
70
  return CameraUtils.fitSphereToCamera(radius, fovDeg, aspectRatio)
52
71
  end
53
72
 
73
+ --[=[
74
+ Like [CameraUtils.fitBoundingBoxToCamera], but frames the box in a sub-rect of the viewport.
75
+ See [CameraUtils.fitSphereToCameraRect].
76
+
77
+ ```lua
78
+ -- Keep a hat clear of a label along the bottom of a viewport frame
79
+ local viewportSize = viewportFrame.AbsoluteSize
80
+ local labelTop = label.AbsolutePosition.Y - viewportFrame.AbsolutePosition.Y
81
+ local visibleRect = Rect.new(0, 0, 1, labelTop / viewportSize.Y)
82
+
83
+ local boxCFrame, size = hat:GetBoundingBox()
84
+ local offset = CameraUtils.fitBoundingBoxToCameraRect(size, camera.FieldOfView, viewportSize.X / viewportSize.Y, visibleRect)
85
+ camera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)
86
+ ```
87
+
88
+ @param size Vector3 -- Size of the bounding box
89
+ @param fovDeg number -- Field of view in degrees (vertical)
90
+ @param aspectRatio number -- Aspect ratio of the screen
91
+ @param rect Rect -- Normalized viewport rect (0 to 1, top-left origin)
92
+ @return Vector3 -- Camera position relative to the bounding box center, in camera space
93
+ ]=]
94
+ function CameraUtils.fitBoundingBoxToCameraRect(size: Vector3, fovDeg: number, aspectRatio: number, rect: Rect): Vector3
95
+ local radius = CameraUtils.getCubeoidDiameter(size) / 2
96
+ return CameraUtils.fitSphereToCameraRect(radius, fovDeg, aspectRatio, rect)
97
+ end
98
+
99
+ --[=[
100
+ Like [CameraUtils.fitBoundingBoxToCameraRect], but fits the box's corners exactly for the given
101
+ camera rotation instead of its bounding sphere. Much tighter for boxes that aren't cube shaped,
102
+ but the distance changes as the camera rotates around the box.
103
+
104
+ ```lua
105
+ local boxCFrame, size = model:GetBoundingBox()
106
+ local offset = CameraUtils.fitBoundingBoxToCameraRectExact(boxCFrame, size, rotation, camera.FieldOfView, aspectRatio, visibleRect)
107
+ camera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)
108
+ ```
109
+
110
+ @param boxCFrame CFrame -- CFrame of the bounding box
111
+ @param size Vector3 -- Size of the bounding box
112
+ @param rotation CFrame -- Camera rotation. Position is ignored.
113
+ @param fovDeg number -- Field of view in degrees (vertical)
114
+ @param aspectRatio number -- Aspect ratio of the screen
115
+ @param subrect Rect -- Normalized viewport rect (0 to 1, top-left origin)
116
+ @return Vector3 -- Camera position relative to the bounding box center, in camera space
117
+ ]=]
118
+ function CameraUtils.fitBoundingBoxToCameraRectExact(
119
+ boxCFrame: CFrame,
120
+ size: Vector3,
121
+ rotation: CFrame,
122
+ fovDeg: number,
123
+ aspectRatio: number,
124
+ subrect: Rect
125
+ ): Vector3
126
+ local points = CameraUtils.boundingBoxPoints(boxCFrame, size)
127
+ for index, point in points do
128
+ points[index] = rotation:VectorToObjectSpace(point - boxCFrame.Position)
129
+ end
130
+
131
+ return CameraUtils.fitPointsToCameraRect(points, fovDeg, aspectRatio, subrect)
132
+ end
133
+
54
134
  --[=[
55
135
  Fits a sphere to the camera, computing how far back to zoom the camera from
56
136
  the center of the sphere.
@@ -69,6 +149,205 @@ function CameraUtils.fitSphereToCamera(radius: number, fovDeg: number, aspectRat
69
149
  return radius / math.sin(halfFov)
70
150
  end
71
151
 
152
+ --[=[
153
+ Like [CameraUtils.fitSphereToCamera], but frames the sphere in a sub-rect of the viewport,
154
+ such as the space left visible by UI. Rotation is unchanged.
155
+
156
+ Returns both the distance and the sideways shift that centers the sphere in the rect.
157
+ The distance alone would center the sphere on the full viewport, under the UI.
158
+
159
+ Off-center spheres project stretched away from the viewport center, so the sphere's outline
160
+ is centered in the rect rather than its center point. It touches both edges on the limiting axis.
161
+
162
+ ```lua
163
+ -- Frame a sphere left of a panel covering the right 40% of the screen
164
+ local offset = CameraUtils.fitSphereToCameraRect(radius, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 0.6, 1))
165
+ camera.CFrame = CFrame.new(center) * rotation * CFrame.new(offset)
166
+ ```
167
+
168
+ @param radius number -- Radius of the sphere
169
+ @param fovDeg number -- Field of view in degrees (vertical)
170
+ @param aspectRatio number -- Aspect ratio of the screen
171
+ @param subrect Rect -- Normalized viewport rect (0 to 1, top-left origin)
172
+ @return Vector3 -- Camera position relative to the sphere center, in camera space
173
+ ]=]
174
+ function CameraUtils.fitSphereToCameraRect(radius: number, fovDeg: number, aspectRatio: number, subrect: Rect): Vector3
175
+ assert(subrect.Width > 0 and subrect.Height > 0, "Bad rect size")
176
+
177
+ -- A sphere tangent to both edges of an axis sits on the bisector, at radius / sin(halfAngle) from the camera
178
+ local centerAngle, halfAngle = CameraUtils._getSubRectAngles(fovDeg, aspectRatio, subrect)
179
+ local depth = math.max(
180
+ radius * math.cos(centerAngle.X) / math.sin(halfAngle.X),
181
+ radius * math.cos(centerAngle.Y) / math.sin(halfAngle.Y)
182
+ )
183
+
184
+ local shift = CameraUtils.getCameraSubRectShift(depth, fovDeg, aspectRatio, subrect)
185
+ return Vector3.new(shift.X, shift.Y, depth)
186
+ end
187
+
188
+ --[=[
189
+ Fits points into a sub-rect of the viewport exactly, without rotating the camera. Returns the
190
+ closest camera position that keeps every point inside the rect. On the limiting axis the points
191
+ touch both edges, and on the other axis they're centered between the edges.
192
+
193
+ For a fixed rotation, keeping a point inside an edge is linear in the camera position, so the
194
+ closest distance has a closed form per axis.
195
+
196
+ @param points { Vector3 } -- Points in camera space (x right, y up, looking down -z), relative to an origin
197
+ @param fovDeg number -- Field of view in degrees (vertical)
198
+ @param aspectRatio number -- Aspect ratio of the screen
199
+ @param subrect Rect -- Normalized viewport rect (0 to 1, top-left origin)
200
+ @return Vector3 -- Camera position relative to the origin, in camera space
201
+ ]=]
202
+ function CameraUtils.fitPointsToCameraRect(
203
+ points: { Vector3 },
204
+ fovDeg: number,
205
+ aspectRatio: number,
206
+ subrect: Rect
207
+ ): Vector3
208
+ local bounds = CameraUtils._getPointsSubRectBounds(points, fovDeg, aspectRatio, subrect)
209
+
210
+ -- The camera's allowed x and y ranges only overlap once it's far enough back
211
+ local spans = (bounds.maxSums - bounds.minSums) / (bounds.highEdges - bounds.lowEdges)
212
+ local depth = math.max(spans.X, spans.Y, bounds.nearestZ)
213
+
214
+ local shift = CameraUtils._getPointsSubRectShift(bounds, depth)
215
+ return Vector3.new(shift.X, shift.Y, depth)
216
+ end
217
+
218
+ --[=[
219
+ Computes the camera-space shift that centers points in a sub-rect of the viewport at the given
220
+ depth, without rotating the camera. Matches [CameraUtils.fitPointsToCameraRect] at its depth.
221
+
222
+ Use this when the distance comes from elsewhere, such as a spring. Unlike
223
+ [CameraUtils.getCameraSubRectShift], this centers the points' projected outline.
224
+
225
+ @param points { Vector3 } -- Points in camera space (x right, y up, looking down -z), relative to an origin
226
+ @param depth number -- Distance from the camera to the origin along the look vector
227
+ @param fovDeg number -- Field of view in degrees (vertical)
228
+ @param aspectRatio number -- Aspect ratio of the screen
229
+ @param subrect Rect -- Normalized viewport rect (0 to 1, top-left origin)
230
+ @return Vector3 -- Camera-space offset
231
+ ]=]
232
+ function CameraUtils.getPointsCameraSubRectShift(
233
+ points: { Vector3 },
234
+ depth: number,
235
+ fovDeg: number,
236
+ aspectRatio: number,
237
+ subrect: Rect
238
+ ): Vector3
239
+ local bounds = CameraUtils._getPointsSubRectBounds(points, fovDeg, aspectRatio, subrect)
240
+ local shift = CameraUtils._getPointsSubRectShift(bounds, depth)
241
+ return Vector3.new(shift.X, shift.Y, 0)
242
+ end
243
+
244
+ --[=[
245
+ Returns the 8 corners of a bounding box in world space.
246
+
247
+ @param boxCFrame CFrame -- CFrame of the bounding box
248
+ @param size Vector3 -- Size of the bounding box
249
+ @return { Vector3 }
250
+ ]=]
251
+ function CameraUtils.boundingBoxPoints(boxCFrame: CFrame, size: Vector3): { Vector3 }
252
+ local halfSize = size / 2
253
+ local points = table.create(#BOX_CORNERS)
254
+ for _, corner in BOX_CORNERS do
255
+ table.insert(points, boxCFrame:PointToWorldSpace(halfSize * corner))
256
+ end
257
+ return points
258
+ end
259
+
260
+ --[[
261
+ A point p is inside an edge with slope s when p.Y - camera.Y <= s * (camera.Z - p.Z) (or >=),
262
+ which bounds camera.Y by p.Y + s * p.Z - s * camera.Z. The same holds for X. Collects the
263
+ tightest of those bounds over all points, with the rect edges as slopes off the look axis.
264
+ ]]
265
+ function CameraUtils._getPointsSubRectBounds(
266
+ points: { Vector3 },
267
+ fovDeg: number,
268
+ aspectRatio: number,
269
+ subrect: Rect
270
+ ): PointsSubRectBounds
271
+ assert(#points > 0, "Bad points")
272
+ assert(subrect.Width > 0 and subrect.Height > 0, "Bad rect size")
273
+
274
+ local tanHalfY = math.tan(0.5 * math.rad(fovDeg))
275
+ local tanHalfX = tanHalfY * aspectRatio
276
+ local lowEdges = Vector2.new((2 * subrect.Min.X - 1) * tanHalfX, (1 - 2 * subrect.Max.Y) * tanHalfY)
277
+ local highEdges = Vector2.new((2 * subrect.Max.X - 1) * tanHalfX, (1 - 2 * subrect.Min.Y) * tanHalfY)
278
+
279
+ local minSums = Vector2.new(math.huge, math.huge)
280
+ local maxSums = Vector2.new(-math.huge, -math.huge)
281
+ local nearestZ = -math.huge
282
+ for _, point in points do
283
+ local xy = Vector2.new(point.X, point.Y)
284
+ minSums = minSums:Min(xy + lowEdges * point.Z)
285
+ maxSums = maxSums:Max(xy + highEdges * point.Z)
286
+ nearestZ = math.max(nearestZ, point.Z)
287
+ end
288
+
289
+ return {
290
+ lowEdges = lowEdges,
291
+ highEdges = highEdges,
292
+ minSums = minSums,
293
+ maxSums = maxSums,
294
+ nearestZ = nearestZ,
295
+ }
296
+ end
297
+
298
+ -- Centers the camera between its allowed range on each axis, which collapses on the limiting axis
299
+ function CameraUtils._getPointsSubRectShift(bounds: PointsSubRectBounds, depth: number): Vector2
300
+ return ((bounds.maxSums + bounds.minSums) - (bounds.highEdges + bounds.lowEdges) * depth) / 2
301
+ end
302
+
303
+ --[=[
304
+ Computes the camera-space shift that centers a subject at `depth` in a sub-rect of the
305
+ viewport, without rotating the camera.
306
+
307
+ The subject is placed on the angle bisector of the rect's edges rather than its screen-space
308
+ center, since off-center objects project stretched away from the viewport center. This centers
309
+ the outline of a sphere at any depth and radius.
310
+
311
+ Use this when the distance comes from elsewhere, such as a spring. Otherwise prefer
312
+ [CameraUtils.fitSphereToCameraRect].
313
+
314
+ ```lua
315
+ -- Center the subject above a dialog box covering the bottom 30% of the screen
316
+ local shift = CameraUtils.getCameraSubRectShift(distance, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 1, 0.7))
317
+ camera.CFrame = CFrame.new(subjectPosition) * rotation * CFrame.new(shift + Vector3.new(0, 0, distance))
318
+ ```
319
+
320
+ @param depth number -- Distance from the camera to the subject along the look vector
321
+ @param fovDeg number -- Field of view in degrees (vertical)
322
+ @param aspectRatio number -- Aspect ratio of the screen
323
+ @param subrect Rect -- Normalized viewport rect (0 to 1, top-left origin)
324
+ @return Vector3 -- Camera-space offset
325
+ ]=]
326
+ function CameraUtils.getCameraSubRectShift(depth: number, fovDeg: number, aspectRatio: number, subrect: Rect): Vector3
327
+ local centerAngle = CameraUtils._getSubRectAngles(fovDeg, aspectRatio, subrect)
328
+ return Vector3.new(-math.tan(centerAngle.X) * depth, -math.tan(centerAngle.Y) * depth, 0)
329
+ end
330
+
331
+ --[[
332
+ Returns the angle bisector and half angle of the sub-rect's edges on each screen axis,
333
+ measured off the look axis (x right, y up). Each axis can be handled on its own, since a
334
+ plane through the camera along one axis only depends on the other axis and depth.
335
+ ]]
336
+ function CameraUtils._getSubRectAngles(fovDeg: number, aspectRatio: number, subrect: Rect): (Vector2, Vector2)
337
+ local tanHalfY = math.tan(0.5 * math.rad(fovDeg))
338
+ local tanHalfX = tanHalfY * aspectRatio
339
+
340
+ local left = math.atan((2 * subrect.Min.X - 1) * tanHalfX)
341
+ local right = math.atan((2 * subrect.Max.X - 1) * tanHalfX)
342
+ local top = math.atan((1 - 2 * subrect.Min.Y) * tanHalfY)
343
+ local bottom = math.atan((1 - 2 * subrect.Max.Y) * tanHalfY)
344
+
345
+ local centerAngle = Vector2.new(left + right, bottom + top) / 2
346
+ local halfAngle = Vector2.new(right - left, top - bottom) / 2
347
+
348
+ return centerAngle, halfAngle
349
+ end
350
+
72
351
  --[=[
73
352
  Checks if a position is on screen on a camera
74
353
 
@@ -59,3 +59,176 @@ describe("CameraUtils.fitSphereToCamera", function()
59
59
  expect(portrait).toBeGreaterThan(landscape)
60
60
  end)
61
61
  end)
62
+
63
+ local FULL_RECT = Rect.new(0, 0, 1, 1)
64
+
65
+ describe("CameraUtils.fitSphereToCameraRect", function()
66
+ it("should match fitSphereToCamera for the full viewport in landscape", function()
67
+ local expected = CameraUtils.fitSphereToCamera(5, 35, 16 / 9)
68
+ local offset = CameraUtils.fitSphereToCameraRect(5, 35, 16 / 9, FULL_RECT)
69
+ expect(offset.X).toBeCloseTo(0, 5)
70
+ expect(offset.Y).toBeCloseTo(0, 5)
71
+ expect(offset.Z).toBeCloseTo(expected, 5)
72
+ end)
73
+
74
+ it("should match fitSphereToCamera for the full viewport in portrait", function()
75
+ local expected = CameraUtils.fitSphereToCamera(5, 35, 0.5)
76
+ local offset = CameraUtils.fitSphereToCameraRect(5, 35, 0.5, FULL_RECT)
77
+ expect(offset.X).toBeCloseTo(0, 5)
78
+ expect(offset.Y).toBeCloseTo(0, 5)
79
+ expect(offset.Z).toBeCloseTo(expected, 5)
80
+ end)
81
+
82
+ it("should increase distance for a smaller rect", function()
83
+ local full = CameraUtils.fitSphereToCameraRect(5, 35, 16 / 9, FULL_RECT)
84
+ local partial = CameraUtils.fitSphereToCameraRect(5, 35, 16 / 9, Rect.new(0, 0, 1, 0.7))
85
+ expect(partial.Z).toBeGreaterThan(full.Z)
86
+ end)
87
+ end)
88
+
89
+ describe("CameraUtils.fitBoundingBoxToCameraRect", function()
90
+ it("should match fitBoundingBoxToCamera for the full viewport", function()
91
+ local size = Vector3.new(4, 6, 2)
92
+ local expected = CameraUtils.fitBoundingBoxToCamera(size, 35, 16 / 9)
93
+ local offset = CameraUtils.fitBoundingBoxToCameraRect(size, 35, 16 / 9, FULL_RECT)
94
+ expect(offset.X).toBeCloseTo(0, 5)
95
+ expect(offset.Y).toBeCloseTo(0, 5)
96
+ expect(offset.Z).toBeCloseTo(expected, 5)
97
+ end)
98
+
99
+ it("should match fitSphereToCameraRect with the bounding sphere", function()
100
+ local size = Vector3.new(4, 6, 2)
101
+ local rect = Rect.new(0, 0, 1, 0.7)
102
+ local radius = CameraUtils.getCubeoidDiameter(size) / 2
103
+ local expected = CameraUtils.fitSphereToCameraRect(radius, 35, 16 / 9, rect)
104
+ local offset = CameraUtils.fitBoundingBoxToCameraRect(size, 35, 16 / 9, rect)
105
+ expect((offset - expected).Magnitude).toBeCloseTo(0, 5)
106
+ end)
107
+ end)
108
+
109
+ describe("CameraUtils.fitBoundingBoxToCameraRectExact", function()
110
+ it("should fit a face-on cube so its front face fills the viewport height", function()
111
+ local size, fovDeg = 4, 35
112
+ local offset = CameraUtils.fitBoundingBoxToCameraRectExact(
113
+ CFrame.new(),
114
+ Vector3.one * size,
115
+ CFrame.new(),
116
+ fovDeg,
117
+ 16 / 9,
118
+ FULL_RECT
119
+ )
120
+
121
+ -- The front face sits size / 2 closer than the center, and its half height fills tan(fov / 2)
122
+ expect(offset.X).toBeCloseTo(0, 5)
123
+ expect(offset.Y).toBeCloseTo(0, 5)
124
+ expect(offset.Z).toBeCloseTo(size / 2 + size / (2 * math.tan(math.rad(fovDeg) / 2)), 5)
125
+ end)
126
+
127
+ it("should fit a wide box closer than its bounding sphere", function()
128
+ local size = Vector3.new(10, 2, 2)
129
+ local exact =
130
+ CameraUtils.fitBoundingBoxToCameraRectExact(CFrame.new(), size, CFrame.new(), 35, 16 / 9, FULL_RECT)
131
+ local sphere = CameraUtils.fitBoundingBoxToCameraRect(size, 35, 16 / 9, FULL_RECT)
132
+ expect(exact.Z).toBeLessThan(sphere.Z)
133
+ end)
134
+
135
+ it("should move the camera down when the bottom of the screen is covered", function()
136
+ local offset = CameraUtils.fitBoundingBoxToCameraRectExact(
137
+ CFrame.new(),
138
+ Vector3.new(4, 6, 2),
139
+ CFrame.Angles(math.rad(-20), math.rad(30), 0),
140
+ 35,
141
+ 16 / 9,
142
+ Rect.new(0, 0, 1, 0.7)
143
+ )
144
+ expect(offset.Y).toBeLessThan(0)
145
+ end)
146
+ end)
147
+
148
+ describe("CameraUtils.boundingBoxPoints", function()
149
+ it("should return the 8 corners of the box", function()
150
+ local points = CameraUtils.boundingBoxPoints(CFrame.new(10, 0, 0), Vector3.new(2, 4, 6))
151
+ expect(#points).toEqual(8)
152
+
153
+ local min, max = points[1], points[1]
154
+ for _, point in points do
155
+ min = min:Min(point)
156
+ max = max:Max(point)
157
+ end
158
+ expect((min - Vector3.new(9, -2, -3)).Magnitude).toBeCloseTo(0, 5)
159
+ expect((max - Vector3.new(11, 2, 3)).Magnitude).toBeCloseTo(0, 5)
160
+ end)
161
+ end)
162
+
163
+ describe("CameraUtils.getPointsCameraSubRectShift", function()
164
+ it("should match fitPointsToCameraRect at its depth", function()
165
+ local rotation = CFrame.Angles(math.rad(-30), math.rad(35), 0)
166
+ local points = CameraUtils.boundingBoxPoints(CFrame.Angles(0, math.rad(20), 0), Vector3.new(8, 3, 2))
167
+ for index, point in points do
168
+ points[index] = rotation:VectorToObjectSpace(point)
169
+ end
170
+
171
+ local rect = Rect.new(0.1, 0.1, 0.6, 0.65)
172
+ local fit = CameraUtils.fitPointsToCameraRect(points, 70, 16 / 9, rect)
173
+ local shift = CameraUtils.getPointsCameraSubRectShift(points, fit.Z, 70, 16 / 9, rect)
174
+
175
+ expect(shift.X).toBeCloseTo(fit.X, 5)
176
+ expect(shift.Y).toBeCloseTo(fit.Y, 5)
177
+ expect(shift.Z).toEqual(0)
178
+ end)
179
+ end)
180
+
181
+ describe("CameraUtils.getCameraSubRectShift", function()
182
+ local function projectToNdc(offset: Vector3, depth: number, fovDeg: number, aspectRatio: number): Vector2
183
+ -- Subject sits on the original look axis; the camera has moved by offset
184
+ local relative = Vector3.new(0, 0, -depth) - offset
185
+ local tanHalfFov = math.tan(math.rad(fovDeg) / 2)
186
+ return Vector2.new(relative.X / (depth * tanHalfFov * aspectRatio), relative.Y / (depth * tanHalfFov))
187
+ end
188
+
189
+ it("should return zero for the full viewport", function()
190
+ local offset = CameraUtils.getCameraSubRectShift(10, 35, 16 / 9, FULL_RECT)
191
+ expect(offset.Magnitude).toBeCloseTo(0, 5)
192
+ end)
193
+
194
+ it("should move the camera down when the bottom of the screen is covered", function()
195
+ local offset = CameraUtils.getCameraSubRectShift(10, 35, 16 / 9, Rect.new(0, 0, 1, 0.7))
196
+ expect(offset.X).toBeCloseTo(0, 5)
197
+ expect(offset.Y).toBeLessThan(0)
198
+ expect(offset.Z).toEqual(0)
199
+ end)
200
+
201
+ -- NDC of the angle bisector between two NDC edges on one axis
202
+ local function getBisectorNdc(lowNdc: number, highNdc: number, tanHalf: number): number
203
+ return math.tan((math.atan(lowNdc * tanHalf) + math.atan(highNdc * tanHalf)) / 2) / tanHalf
204
+ end
205
+
206
+ it("should project the subject onto the angle bisector of a bottom-covered rect", function()
207
+ local depth, fovDeg, aspectRatio = 10, 35, 16 / 9
208
+ local offset = CameraUtils.getCameraSubRectShift(depth, fovDeg, aspectRatio, Rect.new(0, 0, 1, 0.7))
209
+ local ndc = projectToNdc(offset, depth, fovDeg, aspectRatio)
210
+
211
+ -- Rect spans -0.4 to 1 in NDC. The bisector sits just below its screen-space center of 0.3.
212
+ expect(ndc.X).toBeCloseTo(0, 5)
213
+ expect(ndc.Y).toBeCloseTo(getBisectorNdc(-0.4, 1, math.tan(math.rad(fovDeg) / 2)), 5)
214
+ expect(ndc.Y).toBeLessThan(0.3)
215
+ end)
216
+
217
+ it("should project the subject onto the angle bisector of an off-center rect", function()
218
+ local depth, fovDeg, aspectRatio = 25, 50, 0.5
219
+ local rect = Rect.new(0.5, 0.1, 0.9, 0.5)
220
+ local offset = CameraUtils.getCameraSubRectShift(depth, fovDeg, aspectRatio, rect)
221
+ local ndc = projectToNdc(offset, depth, fovDeg, aspectRatio)
222
+
223
+ local tanHalfY = math.tan(math.rad(fovDeg) / 2)
224
+ expect(ndc.X).toBeCloseTo(getBisectorNdc(0, 0.8, tanHalfY * aspectRatio), 5)
225
+ expect(ndc.Y).toBeCloseTo(getBisectorNdc(0, 0.8, tanHalfY), 5)
226
+ end)
227
+
228
+ it("should scale linearly with depth", function()
229
+ local rect = Rect.new(0, 0, 1, 0.7)
230
+ local near = CameraUtils.getCameraSubRectShift(10, 35, 16 / 9, rect)
231
+ local far = CameraUtils.getCameraSubRectShift(20, 35, 16 / 9, rect)
232
+ expect(far.Y).toBeCloseTo(2 * near.Y, 5)
233
+ end)
234
+ end)
@@ -17,6 +17,8 @@ local JestUtils = require("JestUtils")
17
17
  local Maid = require("Maid")
18
18
  local PlayerMock = require("PlayerMock")
19
19
  local ServiceBag = require("ServiceBag")
20
+ local SignalTestUtils = require("SignalTestUtils")
21
+ local StepUtils = require("StepUtils")
20
22
  local TransitionModel = require("TransitionModel")
21
23
 
22
24
  local describe = Jest.Globals.describe
@@ -24,6 +26,7 @@ local expect = Jest.Globals.expect
24
26
  local it = Jest.Globals.it
25
27
 
26
28
  type Controller = {
29
+ maid: Maid.Maid,
27
30
  cameraStack: CameraStack.CameraStack,
28
31
  cameraEffect: CustomCameraEffect.CustomCameraEffect,
29
32
  tweener: CameraStateTweener.CameraStateTweener,
@@ -59,6 +62,7 @@ local function setup(options: { speed: number? }?): Controller
59
62
  end
60
63
 
61
64
  local controller: Controller = {
65
+ maid = maid,
62
66
  cameraStack = cameraStack,
63
67
  cameraEffect = cameraEffect,
64
68
  tweener = tweener,
@@ -66,8 +70,8 @@ local function setup(options: { speed: number? }?): Controller
66
70
  -- a frame has to pass before the transition notices it arrived.
67
71
  advance = function(seconds: number)
68
72
  now += seconds
69
- task.wait()
70
- task.wait()
73
+ StepUtils.getAnimationStepSignal():Wait()
74
+ StepUtils.deferWait()
71
75
  end,
72
76
  Destroy = function(_self)
73
77
  maid:DoCleaning()
@@ -341,13 +345,10 @@ describe("CameraStateTweener:SetShownTarget", function()
341
345
 
342
346
  it("retargets and runs the show transition again when changed while shown", function()
343
347
  local controller = setup()
344
- local showingComplete = 0
345
- controller.tweener.ShowingComplete:Connect(function()
346
- showingComplete += 1
347
- end)
348
+ local showingComplete = SignalTestUtils.countFires(controller.maid, controller.tweener.ShowingComplete)
348
349
 
349
350
  controller.tweener:Show(true)
350
- expect(showingComplete).toBe(1)
351
+ expect(showingComplete()).toBe(1)
351
352
 
352
353
  controller.tweener:SetShownTarget(0.5)
353
354
 
@@ -356,7 +357,7 @@ describe("CameraStateTweener:SetShownTarget", function()
356
357
 
357
358
  controller.advance(5)
358
359
 
359
- expect(showingComplete).toBe(2)
360
+ expect(showingComplete()).toBe(2)
360
361
  expect(controller.tweener:GetPercentVisible()).toBe(0.5)
361
362
 
362
363
  controller:Destroy()
@@ -423,17 +424,14 @@ describe("CameraStateTweener as a transition model", function()
423
424
 
424
425
  it("fires ShowingComplete only once the fade arrives", function()
425
426
  local controller = setup()
426
- local showingComplete = 0
427
- controller.tweener.ShowingComplete:Connect(function()
428
- showingComplete += 1
429
- end)
427
+ local showingComplete = SignalTestUtils.countFires(controller.maid, controller.tweener.ShowingComplete)
430
428
 
431
429
  controller.tweener:Show()
432
- expect(showingComplete).toBe(0)
430
+ expect(showingComplete()).toBe(0)
433
431
 
434
432
  controller.advance(5)
435
433
 
436
- expect(showingComplete).toBe(1)
434
+ expect(showingComplete()).toBe(1)
437
435
 
438
436
  controller:Destroy()
439
437
  end)