Advertisement
memesbruh03

terrain gen 2 (more laggy)

Sep 1st, 2016
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. local h = Instance.new("Hint",workspace)
  2. h.Text = "Here we go..."
  3.  
  4. --creates a new triangle from three points
  5. function triangle(a,b,c)
  6. --get the points in order, b to a needs to be the longest side
  7.     if (b-c).magnitude>(a-b).magnitude and (b-c).magnitude>(a-c).magnitude then  
  8.         c,a=a,c;  
  9.     elseif (a-c).magnitude>(a-b).magnitude then  
  10.         c,b=b,c;
  11.     end
  12. --get some extra points
  13.     local mid=(a+b)/2;--point between a and b, triangles split on c-mid
  14.     local v0=c+(a-mid);--point squaring up the triangle
  15.     local v1=c+(b-mid);--point squaring up the triangle
  16.  
  17. --[[    v0   C    v1
  18.         *    *    *
  19.             / \
  20.            /   \
  21.         A / mid \ B
  22.          *___*___*
  23. --]]
  24.  
  25. --create model
  26.     local m=Instance.new("Model");
  27.     m.Name="Triangle";
  28. --create the triangles
  29. --a,mid,c
  30.     local p=Instance.new("WedgePart",m);
  31.     p.Anchored=true;
  32.     p.formFactor="Custom";
  33.     p.BottomSurface="Smooth";
  34.     p.Size=Vector3.new(0.2,(c-mid).magnitude,(a-mid).magnitude);
  35.     local pos=(a+mid+c+v0)/4;--position
  36.     local f_vec=CFrame.new(pos,(v0+a)/2).lookVector*(-1);--front lookVector
  37.     local t_vec=CFrame.new(pos,(v0+c)/2).lookVector;--top lookVector
  38.     local r_vec=t_vec:Cross(f_vec);--right lookVector
  39.     p.CFrame=CFrame.new(
  40.         pos.x,pos.y,pos.z,
  41.         r_vec.x,t_vec.x,f_vec.x,
  42.         r_vec.y,t_vec.y,f_vec.y,
  43.         r_vec.z,t_vec.z,f_vec.z
  44.     );
  45. --b,mid,c
  46.     local p=Instance.new("WedgePart",m);
  47.     p.Anchored=true;
  48.     p.BottomSurface="Smooth";
  49.     p.Size=Vector3.new(0.2,(c-mid).magnitude,(b-mid).magnitude);
  50.     local pos=(b+mid+c+v1)/4;--position
  51.     local f_vec=CFrame.new(pos,(v1+b)/2).lookVector*(-1);--front lookVector
  52.     local t_vec=CFrame.new(pos,(v1+c)/2).lookVector;--top lookVector
  53.     local r_vec=t_vec:Cross(f_vec);--right lookVector
  54.     p.CFrame=CFrame.new(
  55.         pos.x,pos.y,pos.z,
  56.         r_vec.x,t_vec.x,f_vec.x,
  57.         r_vec.y,t_vec.y,f_vec.y,
  58.         r_vec.z,t_vec.z,f_vec.z
  59.     );
  60. --return the model
  61.     return m;
  62. end
  63.  
  64. --colors a brick perdy colors
  65. function color(p)
  66. if p.Position.Y>50 then
  67. p.BrickColor=BrickColor.new("Medium green");
  68. p.Material = "Grass";
  69. elseif p.Position.Y>5 then
  70. p.BrickColor=BrickColor.new("Bright green");
  71. p.Material = "Grass";
  72. else
  73. p.BrickColor=BrickColor.new("Cool yellow");
  74. p.Material = "Sand";
  75. end end
  76.  
  77.  
  78. ----------------------------
  79. --sploch terrain generator--
  80. ----------------------------
  81. local sploches=10;--number of sploches to be used
  82. local sploch_radius=15;--radius of one sploch
  83. local sploch_height=20;--max height of one sploch
  84. local xSize=40;--width
  85. local ySize=40;--length
  86. local map={};--the map of the terrain
  87.  
  88. --default the map to 0's
  89. for y=-ySize/2,ySize/2 do
  90. map[y]={};
  91. for x=-xSize/2,xSize/2 do
  92. map[y][x]=0;
  93. end end
  94.  
  95. --for every sploch, build up the map in a hill at random spot
  96. for _=1,sploches do
  97. local c=Vector2.new(math.random(-xSize/2,xSize/2),math.random(-ySize/2,ySize/2));--center of sploch
  98. --for all of the surrounding points
  99. for x=c.x-sploch_radius,c.x+sploch_radius do
  100. for y=c.y-sploch_radius,c.y+sploch_radius do
  101. --is it in the map?
  102. if x<=xSize/2 and x>=-xSize/2 and y<=ySize/2 and y>=-ySize/2 then
  103. local v=Vector2.new(x,y);--the current node
  104. --build up point based on distance from
  105. map[y][x]=map[y][x]+((v-c).magnitude<=sploch_radius and(1-(v-c).magnitude/sploch_radius)*sploch_height or 0);
  106. end end end
  107. end
  108.  
  109. --build terrain based on points
  110. for x=-xSize/2,xSize/2 do
  111. wait();
  112. for y=-ySize/2,ySize/2 do
  113. if map[y+1] and map[y][x+1] then
  114. local t0=triangle(Vector3.new(x*10,map[y][x],y*10),Vector3.new((x+1)*10,map[y][x+1],y*10),Vector3.new(x*10,map[y+1][x],(y+1)*10));
  115. for _,v in pairs(t0:GetChildren()) do color(v); end
  116. t0.Parent=workspace;
  117. local t1=triangle(Vector3.new((x+1)*10,map[y+1][x+1],(y+1)*10),Vector3.new((x+1)*10,map[y][x+1],y*10),Vector3.new(x*10,map[y+1][x],(y+1)*10)
  118. );
  119. for _,v in pairs(t1:GetChildren()) do color(v); end
  120. t1.Parent=workspace;
  121. end end end
  122.  
  123. --water
  124. local b=Instance.new("Part",workspace);
  125. b.Anchored=true;
  126. b.CanCollide=false;
  127. b.BrickColor=BrickColor.new("Bright blue");
  128. b.Transparency=0.5;
  129. b.Reflectance = 0.5;
  130. b.Size=Vector3.new(xSize*10,0.2,ySize*10);
  131. b.CFrame=CFrame.new(0,3,0);
  132. b.Material = "SmoothPlastic";
  133.  
  134. workspace.Base:Destroy()
  135. h.Text = "It's complete!!!"
  136. wait(2)
  137. h:Remove()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement