nodeboxes.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. xdecor.box = {
  2. slab_y = function(height, shift)
  3. return {-0.5, -0.5 + (shift or 0), -0.5, 0.5, -0.5 + height +
  4. (shift or 0), 0.5}
  5. end,
  6. slab_z = function(depth)
  7. return {-0.5, -0.5, -0.5 + depth, 0.5, 0.5, 0.5}
  8. end,
  9. bar_y = function(radius)
  10. return {-radius, -0.5, -radius, radius, 0.5, radius}
  11. end,
  12. cuboid = function(radius_x, radius_y, radius_z)
  13. return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z}
  14. end
  15. }
  16. xdecor.nodebox = {
  17. regular = {type="regular"},
  18. null = {type="fixed", fixed={0,0,0,0,0,0}}
  19. }
  20. xdecor.pixelbox = function(size, boxes)
  21. local fixed = {}
  22. for _, box in pairs(boxes) do
  23. -- `unpack` has been changed to `table.unpack` in newest Lua versions.
  24. local x, y, z, w, h, l = unpack(box)
  25. fixed[#fixed+1] = {
  26. (x / size) - 0.5,
  27. (y / size) - 0.5,
  28. (z / size) - 0.5,
  29. ((x + w) / size) - 0.5,
  30. ((y + h) / size) - 0.5,
  31. ((z + l) / size) - 0.5
  32. }
  33. end
  34. return {type="fixed", fixed=fixed}
  35. end
  36. local mt = {}
  37. mt.__index = function(table, key)
  38. local ref = xdecor.box[key]
  39. local ref_type = type(ref)
  40. if ref_type == "function" then
  41. return function(...)
  42. return {type="fixed", fixed=ref(...)}
  43. end
  44. elseif ref_type == "table" then
  45. return {type="fixed", fixed=ref}
  46. elseif ref_type == "nil" then
  47. error(key.."could not be found among nodebox presets and functions")
  48. end
  49. error("unexpected datatype "..tostring(type(ref)).." while looking for "..key)
  50. end
  51. setmetatable(xdecor.nodebox, mt)