Advertisement
Rovo112

Ray Casting

Feb 20th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. local config = {
  2. size = 50, -- video size (size x size)
  3. pixel_size = {10, 10}, -- how big will each pixel be?
  4. dist = 300, -- view distance
  5. clear_color = Color3.new(0, 0.5, 1),-- what color will a nil pixel be?
  6. fov = {70, 70}, -- field of view per axis
  7. fog_color = Color3.new(1, 1, 1), -- obvious...
  8. interlacing = .1, -- level of interlacing (how many rows of pixels will be skipped per frame + 1)
  9. fps_target = 60, -- target fps for auto_quality (auto_quality MUST be true for this to work)
  10. error_margin = 5, -- how far off the FPS until the interlacing level is adjusted? (auto_quality MUST be true)
  11. auto_quality = true, -- automatically adjust interlacing level for optimum performance
  12. max_interlacing = 40, -- interlacing limit,
  13. ignore_characters = false, -- should the camera not show characters?
  14. lazy_mode = false -- only updates pixels when the camera rotates or moves (may or may not improve performance)
  15. }
  16.  
  17. local player = game:GetService("Players").LocalPlayer
  18. local camera = workspace.CurrentCamera
  19. local time = tick()
  20. local f = 0
  21. local fps = 99
  22. local pixels = {}
  23. local fps_counter = Instance.new("TextLabel")
  24. local interlace_i = Instance.new("TextLabel")
  25. local ignore = {}
  26. local colors = {}
  27. local endpoints = {}
  28.  
  29. local rad, min, max, abs, floor = math.rad, math.min, math.max, math.abs, math.floor
  30.  
  31. function Color(r, g, b)
  32. r = r * 255
  33. g = g * 255
  34. b = b * 255
  35.  
  36. r = floor(r+0.5)
  37. g = floor(g+0.5)
  38. b = floor(b+0.5)
  39.  
  40. local i = r + (g * (255^2)) + (b * (255^3))
  41. local color = colors[i]
  42.  
  43. if color == nil then
  44. color = Color3.new(r / 255, g / 255, b / 255)
  45. colors[i] = color
  46. end
  47.  
  48. return color
  49. end
  50.  
  51. function OnUpdate()
  52. local start = camera.CoordinateFrame.p
  53. local delta = tick() - time
  54. time = tick()
  55.  
  56. local t = tick() % 84000
  57.  
  58. if t % 5 == 0 then
  59. if config.ignore_characters then
  60. ignore = {}
  61. for i, v in ipairs(game.Players:GetChildren()) do
  62. table.insert(ignore, v.Character)
  63. end
  64. else
  65. ignore = {}
  66. end
  67. end
  68.  
  69. -- fps counter
  70.  
  71. local new_fps = floor(0.5 + (1 / delta))
  72. if new_fps ~= fps then
  73. fps = new_fps
  74. fps_counter.Text = "FPS: " .. fps
  75. end
  76.  
  77. -- auto quality
  78.  
  79. local needs_update = config.auto_quality and abs(config.fps_target - fps) > config.error_margin
  80.  
  81. if needs_update then
  82. if fps < config.fps_target then
  83. if config.interlacing < config.max_interlacing then
  84. config.interlacing = config.interlacing + 1
  85. end
  86. else
  87. if config.interlacing > 1 then
  88. config.interlacing = config.interlacing - 1
  89. end
  90. end
  91.  
  92. interlace_i.Text = "Interlacing Level: " .. config.interlacing
  93. end
  94.  
  95. -- begin capture
  96.  
  97. for x = f, config.size - 1, config.interlacing do
  98. for y = 0, config.size - 1 do
  99. local i = y + (x * config.size)
  100. local direction = (camera.CoordinateFrame * CFrame.Angles(rad(-((x / config.size) - 0.5) * config.fov[1]), rad(-((y / config.size) - 0.5) * config.fov[2]), 0)).lookVector
  101. local do_update = endpoints[i] == nil or (endpoints[i][1] ~= direction or endpoints[i][2] ~= camera.CoordinateFrame.p)
  102.  
  103. if not config.lazy_mode or do_update then
  104. if endpoints[i] == nil then
  105. endpoints[i] = {}
  106. end
  107.  
  108. endpoints[i][1] = direction
  109. endpoints[i][2] = camera.CoordinateFrame.p
  110.  
  111. local ray = Ray.new(start, direction * config.dist)
  112.  
  113. local object, hit = workspace:FindPartOnRayWithIgnoreList(ray, ignore)
  114.  
  115. if object ~= nil and object:IsA("Part") and pixels[i] ~= nil then
  116. local t = object.Transparency
  117. local brightness = 1 - max(0, min(1, ((start - hit).magnitude / config.dist)))
  118. local r, g, b = object.Color.r * (1-t), object.Color.g * (1-t), object.Color.b * (1-t)
  119.  
  120. local fog = {
  121. (1 - brightness) * config.fog_color.r,
  122. (1 - brightness) * config.fog_color.g,
  123. (1 - brightness) * config.fog_color.b
  124. }
  125.  
  126. pixels[i].BackgroundColor3 = Color(
  127. fog[1] + ((r + (config.clear_color.r * t)) * brightness),
  128. fog[2] + ((g + (config.clear_color.g * t)) * brightness),
  129. fog[3] + ((b + (config.clear_color.b * t)) * brightness)
  130. )
  131.  
  132. elseif pixels[i] ~= nil then
  133. pixels[i].BackgroundColor3 = config.clear_color
  134. end
  135. end
  136. end
  137. end
  138.  
  139. f = f + 1
  140. if f > config.interlacing then
  141. f = 0
  142. end
  143. end
  144.  
  145. function Load()
  146. local ui = Instance.new("ScreenGui", player:WaitForChild("PlayerGui"))
  147.  
  148. for x = 0, config.size - 1 do
  149. for y = 0, config.size - 1 do
  150. local pixel = Instance.new("Frame")
  151. pixel.Size = UDim2.new(0, config.pixel_size[1], 0, config.pixel_size[2])
  152. pixel.Position = UDim2.new(0, x * config.pixel_size[1], 0, y * config.pixel_size[2])
  153. pixel.BorderSizePixel = 0
  154. pixel.Parent = ui
  155.  
  156. pixels[x + (y * config.size)] = pixel
  157. end
  158. end
  159.  
  160. if config.ignore_characters then
  161. ignore = {}
  162. for i, v in ipairs(game.Players:GetChildren()) do
  163. table.insert(ignore, v.Character)
  164. end
  165. end
  166.  
  167. fps_counter.Parent = ui
  168. fps_counter.Size = UDim2.new(0, max(64, (config.size * config.pixel_size[1])), 0, 32)
  169. fps_counter.Position = UDim2.new(0, 0, 0, (config.size * config.pixel_size[2]) + 0)
  170. fps_counter.BorderSizePixel = 0
  171.  
  172. interlace_i.Parent = ui
  173. interlace_i.Size = UDim2.new(0, max(64, (config.size * config.pixel_size[1])), 0, 32)
  174. interlace_i.Position = UDim2.new(0, 0, 0, (config.size * config.pixel_size[2]) + 32)
  175. interlace_i.BorderSizePixel = 0
  176.  
  177. interlace_i.Text = "Interlacing Level: " .. config.interlacing
  178.  
  179. game:GetService("RunService").RenderStepped:connect(OnUpdate)
  180. end
  181.  
  182. Load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement