Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local size = Vector3.new(.04, .04, 0)
- local dist = 10^4
- local canvas = Vector2.new(5, 5)
- local s3 = math.sqrt(3)
- local wfov = math.rad(90)
- local hfov = math.rad(90)
- local maxr = 20
- local Tool = Instance.new("Tool", owner:WaitForChild("Backpack"))
- Tool.Name = "Renderer"
- local handle = Instance.new("Part", Tool)
- handle.Name = "Handle"
- handle.Size = Vector3.new(1, 1, 1)
- local poges = Instance.new("SurfaceGui", handle)
- poges.Face = "Top"
- local tl = Instance.new("TextBox", poges)
- tl.BackgroundColor3 = Color3.new()
- tl.TextColor3 = Color3.new(1, 1, 1)
- tl.PlaceholderColor3 = Color3.new(1,1,1)
- tl.TextScaled = true
- tl.Size = UDim2.fromScale(1, 1)
- tl.Text = ""
- local total = math.round((canvas.X/size.X) * (canvas.Y/size.Y))
- print("Stats:")
- print(tostring(math.round((canvas.X/size.X) * (canvas.Y/size.Y))).." total pixels")
- function CFS(ray, params)
- local YUIOP = workspace:Raycast(ray.Position + ray.Normal*.05, game:GetService("Lighting"):GetSunDirection()*dist, params)
- return YUIOP ~= nil, YUIOP
- end
- function TMP(a, p, s, pa, c, co)
- local par
- p = p or Vector3.new()
- s = s or Vector3.one
- pa = pa or script
- c = c or CFrame.new()
- co = co or Color3.new()
- local s, e = pcall(function()
- par = Instance.new("SpawnLocation", pa)
- par.Anchored = a
- par.Color = co
- par.Size = s
- par.CFrame = c
- par.Enabled = false
- end)
- if e then
- if par ~= nil then
- par:Destroy()
- par = nil
- end
- task.wait(2)
- return TMP(a, p, s, pa, c, co)
- end
- if (not e) and (par ~= nil) then
- return par
- end
- end
- function round(x, t)
- return math.round(x/t)*t
- end
- function cmult(c,s)
- return Color3.new(c.R*s, c.G*s, c.B*s)
- end
- function lerp(v1, v2, t)
- return v1:Lerp(v2, t)
- end
- function shade(ray, color)
- local sundir = game:GetService("Lighting"):GetSunDirection()
- local normc = cmult(color, (ray.Normal:Dot(sundir) + 1)/(2))
- local shadc = cmult(color, .5)
- local shad, cray = CFS(ray)
- if shad then return cmult(normc, .5):Lerp(ray.Instance.Color, cray.Instance.Transparency) end
- if ray.Instance.Transparency > 0 then
- return normc:Lerp(color, ray.Instance.Transparency)
- end
- return normc
- end
- function reflect(ray, color, r, skc, param, origin)
- if r >= maxr then
- return ray, color, r
- end
- if ray.Instance.Reflectance == 0 then return ray, color, r end
- local dir = Reflect(origin, ray.Position, ray.Normal).Unit*dist
- --local thypart = TMP(true, dir.Unit*5 + ray.Position, size, script)
- --thypart.Size = Vector3.new(size.X, size.Y, 5)
- --thypart.CFrame = CFrame.lookAt((dir.Unit*2.5 + ray.Position), ray.Position)
- local R_Ray = workspace:Raycast(ray.Position, dir, param)
- if R_Ray then
- local R_Color = lerp(R_Ray.Instance.Color, color, 1-ray.Instance.Reflectance)
- if R_Ray.Instance.Reflectance > 0 then
- return reflect(R_Ray, R_Color, r+1, skc, param, ray.Position)
- end
- return R_Ray, R_Color, r+1
- end
- return false, sky(dir):Lerp(ray.Instance.Color, 1-ray.Instance.Reflectance), r
- end
- function dirfromxy(x, y, xit, yit, handlecf)
- local up = (CFrame.Angles(hfov/2, 0, 0)).LookVector
- local right = (CFrame.Angles(0, -wfov/2, 0)).LookVector
- local down = (CFrame.Angles(-hfov/2, 0, 0)).LookVector
- local left = (CFrame.Angles(0, wfov/2, 0)).LookVector
- local v = CFrame.new(lerp(left, right, x/xit).X, lerp(up, down, y/yit).Y, up.Z)
- local hcfpos = handlecf.Position
- local tcfpos = (handlecf * v).Position
- return CFrame.lookAt(hcfpos, tcfpos).LookVector * (tcfpos - hcfpos).Magnitude
- end
- function Reflect(start, hitpoint, normal)
- local v = hitpoint - start
- local dir = (v - 2*v:Dot(normal)*normal)
- return dir
- end
- function sky(dir)
- return Color3.new(1, 1, 1):Lerp(Color3.new(0, 0.48, .8), round((math.abs(dir.Unit.Y))^(.5), .03))
- end
- function transpare(ray, col, r, origin, param, skc)
- local newray = workspace:Raycast((ray.Position - origin).Unit*(2^(-10)) + ray.Position, (ray.Position - origin).Unit*dist, param)
- if newray then
- local newcol = col:Lerp(newray.Instance.Color, (ray.Instance.Transparency)/r)
- if newray.Instance.Transparency > 0 then
- return transpare(newray, newcol, r+1, ray.Position, param, skc)
- end
- return newray, newcol, r+1
- end
- local dir = ray.Position - origin
- return ray, sky(dir):Lerp(col, (1-ray.Instance.Transparency)/r), r
- end
- function ALL(r, c, p, rr, skc, origin)
- local oray = r
- local col = c
- local ray = r
- local it = rr
- if r.Instance.Reflectance > 0 then
- local nr, nc, nrr = reflect(r, c, rr, skc, p, origin)
- col = lerp(nc, Color3.new(), nrr/maxr); ray = nr; it = nrr
- if r then
- if r.Instance.Transparency > 0 then
- local z, x, c = transpare(r, col, 1, origin, p, skc)
- col = x; ray = nr
- end
- end
- end
- --if ray then
- -- if ray.Instance.Transparency > 0 then
- -- local _or, oc, orr = transpare(ray, col, 1, origin, p, skc)
- -- col = oc; ray = _or; it = orr
- -- end
- --end
- if ray then
- return shade(ray, col)
- end
- return col
- end
- function threshold(a, b, t)
- return math.sqrt((a.R - b.R)^2 + (a.G - b.G)^2 + (a.B - b.B)^2) < t
- 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 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 onclick()
- task.spawn(function()
- local wm = Instance.new("WorldModel", script)
- local handlecf = handle.CFrame
- local cols = {}
- local theblocks
- local maxx = canvas.X/size.X
- local maxy = canvas.Y/size.Y
- local t = 0
- local rcpms = RaycastParams.new()
- rcpms.FilterDescendantsInstances = {wm, script}
- rcpms.FilterType = Enum.RaycastFilterType.Blacklist
- for x = 0,maxx do
- cols[x] = {}
- for y = 0,maxy do
- t += 1
- local col = Color3.new(0, 0.6, 1)
- local dir = dirfromxy(x, y, maxx, maxy, handlecf)
- col = Color3.new(1, 1, 1):Lerp(cmult(col, .8), round((math.abs(dir.Unit.Y))^(.5), .03))
- --TMP(wm, CFrame.new(dir*10 + handlecf.Position), Color3.new(), Vector3.one)
- local ray = workspace:Raycast(handlecf.Position, dir*dist, rcpms)
- if ray then
- col = ALL(ray, ray.Instance.Color, rcpms, 1, col, handlecf.Position)
- end
- cols[x][y] = col
- tl.Text = "Casting: "..tostring(round(t/((maxx+1)*(maxy+1)), .001)*100).."%"
- if y % 15 == 0 then
- task.wait()
- end
- end
- task.wait()
- end
- theblocks = GM(cols)
- for i = 1, #theblocks do
- local cur = theblocks[i]
- local x = cur[1]
- local y = (cur[2] + cur[3])/2
- --print(x, y)
- local CF = handlecf * CFrame.new((x - maxx/2)*size.X, (-y + maxy/2)*size.Y, -.2)
- local pard = TMP(true, false, size, wm, CF, cur[4])
- pard.Size = Vector3.new(size.X, size.Y*math.abs(cur[3] - cur[2]), size.Z)
- pard.Material = "SmoothPlastic"
- --local sb = Instance.new("SelectionBox", pard)
- --sb.Adornee = pard
- --sb.LineThickness = 0.0002
- tl.Text = "Building: "..tostring(round((i-1)/(#theblocks-1), 0.01)*100).."%"
- if i % 3 == 0 then
- task.wait()
- end
- end
- end)
- end
- Tool.Activated:Connect(onclick)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement