Skip to content

Commit 73aeb1d

Browse files
Reorganize mesh logic
1 parent 19c1a3f commit 73aeb1d

2 files changed

Lines changed: 84 additions & 31 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
if SERVER then
2+
function ACF.ProcessMesh(Entity, Meshes)
3+
local MeshData = { Verts = {}, Convexes = {} }
4+
local Lookup = {}
5+
6+
local function GetIndex(Pos)
7+
local Key = Pos.x .. " " .. Pos.y .. " " .. Pos.z
8+
local Index = Lookup[Key]
9+
if not Index then
10+
MeshData.Verts[#MeshData.Verts + 1] = Pos
11+
Index = #MeshData.Verts
12+
Lookup[Key] = Index
13+
end
14+
return Index
15+
end
16+
17+
for _, Convex in ipairs(Meshes) do
18+
local ConvexTris = {}
19+
MeshData.Convexes[#MeshData.Convexes + 1] = ConvexTris
20+
21+
for I = 1, #Convex, 3 do
22+
local A = Entity:LocalToWorld(Convex[I].pos)
23+
local B = Entity:LocalToWorld(Convex[I + 1].pos)
24+
local C = Entity:LocalToWorld(Convex[I + 2].pos)
25+
26+
ConvexTris[#ConvexTris + 1] = Vector(GetIndex(A), GetIndex(B), GetIndex(C))
27+
end
28+
end
29+
30+
Entity.ACF_Volumetric_Mesh = MeshData
31+
end
32+
33+
hook.Add("ACF_OnLoadAddon", "ACF_Volumetric_Detours", function()
34+
local Detours = ACF and ACF.Detours
35+
print("Loading ACF Volumetric Detours", Detours)
36+
37+
local PhysicsInit PhysicsInit = Detours.Metatable("Entity", "PhysicsInit", function(self, ...)
38+
print("physicsinit", self)
39+
40+
return PhysicsInit(self, ...)
41+
end)
42+
43+
local PhysInitConvex_Orig PhysInitConvex_Orig = Detours.Metatable("Entity", "PhysicsInitConvex", function(self, Mesh, ...)
44+
print("physinitconvex", self, Mesh)
45+
46+
return PhysInitConvex_Orig(self, Mesh, ...)
47+
end)
48+
49+
local PhysInitMultiConvex_Orig PhysInitMultiConvex_Orig = Detours.Metatable("Entity", "PhysicsInitMultiConvex", function(self, Meshes, ...)
50+
print("physinitmulticonvex", self, Meshes)
51+
52+
return PhysInitMultiConvex_Orig(self, Meshes, ...)
53+
end)
54+
55+
local PhysicsInitStatic_Orig PhysicsInitStatic_Orig = Detours.Metatable("Entity", "PhysicsInitStatic", function(self, ...)
56+
print("physicsinitstatic", self)
57+
58+
return PhysicsInitStatic_Orig(self, ...)
59+
end)
60+
61+
hook.Add("OnEntityCreated", "ACF_Volumetric_Detours", function(ent)
62+
timer.Simple(0, function()
63+
if IsValid(ent) and IsValid(ent:GetPhysicsObject()) then
64+
print("OnEntityCreated", ent, ent:GetPhysicsObject())
65+
end
66+
end)
67+
end)
68+
end)
69+
end

lua/weapons/gmod_tool/stools/acfarmormesh.lua

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ The basic controller functions were modified from Prop To Mesh ()
1010
--]]
1111

1212
local ACF = ACF
13-
local Entities = ACF.Classes.Entities
13+
local Entities = ACF.Classes.Entities
1414
local ProcArmorTypes = ACF.Classes.ProcArmorTypes
15-
local Messages = ACF.Utilities.Messages
16-
local IsValid = IsValid
15+
local Messages = ACF.Utilities.Messages
16+
local IsValid = IsValid
17+
local debugoverlay = debugoverlay
1718

1819
TOOL.Category = (ACF.CustomToolCategory and ACF.CustomToolCategory:GetBool()) and "ACF" or "Construction"
1920
TOOL.Name = "#tool.acfarmormesh.name"
@@ -244,40 +245,23 @@ elseif SERVER then -- Serverside-only stuff
244245
local Phys = Entity:GetPhysicsObject()
245246
local Mesh = Phys and Phys:GetMeshConvexes()
246247
if Mesh then
247-
local MeshData = { Verts = {}, Tris = {} }
248-
local Lookup = {}
249-
250-
local function GetIndex(Pos)
251-
local Key = Pos.x .. " " .. Pos.y .. " " .. Pos.z
252-
local Index = Lookup[Key]
253-
if not Index then
254-
MeshData.Verts[#MeshData.Verts + 1] = Pos
255-
Index = #MeshData.Verts
256-
Lookup[Key] = Index
257-
end
258-
return Index
259-
end
248+
ACF.ProcessMesh(Entity, Mesh)
249+
250+
local MeshData = Entity.ACF_Volumetric_Mesh
251+
local Verts = MeshData.Verts
260252

261-
for Index, Convex in ipairs(Mesh) do
253+
for Index, ConvexTris in ipairs(MeshData.Convexes) do
262254
local Col = HSVToColor((Index * 47) % 360, 1, 1)
263255
Col.a = 150
264256

265-
local Average = Vector()
266-
for I = 1, #Convex, 3 do
267-
local A = Phys:LocalToWorld(Convex[I].pos)
268-
local B = Phys:LocalToWorld(Convex[I + 1].pos)
269-
local C = Phys:LocalToWorld(Convex[I + 2].pos)
270-
271-
MeshData.Tris[#MeshData.Tris + 1] = { GetIndex(A), GetIndex(B), GetIndex(C) }
272-
273-
debugoverlay.Triangle(A, B, C, 5, Col, true)
274-
Average = Average + A + B + C
257+
local AveragePos = Vector(0, 0, 0)
258+
for _, Tri in ipairs(ConvexTris) do
259+
debugoverlay.Triangle(Verts[Tri[1]], Verts[Tri[2]], Verts[Tri[3]], 5, Col, true)
260+
AveragePos = AveragePos + (Verts[Tri[1]] + Verts[Tri[2]] + Verts[Tri[3]])
275261
end
276-
Average = Average / (#Convex / 3 * 3)
277-
debugoverlay.Text(Average, Index, 5, Col, true)
262+
AveragePos = AveragePos / (3 * #ConvexTris)
263+
debugoverlay.Text(AveragePos, tostring(Index), 5, Col, true)
278264
end
279-
280-
Entity.ACF_Armor_Mesh = MeshData
281265
end
282266
end
283267
return true

0 commit comments

Comments
 (0)