Type class updates.

This commit is contained in:
jussi
2024-11-20 17:10:42 +02:00
parent cddfc09ccc
commit cf2c2eb05b
8 changed files with 245 additions and 33 deletions

View File

@@ -3,6 +3,8 @@ if table.unpack == nil then
table.unpack = unpack
end
local Vector3 = Vector3 or require( "vector3" )
local Color = {}
local metatable = {
__index = Color,
@@ -45,8 +47,8 @@ function Color:new( r, g, b, a )
local object = setmetatable( {}, metatable )
object.r = r or 255
object.g = g or 255
object.b = b or 255
object.g = g or object.r
object.b = b or object.g
object.a = a or 255
return object
@@ -73,8 +75,8 @@ end
function Color:set( r, g, b, a )
self.r = r or 255
self.g = g or 255
self.b = b or 255
self.g = g or self.r
self.b = b or self.g
self.a = a or 255
end
@@ -171,6 +173,15 @@ function Color:round()
)
end
function Color:mix( c )
return Color:temp(
( self.r + c.r ) / 2,
( self.g + c.g ) / 2,
( self.b + c.b ) / 2,
( self.a + c.a ) / 2
):round()
end
function Color:invert()
return Color:new( 255 - self.r, 255 - self.g, 255 - self.b, self.a )
end
@@ -191,8 +202,8 @@ function Color:temp( r, g, b, a )
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.r = r or 255
object.g = g or 255
object.b = b or 255
object.g = g or object.r
object.b = b or object.g
object.a = a or 255
return object

View File

@@ -3,31 +3,15 @@ if table.unpack == nil then
table.unpack = unpack
end
local function copyMatrix( orig )
local copy = RL.MatrixIdentity()
if orig ~= nil then
for y = 1, 4 do
for x = 1, 4 do
if orig[x][y] ~= nil then
copy[x][y] = orig[x][y]
end
end
end
end
return copy
end
local Matrix = {}
local metatable = {
__index = Matrix,
__tostring = function( m )
return "{\n"
.." {"..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].."},\n"
.." {"..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].."},\n"
.." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n"
.." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n"
.." {"..m[1][1]..", "..m[1][2]..", "..m[1][3]..", "..m[1][4].."},\n"
.." {"..m[2][1]..", "..m[2][2]..", "..m[2][3]..", "..m[2][4].."},\n"
.." {"..m[3][1]..", "..m[3][2]..", "..m[3][3]..", "..m[3][4].."},\n"
.." {"..m[4][1]..", "..m[4][2]..", "..m[4][3]..", "..m[4][4].."},\n"
.."}"
end,
__add = function( m1, m2 )
@@ -44,22 +28,40 @@ local metatable = {
end,
}
function Matrix:copyMatrix( orig )
if orig ~= nil then
for y = 1, 4 do
for x = 1, 4 do
if orig[x][y] ~= nil then
self[x][y] = orig[x][y]
end
end
end
end
end
function Matrix:new( m )
local object = setmetatable( {}, metatable )
object.m = copyMatrix( m )
-- Raylib style transposed matrix.
for x = 1, 4 do
object[x] = {}
for y = 1, 4 do
table.insert( object[x], m[x][y] )
end
end
return object
end
function Matrix:set( m )
self.m = copyMatrix( m )
self:copyMatrix( m )
end
function Matrix:serialize()
local str = { "Matrix:new({" }
for i, row in ipairs( self.m ) do
for i, row in ipairs( self ) do
table.insert( str, "{" )
for c, v in ipairs( row ) do
@@ -79,7 +81,7 @@ function Matrix:serialize()
end
function Matrix:clone()
return Matrix:new( self.m )
return Matrix:new( self )
end
function Matrix:determinant()
@@ -154,4 +156,28 @@ function Matrix:lookAt( eye, target, up )
return Matrix:new( RL.MatrixLookAt( eye, target, up ) )
end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, Matrix:new( RL.MatrixIdentity() ) )
end
function Matrix:temp( m )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object:copyMatrix( m )
return object
end
function Matrix:getTempId()
return curTemp
end
return Matrix

View File

@@ -151,7 +151,7 @@ function Quaternion:fromMatrix( mat )
end
function Quaternion:toMatrix()
return Matrix:newT( RL.QuaternionToMatrix( self ) )
return Matrix:new( RL.QuaternionToMatrix( self ) )
end
function Quaternion:fromAxisAngle( axis, angle )

View File

@@ -2,7 +2,6 @@
if table.unpack == nil then
table.unpack = unpack
end
local Vector2 = Vector2 or require( "vector2" )
local Rectangle = {}
@@ -73,6 +72,17 @@ function Rectangle:newR( r )
return object
end
function Rectangle:newV( position, size )
local object = setmetatable( {}, metatable )
object.x = position.x
object.y = position.y
object.width = size.x
object.height = size.y
return object
end
function Rectangle:set( x, y, width, height )
self.x = x or 0
self.y = y or self.x
@@ -91,6 +101,13 @@ function Rectangle:setR( r )
self.height = r.height
end
function Rectangle:setV( position, size )
self.x = position.x
self.y = position.y
self.width = size.x
self.height = size.y
end
function Rectangle:serialize()
return "Rectangle:new("..self.x..","..self.y..","..self.width..","..self.height..")"
end
@@ -221,6 +238,20 @@ function Rectangle:tempR( r )
return object
end
function Rectangle:tempV( position, size )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.x = position.x
object.y = position.y
object.width = size.x
object.height = size.y
return object
end
function Rectangle:getTempId()
return curTemp
end