Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tool = Instance.new("Tool", owner.Backpack)
- local handle = Instance.new("Part", tool)
- handle.Size = Vector3.one
- handle.Name = "Handle"
- local cam = handle
- local fov = math.rad(90)
- local rpp = 40
- local maxb = 100
- local s_size = 9
- local px = s_size/80
- local z = 1
- local dist = 1000
- local t = 0
- local colors = {}
- local total = (s_size/px)^(1+1)
- local fold = Instance.new("WorldModel", script)
- fold.Name = "parts"
- function threshold(a, b)
- return math.sqrt((b.R - a.R)^2 + (b.G - a.G)^2 + (b.B - a.B)^2) <= 0.000
- end
- function GM(cols)
- local blocks = {}
- for i = 1,#cols do
- local started = 0
- for j = 1,#cols[i] do
- local prev = cols[i][j-1]
- local cur = cols[i][j]
- if not threshold(prev, cur) then
- table.insert(blocks, {i, started, j, cols[i][j-1]})
- started = j
- end
- if j == #cols[i] then
- table.insert(blocks, {i, started, j, cols[i][j]})
- end
- end
- end
- print("-----------------")
- print(tostring(#blocks).." greedy meshing")
- print(tostring(math.round((1 - #blocks/total) * 100)).."% efficiency")
- return blocks
- end
- function getraydir(cf: CFrame, x, y)
- local up = CFrame.Angles(fov*0.5, 0, 0)
- local right = CFrame.Angles(0, -fov*0.5, 0)
- local down = CFrame.Angles(-fov*0.5, 0, 0)
- local left = CFrame.Angles(0, fov*0.5, 0)
- local v = CFrame.new(left.LookVector:Lerp(right.LookVector, x).X, down.LookVector:Lerp(up.LookVector, y).Y, up.LookVector.Z)
- return (cf.Rotation * v).Position
- end
- function randomdir()
- local v = Vector3.new(math.random()-.5, math.random()-.5, math.random()-.5)
- return v.Unit
- end
- function randomdironsurface(normal)
- return (randomdir() + normal).Unit
- end
- function reflect(v, normal)
- return v - v:Dot(normal) * normal * 2
- end
- function v3(col)
- return Vector3.new(col.R, col.G, col.B)
- end
- function col(v3)
- return Color3.new(v3.X, v3.Y, v3.Z)
- end
- function round(x, t)
- return math.round(x/t)*t
- end
- function background(dir, origin)
- local sky = Color3.new(1, 1, 1):Lerp(Color3.new(0, 0.38-.1, .6), round((math.abs(dir.Unit.Y))^(.15), .03))
- local focus = 100
- local intensity = 100
- local sundir = game:GetService("Lighting"):GetSunDirection()
- local sun = ((dir.Unit:Dot(sundir) + 1)*0.5)^focus * intensity
- sun = Color3.new(sun, sun, sun)
- return col((v3(sky) + v3(sun)*0.5):Min(Vector3.one))
- --return sun
- end
- function path(ray: RaycastResult, dir, params, camcf)
- local color = Vector3.new(1,1,1)
- local totallight = Vector3.new()
- local rray = ray
- local rdir = dir
- local rorigin = camcf.Position
- if ray == nil then
- return background(dir, camcf)
- end
- for i = 1, maxb do
- if rray then
- if not (rray.Instance:GetAttribute("lcol")) then
- rray.Instance:SetAttribute("lcol", Vector3.zero)
- end
- if not (rray.Instance:GetAttribute("lstr")) then
- rray.Instance:SetAttribute("lstr", 0)
- end
- local pl = rray.Instance:FindFirstChildOfClass("PointLight")
- if pl then
- pl.Parent:SetAttribute("lcol", v3(pl.Color))
- pl.Parent:SetAttribute("lstr", pl.Brightness)
- end
- local newdir = randomdironsurface(ray.Normal)
- local newray = workspace:Raycast(ray.Position, newdir*dist, params)
- local rrayhit = rray.Instance
- local lightstrength = rrayhit:GetAttribute("lcol") * rrayhit:GetAttribute("lstr")
- totallight += lightstrength * color
- color *= v3(rray.Instance.Color)
- rray = newray
- rdir = newdir
- rorigin = ray.Position
- else
- totallight += v3(background(rdir, rorigin)) * color
- break
- end
- end
- return totallight
- end
- function rend()
- local camcf = cam.CFrame
- for x = 0,s_size,px do
- colors[math.round(x/px)] = {}
- for y = 0,s_size,px do
- t+=1
- local cur_p = camcf * CFrame.new(x - s_size*0.5, y - s_size*0.5, -z)
- local raydir = getraydir(camcf, x/s_size, y/s_size)
- local params = RaycastParams.new()
- params.FilterDescendantsInstances = {fold, cam}
- params.FilterType = Enum.RaycastFilterType.Exclude
- local ray = workspace:Raycast(camcf.Position, raydir*dist, params)
- local color = Vector3.new()
- if ray ~= nil then
- for i = 1,rpp do
- color += path(ray, raydir*dist, params, camcf)
- end
- else
- color = v3(background(raydir, camcf.Position))*rpp
- end
- color /= rpp
- color = color:Min(color, Vector3.one)
- color = col(color)
- colors[math.round(x/px)][math.round(y/px)] = color
- local p = round(t/total, .01)
- handle.Size = Vector3.new(1, p, 1)
- if (t % (10)) == 0 then
- task.wait()
- end
- end
- end
- t = 0
- local gmd = GM(colors)
- local function the(v)
- return v*px
- end
- for i = 1,#gmd do
- t+=1
- local current = gmd[i]
- local x = the(current[1])
- local y = the(current[1+1] + current[3])/2
- --print(x, y)
- local cur_p = camcf * CFrame.new(x - s_size*0.5, y - s_size*0.5, -z)
- local part = Instance.new('Part', fold)
- part.Size = Vector3.new(px, px*math.abs(current[1+1] - current[3]), .01)
- --part.Position = raydir*10 + camcf.Position
- part.CFrame = cur_p
- part.Color = current[4]
- part.Anchored = true
- if t % 4 == 0 then
- task.wait()
- end
- end
- end
- tool.Activated:Connect(rend)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement