_Ziper_YT_

Untitled

Jan 19th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. -- Uses monitor from Tom's Peripherals as output https://modrinth.com/mod/toms-peripherals
  2. local rc = peripheral.find("raycaster")
  3. local gpu = peripheral.find("tm_gpu")
  4.  
  5. local euler_mode = false
  6. local depth_map = false
  7. local cache = false
  8.  
  9. local width_roll_range = math.rad(45)
  10. local height_pitch_range = math.rad(45)
  11. local max_distance = 20
  12. local vector_fov = 1
  13.  
  14. gpu.refreshSize()
  15. gpu.fill()
  16. gpu.sync()
  17.  
  18. gpu.setSize(32)
  19. local width, height = gpu.getSize()
  20. local img = gpu.newImage(width, height)
  21.  
  22. height_pitch_range = height_pitch_range * (height/width)
  23.  
  24. local table_insert = table.insert
  25.  
  26. local function linspace(start, end_, num)
  27. local linspaced = {}
  28. if num == 0 then return linspaced end
  29. if num == 1 then
  30. table_insert(linspaced, start)
  31. return linspaced
  32. end
  33.  
  34. local delta = (end_ - start) / (num - 1)
  35.  
  36. for i = 0, num-2 do
  37. table_insert(linspaced, start+delta*i)
  38. end
  39. table_insert(linspaced, end_)
  40.  
  41. return linspaced
  42. end
  43.  
  44. local function split(inputstr, sep)
  45. if sep == nil then sep = "%s" end
  46. local t={}
  47. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do table.insert(t, str) end
  48. return t
  49. end
  50.  
  51. local function fromBlit(hex)
  52. if #hex ~= 1 then return nil end
  53. local value = tonumber(hex, 16)
  54. if not value then return nil end
  55.  
  56. return 2 ^ value
  57. end
  58.  
  59. local function clamp(num)
  60. return math.min(1, math.max(num, 0))
  61. end
  62.  
  63. local function fromBlitChar(chr, item)
  64. if (item.distance == nil) then item.distance=max_distance end
  65. local n = clamp(1 - (item.distance / max_distance) + 0.2)
  66.  
  67. local r, g, b = term.nativePaletteColor(fromBlit(chr))
  68. r, g, b = r*n, g*n, b*n
  69. return {math.floor(r*255), math.floor(g*255), math.floor(b*255)}
  70. end
  71.  
  72. local data = {
  73. block = {
  74. minecraft = {
  75. gold_block = "4",
  76.  
  77. polished_diorite = "8",
  78. stone = "8",
  79. iron_ore = "8",
  80. gravel = "8",
  81. coal_ore = "8",
  82. emerald_ore = "8",
  83.  
  84. waxed_cut_copper_slab = "1",
  85. spruce_door = "1",
  86. dirt = "1",
  87.  
  88. oak_planks = "c",
  89. lectern = "c",
  90. oak_log = "c",
  91. acacia_log = "c",
  92.  
  93. grass_block = "d",
  94. grass = "d",
  95. tall_grass = "d",
  96.  
  97. oak_leaves = "5",
  98. acacia_leaves = "5",
  99.  
  100. crimson_planks = "a",
  101. }
  102. }
  103. }
  104.  
  105. local function get_color_normal(item)
  106. if item.is_block then
  107. local res = data
  108. for _, key in ipairs(split(item.block_type, ".")) do
  109. res = res[key]
  110. if res == nil then return fromBlitChar("b", item) end
  111. end
  112. if res == nil then return fromBlitChar("b", item) end
  113. return fromBlitChar(res, item)
  114. end
  115.  
  116. return fromBlitChar("e", item)
  117. end
  118.  
  119. local function get_color_depthmap(item)
  120. if (item.distance == nil) then return 255 end
  121. local num = (item.distance / max_distance) * 255
  122. return {math.floor(num), math.floor(num), math.floor(num)}
  123. end
  124.  
  125. local start = os.epoch("utc")
  126. local i = 0
  127. local function yield()
  128. if (os.epoch("utc") - start > 1000) then
  129. print("pixels " .. i)
  130. i = 0
  131. os.queueEvent("yield")
  132. os.pullEvent("yield")
  133. start = os.epoch("utc")
  134. end
  135. i = i + 1
  136. end
  137.  
  138.  
  139. local x_axis = linspace(-width_roll_range, width_roll_range, width)
  140. local y_axis = linspace(-height_pitch_range, height_pitch_range, height)
  141.  
  142. for y=1, height do
  143. for x=1, width do
  144. yield()
  145.  
  146. local yr = y_axis[y]
  147. local xr = x_axis[x]
  148.  
  149. local item = rc.raycast(max_distance, {yr, xr, vector_fov}, euler_mode, true, cache)
  150. local r, g, b = table.unpack(depth_map and get_color_depthmap(item) or get_color_normal(item))
  151.  
  152. img.setRGB(x-2, height - y-1, r, g, b)
  153. gpu.drawImage(1, 1, img.ref())
  154. gpu.sync()
  155. end end
  156.  
  157. --gpu.drawImage(1, 1, img.ref())
  158. --gpu.sync()
Add Comment
Please, Sign In to add comment