Advertisement
oopsrainbow4

Terrain Triangle

Aug 15th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. local X, Z = 15, 15
  2.  
  3. local wedge = Instance.new("WedgePart");
  4. wedge.Anchored = true;
  5. wedge.TopSurface = Enum.SurfaceType.Smooth;
  6. wedge.BottomSurface = Enum.SurfaceType.Smooth;
  7.  
  8. local function draw3dTriangle(a, b, c)
  9.     local ab, ac, bc = b - a, c - a, c - b;
  10.     local abd, acd, bcd = ab:Dot(ab), ac:Dot(ac), bc:Dot(bc);
  11.  
  12.     if (abd > acd and abd > bcd) then
  13.         c, a = a, c;
  14.     elseif (acd > bcd and acd > abd) then
  15.         a, b = b, a;
  16.     end
  17.  
  18.     ab, ac, bc = b - a, c - a, c - b;
  19.  
  20.     local right = ac:Cross(ab).unit;
  21.     local up = bc:Cross(right).unit;
  22.     local back = bc.unit;
  23.  
  24.     local height = math.abs(ab:Dot(up));
  25.  
  26.     local w1 = wedge:Clone();
  27.     w1.Size = Vector3.new(0, height, math.abs(ab:Dot(back)));
  28.     w1.CFrame = CFrame.fromMatrix((a + b)/2, right, up, back);
  29.     w1.Parent = workspace;
  30.  
  31.     local w2 = wedge:Clone();
  32.     w2.Size = Vector3.new(0, height, math.abs(ac:Dot(back)));
  33.     w2.CFrame = CFrame.fromMatrix((a + c)/2, -right, up, -back);
  34.     w2.Parent = workspace;
  35.  
  36.     return w1, w2;
  37. end
  38.  
  39. local positionGrid = {}
  40.  
  41. for x = 0, X do
  42.     positionGrid[x] = {}
  43.    
  44.     for z = 0, Z do
  45.         positionGrid[x][z] = Vector3.new(x*5, math.noise(x/10, z/10) * 10, z*5)
  46.     end
  47. end
  48.  
  49. for x = 0, X-1 do
  50.     for z = 0, Z-1 do
  51.         local a = positionGrid[x][z]
  52.         local b = positionGrid[x+1][z]
  53.         local c = positionGrid[x][z+1]
  54.         local d = positionGrid[x+1][z+1]
  55.        
  56.         draw3dTriangle(a, b, c)
  57.         draw3dTriangle(b, c, d)
  58.     end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement