Advertisement
eea

newrend

eea
May 8th, 2023 (edited)
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. local tool = Instance.new("Tool", owner.Backpack)
  2. local handle = Instance.new("Part", tool)
  3. handle.Size = Vector3.one
  4. handle.Name = "Handle"
  5. local cam = handle
  6. local fov = math.rad(90)
  7. local rpp = 40
  8. local maxb = 100
  9. local s_size = 9
  10. local px = s_size/80
  11. local z = 1
  12. local dist = 1000
  13. local t = 0
  14. local colors = {}
  15. local total = (s_size/px)^(1+1)
  16. local fold = Instance.new("WorldModel", script)
  17. fold.Name = "parts"
  18.  
  19. function threshold(a, b)
  20.     return math.sqrt((b.R - a.R)^2 + (b.G - a.G)^2 + (b.B - a.B)^2) <= 0.000
  21. end
  22.  
  23. function GM(cols)
  24.     local blocks = {}
  25.     for i = 1,#cols do
  26.         local started = 0
  27.         for j = 1,#cols[i] do
  28.             local prev = cols[i][j-1]
  29.             local cur = cols[i][j]
  30.             if not threshold(prev, cur) then
  31.                 table.insert(blocks, {i, started, j, cols[i][j-1]})
  32.                 started = j
  33.             end
  34.             if j == #cols[i] then
  35.                 table.insert(blocks, {i, started, j, cols[i][j]})
  36.             end
  37.         end
  38.     end
  39.     print("-----------------")
  40.     print(tostring(#blocks).." greedy meshing")
  41.     print(tostring(math.round((1 - #blocks/total) * 100)).."% efficiency")
  42.     return blocks
  43. end
  44.  
  45. function getraydir(cf: CFrame, x, y)
  46.     local up = CFrame.Angles(fov*0.5, 0, 0)
  47.     local right = CFrame.Angles(0, -fov*0.5, 0)
  48.     local down = CFrame.Angles(-fov*0.5, 0, 0)
  49.     local left = CFrame.Angles(0, fov*0.5, 0)
  50.     local v = CFrame.new(left.LookVector:Lerp(right.LookVector, x).X, down.LookVector:Lerp(up.LookVector, y).Y, up.LookVector.Z)
  51.     return (cf.Rotation * v).Position
  52. end
  53.  
  54. function randomdir()
  55.     local v = Vector3.new(math.random()-.5, math.random()-.5, math.random()-.5)
  56.     return v.Unit
  57. end
  58.  
  59. function randomdironsurface(normal)
  60.     return (randomdir() + normal).Unit
  61. end
  62.  
  63. function reflect(v, normal)
  64.     return v - v:Dot(normal) * normal * 2
  65. end
  66.  
  67. function v3(col)
  68.     return Vector3.new(col.R, col.G, col.B)
  69. end
  70.  
  71. function col(v3)
  72.     return Color3.new(v3.X, v3.Y, v3.Z)
  73. end
  74.  
  75. function round(x, t)
  76.     return math.round(x/t)*t
  77. end
  78.  
  79. function background(dir, origin)
  80.     local sky = Color3.new(1, 1, 1):Lerp(Color3.new(0, 0.38-.1, .6), round((math.abs(dir.Unit.Y))^(.15), .03))
  81.     local focus = 100
  82.     local intensity = 100
  83.     local sundir = game:GetService("Lighting"):GetSunDirection()
  84.     local sun = ((dir.Unit:Dot(sundir) + 1)*0.5)^focus * intensity
  85.     sun = Color3.new(sun, sun, sun)
  86.     return col((v3(sky) + v3(sun)*0.5):Min(Vector3.one))
  87.     --return sun
  88. end
  89.  
  90. function path(ray: RaycastResult, dir, params, camcf)
  91.     local color = Vector3.new(1,1,1)
  92.     local totallight = Vector3.new()
  93.     local rray = ray
  94.     local rdir = dir
  95.     local rorigin = camcf.Position
  96.     if ray == nil then
  97.         return background(dir, camcf)
  98.     end
  99.     for i = 1, maxb do
  100.         if rray then
  101.             if not (rray.Instance:GetAttribute("lcol")) then
  102.                 rray.Instance:SetAttribute("lcol", Vector3.zero)
  103.             end
  104.             if not (rray.Instance:GetAttribute("lstr")) then
  105.                 rray.Instance:SetAttribute("lstr", 0)
  106.             end
  107.             local pl = rray.Instance:FindFirstChildOfClass("PointLight")
  108.             if pl then
  109.                 pl.Parent:SetAttribute("lcol", v3(pl.Color))
  110.                 pl.Parent:SetAttribute("lstr", pl.Brightness)
  111.             end
  112.             local newdir = randomdironsurface(ray.Normal)
  113.             local newray = workspace:Raycast(ray.Position, newdir*dist, params)
  114.             local rrayhit = rray.Instance
  115.             local lightstrength = rrayhit:GetAttribute("lcol") * rrayhit:GetAttribute("lstr")
  116.             totallight += lightstrength * color
  117.             color *= v3(rray.Instance.Color)
  118.             rray = newray
  119.             rdir = newdir
  120.             rorigin = ray.Position
  121.         else
  122.             totallight += v3(background(rdir, rorigin)) * color
  123.             break
  124.         end
  125.     end
  126.     return totallight
  127. end
  128.  
  129. function rend()
  130. local camcf = cam.CFrame
  131. for x = 0,s_size,px do
  132.     colors[math.round(x/px)] = {}
  133.     for y = 0,s_size,px do
  134.         t+=1
  135.         local cur_p = camcf * CFrame.new(x - s_size*0.5, y - s_size*0.5, -z)
  136.         local raydir = getraydir(camcf, x/s_size, y/s_size)
  137.         local params = RaycastParams.new()
  138.         params.FilterDescendantsInstances = {fold, cam}
  139.         params.FilterType = Enum.RaycastFilterType.Exclude
  140.         local ray = workspace:Raycast(camcf.Position, raydir*dist, params)
  141.         local color = Vector3.new()
  142.         if ray ~= nil then
  143.             for i = 1,rpp do
  144.                 color += path(ray, raydir*dist, params, camcf)
  145.             end
  146.             else
  147.             color = v3(background(raydir, camcf.Position))*rpp
  148.         end
  149.         color /= rpp
  150.         color = color:Min(color, Vector3.one)
  151.         color = col(color)
  152.         colors[math.round(x/px)][math.round(y/px)] = color
  153.         local p = round(t/total, .01)
  154.         handle.Size = Vector3.new(1, p, 1)
  155.         if (t % (10)) == 0 then
  156.             task.wait()
  157.         end
  158.     end
  159. end
  160. t = 0
  161. local gmd = GM(colors)
  162. local function the(v)
  163.     return v*px
  164. end
  165. for i = 1,#gmd do
  166.     t+=1
  167.     local current = gmd[i]
  168.     local x = the(current[1])
  169.     local y = the(current[1+1] + current[3])/2
  170.     --print(x, y)
  171.     local cur_p = camcf * CFrame.new(x - s_size*0.5, y - s_size*0.5, -z)
  172.     local part = Instance.new('Part', fold)
  173.     part.Size = Vector3.new(px, px*math.abs(current[1+1] - current[3]), .01)
  174.     --part.Position = raydir*10 + camcf.Position
  175.     part.CFrame = cur_p
  176.     part.Color = current[4]
  177.     part.Anchored = true
  178.     if t % 4 == 0 then
  179.         task.wait()
  180.     end
  181. end
  182. end
  183.  
  184. tool.Activated:Connect(rend)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement