Advertisement
eea

Enhanced photograph V2!

eea
Nov 20th, 2022 (edited)
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. local size = Vector3.new(.04, .04, 0)
  2. local dist = 10^4
  3. local canvas = Vector2.new(5, 5)
  4. local s3 = math.sqrt(3)
  5. local wfov = math.rad(90)
  6. local hfov = math.rad(90)
  7. local maxr = 20
  8. local Tool = Instance.new("Tool", owner:WaitForChild("Backpack"))
  9. Tool.Name = "Renderer"
  10. local handle = Instance.new("Part", Tool)
  11. handle.Name = "Handle"
  12. handle.Size = Vector3.new(1, 1, 1)
  13. local poges = Instance.new("SurfaceGui", handle)
  14. poges.Face = "Top"
  15. local tl = Instance.new("TextBox", poges)
  16. tl.BackgroundColor3 = Color3.new()
  17. tl.TextColor3 = Color3.new(1, 1, 1)
  18. tl.PlaceholderColor3 = Color3.new(1,1,1)
  19. tl.TextScaled = true
  20. tl.Size = UDim2.fromScale(1, 1)
  21. tl.Text = ""
  22. local total = math.round((canvas.X/size.X) * (canvas.Y/size.Y))
  23. print("Stats:")
  24. print(tostring(math.round((canvas.X/size.X) * (canvas.Y/size.Y))).." total pixels")
  25.  
  26. function CFS(ray, params)
  27.     local YUIOP = workspace:Raycast(ray.Position + ray.Normal*.05, game:GetService("Lighting"):GetSunDirection()*dist, params)
  28.     return YUIOP ~= nil, YUIOP
  29. end
  30.  
  31. function TMP(a, p, s, pa, c, co)
  32.     local par
  33.     p = p or Vector3.new()
  34.     s = s or Vector3.one
  35.     pa = pa or script
  36.     c = c or CFrame.new()
  37.     co = co or Color3.new()
  38.     local s, e = pcall(function()
  39.         par = Instance.new("SpawnLocation", pa)
  40.         par.Anchored = a
  41.         par.Color = co
  42.         par.Size = s
  43.         par.CFrame = c
  44.         par.Enabled = false
  45.     end)
  46.     if e then
  47.         if par ~= nil then
  48.             par:Destroy()
  49.             par = nil
  50.         end
  51.         task.wait(2)
  52.         return TMP(a, p, s, pa, c, co)
  53.     end
  54.    
  55.     if (not e) and (par ~= nil) then
  56.         return par
  57.     end
  58. end
  59.  
  60. function round(x, t)
  61.     return math.round(x/t)*t
  62. end
  63.  
  64. function cmult(c,s)
  65.     return Color3.new(c.R*s, c.G*s, c.B*s)
  66. end
  67.  
  68. function lerp(v1, v2, t)
  69.     return v1:Lerp(v2, t)
  70. end
  71.  
  72. function shade(ray, color)
  73.     local sundir = game:GetService("Lighting"):GetSunDirection()
  74.     local normc = cmult(color, (ray.Normal:Dot(sundir) + 1)/(2))
  75.     local shadc = cmult(color, .5)
  76.     local shad, cray = CFS(ray)
  77.     if shad then return cmult(normc, .5):Lerp(ray.Instance.Color, cray.Instance.Transparency) end
  78.     if ray.Instance.Transparency > 0 then
  79.         return normc:Lerp(color, ray.Instance.Transparency)
  80.     end
  81.     return normc
  82. end
  83.  
  84. function reflect(ray, color, r, skc, param, origin)
  85.     if r >= maxr then
  86.         return ray, color, r
  87.     end
  88.     if ray.Instance.Reflectance == 0 then return ray, color, r end
  89.     local dir = Reflect(origin, ray.Position, ray.Normal).Unit*dist
  90.     --local thypart = TMP(true, dir.Unit*5 + ray.Position, size, script)
  91.     --thypart.Size = Vector3.new(size.X, size.Y, 5)
  92.     --thypart.CFrame = CFrame.lookAt((dir.Unit*2.5 + ray.Position), ray.Position)
  93.     local R_Ray = workspace:Raycast(ray.Position, dir, param)
  94.     if R_Ray then
  95.         local R_Color = lerp(R_Ray.Instance.Color, color, 1-ray.Instance.Reflectance)
  96.         if R_Ray.Instance.Reflectance > 0 then
  97.             return reflect(R_Ray, R_Color, r+1, skc, param, ray.Position)
  98.         end
  99.         return R_Ray, R_Color, r+1
  100.     end
  101.     return false, sky(dir):Lerp(ray.Instance.Color, 1-ray.Instance.Reflectance), r
  102. end
  103.  
  104. function dirfromxy(x, y, xit, yit, handlecf)
  105.     local up = (CFrame.Angles(hfov/2, 0, 0)).LookVector
  106.     local right = (CFrame.Angles(0, -wfov/2, 0)).LookVector
  107.     local down = (CFrame.Angles(-hfov/2, 0, 0)).LookVector
  108.     local left = (CFrame.Angles(0, wfov/2, 0)).LookVector
  109.     local v = CFrame.new(lerp(left, right, x/xit).X, lerp(up, down, y/yit).Y, up.Z)
  110.     local hcfpos = handlecf.Position
  111.     local tcfpos = (handlecf * v).Position
  112.     return CFrame.lookAt(hcfpos, tcfpos).LookVector * (tcfpos - hcfpos).Magnitude
  113. end
  114.  
  115. function Reflect(start, hitpoint, normal)
  116.     local v = hitpoint - start
  117.     local dir = (v - 2*v:Dot(normal)*normal)
  118.     return dir
  119. end
  120.  
  121. function sky(dir)
  122.     return Color3.new(1, 1, 1):Lerp(Color3.new(0, 0.48, .8), round((math.abs(dir.Unit.Y))^(.5), .03))
  123. end
  124.  
  125. function transpare(ray, col, r, origin, param, skc)
  126.     local newray = workspace:Raycast((ray.Position - origin).Unit*(2^(-10)) + ray.Position, (ray.Position - origin).Unit*dist, param)
  127.     if newray then
  128.         local newcol = col:Lerp(newray.Instance.Color, (ray.Instance.Transparency)/r)
  129.         if newray.Instance.Transparency > 0 then
  130.             return transpare(newray, newcol, r+1, ray.Position, param, skc)
  131.         end
  132.         return newray, newcol, r+1
  133.     end
  134.     local dir = ray.Position - origin
  135.     return ray, sky(dir):Lerp(col, (1-ray.Instance.Transparency)/r), r
  136. end
  137.  
  138. function ALL(r, c, p, rr, skc, origin)
  139.     local oray = r
  140.     local col = c
  141.     local ray = r
  142.     local it = rr
  143.     if r.Instance.Reflectance > 0 then
  144.         local nr, nc, nrr = reflect(r, c, rr, skc, p, origin)
  145.         col = lerp(nc, Color3.new(), nrr/maxr); ray = nr; it = nrr
  146.         if r then
  147.             if r.Instance.Transparency > 0 then
  148.                local z, x, c = transpare(r, col, 1, origin, p, skc)
  149.                col = x; ray = nr
  150.             end
  151.         end
  152.     end
  153.     --if ray then
  154.     --    if ray.Instance.Transparency > 0 then
  155.     --        local _or, oc, orr = transpare(ray, col, 1, origin, p, skc)
  156.     --        col = oc; ray = _or; it = orr
  157.     --    end
  158.     --end
  159.     if ray then
  160.         return shade(ray, col)
  161.     end
  162.     return col
  163. end
  164.  
  165. function threshold(a, b, t)
  166.     return math.sqrt((a.R - b.R)^2 + (a.G - b.G)^2 + (a.B - b.B)^2) < t
  167. end
  168.  
  169. function GM(cols)
  170.     local blocks = {}
  171.     for i = 1,#cols do
  172.         local started = 0
  173.         for j = 1,#cols[i] do
  174.             local prev = cols[i][j-1]
  175.             local cur = cols[i][j]
  176.             if prev ~= cur then
  177.                 table.insert(blocks, {i, started, j, cols[i][j-1]})
  178.                 started = j
  179.             end
  180.             if j == #cols[i] then
  181.                 table.insert(blocks, {i, started, j, cols[i][j]})
  182.             end
  183.         end
  184.     end
  185.     print("-----------------")
  186.     print(tostring(#blocks).." greedy meshing")
  187.     print(tostring(math.round((1 - #blocks/total) * 100)).."% efficiency")
  188.     return blocks
  189. end
  190.  
  191. function onclick()
  192.     task.spawn(function()
  193.     local wm = Instance.new("WorldModel", script)
  194.     local handlecf = handle.CFrame
  195.     local cols = {}
  196.     local theblocks
  197.     local maxx = canvas.X/size.X
  198.     local maxy = canvas.Y/size.Y
  199.     local t = 0
  200.     local rcpms = RaycastParams.new()
  201.     rcpms.FilterDescendantsInstances = {wm, script}
  202.     rcpms.FilterType = Enum.RaycastFilterType.Blacklist
  203.     for x = 0,maxx do
  204.         cols[x] = {}
  205.         for y = 0,maxy do
  206.             t += 1
  207.             local col = Color3.new(0, 0.6, 1)
  208.             local dir = dirfromxy(x, y, maxx, maxy, handlecf)
  209.             col = Color3.new(1, 1, 1):Lerp(cmult(col, .8), round((math.abs(dir.Unit.Y))^(.5), .03))
  210.             --TMP(wm, CFrame.new(dir*10 + handlecf.Position), Color3.new(), Vector3.one)
  211.             local ray = workspace:Raycast(handlecf.Position, dir*dist, rcpms)
  212.             if ray then
  213.                 col = ALL(ray, ray.Instance.Color, rcpms, 1, col, handlecf.Position)
  214.             end
  215.             cols[x][y] = col
  216.             tl.Text = "Casting: "..tostring(round(t/((maxx+1)*(maxy+1)), .001)*100).."%"
  217.             if y % 15 == 0 then
  218.                 task.wait()
  219.             end
  220.         end
  221.         task.wait()
  222.     end
  223.     theblocks = GM(cols)
  224.     for i = 1, #theblocks do
  225.         local cur = theblocks[i]
  226.         local x = cur[1]
  227.         local y = (cur[2] + cur[3])/2
  228.         --print(x, y)
  229.         local CF = handlecf * CFrame.new((x - maxx/2)*size.X, (-y + maxy/2)*size.Y, -.2)
  230.         local pard = TMP(true, false, size, wm, CF, cur[4])
  231.         pard.Size = Vector3.new(size.X, size.Y*math.abs(cur[3] - cur[2]), size.Z)
  232.         pard.Material = "SmoothPlastic"
  233.         --local sb = Instance.new("SelectionBox", pard)
  234.         --sb.Adornee = pard
  235.         --sb.LineThickness = 0.0002
  236.         tl.Text = "Building: "..tostring(round((i-1)/(#theblocks-1), 0.01)*100).."%"
  237.         if i % 3 == 0 then
  238.             task.wait()
  239.         end
  240.     end
  241.     end)
  242. end
  243. Tool.Activated:Connect(onclick)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement