Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- function module:NewRender(base: number, height: number, resolution: Vector2, start: CFrame, maxdist: number, params: RaycastParams, fov: number?, handle: Part, maxr: number)
- local renderinfo = {}
- renderinfo[1] = base
- renderinfo[2] = height
- renderinfo[3] = resolution or Vector2.new(.5, .5)
- renderinfo[4] = start
- renderinfo[5] = maxdist or 100
- renderinfo[6] = params
- renderinfo[7] = {}
- renderinfo[8] = math.rad(fov or 90)
- renderinfo[9] = handle
- renderinfo[10] = maxr or 10
- return renderinfo
- end
- function module:GetReflected(origin: Vector3, point: Vector3, normal: Vector3)
- local vec = point - origin
- local dir = vec - 2 * vec:Dot(normal) * normal
- return dir
- end
- function module:Shadow(ray: RaycastResult, color: Color3, p: RaycastParams, max: number, shadowdef: number)
- if (ray.Instance.Transparency < 1) then
- max = max or 0
- local sundir = game:GetService("Lighting"):GetSunDirection()
- local shadowtest = module:Raycast(ray.Position+sundir*(2^-16), sundir*12345678, p)
- --local m = math.max(1-module:GetVectorAngles(sundir, ray.Normal)/math.pi, max)
- local m = math.max(sundir:Dot(ray.Normal)*.5 + .5, max)
- local s_color = module:C_mult(color, m)
- if shadowtest then
- if (shadowtest.Instance ~= ray.Instance) then
- return module:C_mult(s_color, shadowdef)
- end
- return s_color
- end
- return s_color
- end
- return color
- end
- function module:GetVectorAngles(a: Vector3, b: Vector3)
- return math.acos(a:Dot(b)/(a.Magnitude*b.Magnitude))
- end
- function module:ReflectRay(render, pixel: Vector3, ray: RaycastResult, color: Color3, r: number, s)
- if ray.Instance.Reflectance ~= 0 and r < render[10] then
- local dir = module:GetReflected(pixel, ray.Position, ray.Normal)
- local r_ray = module:Raycast(ray.Position, dir.Unit*render[5], render[6])
- if r_ray ~= nil then
- local t_color = r_ray.Instance.Color:Lerp(color, 1-ray.Instance.Reflectance)
- if r_ray.Instance.Reflectance ~= 0 then
- task.wait(.01)
- return module:ReflectRay(render, ray.Position, r_ray, t_color, r+1, s)
- end
- return r_ray, t_color, r+1
- end
- if r_ray == nil then
- return ray, s, r+1
- end
- end
- return ray, color, r
- end
- function makePart(a, p, s, pa)
- local par
- local s, e = pcall(function()
- par = Instance.new("Part", pa)
- par.Anchored = a
- par.Position = p
- par.Size = s
- end)
- if e then
- if par ~= nil then
- par:Destroy()
- par = nil
- end
- task.wait(2)
- return trymakePart(a, p, s, pa)
- end
- if (not e) and (par ~= nil) then
- return par
- end
- end
- function module:Raycast(start: Vector3, dir: Vector3, p: RaycastParams)
- return workspace:Raycast(start, dir, p)
- end
- function module:C_mult(color: Color3, scalar: number)
- return Color3.new(color.R*scalar, color.G*scalar, color.B*scalar)
- end
- function module:C_mult2(color: Color3, color2: Color3)
- return Color3.new(color.R*color2.R, color.B*color2.B, color.G*color2.G)
- end
- function lerp(min, max, t)
- return (max-min)*t + min
- end
- function module:Render(render)
- local xit = math.floor(render[1] / render[3].X)
- local yit = math.floor(render[2] / render[3].Y)
- local seed = math.random()*1000000
- local sky = Color3.new(0, 0.6, 1)
- local max_r = render[10]
- local stc = render[9].CFrame * CFrame.Angles(0, math.rad(90), 0)
- for x = 0,xit do
- render[7][x] = {}
- for y = 0, yit do
- local fov = render[8]
- local left = CFrame.Angles(0, fov/2, 0)
- local right = CFrame.Angles(0, -fov/2, 0)
- local bottom = CFrame.Angles(-fov/2, 0, 0)
- local top = CFrame.Angles(fov/2, 0, 0)
- local forward = Vector3.new(0, 0, -1)
- local xpos = lerp(left.LookVector, right.LookVector, x/xit)
- local ypos = lerp(top.LookVector, bottom.LookVector, y/yit)
- local relpos = CFrame.new(xpos.X, ypos.Y, -left.LookVector:Dot(forward))
- local dir = CFrame.new(stc.Position, (stc * relpos).Position).LookVector * render[5]
- --makePart(true, (stc * relpos).Position, Vector3.new(.01, .01, .01), workspace)
- local ray = module:Raycast(stc.Position, dir, render[6])
- local clouds = math.clamp((math.noise(x*render[3].X/7+seed, y*render[3].Y/7+seed)+.5)^2, 0, 1)
- local sky_c = sky:Lerp(Color3.new(1,1,1), clouds)
- --task.wait(.2)
- --print(math.deg(module:GetVectorAngles(xpos, forward)))
- if ray ~= nil then
- local r_ray, r_color, r = module:ReflectRay(render, stc.Position, ray, ray.Instance.Color, 0, sky_c)
- local r_ray_shadow = module:Shadow(r_ray, r_color, render[6], .4, .5)
- --makePart(true, r_ray.Position, Vector3.new(render[3].X, render[3].Y, 0.01), script)
- if (r_ray == ray) then
- render[7][x][y] = r_ray_shadow:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
- end
- if r_color == sky then
- render[7][x][y] = sky_c:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
- end
- if r_ray ~= ray then
- render[7][x][y] = r_ray_shadow:Lerp(Color3.new(), math.clamp(r/max_r, 0, 1))
- --local pos = Instance.new("SpawnLocation", workspace)
- --pos.Position = r_ray.Position
- --pos.Anchored = true
- end
- else
- render[7][x][y] = sky_c
- end
- end
- if x % 20 == 0 then
- task.wait()
- end
- pcall(function() render[9].SurfaceGui.TextBox.PlaceholderText = math.round(x/xit*100).."%" end)
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement