Created
May 3, 2019 16:31
-
-
Save MikuAuahDark/4f3110aa3128ca1f9e999136c01d27b6 to your computer and use it in GitHub Desktop.
Extend functionality of LOVE 11.0 Transform object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Script that monkeypatch LOVE Transform | |
-- to add 3D support | |
local love = require("love") | |
local t = love.math.newTransform() | |
local t2 = love.math.newTransform() | |
local transformMt = getmetatable(t) | |
local function applyDump(ts, ...) | |
t2:reset() | |
t2:apply(ts) | |
return "row", t:setMatrix(...):apply(t2):getMatrix() | |
end | |
function transformMt:translate3D(x, y, z) | |
return self:setMatrix(applyDump(self, | |
1, 0, 0, x, | |
0, 1, 0, y, | |
0, 0, 1, z, | |
0, 0, 0, 1 | |
)) | |
end | |
function transformMt:scale3D(x, y, z) | |
x, y, z = x or 1, y or x or 1, z or x or 1 | |
return self:setMatrix(applyDump(self, | |
x, 0, 0, 0, | |
0, y, 0, 0, | |
0, 0, z, 0, | |
0, 0, 0, 1 | |
)) | |
end | |
function transformMt:quaternionRotate(rot, x, y, z) | |
local len = math.sqrt(x*x + y*y + z*z) | |
if len == 0 then return end | |
x, y, z = x/len, y/len, z/len | |
local c, s = math.cos(rot), math.sin(rot) | |
return self:setMatrix(applyDump(self, | |
x*x*(1-c)+c, y*x*(1-c)+z*s, x*z*(1-c)-y*s, 0, | |
x*y*(1-c)-z*s, y*y*(1-c)+c, y*z*(1-c)+x*s, 0, | |
x*z*(1-c)+y*s, y*z*(1-c)-x*s, z*z*(1-c)+c, 0, | |
0, 0, 0, 1 | |
)) | |
end | |
function transformMt:rotateX(rot) | |
local c, s = math.cos(rot), math.sin(rot) | |
return self:setMatrix(applyDump(self, | |
1, 0, 0, 0, | |
0, c,-s, 0, | |
0, s, c, 0, | |
0, 0, 0, 1 | |
)) | |
end | |
function transformMt:rotateY(rot) | |
local c, s = math.cos(rot), math.sin(rot) | |
return self:setMatrix(applyDump(self, | |
c, 0, s, 0, | |
0, 1, 0, 0, | |
-s, 0, c, 0, | |
0, 0, 0, 1 | |
)) | |
end | |
transformMt.rotateZ = transformMt.rotate | |
function transformMt:transpose() | |
return self:setMatrix("column", self:getMatrix()) | |
end | |
return true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment