szymski

Untitled

Jul 19th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. --@ainclude abstract_tracer_util.txt
  2.  
  3. OPTIMIZATIONS = {
  4. TRIANGLE_SORT = true,
  5. BACKSIDE_REMOVE = true,
  6. PLANE_INTERSECT_CHECK = false,
  7. BOUNDING_BOX_CHECK = true,
  8. INTERSECT_OPT1 = true,
  9. INTERSECT_OPT2 = true,
  10. GRID_GROUPING = true,
  11. GRID_SPLIT_PER_DIM = 2,
  12. }
  13.  
  14. local util = require("abstract_tracer_util")
  15.  
  16. local handler = { }
  17. handler.Name = "Abstract ray tracing"
  18.  
  19. local iter = 0
  20.  
  21. local scene
  22.  
  23. function handler.init()
  24. -- iter = 0
  25. --local a = Angle(iter * 5, iter * 3, iter * 2)
  26.  
  27. scene = {
  28. camera = {
  29. pos = Vector(0, -10, 3),
  30. --ang = Angle(0, 0, 0),
  31. fov = "TODO",
  32. },
  33. sunDir = Vector(-0.1, 0.2, -0.8):getNormalized(),
  34. triangles = {
  35. {
  36. {
  37. pos = Vector(2, 0, -1):getRotated(a),
  38. color = Vector(1, 0, 1),
  39. uv = Vector(0, 0),
  40. },
  41. {
  42. pos = Vector(0, 0, 1):getRotated(a),
  43. color = Vector(0, 1, 0),
  44. uv = Vector(0, 1),
  45. },
  46. {
  47. pos = Vector(0, 0, -1):getRotated(a),
  48. color = Vector(0, 0.55, 1),
  49. uv = Vector(1, 0),
  50. },
  51. },
  52. {
  53. {
  54. pos = Vector(0, 0, -1):getRotated(a),
  55. color = Vector(0, 0.55, 1),
  56. },
  57. {
  58. pos = Vector(0, 0, 1):getRotated(a),
  59. color = Vector(0, 1, 0),
  60. },
  61. {
  62. pos = Vector(0, -2, -1):getRotated(a),
  63. color = Vector(1, 0, 1),
  64. },
  65. },
  66. },
  67. boundingBox = nil,
  68. grid = nil,
  69. }
  70.  
  71. scene.triangles = {}
  72.  
  73. ----- TEMP OFF LOVE ------
  74.  
  75.  
  76. -- -- -- local mesh = mesh.getModelMeshes("models/props_lab/huladoll.mdl", 0)[1]
  77. -- -- -- -- printTable(mesh.triangles)
  78. -- -- -- print(mesh.material)
  79. -- -- -- -- material = material.load(mesh.material)
  80. -- -- -- texMaterial = material.createFromImage("data/sf_textures/hula_sheet.png", "noclamp")
  81. -- -- -- -- print(material:getTexture("$basetexture"))
  82. -- -- -- --material = material.load("phoenix_storms/FuturisticTrackRamp_1-2")
  83. -- -- -- local mdlTris = mesh.triangles
  84. -- -- -- for i = 1, #mdlTris / 3 do
  85. -- -- -- local tri = {}
  86.  
  87. -- -- -- for j = 1, 3 do
  88. -- -- -- local vertexIdx = (i - 1) * 3 + j
  89. -- -- -- local vertex = mdlTris[vertexIdx]
  90.  
  91. -- -- -- tri[#tri + 1] = {
  92. -- -- -- pos = vertex.pos:getRotated(a),
  93. -- -- -- uv = Vector(vertex.u, vertex.v),
  94. -- -- -- normal = vertex.normal,
  95. -- -- -- }
  96. -- -- -- end
  97.  
  98. -- -- -- scene.triangles[#scene.triangles + 1] = tri
  99. -- -- -- end
  100.  
  101. -- -- -- for k, tri in pairs(scene.triangles) do
  102. -- -- -- local meanPos = (tri[1].pos + tri[2].pos + tri[3].pos) / 3
  103. -- -- -- tri[4] = (scene.camera.pos - meanPos):getLength()
  104. -- -- -- end
  105.  
  106. -- -- -- if OPTIMIZATIONS.TRIANGLE_SORT then
  107. -- -- -- table.sort(scene.triangles, function(a, b)
  108. -- -- -- return a[4] < b[4]
  109. -- -- -- end)
  110. -- -- -- end
  111.  
  112. -- -- -- print(scene.triangles[1][1])
  113.  
  114. ----- TEMP OFF LOVE ------
  115.  
  116. -- render.createRenderTarget("tex")
  117. -- render.selectRenderTarget("tex")
  118.  
  119. -- render.setColor(Color(255, 255, 255))
  120. -- render.setMaterial(material)
  121. -- render.drawTexturedRectUV(0, 0, 512, 512, 0, 0, 1, 1)
  122.  
  123. -- while true do
  124. -- coroutine.yield()
  125. -- end
  126.  
  127. -- render.selectRenderTarget("rt")
  128.  
  129. if OPTIMIZATIONS.BOUNDING_BOX_CHECK then
  130. local min = { nil, nil, nil }
  131. local max = { nil, nil, nil }
  132.  
  133. for k, tri in pairs(scene.triangles) do
  134. for i = 1, 3 do
  135. local vertex = tri[i]
  136.  
  137. min[1] = math.min(min[1] or 0, vertex.pos.x)
  138. min[2] = math.min(min[2] or 0, vertex.pos.y)
  139. min[3] = math.min(min[3] or 0, vertex.pos.z)
  140.  
  141. max[1] = math.max(max[1] or 0, vertex.pos.x)
  142. max[2] = math.max(max[2] or 0, vertex.pos.y)
  143. max[3] = math.max(max[3] or 0, vertex.pos.z)
  144. end
  145. end
  146.  
  147. scene.boundingBox = {
  148. min = Vector(unpack(min)),
  149. max = Vector(unpack(max)),
  150. }
  151.  
  152. printTable(scene.boundingBox)
  153. end
  154.  
  155. if OPTIMIZATIONS.GRID_GROUPING then
  156. scene.grid = {}
  157.  
  158. local bbSize = scene.boundingBox.max - scene.boundingBox.max
  159. local cubeSize = bbSize / OPTIMIZATIONS.GRID_SPLIT_PER_DIM
  160.  
  161. for ix = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
  162. -- local minX =
  163.  
  164. for iy = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
  165. for iz = 1, OPTIMIZATIONS.GRID_SPLIT_PER_DIM do
  166. local cube = {
  167.  
  168. }
  169. end
  170. end
  171. end
  172. end
  173. end
  174.  
  175. function handler.increment()
  176. iter = iter + 1
  177. handler.init()
  178. end
  179.  
  180. function sampleTexUV(uv)
  181. return Vector(uv.u, uv.v, 0)
  182. end
  183.  
  184. function sampleTex(uv)
  185. local u, v = uv.u, uv.v
  186.  
  187. local size = 0.1
  188.  
  189. local check = false
  190.  
  191. if v % size < size / 2 then
  192. check = not check
  193. end
  194.  
  195. if (u) % size < size / 2 then
  196. check = not check
  197. end
  198.  
  199. return check and Vector(1, 1, 1) or Vector(1, 0.2, 0.2)
  200. end
  201.  
  202. function coloredTex(uv)
  203. local u, v = uv.u, uv.v
  204. -- print(math.floor(u * material:getWidth()), math.floor(v * material:getHeight()))
  205. local color = texMaterial:getColor(math.floor(u * texMaterial:getWidth()), math.floor(v * texMaterial:getHeight()))
  206. -- print(color.r, color.g, color.b)
  207. return Vector(color.r, color.g, color.b) / 255
  208. end
  209.  
  210. function handler.getPixel(pX, pY)
  211. local uv = {
  212. u = ((pX / imageSize) * 2 - 1) * 0.5,
  213. v = -((pY / imageSize) * 2 - 1) * 0.5
  214. }
  215.  
  216. local rayDir = Vector(
  217. uv.u,
  218. 1,
  219. uv.v
  220. ):getNormalized()
  221.  
  222. -- return Vector(math.abs(normalizedPos.x), math.abs(normalizedPos.y))
  223. -- return rayDir
  224.  
  225. if OPTIMIZATIONS.BOUNDING_BOX_CHECK and scene.boundingBox then
  226. local min, max = scene.boundingBox.min, scene.boundingBox.max
  227. local origin = (min + max) / 2
  228. local relMin, relMax = min - origin, max - origin
  229.  
  230. local hit = trace.intersectRayWithOBB(scene.camera.pos, rayDir * 1000, origin, Angle(0, 0, 0), relMin, relMax)
  231. if not hit then
  232. return Vector(0.5, 0.3, 0.1)
  233. end
  234. end
  235.  
  236. for k, tri in pairs(scene.triangles) do
  237. local hit = util.triangleIntersect(scene.camera.pos, rayDir, tri)
  238. if hit then
  239. local len = (hit.pos - scene.camera.pos):getLength()
  240. -- return hit.normal -- * (len / 10)
  241. -- return Vector(hit.uv.u, hit.uv.v, 0) -- * (len / 10)
  242. -- return sampleTex(hit.uv) -- * (len / 10)
  243. -- return sampleTexUV(hit.uv) -- * (len / 10)
  244. -- return hit.color -- * (len / 10)
  245.  
  246. -- return sampleTex(hit.uv) * (0.1 + 0.9 * math.max(0, hit.normal:dot(scene.sunDir)))
  247. return coloredTex(hit.uv) * (0.1 + 0.9 * math.max(0, hit.normal:dot(scene.sunDir)))
  248. end
  249. end
  250.  
  251. return Vector(0.05, 0.1, 0.2)
  252. end
  253.  
  254. return handler
  255.  
Advertisement
Add Comment
Please, Sign In to add comment