Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --@ainclude abstract_tracer_util.txt
- OPTIMIZATIONS = {
- TRIANGLE_SORT = true,
- BACKSIDE_REMOVE = true,
- PLANE_INTERSECT_CHECK = false,
- BOUNDING_BOX_CHECK = true,
- INTERSECT_OPT1 = true,
- INTERSECT_OPT2 = true,
- GRID_GROUPING = true,
- GRID_SPLIT_PER_DIM = 2,
- }
- local util = require("abstract_tracer_util")
- local handler = { }
- handler.Name = "Abstract ray tracing"
- local iter = 0
- local scene
- function handler.init()
- -- iter = 0
- --local a = Angle(iter * 5, iter * 3, iter * 2)
- scene = {
- camera = {
- pos = Vector(0, -10, 3),
- --ang = Angle(0, 0, 0),
- fov = "TODO",
- },
- sunDir = Vector(-0.1, 0.2, -0.8):getNormalized(),
- triangles = {
- {
- {
- pos = Vector(2, 0, -1):getRotated(a),
- color = Vector(1, 0, 1),
- uv = Vector(0, 0),
- },
- {
- pos = Vector(0, 0, 1):getRotated(a),
- color = Vector(0, 1, 0),
- uv = Vector(0, 1),
- },
- {
- pos = Vector(0, 0, -1):getRotated(a),
- color = Vector(0, 0.55, 1),
- uv = Vector(1, 0),
- },
- },
- {
- {
- pos = Vector(0, 0, -1):getRotated(a),
- color = Vector(0, 0.55, 1),
- },
- {
- pos = Vector(0, 0, 1):getRotated(a),
- color = Vector(0, 1, 0),
- },
- {
- pos = Vector(0, -2, -1):getRotated(a),
- color = Vector(1, 0, 1),
- },
- },
- },
- boundingBox = nil,
- grid = nil,
- }
- scene.triangles = {}
- ----- TEMP OFF LOVE ------
- -- -- -- local mesh = mesh.getModelMeshes("models/props_lab/huladoll.mdl", 0)[1]
- -- -- -- -- printTable(mesh.triangles)
- -- -- -- print(mesh.material)
- -- -- -- -- material = material.load(mesh.material)
- -- -- -- texMaterial = material.createFromImage("data/sf_textures/hula_sheet.png", "noclamp")
- -- -- -- -- print(material:getTexture("$basetexture"))
- -- -- -- --material = material.load("phoenix_storms/FuturisticTrackRamp_1-2")
- -- -- -- local mdlTris = mesh.triangles
- -- -- -- for i = 1, #mdlTris / 3 do
- -- -- -- local tri = {}
- -- -- -- for j = 1, 3 do
- -- -- -- local vertexIdx = (i - 1) * 3 + j
- -- -- -- local vertex = mdlTris[vertexIdx]
- -- -- -- tri[#tri + 1] = {
- -- -- -- pos = vertex.pos:getRotated(a),
- -- -- -- uv = Vector(vertex.u, vertex.v),
- -- -- -- normal = vertex.normal,
- -- -- -- }
- -- -- -- end
- -- -- -- scene.triangles[#scene.triangles + 1] = tri
- -- -- -- end
- -- -- -- for k, tri in pairs(scene.triangles) do
- -- -- -- local meanPos = (tri[1].pos + tri[2].pos + tri[3].pos) / 3
- -- -- -- tri[4] = (scene.camera.pos - meanPos):getLength()
- -- -- -- end
- -- -- -- if OPTIMIZATIONS.TRIANGLE_SORT then
- -- -- -- table.sort(scene.triangles, function(a, b)
- -- -- -- return a[4] < b[4]
- -- -- -- end)
- -- -- -- end
- -- -- -- print(scene.triangles[1][1])
- ----- TEMP OFF LOVE ------
- -- render.createRenderTarget("tex")
- -- render.selectRenderTarget("tex")
- -- render.setColor(Color(255, 255, 255))
- -- render.setMaterial(material)
- -- render.drawTexturedRectUV(0, 0, 512, 512, 0, 0, 1, 1)
- -- while true do
- -- coroutine.yield()
- -- end
- -- render.selectRenderTarget("rt")
- if OPTIMIZATIONS.BOUNDING_BOX_CHECK then
- local min = { nil, nil, nil }
- local max = { nil, nil, nil }
- for k, tri in pairs(scene.triangles) do
- for i = 1, 3 do
- local vertex = tri[i]
- min[1] = math.min(min[1] or 0, vertex.pos.x)
- min[2] = math.min(min[2] or 0, vertex.pos.y)
- min[3] = math.min(min[3] or 0, vertex.pos.z)
- max[1] = math.max(max[1] or 0, vertex.pos.x)
- max[2] = math.max(max[2] or 0, vertex.pos.y)
- max[3] = math.max(max[3] or 0, vertex.pos.z)
- end
- end
- scene.boundingBox = {
- min = Vector(unpack(min)),
- max = Vector(unpack(max)),
- }
- printTable(scene.boundingBox)
- end
- if OPTIMIZATIONS.GRID_GROUPING then
- scene.grid = {}
- local bbSize = scene.boundingBox.max - scene.boundingBox.max
- local cubeSize = bbSize / OPTIMIZATIONS.GRID_SPLIT_PER_DIM
- for ix = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
- -- local minX =
- for iy = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
- for iz = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
- local cube = {
- }
- end
- end
- end
- end
- end
- function handler.increment()
- iter = iter + 1
- handler.init()
- end
- function sampleTexUV(uv)
- return Vector(uv.u, uv.v, 0)
- end
- function sampleTex(uv)
- local u, v = uv.u, uv.v
- local size = 0.1
- local check = false
- if v % size < size / 2 then
- check = not check
- end
- if (u) % size < size / 2 then
- check = not check
- end
- return check and Vector(1, 1, 1) or Vector(1, 0.2, 0.2)
- end
- function coloredTex(uv)
- local u, v = uv.u, uv.v
- -- print(math.floor(u * material:getWidth()), math.floor(v * material:getHeight()))
- local color = texMaterial:getColor(math.floor(u * texMaterial:getWidth()), math.floor(v * texMaterial:getHeight()))
- -- print(color.r, color.g, color.b)
- return Vector(color.r, color.g, color.b) / 255
- end
- function handler.getPixel(pX, pY)
- local uv = {
- u = ((pX / imageSize) * 2 - 1) * 0.5,
- v = -((pY / imageSize) * 2 - 1) * 0.5
- }
- local rayDir = Vector(
- uv.u,
- 1,
- uv.v
- ):getNormalized()
- -- return Vector(math.abs(normalizedPos.x), math.abs(normalizedPos.y))
- -- return rayDir
- if OPTIMIZATIONS.BOUNDING_BOX_CHECK and scene.boundingBox then
- local min, max = scene.boundingBox.min, scene.boundingBox.max
- local origin = (min + max) / 2
- local relMin, relMax = min - origin, max - origin
- local hit = trace.intersectRayWithOBB(scene.camera.pos, rayDir * 1000, origin, Angle(0, 0, 0), relMin, relMax)
- if not hit then
- return Vector(0.5, 0.3, 0.1)
- end
- end
- for k, tri in pairs(scene.triangles) do
- local hit = util.triangleIntersect(scene.camera.pos, rayDir, tri)
- if hit then
- local len = (hit.pos - scene.camera.pos):getLength()
- -- return hit.normal -- * (len / 10)
- -- return Vector(hit.uv.u, hit.uv.v, 0) -- * (len / 10)
- -- return sampleTex(hit.uv) -- * (len / 10)
- -- return sampleTexUV(hit.uv) -- * (len / 10)
- -- return hit.color -- * (len / 10)
- -- return sampleTex(hit.uv) * (0.1 + 0.9 * math.max(0, hit.normal:dot(scene.sunDir)))
- return coloredTex(hit.uv) * (0.1 + 0.9 * math.max(0, hit.normal:dot(scene.sunDir)))
- end
- end
- return Vector(0.05, 0.1, 0.2)
- end
- return handler
Advertisement
Add Comment
Please, Sign In to add comment