Advertisement
cfox04

Untitled

Nov 26th, 2020
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local FixedSizeList = require(ReplicatedStorage.FixedSizeList)
  3. local Edge = require(ReplicatedStorage.Edge)
  4. local Polygon = require(ReplicatedStorage.Polygon)
  5. local DrawTriangle = require(ReplicatedStorage.DrawTriangle)
  6.  
  7. local Vertices = {}
  8. local Triangles = {}
  9. local Resolution
  10. local Radius
  11.  
  12. -- Indices of the vertex pairs that make up each of the initial 12 edges
  13. local VERTEX_PAIRS =  {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2, 3, 3, 4, 4, 1, 5, 1, 5, 2, 5, 3, 5, 4}
  14. -- Indices of the edge triplets that make up the initial 8 faces
  15. local EDGE_TRIPLETS =  {0, 1, 4, 1, 2, 5, 2, 3, 6, 3, 0, 7, 8, 9, 4, 9, 10, 5, 10, 11, 6, 11, 8, 7}
  16. -- The six initial vertices
  17. local BASE_VERTICES = {Vector3.FromNormalId(1), Vector3.FromNormalId(3), Vector3.FromNormalId(2), Vector3.FromNormalId(0), Vector3.FromNormalId(5), Vector3.FromNormalId(4)}
  18.  
  19. local vertices = FixedSizeList.new()
  20. local triangles = FixedSizeList.new()
  21.  
  22. local numDivisions
  23. local numVertsPerFace
  24.  
  25. local TerrainFolder = Instance.new("Folder", workspace)
  26. TerrainFolder.Name = "Terrain"
  27.  
  28. local SphereMesh = {}
  29.  
  30. local function DrawVertices()
  31.     for _, vertex in pairs(Vertices) do
  32.         local part = Instance.new("Part", TerrainFolder)
  33.         part.Size = Vector3.new(1, 1, 1)
  34.         part.BrickColor = BrickColor.new("Really red")
  35.         part.Material = Enum.Material.Neon
  36.         part.Anchored = true
  37.         part.Position = vertex
  38.     end
  39. end
  40.  
  41. local function DrawTriangles(polygon)
  42.     if not polygon.Next or not TerrainFolder then
  43.         return
  44.     end
  45.  
  46.     DrawTriangle.Draw(polygon.Previous.Position, polygon.Position, polygon.Next.Position, TerrainFolder)
  47.  
  48.     wait(0.001)
  49.  
  50.     DrawTriangles(polygon.Next)
  51. end
  52.  
  53. local function CreateVertex(vertex, parent)
  54.     local part = Instance.new("Part", parent)
  55.     part.Size = Vector3.new(0.5, 0.5, 0.5)
  56.     part.BrickColor = BrickColor.new("Really red")
  57.     part.Material = Enum.Material.Neon
  58.     part.Anchored = true
  59.     --part.Position = position + Vector3.new(100, 100, 100)
  60.     vertex = vertex.Unit
  61.     part.Position = Vector3.new(vertex.X * Radius, vertex.Y * Radius, vertex.Z * Radius)
  62.     wait(0.01)
  63. end
  64.  
  65. local function CreateFace(sideA, sideB, bottom, reverse)
  66.     local numPointsInEdge = #sideA.vertexIndices
  67.     local vertexMap = FixedSizeList.new() --new FixedSizeList<int> (numVertsPerFace)
  68.     vertexMap:Add(sideA.vertexIndices[0]) -- top of triangle
  69.  
  70.     for i = 1, numPointsInEdge - 1 do
  71.         -- Side A vertex
  72.         vertexMap:Add(sideA.vertexIndices[i])
  73.  
  74.         -- Add vertices between sideA and sideB
  75.         local sideAVertex = vertices.items[sideA.vertexIndices[i]]
  76.         local sideBVertex = vertices.items[sideB.vertexIndices[i]]
  77.         local numInnerPoints = i - 1
  78.         for j = 0, numInnerPoints do
  79.             local t = (j + 1) / (numInnerPoints + 1)
  80.             vertexMap:Add(vertices.nextIndex)
  81.             vertices:Add(sideAVertex:Lerp(sideBVertex, t))--vertices:Add (Slerp (sideAVertex, sideBVertex, t))
  82.         end
  83.  
  84.         -- Side B vertex
  85.         vertexMap:Add(sideB.vertexIndices[i])
  86.     end
  87.  
  88.     -- Add bottom edge vertices
  89.     for i = 1, numPointsInEdge do
  90.         vertexMap:Add(bottom.vertexIndices[i])
  91.     end
  92.  
  93.     -- Triangulate
  94.     local numRows = numDivisions + 1
  95.     for row = 0, numRows do
  96.         -- vertices down left edge follow quadratic sequence: 0, 1, 3, 6, 10, 15...
  97.         -- the nth term can be calculated with: (n^2 - n)/2
  98.         local topVertex = ((row + 1) * (row + 1) - row - 1) / 2
  99.         local bottomVertex = ((row + 2) * (row + 2) - row - 2) / 2
  100.  
  101.         local numTrianglesInRow = 1 + 2 * row
  102.         for column = 0, numTrianglesInRow do
  103.             local v0, v1, v2
  104.  
  105.             if column % 2 == 0 then
  106.                 v0 = topVertex
  107.                 v1 = bottomVertex + 1
  108.                 v2 = bottomVertex
  109.                 topVertex = topVertex + 1
  110.                 bottomVertex = bottomVertex + 1
  111.             else
  112.                 v0 = topVertex
  113.                 v1 = bottomVertex
  114.                 v2 = topVertex - 1
  115.             end
  116.  
  117.             triangles:Add(vertexMap.items[v0])
  118.             triangles:Add(vertexMap.items[v0])
  119.             triangles:Add(vertexMap.items[reverse and v2 or v1])
  120.             triangles:Add(vertexMap.items[reverse and v1 or v2])
  121.         end
  122.     end
  123. end
  124.  
  125. function SphereMesh.GenerateSphere(resolution, radius)
  126.     Resolution = resolution
  127.     Radius = radius
  128.     numDivisions = math.max(0, resolution)
  129.     numVertsPerFace = ((numDivisions + 3) * (numDivisions + 3) - (numDivisions + 3)) / 2
  130.     local numVerts = numVertsPerFace * 8 - (numDivisions + 2) * 12 + 6
  131.     local numTriesPerFace = (numDivisions + 1) * (numDivisions + 1)
  132.    
  133.     --vertices = new FixedSizeList<Vector3> (numVerts)
  134.     --triangles = new FixedSizeList<int> (numTrisPerFace * 8 * 3)
  135.  
  136.     vertices:AddRange(BASE_VERTICES)
  137.  
  138.     -- Create 12 edges, with n vertices added along them (n = numDivisions)
  139.     local edges = {} --Edge[] edges = new Edge[12]
  140.     for i = 1, #VERTEX_PAIRS, 2 do
  141.         local startVertex = vertices.items[VERTEX_PAIRS[i]]
  142.         local endVertex = vertices.items[VERTEX_PAIRS[i + 1]]
  143.  
  144.         local edgeVertexIndices = {}-- int[numDivisions + 2]
  145.         edgeVertexIndices[0] = VERTEX_PAIRS[i]
  146.  
  147.         -- Add vertices along edge
  148.         for divisionIndex = 0, numDivisions do
  149.             local t = (divisionIndex + 1.0) / (numDivisions + 1.0)
  150.             edgeVertexIndices[divisionIndex + 1] = vertices.nextIndex
  151.             vertices:Add(startVertex:Lerp(endVertex, t)) -- vertices:Add (Slerp (startVertex, endVertex, t))
  152.         end
  153.        
  154.         edgeVertexIndices[numDivisions + 1] = VERTEX_PAIRS[i + 1]
  155.         local edgeIndex = math.floor(i / 2)
  156.         edges[edgeIndex] = Edge.new(edgeVertexIndices)
  157.     end
  158.  
  159.     -- Create faces
  160.     for i = 1, #EDGE_TRIPLETS, 3 do
  161.         local faceIndex = i / 3
  162.         local reverse = faceIndex >= 4
  163.         CreateFace(edges[EDGE_TRIPLETS[i]], edges[EDGE_TRIPLETS[i + 1]], edges[EDGE_TRIPLETS[i + 2]], reverse)
  164.     end
  165.  
  166.     Vertices = vertices.items
  167.     Triangles = triangles.items
  168.    
  169.     for i, vertex in pairs(Vertices) do
  170.         vertex = vertex.Unit
  171.         Vertices[i] = Vector3.new(vertex.X * radius, vertex.Y * radius, vertex.Z * radius)
  172.     end
  173.    
  174.     DrawVertices()
  175.     --DrawTriangles(Polygon.CreatePolygonFromPoints(Vertices))
  176. end
  177.  
  178. return SphereMesh
Advertisement
RAW Paste Data Copied
Advertisement