CloneTrooper1019

[Roblox] Fast AABB

Jan 30th, 2018
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 KB | None | 0 0
  1. local inf = math.huge
  2. local axes = {'X','Y','Z'}
  3.  
  4. local function fastComputeAABB(model)
  5.     local min = { inf, inf, inf }
  6.     local max = { -inf, -inf, -inf }
  7.  
  8.     for _,obj in pairs(model:GetDescendants()) do
  9.         if obj:IsA("BasePart") then
  10.             local cf = obj.CFrame
  11.             local origin = cf.p
  12.             local up,right,look = cf.upVector,cf.rightVector,cf.lookVector
  13.            
  14.             local halfSize = obj.Size/2
  15.             local hx,hy,hz = halfSize.X, halfSize.Y, halfSize.Z
  16.             local worldR,worldU,worldL,vertex
  17.  
  18.             for x = -1,1,2 do
  19.                 worldR = right * (x * hx)
  20.                 for y = -1,1,2 do
  21.                     worldU = up * (y * hy)
  22.                     for z = -1,1,2 do
  23.                         worldL = look * (z * hz)
  24.                         vertex = (origin + worldR + worldU + worldL)
  25.                         for axisId = 1,3 do
  26.                             local coord = vertex[axes[axisId]]
  27.                             if coord < min[axisId] then
  28.                                 min[axisId] = coord
  29.                             end
  30.                             if coord > max[axisId] then
  31.                                 max[axisId] = coord
  32.                             end
  33.                         end
  34.                     end
  35.                 end
  36.             end
  37.         end
  38.     end
  39.    
  40.     min = Vector3.new(unpack(min))
  41.     max = Vector3.new(unpack(max))
  42.    
  43.     return Region3.new(min,max)
  44. end
Add Comment
Please, Sign In to add comment