Created
July 2, 2018 07:25
-
-
Save CapsAdmin/392df2d92bdbd7633f2e255ed7ab2edf to your computer and use it in GitHub Desktop.
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
--[[object:GetParent() | |
object:GetTransform() -- find nearest transform in parents | |
uid | |
networking | |
transform can have a box which is meters in 3d and pixels in 2d | |
transform | |
physics | |
collision model | |
model | |
material | |
script | |
self.Parent.model.material:SetVector() | |
transform | |
light | |
]] | |
do | |
local META = prototype.CreateTemplate("object", "base_object") | |
runfile("lua/libraries/prototype/parenting_template.lua", META) | |
function META:GetTransform() | |
for i, v in ipairs(self:GetParentList()) do | |
if v.ClassName == "transform" then | |
return v | |
end | |
end | |
error("no transform found", 2) | |
end | |
META:Register() | |
function Object(name) | |
local obj = prototype.CreateObject(prototype.GetRegistered("object", name)) | |
return obj | |
end | |
function RegisterObject(META) | |
META.TypeBase = "base_object" | |
META.Type = "object" | |
prototype.Register(META) | |
end | |
end | |
do | |
local META = prototype.CreateTemplate() | |
META.ClassName = "rectangle" | |
META:StartStorable() | |
META:GetSet("Color", Color(1,1,1,1)) | |
META:EndStorable() | |
function META:Draw() | |
render2d.SetWorldMatrix(self:GetTransform():GetMatrix()) | |
gfx.DrawRect(0,0,1,1, nil, self.Color:Unpack()) | |
end | |
RegisterObject(META) | |
end | |
do | |
local META = prototype.CreateTemplate() | |
META.ClassName = "transform" | |
META:GetSet("TRMatrix", Matrix44()) | |
META:GetSet("ScaleMatrix", Matrix44()) | |
META:StartStorable() | |
META:GetSet("Position", Vec3(0, 0, 0), {callback = "InvalidateTRMatrix"}) | |
META:GetSet("Rotation", Quat(0, 0, 0, 1), {callback = "InvalidateTRMatrix"}) | |
META:GetSet("Scale", Vec3(1, 1, 1), {callback = "InvalidateScaleMatrix"}) | |
--META:GetSet("Shear", Vec3(0, 0, 0), {callback = "InvalidateScaleMatrix"}) | |
META:GetSet("Size", Vec3(1, 1, 1), {callback = "InvalidateScaleMatrix"}) | |
META:GetSet("SkipRebuild", false) | |
META:EndStorable() | |
META:GetSet("OverridePosition", nil, {callback = "InvalidateTRMatrix"}) | |
META:GetSet("OverrideRotation", nil, {callback = "InvalidateTRMatrix"}) | |
META:GetSet("AABB", AABB(-1,-1,-1, 1,1,1), {callback = "InvalidateTRMatrix"}) | |
function META:Initialize() | |
self.temp_scale = Vec3(1, 1, 1) | |
self.visible_matrix_cache = {} | |
for i = 1, 8 do | |
self.visible_matrix_cache[i] = Matrix44() | |
end | |
end | |
function META:GetTRPosition() | |
local x, y, z = self.TRMatrix:GetTranslation() | |
return Vec3(-y, -x, -z) | |
end | |
function META:SetTRPosition(vec) | |
self.TRMatrix:SetTranslation(vec.x, vec.y, vec.z) | |
end | |
function META:GetTRAngles() | |
return self.TRMatrix:GetRotation():GetAngles() | |
end | |
function META:SetTRAngles(ang) | |
self.TRMatrix:SetRotation(Quat():SetAngles(ang)) | |
end | |
function META:GetTRRotation() | |
return self.TRMatrix:GetRotation() | |
end | |
function META:SetTRRotation(quat) | |
self.TRMatrix:SetRotation(quat) | |
end | |
function META:GetAngles() | |
return self.Rotation:GetAngles() | |
end | |
function META:SetAngles(ang) | |
self.Rotation:SetAngles(ang) | |
self:InvalidateTRMatrix() | |
end | |
function META:GetTranslatedAABB() | |
return self.translated_aabb | |
end | |
function META:GetBoundingSphere() | |
return self.bounding_sphere | |
end | |
function META:GetCameraDistance() | |
local x = self.TRMatrix.m30 - -render3d.camera.Position.y | |
local y = self.TRMatrix.m31 - -render3d.camera.Position.x | |
local z = self.TRMatrix.m32 - -render3d.camera.Position.z | |
return x * x + y * y + z * z | |
end | |
function META:OnParent() | |
self:InvalidateTRMatrix() | |
self:RebuildMatrix() | |
end | |
function META:GetMatrix() | |
self:RebuildMatrix() | |
return self.FinalMatrix | |
end | |
function META:InvalidateScaleMatrix() | |
self.temp_scale = self.Scale * (1/self.Size) | |
if not self.rebuild_tr_matrix then | |
for _, v in ipairs(self:GetChildrenList()) do | |
if v.ClassName == "transform" then | |
v.rebuild_scale_matrix = true | |
v.rebuild_tr_matrix = true | |
end | |
end | |
end | |
self.rebuild_scale_matrix = true | |
self.rebuild_tr_matrix = true | |
end | |
function META:InvalidateTRMatrix() | |
if not self.rebuild_tr_matrix then | |
for _, v in ipairs(self.Entity:GetChildrenList()) do | |
if v.ClassName == "transform" then | |
v.rebuild_tr_matrix = true | |
end | |
end | |
end | |
self.rebuild_tr_matrix = true | |
end | |
function META:RebuildMatrix() | |
if self.rebuild_scale_matrix and (self.temp_scale.x ~= 1 or self.temp_scale.y ~= 1 or self.temp_scale.z ~= 1) then | |
self.ScaleMatrix:Identity() | |
self.ScaleMatrix:Scale(self.temp_scale.y, self.temp_scale.x, self.temp_scale.z) | |
--self.ScaleMatrix:Shear(self.Shear) | |
end | |
if self.rebuild_tr_matrix and not self.SkipRebuild then | |
local pos = self.Position | |
local rot = self.Rotation | |
if self.OverrideRotation then | |
rot = self.OverrideRotation | |
end | |
if self.OverridePosition then | |
pos = self.OverridePosition | |
end | |
self.TRMatrix:Identity() | |
self.TRMatrix:SetTranslation(-pos.y, -pos.x, -pos.z) | |
self.TRMatrix:SetRotation(rot) | |
if self.Entity:HasParent() then | |
local parent_transform = self.Entity.Parent:GetComponent("transform") | |
if not parent_transform then | |
for _, ent in ipairs(self.Entity:GetParentList()) do | |
parent_transform = ent:GetComponent("transform") | |
if parent_transform then | |
break | |
end | |
end | |
end | |
if parent_transform then | |
self.TRMatrix = parent_transform.TRMatrix * self.TRMatrix | |
end | |
end | |
end | |
if self.rebuild_tr_matrix or self.rebuild_scale_matrix then | |
local aabb = self:GetAABB():Copy() | |
local x,y,z = self.TRMatrix:GetTranslation() | |
aabb:SetMax(aabb:GetMax() * self.temp_scale * 3) | |
aabb:SetMin(aabb:GetMin() * self.temp_scale * 3) -- todo: proper rotation | |
aabb.min_x = aabb.min_x + x | |
aabb.min_y = aabb.min_y + y | |
aabb.min_z = aabb.min_z + z | |
aabb.max_x = aabb.max_x + x | |
aabb.max_y = aabb.max_y + y | |
aabb.max_z = aabb.max_z + z | |
self.translated_aabb = aabb | |
self.bounding_sphere = aabb:GetMin():Distance(aabb:GetMax()) | |
if self.temp_scale.x ~= 1 or self.temp_scale.y ~= 1 or self.temp_scale.z ~= 1 then | |
self.FinalMatrix = self.TRMatrix * self.ScaleMatrix | |
else | |
self.FinalMatrix = self.TRMatrix | |
end | |
end | |
self.rebuild_tr_matrix = false | |
self.rebuild_scale_matrix = false | |
end | |
function META:IsPointsVisible(points, view) | |
view = view or render.GetProjectionViewMatrix() | |
local matrix = self:GetMatrix() | |
for _, pos in ipairs(points) do | |
local x, y, z = view:GetMultiplied(matrix, Matrix44():SetTranslation(pos.x, pos.y, pos.z)):GetClipCoordinates() | |
if | |
(x > -1 and x < 1) and | |
(y > -1 and y < 1) and | |
(z > -1 and z < 1) | |
then | |
return true | |
end | |
end | |
return false | |
end | |
RegisterObject(META) | |
end | |
local transform = Object("transform") | |
local rectangle = Object("rectangle") | |
transform:AddChild(rectangle) | |
event.AddListener("DrawGUI", "", function() | |
for i,v in ipairs(transform:GetChildrenList()) do | |
if v.Draw then | |
v:Draw() | |
end | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment