Hello!
I am currently trying to make a Viewport frame which acts as a mirror. This does work, however the reflection is flipped the wrong way. Is it possible to invert the X axis?
I have a LocalScript in the SurfaceGUI with the Viewport frame for the mirror:
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local instance,newRay = Instance.new,Ray.new
local V2,V3,CF,UDIM2 = Vector2.new,Vector3.new,CFrame.new,UDim2.new
local Insert,Rand,ABS = table.insert,math.random,math.abs
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local ViewportFrame = script.Parent.ViewportFrame
local ReflectionCamera = workspace.ReflectionCam
local Screen = workspace.Screen
local Camera = instance("Camera")
Camera.CFrame = ReflectionCamera.CFrame
ViewportFrame.CurrentCamera = Camera
ReflectionCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
Camera.CFrame = ReflectionCamera.CFrame
end)
local DEG = math.deg
local function inFOV (p0, p1)
local X1, Y1, Z1 = p0:ToOrientation()
local CF = CF(p0.p, p1.p)
local X2, Y2, Z2 = CF:ToOrientation()
return V3(DEG(X1-X2), DEG(Y1-Y2), DEG(Z1-Z2))
end
local Humanoids = {}
local Parts = {}
local function RenderVersion(Object)
local Descendants = Object:GetDescendants()
for i = 1, #Descendants do
local Children = Descendants[i]
if (Children:IsA("Script") or Children:IsA("Sound") or Children:IsA("ManualWeld") or Children:IsA("BasePart"))then
Children:Destroy()
end
end
return Object
end
local function RenderHumanoid(Model, Parent, MainModel)
local ModelParts = Model:GetChildren()
for i = 1, #ModelParts do
local Part = ModelParts[i]
if not Part:IsA("Script") then
local Arch = Part.Archivable
Part.Archivable = true
local RenderClone = Part:Clone()
Part.Archivable = Arch
if Part:IsA("MeshPart") or Part:IsA("Part") then
PartUpdater = RunService.Heartbeat:Connect(function()
if Part then
RenderClone.CFrame = Part.CFrame
else
RenderClone:Destroy()
PartUpdater:Disconnect()
end
end)
elseif Part:IsA("Accoutrement") then
PartUpdater = RunService.Heartbeat:Connect(function()
if Part then
RenderClone.Handle.CFrame = Part.Handle.CFrame
else
RenderClone:Destroy()
PartUpdater:Disconnect()
end
end)
elseif Part:IsA("Script") then
RenderClone:Destroy()
end
RenderClone.Parent = Parent
end
end
end
local function RenderObj(Obj, Parent, Map)
local ObjParts = Obj:GetChildren()
for i = 1, #ObjParts do
local Part = ObjParts[i]
if Map then
if Part:IsA("BasePart") or Part:IsA("Model") or Part:IsA("Folder") then
local Arch2 = Part.Archivable
Part.Archivable = true
local RenderClone = RenderVersion(Part:Clone())
Part.Archivable = Arch2
RenderObj(Part, RenderClone, Map)
RenderClone.Parent = Parent
end
else
if not Part:IsDescendantOf(workspace.Map) and Part ~= workspace.Map then
if not Parts[Part] and not Humanoids[Part] then
if Part:IsA("Model") or Part:IsA("Folder") then
if Part:FindFirstChildWhichIsA("Humanoid", true) then
Humanoids[Part] = true
local ModelClone = instance("Model")
ModelClone.Name = Part.Name
RenderHumanoid(Part, ModelClone,ModelClone)
ModelClone.Parent = Parent
else
local ModelClone = instance("Model")
ModelClone.Name = Part.Name
Part.ChildAdded:Connect(function(Child)
wait()
RenderObj(Part, ModelClone, Map)
end)
RenderObj(Part, ModelClone, false)
ModelClone.Parent = Parent
end
elseif Part:IsA("BasePart") then
if Part ~= workspace.Terrain then
Parts[Part] = true
local Arch3 = Part.Archivable
Part.Archivable = true
local RenderClone = RenderVersion(Part:Clone())
Part.Archivable = Arch3
Part.ChildAdded:Connect(function(Child)
wait()
RenderObj(Part, RenderClone, Map)
end)
local LastCF, LastTP, LastColor, LastSize, LastMat = Part.CFrame,Part.Transparency,Part.Color,Part.Size,Part.Material
spawn(function()
while wait() do
if Part then
local FOV = inFOV(Camera.CFrame,Part.CFrame)
if (ABS(FOV.Y+FOV.X) *2 ) < (Camera.FieldOfView + 40) then
if LastCF ~= Part.CFrame then
LastCF = Part.CFrame
RenderClone.CFrame = Part.CFrame
end
if LastTP ~= Part.Transparency then
LastTP = Part.Transparency
RenderClone.Transparency = Part.Transparency
end
if LastColor ~= Part.Color then
LastColor = Part.Color
RenderClone.Color = Part.Color
end
if LastSize ~= Part.Size then
LastSize = Part.Size
RenderClone.Size = Part.Size
end
if LastMat ~= Part.Material then
LastMat = Part.Material
RenderClone.Material = Part.Material
end
end
else
RenderClone:Destroy()
Parts[Part] = false
break
end
end
end)
RenderObj(Part,RenderClone,Map)
RenderClone.Parent = Parent
end
end
end
end
end
end
end
wait(1)
local MapClone = Instance.new("Mode\l")
MapClone.Name = 'Map'
RenderObj(workspace.Map, MapClone,true)
MapClone.Parent = ViewportFrame
RenderObj(workspace, ViewportFrame)
workspace.ChildAdded:Connect(function(Child)
wait()
RenderObj(workspace, ViewportFrame)
end)
If I could get some help with this, it would be great. Thanks!
