local W=require(script.Parent:WaitForChild("\067\079\078\083\084\065\078\084\083"))local U=require(script.Parent:WaitForChild("\084\114\105\097\110\103\108\101"))local V=math.pi/2 local H=W.TAU local M=W.GAP local x=W.PART_PER_UNIT local S=W.CENTER local v=W.EXTERIOR_RADIUS local j=W.EX_OFFSET local c=W.G_OFFSET local d=Instance.new("\086\105\101\119\112\111\114\116\070\114\097\109\101")d.Ambient=Color3.new(1,1,1)d.LightColor=Color3.new(1,1,1)d.LightDirection=Vector3.new(0,0,-1)d.BackgroundTransparency=1 d.Size=UDim2.new(1,0,1,0)local G=Instance.new("\067\097\109\101\114\097")G.CameraType=Enum.CameraType.Scriptable G.CFrame=CFrame.new()G.FieldOfView=W.FOV local function a(W,U,V)local H=U:Inverse()for W,U in next,W:GetDescendants()do U.CFrame=V*(H*U.CFrame)end end local function s(W,V,x,v)local j=Instance.new("\077\111\100\101\108")local c=(H*x)/W-M local d=(H*V)/W-M local G=c/x local a=d/V local s=G-a local b={}local N={}local o=math.ceil(c/v)for W=0,o,1 do b[W+1]=(S*CFrame.fromEulerAnglesXYZ(0,0,(W/o)*G))*Vector3.new(x,0,0)N[W+1]=(S*CFrame.fromEulerAnglesXYZ(0,0,s/2+(W/o)*a))*Vector3.new(V,0,0)end for W=1,o,1 do local V=b[W]local H=N[W]local M=b[W+1]local x=N[W+1]U(j,V,H,M)U(j,H,M,x)end return j end local function b(W,U,V)V=V or 0 local b=(1-U)*v-1 local N=b-2 local o=s(W,(1-U)*v,v,x)local g=s(W,N,b,x/2)local q=Instance.new("\070\114\097\109\101")local C=Instance.new("\070\114\097\109\101")local r=Instance.new("\070\114\097\109\101")C.BackgroundTransparency=1 r.BackgroundTransparency=1 C.Size=UDim2.new(1,0,1,0)r.Size=UDim2.new(1,0,1,0)C.Name="\082\097\100\105\097\108"r.Name="\065\116\116\097\099\104"C.Parent=q r.Parent=q local K=U*v local X=v-K local t=1-U/2 local R=(H*v)/W local T=R/v local w=(H*X)/W-M local e=w/X local D=Vector2.new(math.cos(e),math.sin(e))*X-Vector2.new(X,0)local O=math.min(D.Magnitude/(v*2),.18)for U=0,W-1,1 do local M=d:Clone()local x=G:Clone()M.CurrentCamera=x M.Name=U+1 local v=(U/W)*H+V local s=o:Clone()a(s,S,S*CFrame.fromEulerAnglesXYZ(0,0,v+j))s.Parent=M local b=((v-j)+T/2)+c local N=(-math.cos(b)/2)*t local g=(math.sin(b)/2)*t local q=Instance.new("\070\114\097\109\101")q.Name=U+1 q.BackgroundTransparency=1 q.BackgroundColor3=Color3.new()q.BorderSizePixel=0 q.AnchorPoint=Vector2.new(.5,.5)q.Position=UDim2.new(.5+N,0,.5+g,0)q.Size=UDim2.new(O,0,O,0)q.Parent=r x.Parent=M M.Parent=C end o:Destroy()local z=d:Clone()local I=G:Clone()z.CurrentCamera=I z.Name="\082\097\100\105\097\108\068\105\097\108"local F=M/(2*b)local B=-H/4+F a(g,S,S*CFrame.fromEulerAnglesXYZ(0,0,V+B))g.Parent=z z.Parent=q q.BackgroundTransparency=1 q.SizeConstraint=Enum.SizeConstraint.RelativeYY q.Size=UDim2.new(1,0,1,0)return q end return b
-- CONSTANTS

local CONSTANTS = require(script.Parent:WaitForChild("CONSTANTS"))
local Triangle = require(script.Parent:WaitForChild("Triangle"))

local PI2 = math.pi/2
local TAU = CONSTANTS.TAU

local GAP = CONSTANTS.GAP
local PART_PER_UNIT = CONSTANTS.PART_PER_UNIT

local CENTER = CONSTANTS.CENTER
local EXTERIOR_RADIUS = CONSTANTS.EXTERIOR_RADIUS
local EX_OFFSET = CONSTANTS.EX_OFFSET
local G_OFFSET = CONSTANTS.G_OFFSET

local VPF = Instance.new("ViewportFrame")
VPF.Ambient = Color3.new(1, 1, 1)
VPF.LightColor = Color3.new(1, 1, 1)
VPF.LightDirection = Vector3.new(0, 0, -1)
VPF.BackgroundTransparency = 1
VPF.Size = UDim2.new(1, 0, 1, 0)

local CAMERA = Instance.new("Camera")
CAMERA.CameraType = Enum.CameraType.Scriptable
CAMERA.CFrame = CFrame.new()
CAMERA.FieldOfView = CONSTANTS.FOV

--

local function pivotAround(model, pivotCF, newCF)
	local invPivotCF = pivotCF:Inverse()
	for _, part in next, model:GetDescendants() do
		part.CFrame = newCF * (invPivotCF * part.CFrame)
	end
end

local function createSection(subN, interior_radius, exterior_radius, ppu)
	local subModel = Instance.new("Model")
	
	local exCircum = (TAU*exterior_radius)/subN - GAP
	local inCircum = (TAU*interior_radius)/subN - GAP

	local exTheta = exCircum / (exterior_radius)
	local inTheta = inCircum / (interior_radius)
	local diffTheta = exTheta - inTheta
	
	local exPoints = {}
	local inPoints = {}
	
	local nParts = math.ceil(exCircum/ppu)
	
	for i = 0, nParts do
		exPoints[i + 1] = CENTER * CFrame.fromEulerAnglesXYZ(0, 0, (i/nParts)*exTheta) * Vector3.new(exterior_radius, 0, 0)
		inPoints[i + 1] = CENTER * CFrame.fromEulerAnglesXYZ(0, 0, diffTheta/2 + (i/nParts)*inTheta) * Vector3.new(interior_radius, 0, 0)
	end
	
	for i = 1, nParts do
		local a = exPoints[i]
		local b = inPoints[i]
		local c = exPoints[i + 1]
		local d = inPoints[i + 1]
		
		Triangle(subModel, a, b, c)
		Triangle(subModel, b, c, d)
	end
	
	return subModel
end

local function createRadial(subN, tPercent, rotation)
	rotation = rotation or 0
	
	local dialEx = (1 - tPercent)*EXTERIOR_RADIUS - 1
	local dialIn = dialEx - 2
	
	local section = createSection(subN, (1 - tPercent)*EXTERIOR_RADIUS, EXTERIOR_RADIUS, PART_PER_UNIT)
	local innerSection = createSection(subN, dialIn, dialEx, PART_PER_UNIT/2)
	
	local frame = Instance.new("Frame")
	local radialFrame = Instance.new("Frame")
	local attachFrame = Instance.new("Frame")
	radialFrame.BackgroundTransparency = 1
	attachFrame.BackgroundTransparency = 1
	radialFrame.Size = UDim2.new(1, 0, 1, 0)
	attachFrame.Size = UDim2.new(1, 0, 1, 0)
	radialFrame.Name = "Radial"
	attachFrame.Name = "Attach"
	radialFrame.Parent = frame
	attachFrame.Parent = frame
	
	local thickness = tPercent * EXTERIOR_RADIUS
	local interior_radius = EXTERIOR_RADIUS - thickness
	local inv_tPercent = 1 - tPercent/2
	
	local exCircum = (TAU*EXTERIOR_RADIUS)/subN
	local exTheta = exCircum / EXTERIOR_RADIUS
	local inCircum = (TAU*interior_radius)/subN - GAP
	local inTheta = inCircum / (interior_radius)
	
	local edge = Vector2.new(math.cos(inTheta), math.sin(inTheta))*interior_radius - Vector2.new(interior_radius, 0)
	local edgeLen = math.min(edge.Magnitude / (EXTERIOR_RADIUS*2), 0.18)
	
	for i = 0, subN - 1 do
		local vpf = VPF:Clone()
		local cam = CAMERA:Clone()
		vpf.CurrentCamera = cam
		vpf.Name = i + 1
		
		local theta = (i/subN)*TAU + rotation
		
		local sub = section:Clone()
		pivotAround(sub, CENTER, CENTER * CFrame.fromEulerAnglesXYZ(0, 0, theta + EX_OFFSET))
		sub.Parent = vpf
		
		local t = theta - EX_OFFSET + exTheta/2 + G_OFFSET
		local c = -math.cos(t)/2 * inv_tPercent
		local s = math.sin(t)/2 * inv_tPercent
		
		local attach = Instance.new("Frame")
		attach.Name = i + 1
		attach.BackgroundTransparency = 1
		attach.BackgroundColor3 = Color3.new()
		attach.BorderSizePixel = 0
		attach.AnchorPoint = Vector2.new(0.5, 0.5)
		attach.Position = UDim2.new(0.5 + c, 0, 0.5 + s, 0)
		attach.Size = UDim2.new(edgeLen, 0, edgeLen, 0)
		attach.Parent = attachFrame
		
		cam.Parent = vpf
		vpf.Parent = radialFrame
	end
	
	section:Destroy()
	
	local vpf = VPF:Clone()
	local cam = CAMERA:Clone()
	vpf.CurrentCamera = cam
	vpf.Name = "RadialDial"
	
	local g = GAP / (2*dialEx)
	local off = -TAU/4 + g
	
	pivotAround(innerSection, CENTER, CENTER * CFrame.fromEulerAnglesXYZ(0, 0, rotation + off))
	innerSection.Parent = vpf
	vpf.Parent = frame
	
	frame.BackgroundTransparency = 1
	frame.SizeConstraint = Enum.SizeConstraint.RelativeYY
	frame.Size = UDim2.new(1, 0, 1, 0)
	return frame
end

--

return createRadial