mat4¶
Source: base/tealdef/scripts/mat4.d.tl
Notes
Types of the plain-Lua 4x4 matrix library /scripts/mat4.tl (Mat4Serialized). A matrix is a flat
table of 16 numbers in column-major order (elements 13, 14, 15 hold the translation), the same layout
the game uses for model and construction transforms (transf). The base game usually loads it as
local mat4 = ug_require "/scripts/mat4.tl" as Mat4Serialized.
Example
Mat4Serialized¶
record global base/tealdef/scripts/mat4.d.tl:3
global record Mat4Serialized
Load: local transf = require "/scripts/mat4.tl"
Notes
The 4x4 matrix library returned by ug_require "/scripts/mat4.tl", and the namespace of its Mat4
type. Matrices are flat 16-number tables in column-major order; the translation is in elements 13-15.
Functions
new(col1 : Vec4Serialized.Vec4, col2 : Vec4Serialized.Vec4, col3 : Vec4Serialized.Vec4, col4 : Vec4Serialized.Vec4) : Mat4 base/tealdef/scripts/mat4.d.tl:10
Call as transf.new
Notes
Creates a matrix from four column vectors and attaches the +, - and * operators.
| Parameter | Type | Description |
|---|---|---|
col1 |
Vec4Serialized.Vec4 |
First column (elements 1-4). |
col2 |
Vec4Serialized.Vec4 |
Second column (elements 5-8). |
col3 |
Vec4Serialized.Vec4 |
Third column (elements 9-12). |
col4 |
Vec4Serialized.Vec4 |
Fourth column (elements 13-16); x, y, z are the translation for affine transforms. |
Returns Mat4: The matrix as a flat 16-element table.
identity() : Mat4 base/tealdef/scripts/mat4.d.tl:11
Call as transf.identity
Notes
Returns the 4x4 identity matrix.
Returns Mat4
Used in the base game: 33 times in 12 files
base/content/industries/industryutil.lua:104
base/content/landmarks/landmark_util_serialized.tl:41
base/content/depots/rail/rail/rail_depot.script.lua:359
base/content/stations/air/air/airfield/airfield_modules.script.lua:219
base/content/stations/air/air/heliport/heliport.script.lua:294
base/content/stations/air/air/helipad/helipad.script.lua:148
… and 6 more files.
add(m1 : Mat4, m2 : Mat4) : Mat4 base/tealdef/scripts/mat4.d.tl:13
Call as transf.add
Notes
Returns the element-wise sum m1 + m2.
Returns Mat4
sub(m1 : Mat4, m2 : Mat4) : Mat4 base/tealdef/scripts/mat4.d.tl:14
Call as transf.sub
Notes
Returns the element-wise difference m1 - m2.
Returns Mat4
mul(m1 : Mat4, m2 : Mat4) : Mat4 base/tealdef/scripts/mat4.d.tl:15
Call as transf.mul
Notes
Returns the matrix product m1 * m2, i.e. the transform that applies m2 first and then m1.
Returns Mat4
Details
To place a part relative to a parent transform, write mat4.mul(parentTransf, localTransf).
Example
Used in the base game: 65 times in 21 files
base/content/industries/fieldutil.lua:282
base/content/buildings/townbuildingutil.lua:154
model.transf = transf.mul(transf.transl({ x = 0, y = 0, z = math.lerp(p1[3], p2[3], (alignmentPos[1] - p1[1]) / dx) }), model.transf)
base/content/landmarks/landmark_util_serialized.tl:158
base/content/warehouses/warehouses/warehouse.script.lua:109
base/content/landmarks/hq/hq/headquarter.script.tl:371
base/content/landmarks/hq/hq/headquarter_addon.script.tl:50
… and 15 more files.
gemv(m : Mat4, v : Vec4Serialized.Vec4) : Vec4Serialized.Vec4 base/tealdef/scripts/mat4.d.tl:21
Call as transf.gemv
General matrix-vector multiplication.
| Parameter | Type | Description |
|---|---|---|
m |
Mat4 |
matrix |
v |
Vec4Serialized.Vec4 |
vector |
Returns Vec4Serialized.Vec4: resulting vector
Used in the base game: 30 times in 4 files
base/content/stations/air/air/airport/airport_modules.script.lua:16
base/content/stations/water/water/modules.script.lua:844
base/content/stations/street/street/modular_street_station/modules.script.lua:105
base/content/scripts/scripts/construction/modulesutil.lua:358
flipY(m : Mat4) : Mat4 base/tealdef/scripts/mat4.d.tl:26
Call as transf.flipY
Multiply the y coordinate of each column with -1.
| Parameter | Type | Description |
|---|---|---|
m |
Mat4 |
matrix |
Returns Mat4: matrix with Y flipped
transl(t : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:30
Call as transf.transl
Create translation matrix from a translation vector.
| Parameter | Type | Description |
|---|---|---|
t |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: translation matrix
Used in the base game: 227 times in 36 files
base/content/industries/fieldutil.lua:401
transf = transf.transl( {x = fieldConfig.fields[i].pos[1] , y = fieldConfig.fields[i].pos[2], z = 0,}),
base/content/buildings/townbuildingutil.lua:154
model.transf = transf.mul(transf.transl({ x = 0, y = 0, z = math.lerp(p1[3], p2[3], (alignmentPos[1] - p1[1]) / dx) }), model.transf)
base/content/landmarks/landmark_util_serialized.tl:156
base/content/warehouses/warehouses/warehouse.script.lua:109
base/content/landmarks/hq/hq/headquarter.script.tl:371
base/content/landmarks/hq/hq/headquarter_addon.script.tl:37
… and 30 more files.
scale(t : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:34
Call as transf.scale
Create scale matrix.
| Parameter | Type | Description |
|---|---|---|
t |
Vec3Serialized.Vec3 |
scale vector |
Returns Mat4: scale matrix
Used in the base game: 48 times in 11 files
base/content/warehouses/warehouses/modules.script.lua:353
base/content/base/base/metadataanimationutil.lua:79
base/content/gui/gui/menu/main_menu.css.lua:245
base/content/gui/gui/construction/construction.css.lua:415
base/content/gui/gui/main/tile_list_react_util.css.lua:118
base/content/gui/gui/main/hud_icon_master.css.lua:19
… and 5 more files.
rotZ(rotZ : number) : Mat4 base/tealdef/scripts/mat4.d.tl:38
Call as transf.rotZ
Create rotation matrix for the rotation around Z axis.
| Parameter | Type | Description |
|---|---|---|
rotZ |
number |
rotation angle in radians |
Returns Mat4: rotation matrix
Used in the base game: 19 times in 12 files
base/content/landmarks/landmark_util_serialized.tl:158
base/content/landmarks/hq/hq/headquarter_addon.script.tl:50
base/content/base/base/metadataanimationutil.lua:304
base/content/infrastructure/tunnel/tunnel/tunnel.script.lua:158
base/content/industries/fishing_grounds/fishing_grounds/modules.script.lua:28
base/content/industries/oil_well/oil_well/modules.script.lua:20
… and 6 more files.
rotY(rotY : number) : Mat4 base/tealdef/scripts/mat4.d.tl:42
Call as transf.rotY
Create rotation matrix for the rotation around Y axis.
| Parameter | Type | Description |
|---|---|---|
rotY |
number |
rotation angle in radians |
Returns Mat4: rotation matrix
rotX(rotX : number) : Mat4 base/tealdef/scripts/mat4.d.tl:46
Call as transf.rotX
Create rotation matrix around X axis.
| Parameter | Type | Description |
|---|---|---|
rotX |
number |
rotation angle in radians |
Returns Mat4: rotation matrix
rotZTransl(rotZ : number, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:52
Call as transf.rotZTransl
Create matrix who has rotation around Z and translation
| Parameter | Type | Description |
|---|---|---|
rotZ |
number |
rotation angle |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
Used in the base game: 323 times in 30 files
base/content/landmarks/landmark_util_serialized.tl:413
base/content/landmarks/hq/hq/headquarter.script.tl:427
base/content/depots/rail/rail/rail_depot.script.lua:360
base/content/depots/water/water/water_depot.script.lua:419
base/content/depots/water/water/water_maint_station.script.lua:311
base/content/infrastructure/traffic_light/traffic_light/trafficlightutil.lua:188
… and 24 more files.
rotZYXTransl(rot : Vec3Serialized.Vec3, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:57
Call as transf.rotZYXTransl
Create matrix for ZYX rotations and translation.
| Parameter | Type | Description |
|---|---|---|
rot |
Vec3Serialized.Vec3 |
rotation vector (x is rotZ, y is rotY, z is rotX) and performed in this order |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
Used in the base game: 34 times in 8 files
base/content/warehouses/warehouses/warehouse.script.lua:233
base/content/warehouses/warehouses/modules.script.lua:29
base/content/landmarks/hq/hq/headquarter.script.tl:337
base/content/stations/rail/rail/modular_station/modular_station.script.lua:534
transf = transf.rotZYXTransl(vec3.degToRad(0.0, 0.0, 0.0), vec3.new(i * platformWidth + offset, j * platformLength, 0)),
base/content/stations/air/air/airfield.script.lua:261
base/content/stations/air/air/airport.script.lua:172
… and 2 more files.
rotZYTransl(rot : Vec3Serialized.Vec3, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:62
Call as transf.rotZYTransl
Create a matrix for ZY rotations and translation.
| Parameter | Type | Description |
|---|---|---|
rot |
Vec3Serialized.Vec3 |
rotation vector (x is rotZ, y is rotY, z is unused) and performed in this order |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
rotYCntTransl(rotY : number, cnt : Vec3Serialized.Vec3, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:68
Call as transf.rotYCntTransl
Create matrix for rotation around Y with a center specified and a translation.
| Parameter | Type | Description |
|---|---|---|
rotY |
number |
rotation angle |
cnt |
Vec3Serialized.Vec3 |
center point |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
rotZY(rot : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:73
Call as transf.rotZY
Create a matrix for ZY rotations.
| Parameter | Type | Description |
|---|---|---|
rot |
Vec3Serialized.Vec3 |
rotation vector (x is rotZ, y is rotY, z is unused) and performed in this order |
Returns Mat4: rotation matrix
scaleRotZTransl(scale : number, rotZ : number, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:80
Call as transf.scaleRotZTransl
Create matrix for (homogenous) scaling, rotation around Z, and translation.
| Parameter | Type | Description |
|---|---|---|
scale |
number |
scale factor |
rotZ |
number |
rotation angle |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
Used in the base game: 93 times in 20 files
base/content/industries/fieldutil.lua:282
base/content/gui/gui/music_player/music_player.css.lua:15
base/content/gui/gui/layers/layers.css.lua:21
base/content/gui/gui/context_helper/context_helper.css.lua:19
base/content/gui/gui/line_vehicle_mgmt/vehicle_store.css.lua:65
base/content/gui/gui/line_vehicle_mgmt/line_vehicle_mgmt.css.lua:110
… and 14 more files.
scaleXYZRotZTransl(scale : Vec3Serialized.Vec3, rotZ : number, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:86
Call as transf.scaleXYZRotZTransl
Create matrix for (inhomogenous) scaling, rotation around Z, and translation.
| Parameter | Type | Description |
|---|---|---|
scale |
Vec3Serialized.Vec3 |
scale vector |
rotZ |
number |
rotation angle |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
Used in the base game: 6 times in 4 files
base/content/infrastructure/tunnel/tunnel/tunnel.script.lua:125
base/content/infrastructure/bridge/bridge/bridgeutil.lua:223
base/content/infrastructure/bridge/bridge/bridgeutil2.lua:234
base/content/scripts/scripts/construction/constructionutil.lua:487
scaleRotZYXTransl(scale : Vec3Serialized.Vec3, rot : Vec3Serialized.Vec3, transl : Vec3Serialized.Vec3) : Mat4 base/tealdef/scripts/mat4.d.tl:92
Call as transf.scaleRotZYXTransl
Create matrix for (inhomogenous) scaling, ZYX rotations and translation.
| Parameter | Type | Description |
|---|---|---|
scale |
Vec3Serialized.Vec3 |
scale vector |
rot |
Vec3Serialized.Vec3 |
rotation vector (x is rotZ, y is rotY, z is rotX) and performed in this order |
transl |
Vec3Serialized.Vec3 |
translation vector |
Returns Mat4: combined matrix
getTransl(m : Mat4) : Vec3Serialized.Vec3 base/tealdef/scripts/mat4.d.tl:97
Call as transf.getTransl
Extract translation vector from matrix.
| Parameter | Type | Description |
|---|---|---|
m |
Mat4 |
matrix |
Returns Vec3Serialized.Vec3: translation vector
print(v : Mat4) : string base/tealdef/scripts/mat4.d.tl:102
Call as transf.print
Convert matrix to a string representation.
| Parameter | Type | Description |
|---|---|---|
v |
Mat4 |
matrix |
Returns string: string representation
Mat4Serialized.Mat4¶
record base/tealdef/scripts/mat4.d.tl:4
record Mat4 is {number}
16 entries, column major
Used in the base game: 48 times in 5 files
base/content/landmarks/hq/hq/headquarter.script.tl:344
result.addAlignmentColliderAndGroundTexture = function(cellSize : {Vec2Serialized.Vec2}, fenceCellSize : {Vec2Serialized.Vec2}, transf2 : Mat4Serialized.Mat4)
base/content/landmarks/hq/hq/headquarter_addon.script.tl:7
function data.updateFn(captureParams : table, result : HeadquarterUtilSerialized.HeadquarterConstructionResult, _transform : Mat4Serialized.Mat4, tag : string,
base/content/stations/air/air/heliport/modules.script.tl:7
function data.updateFn(captureParams : table, result : HeadquarterUtilSerialized.HeadquarterConstructionResult, transform : Mat4Serialized.Mat4, tag : string, s
base/content/scripts/scripts/mat4.tl:6
mat4_util.new = function(col1 : Vec4Serialized.Vec4, col2 : Vec4Serialized.Vec4, col3 : Vec4Serialized.Vec4, col4 : Vec4Serialized.Vec4) : Mat4Serialized.Mat4
base/content/scripts/scripts/construction/construction.script.tl:85
Functions
metamethod __add(m1 : Mat4, m2 : Mat4) : Mat4 base/tealdef/scripts/mat4.d.tl:5
Notes
Element-wise sum m1 + m2.
Returns Mat4