Advertisement
eea

raymod

eea
May 6th, 2022 (edited)
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.05 KB | None | 0 0
  1. local module = {}
  2.  
  3. function module:NewRender(base: number, height: number, resolution: Vector2, start: CFrame, maxdist: number, params: RaycastParams, fov: number?, handle: Part, maxr: number)
  4.     local renderinfo = {}
  5.     renderinfo[1] = base
  6.     renderinfo[2] = height
  7.     renderinfo[3] = resolution or Vector2.new(.5, .5)
  8.     renderinfo[4] = start
  9.     renderinfo[5] = maxdist or 100
  10.     renderinfo[6] = params
  11.     renderinfo[7] = {}
  12.     renderinfo[8] = math.rad(fov or 90)
  13.     renderinfo[9] = handle
  14.     renderinfo[10] = maxr or 10
  15.     return renderinfo
  16. end
  17.  
  18. function module:GetReflected(origin: Vector3, point: Vector3, normal: Vector3)
  19.     local vec = point - origin
  20.     local dir = vec - 2 * vec:Dot(normal) * normal
  21.     return dir
  22. end
  23.  
  24. function module:Shadow(ray: RaycastResult, color: Color3, p: RaycastParams, max: number, shadowdef: number)
  25.     if (ray.Instance.Transparency < 1) then
  26.         max = max or 0
  27.         local sundir = game:GetService("Lighting"):GetSunDirection()
  28.         local shadowtest = module:Raycast(ray.Position+sundir*(2^-16), sundir*12345678, p)
  29.       --local m = math.max(1-module:GetVectorAngles(sundir, ray.Normal)/math.pi, max)
  30.         local m = math.max(sundir:Dot(ray.Normal)*.5 + .5, max)
  31.         local s_color = module:C_mult(color, m)
  32.         if shadowtest then
  33.             if (shadowtest.Instance ~= ray.Instance) then
  34.                 return module:C_mult(s_color, shadowdef)
  35.             end
  36.             return s_color
  37.         end
  38.         return s_color
  39.     end
  40.     return color
  41. end
  42.  
  43. function module:GetVectorAngles(a: Vector3, b: Vector3)
  44.     return math.acos(a:Dot(b)/(a.Magnitude*b.Magnitude))
  45. end
  46.  
  47. function module:ReflectRay(render, pixel: Vector3, ray: RaycastResult, color: Color3, r: number, s)
  48.     if ray.Instance.Reflectance ~= 0 and r < render[10] then
  49.         local dir = module:GetReflected(pixel, ray.Position, ray.Normal)
  50.         local r_ray = module:Raycast(ray.Position, dir.Unit*render[5], render[6])
  51.         if r_ray ~= nil then
  52.             local t_color = r_ray.Instance.Color:Lerp(color, 1-ray.Instance.Reflectance)
  53.             if r_ray.Instance.Reflectance ~= 0 then
  54.                 task.wait(.01)
  55.                 return module:ReflectRay(render, ray.Position, r_ray, t_color, r+1, s)
  56.             end
  57.             return r_ray, t_color, r+1
  58.         end
  59.         if r_ray == nil then
  60.             return ray, s, r+1
  61.         end
  62.     end
  63.     return ray, color, r
  64. end
  65.  
  66. function makePart(a, p, s, pa)
  67.     local par
  68.     local s, e = pcall(function()
  69.         par = Instance.new("Part", pa)
  70.         par.Anchored = a
  71.         par.Position = p
  72.         par.Size = s
  73.     end)
  74.     if e then
  75.         if par ~= nil then
  76.             par:Destroy()
  77.             par = nil
  78.         end
  79.         task.wait(2)
  80.         return trymakePart(a, p, s, pa)
  81.     end
  82.    
  83.     if (not e) and (par ~= nil) then
  84.         return par
  85.     end
  86. end
  87.  
  88. function module:Raycast(start: Vector3, dir: Vector3, p: RaycastParams)
  89.     return workspace:Raycast(start, dir, p)
  90. end
  91.  
  92. function module:C_mult(color: Color3, scalar: number)
  93.     return Color3.new(color.R*scalar, color.G*scalar, color.B*scalar)
  94. end
  95.  
  96. function module:C_mult2(color: Color3, color2: Color3)
  97.     return Color3.new(color.R*color2.R, color.B*color2.B, color.G*color2.G)
  98. end
  99.  
  100. function lerp(min, max, t)
  101.     return (max-min)*t + min
  102. end
  103.  
  104. function module:Render(render)
  105.     local xit = math.floor(render[1] / render[3].X)
  106.     local yit = math.floor(render[2] / render[3].Y)
  107.     local seed = math.random()*1000000
  108.     local sky = Color3.new(0, 0.6, 1)
  109.     local max_r = render[10]
  110.     local stc = render[9].CFrame * CFrame.Angles(0, math.rad(90), 0)
  111.     for x = 0,xit do
  112.         render[7][x] = {}
  113.         for y = 0, yit do
  114.             local fov = render[8]
  115.             local left = CFrame.Angles(0, fov/2, 0)
  116.             local right = CFrame.Angles(0, -fov/2, 0)
  117.             local bottom = CFrame.Angles(-fov/2, 0, 0)
  118.             local top = CFrame.Angles(fov/2, 0, 0)
  119.             local forward = Vector3.new(0, 0, -1)
  120.             local xpos = lerp(left.LookVector, right.LookVector, x/xit)
  121.             local ypos = lerp(top.LookVector, bottom.LookVector, y/yit)
  122.             local relpos = CFrame.new(xpos.X, ypos.Y, -left.LookVector:Dot(forward))
  123.             local dir = CFrame.new(stc.Position, (stc * relpos).Position).LookVector * render[5]
  124.           --makePart(true, (stc * relpos).Position, Vector3.new(.01, .01, .01), workspace)
  125.             local ray = module:Raycast(stc.Position, dir, render[6])
  126.             local clouds = math.clamp((math.noise(x*render[3].X/7+seed, y*render[3].Y/7+seed)+.5)^2, 0, 1)
  127.             local sky_c = sky:Lerp(Color3.new(1,1,1), clouds)
  128.           --task.wait(.2)
  129.           --print(math.deg(module:GetVectorAngles(xpos, forward)))
  130.            
  131.             if ray ~= nil then
  132.                 local r_ray, r_color, r = module:ReflectRay(render, stc.Position, ray, ray.Instance.Color, 0, sky_c)
  133.                 local r_ray_shadow = module:Shadow(r_ray, r_color, render[6], .4, .5)
  134.                 --makePart(true, r_ray.Position, Vector3.new(render[3].X, render[3].Y, 0.01), script)
  135.                
  136.                 if (r_ray == ray) then
  137.                     render[7][x][y] = r_ray_shadow:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
  138.                 end
  139.  
  140.                 if r_color == sky then
  141.                     render[7][x][y] = sky_c:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
  142.                 end
  143.                
  144.                 if r_ray ~= ray then
  145.                     render[7][x][y] = r_ray_shadow:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
  146.                     --local pos = Instance.new("SpawnLocation", workspace)
  147.                     --pos.Position = r_ray.Position
  148.                     --pos.Anchored = true
  149.                 end
  150.             else
  151.                 render[7][x][y] = sky_c
  152.             end
  153.         end
  154.         if x % 20 == 0 then
  155.             task.wait()
  156.         end
  157.         pcall(function() render[9].SurfaceGui.TextBox.PlaceholderText = math.round(x/xit*100).."%" end)
  158.     end
  159. end
  160. return module
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement