From 87cc341d81960f1979533736dc1fc44b7acd45e8 Mon Sep 17 00:00:00 2001
From: ultrakatiz <ultrakatiz@gmail.com>
Date: Thu, 19 May 2022 01:44:15 +0200
Subject: [PATCH] added layering depending on z coordinate of faces

---
 shape.lua | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/shape.lua b/shape.lua
index ef54309..5c7195e 100644
--- a/shape.lua
+++ b/shape.lua
@@ -32,7 +32,7 @@ function shape:draw(subs, line, tags, cull)
   local r = self.transform:rotation(true)
   local m = self.transform:matrix(true)
   local c = vector.cardinal(4, 4)
-  local return_str = nil
+  local layer = line.layer
 
   for i = 1, #self.faces do
     local face = self.faces[i]
@@ -42,6 +42,7 @@ function shape:draw(subs, line, tags, cull)
       local vs = face.vertices
 
       local v = matrix.mul_vector(m, vs[#vs] + c) - p
+      local sum = vector.zero(4)
       local str = (tags and "{" .. tags .. "}" or "")
         .. (face.tags and "{" .. face.tags .. "}" or "")
         .. "{\\an7\\pos(" .. p:x() .. ", " .. p:y() .. ")\\p1}"
@@ -49,10 +50,13 @@ function shape:draw(subs, line, tags, cull)
 
       for i = 1, #vs do
         v = matrix.mul_vector(m, vs[i] + c) - p
+        sum = sum + v
         str = str .. "l " .. v:x() .. " " .. v:y() .. " "
       end
 
       l.text = str
+      -- layers must be between 0 and 9999999999
+      l.layer = 500000000 + math.floor(-(p + sum):z() / #vs)
       l.effect = "fx"
       subs.append(l)
     end
-- 
GitLab