local function y(y,q,g)local J=q:Inverse()for y,q in next,y:GetDescendants()do q.CFrame=g*(J*q.CFrame)end end local function q(y,q,g,J)local m=Instance.new("\077\111\100\101\108")local p=(TAU*g)/y-GAP local I=(TAU*q)/y-GAP local v=p/g local Y=I/q local a=v-Y local P={}local C={}local T=math.ceil(p/J)for y=0,T,1 do P[y+1]=(CENTER*CFrame.fromEulerAnglesXYZ(0,0,(y/T)*v))*Vector3.new(g,0,0)C[y+1]=(CENTER*CFrame.fromEulerAnglesXYZ(0,0,a/2+(y/T)*Y))*Vector3.new(q,0,0)end for y=1,T,1 do local q=P[y]local g=C[y]local J=P[y+1]local p=C[y+1]Triangle(m,q,g,J)Triangle(m,g,J,p)end return m end local function g(g,J,m)m=m or 0 local p=(1-J)*EXTERIOR_RADIUS-1 local I=p-2 local v=q(g,(1-J)*EXTERIOR_RADIUS,EXTERIOR_RADIUS,PART_PER_UNIT)local Y=q(g,I,p,PART_PER_UNIT/2)local a=Instance.new("\070\114\097\109\101")local P=Instance.new("\070\114\097\109\101")local C=Instance.new("\070\114\097\109\101")P.BackgroundTransparency=1 C.BackgroundTransparency=1 P.Size=UDim2.new(1,0,1,0)C.Size=UDim2.new(1,0,1,0)P.Name="\082\097\100\105\097\108"C.Name="\065\116\116\097\099\104"P.Parent=a C.Parent=a local T=J*EXTERIOR_RADIUS local s=EXTERIOR_RADIUS-T local o=1-J/2 local i=(TAU*EXTERIOR_RADIUS)/g local M=i/EXTERIOR_RADIUS local A=(TAU*s)/g-GAP local R=A/s local c=Vector2.new(math.cos(R),math.sin(R))*s-Vector2.new(s,0)local d=math.min(c.Magnitude/(EXTERIOR_RADIUS*2),.18)for q=0,g-1,1 do local J=VPF:Clone()local p=CAMERA:Clone()J.CurrentCamera=p J.Name=q+1 local I=(q/g)*TAU+m local Y=v:Clone()y(Y,CENTER,CENTER*CFrame.fromEulerAnglesXYZ(0,0,I+EX_OFFSET))Y.Parent=J local a=((I-EX_OFFSET)+M/2)+G_OFFSET local T=(-math.cos(a)/2)*o local s=(math.sin(a)/2)*o local i=Instance.new("\070\114\097\109\101")i.Name=q+1 i.BackgroundTransparency=1 i.BackgroundColor3=Color3.new()i.BorderSizePixel=0 i.AnchorPoint=Vector2.new(.5,.5)i.Position=UDim2.new(.5+T,0,.5+s,0)i.Size=UDim2.new(d,0,d,0)i.Parent=C p.Parent=J J.Parent=P end v:Destroy()local l=VPF:Clone()local L=CAMERA:Clone()l.CurrentCamera=L l.Name="\082\097\100\105\097\108\068\105\097\108"local H=GAP/(2*p)local n=-TAU/4+H y(Y,CENTER,CENTER*CFrame.fromEulerAnglesXYZ(0,0,m+n))Y.Parent=l l.Parent=a a.BackgroundTransparency=1 a.SizeConstraint=Enum.SizeConstraint.RelativeYY a.Size=UDim2.new(1,0,1,0)return a end


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
