four.Transform

.new (def)

local lib = { type = 'four.Transform' } four.Transform = lib

local Quat = four.Quat local V3 = four.V3 local V4 = four.V4 local M4 = four.M4

-- Transform decomposition/matrix synchronization

local function syncMatrix(tv) tv.matrix = M4.rigidqScale(tv.pos, tv.rot, tv.scale) end

local function syncDecomp(tv) local m = tv.matrix tv.pos = M4.getMove(m) tv.rot = Quat.ofM4(m) tv.scale = M4.getScale(m) end

local function notify(tr, tv) for _, d in ipairs(tv.deps) do d(tr) end end

local is_decomp_key = { pos = true, rot = true, scale = true } if k == "tv" then return rawget(t, k) elseif is_decomp_key[k] then local tv = t.tv if tv.dirty_decomp then syncDecomp(tv) end return tv[k] elseif (k == "matrix") then local tv = t.tv if tv.dirty_matrix then syncMatrix(tv) end return tv[k] else return lib[k] end end

if is_decomp_key[k] then local tv = t.tv if tv.dirty_decomp then syncDecomp(tv) end -- other decomps. tv[k] = v tv.dirty_matrix = true notify(t, tv) elseif (k == "matrix") then local tv = t.tv tv[k] = v tv.dirty_decomp = true tv.dirty_matrix = false notify(t, tv) elseif (k == "deps") then local deps = t.tv.deps deps = {} for _, dep in ipairs(v) do table.insert(deps, dep) end else t[k] = v end end

setmetatable(lib, { __call = function(lib, o) return lib.new(o) end })

-- h2. Constructor

local err_ambig = "Ambiguous transform (matrix and decomposition specified)"

--[[-- @Transform(def)@ is a new transform object. @def@ keys:

  • @pos@, the position component of the transform as @V3@ object.
  • @rot@, the rotation component of the transform as a @Quat@ object.
  • @scale@, the scaling component of the transform.
  • @matrix@, the matrix of the transform (excludes @pos@, @rot@, @scale@).
  • @deps@, weak array of functions called whenever the transform changes.

:set (def)

TODO MISSING DOCUMENTATION

:insertDep (d)

TODO MISSING DOCUMENTATION

TODO MISSING DOCUMENTATION

:removeDep (d)

:lookAt (t, up)

@lookAt(pos[,up])@ rotates the transform so that the forward points at @pos@. If @up@ is unspecified @V3.oy ()@ is used. The final @up@ vector only matches if the forward direction is orthogonal to the forward direction.

TODO

#setTODO MISSING DOCUMENTATION

#insertDepTODO MISSING DOCUMENTATION

#insertDepTODO MISSING DOCUMENTATION