-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrove.lua
More file actions
199 lines (170 loc) · 6.34 KB
/
Copy pathtrove.lua
File metadata and controls
199 lines (170 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-- Renders the sky on the faces of parts tagged `RenderSky:(side)`.
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local sky = Lighting:WaitForChild("sky")
local playerGui = game:GetService'Players'.LocalPlayer:WaitForChild'PlayerGui'
local VEC_XZ = Vector3.new(1, 0, 1)
local VEC_YZ = Vector3.new(0, 1, 1)
local SKYBOX_MAP = {
["Bk"] = CFrame.fromEulerAnglesYXZ(0, math.rad(180), 0),
["Dn"] = CFrame.fromEulerAnglesYXZ(-math.rad(90), math.rad(90), 0),
["Ft"] = CFrame.fromEulerAnglesYXZ(0, 0, 0),
["Lf"] = CFrame.fromEulerAnglesYXZ(0, -math.rad(90), 0),
["Rt"] = CFrame.fromEulerAnglesYXZ(0, math.rad(90), 0),
["Up"] = CFrame.fromEulerAnglesYXZ(math.rad(90), math.rad(90), 0),
}
-- Builds the six-sided skybox model.
local function sky_sphere(sky)
local model = Instance.new("Model")
model.Name = "SkyboxModel"
for prop, rotationCF in SKYBOX_MAP do
local side = Instance.new("Part")
side.Anchored = true
side.Name = prop
side.CFrame = rotationCF
side.Size = Vector3.one
side.Parent = model
local mesh = Instance.new("SpecialMesh")
mesh.MeshId = "rbxassetid://3083991485"
mesh.Offset = Vector3.new(0, 0, -0.683)
mesh.Parent = side
local decal = Instance.new("Decal")
decal.Texture = sky["Skybox" .. prop]
decal.Parent = side
end
return model
end
local function window_new(part, face)
local self = {}
local surfaceGui = Instance.new("SurfaceGui")
surfaceGui.LightInfluence = 0
surfaceGui.Name = "SkyboxViewportWindow"
surfaceGui.Face = face
surfaceGui.Adornee = part
surfaceGui.ClipsDescendants = true
surfaceGui.SizingMode = Enum.SurfaceGuiSizingMode.FixedSize
surfaceGui.ZOffset = 2
surfaceGui.Parent = playerGui
local camera = Instance.new("Camera")
camera.Name = "SkyboxCamera"
camera.Parent = surfaceGui
local viewportFrame = Instance.new("ViewportFrame")
viewportFrame.BackgroundTransparency = 1
viewportFrame.Size = UDim2.fromScale(1, 1)
viewportFrame.Name = "Skybox"
viewportFrame.ZIndex = 1
viewportFrame.Ambient = Color3.new(0.75, 0.75, 0.75)
viewportFrame.LightColor = Color3.new(1, 1, 1)
viewportFrame.LightDirection = -Lighting:GetSunDirection()
viewportFrame.CurrentCamera = camera
viewportFrame.Parent = surfaceGui
self.surfaceGui = surfaceGui
self.skyboxCamera = camera
self.skyboxViewportFrame = viewportFrame
return self
end
-- Called exactly once per window.
local function window_set_sky(self, sky)
local skyCopy = sky:Clone()
skyCopy.Name = "Sky"
skyCopy.Parent = self.skyboxViewportFrame
local skyModel = sky_sphere(sky)
skyModel:ScaleTo(10)
skyModel.Parent = self.skyboxViewportFrame
end
-- Computes the world-space CFrame and size of the adornee's face.
local function window_get_surface(self)
local surfacePart = self.surfaceGui.Adornee
local v = -Vector3.FromNormalId(self.surfaceGui.Face)
local u = Vector3.new(v.Y, math.abs(v.X + v.Z), 0)
local relativeCF = CFrame.fromMatrix(Vector3.zero, u:Cross(v), u, v)
local worldCF = surfacePart.CFrame * CFrame.new(-v * surfacePart.Size / 2) * relativeCF
local size = Vector3.new(
math.abs(relativeCF.XVector:Dot(surfacePart.Size)),
math.abs(relativeCF.YVector:Dot(surfacePart.Size)),
math.abs(relativeCF.ZVector:Dot(surfacePart.Size))
)
return worldCF, size
end
-- Per-frame render; updates `skyboxCamera` from `camera`.
local function window_render(self, camera)
local surfaceCF, surfaceSize = window_get_surface(self)
local cameraCF = camera.CFrame
local cameraPos = cameraCF.Position
local dv = surfaceCF:VectorToObjectSpace(surfaceCF.Position - cameraPos)
local dvXZ = (dv * VEC_XZ).Unit
local dvYZ = dv * VEC_YZ
local dvZ = -dvXZ.Z
local camXZ = (surfaceCF:VectorToObjectSpace(cameraCF.LookVector) * VEC_XZ).Unit
local scale = camXZ:Dot(dvXZ) / dvZ
local tanArcCos = math.sqrt(1 - dvZ * dvZ) / dvZ
local xCross = surfaceCF.YVector:Cross(cameraCF.ZVector)
local xVec = xCross:Dot(xCross) > 0 and xCross.Unit or cameraCF.XVector
local levelCF = CFrame.fromMatrix(cameraPos, xVec, surfaceCF.YVector)
local topC = surfaceCF * Vector3.new(0, surfaceSize.Y / 2, 0)
local botC = surfaceCF * Vector3.new(0, -surfaceSize.Y / 2, 0)
local camTop = levelCF:PointToObjectSpace(topC)
local camBot = levelCF:PointToObjectSpace(botC)
local topD = (camTop * VEC_YZ).Unit
local botD = (camBot * VEC_YZ).Unit
local fovH = 2 * math.tan(math.rad(camera.FieldOfView / 2))
local surfFovH = math.tan(math.sign(topD.Y) * math.acos(-topD.Z))
- math.tan(math.sign(botD.Y) * math.acos(-botD.Z))
local ratio = surfFovH / fovH
local w, h = 1, 1
if self.surfaceGui.SizingMode == Enum.SurfaceGuiSizingMode.FixedSize then
local canvas = self.surfaceGui.CanvasSize
h = (canvas.Y / canvas.X) * (surfaceSize.X / surfaceSize.Y)
end
local dx = math.sign(dv.X * dv.Z) * tanArcCos
local dy = dvYZ.Y / dvYZ.Z * h
local dz = math.abs(scale * ratio * h)
local m = math.max(w, h, math.abs(dx), math.abs(dy), dz)
local renderRot = surfaceCF.Rotation
* CFrame.fromEulerAnglesYXZ(0, math.pi, 0)
* CFrame.new(0, 0, 0, w / m, 0, 0, 0, h / m, 0, dx / m, dy / m, dz / m)
self.skyboxCamera.FieldOfView = camera.FieldOfView
self.skyboxCamera.CFrame = (renderRot + cameraPos).Rotation
end
-- Part -> { [face: EnumItem (NormalId)] = Window }
local windows = {}
local function createwindow(part, face)
local partWindows = windows[part]
if partWindows == nil then
partWindows = {}
windows[part] = partWindows
end
local window = window_new(part, face)
partWindows[face] = window
window_set_sky(window, sky)
-- The game leaves the SurfaceGui in PlayerGui when the part is destroyed;
-- destroying it also removes its descendants (camera, viewport, sky).
part.Destroying:Connect(function()
partWindows[face] = nil
window.surfaceGui:Destroy()
end)
end
local sides = { "Front", "Back", "Left", "Right", "Top", "Bottom" }
for _, side in sides do
local tag = "RenderSky:" .. side
local face = Enum.NormalId[side]
for _, part in CollectionService:GetTagged(tag) do
createwindow(part, face)
end
CollectionService:GetInstanceAddedSignal(tag):Connect(function(part)
createwindow(part, face)
end)
end
RunService.RenderStepped:Connect(function()
-- workspace.CurrentCamera can be nil between frames.
local camera = workspace.CurrentCamera
if camera == nil then
return
end
for _, partWindows in windows do
for _, window in partWindows do
window_render(window, camera)
end
end
end)