I'm doing something like this to currently draw a box around an origin, but surely there's a better, less hacky method?
[lua]
function SWEP:DrawHUD()
blocklist = table.Copy(MapExploits)
local points = {}
local screen = nil
self.Size = self.Weapon:GetNetworkedInt("BoxSize") or 32
self.Mode = self.Weapon:GetNetworkedInt("BoxMode") or 1
table.insert(blocklist, { origin = self.Origin, bsize = self.Size, type = self.Modes[self.Mode], col = Color(150,150,20) })
for k, v in pairs(blocklist) do
screen = v.origin:ToScreen()
points = {}
if screen.x > 0 and screen.x < ScrW() and screen.y > 0 and screen.y < ScrH() then
table.insert(points, (v.origin+Vector(v.bsize,v.bsize,v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(-v.bsize,v.bsize,v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(-v.bsize,-v.bsize,v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(v.bsize,-v.bsize,v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(v.bsize,v.bsize,-v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(-v.bsize,v.bsize,-v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(-v.bsize,-v.bsize,-v.bsize)):ToScreen())
table.insert(points, (v.origin+Vector(v.bsize,-v.bsize,-v.bsize)):ToScreen())
draw.SimpleText( "Block "..k.." -\nType: "..v.type, "default", screen.x, screen.y, Color(255,0,0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
if v.col then
surface.SetDrawColor(v.col)
else
surface.SetDrawColor(Color(255,255,255))
end
for k, v in pairs(points) do
if k % 4 == 0 then
surface.DrawLine(v.x,v.y,points[k-3].x,points[k-3].y)
else
surface.DrawLine(v.x,v.y,points[k+1].x,points[k+1].y)
end
if k <= 4 then
surface.DrawLine(v.x,v.y,points[k+4].x,points[k+4].y)
end
end
end
end
end
[/lua]
MapExploits looks something like this:
[lua]
] lua_run_cl PrintTable(MapExploits)
1:
type = repel
bsize = 32
origin = 374.5938 438.7813 -0.9688
2:
type = repel
bsize = 32
origin = 560.5625 459.3750 -0.9688
[/lua]
Section of the FindInBox think function:
[lua]
local found = {}
local vec = nil
local sz = 0
for k, v in pairs(MapExploits) do
sz = v.bsize
vec = Vector(sz,sz,sz)
found = ents.FindInBox(v.origin-vec,v.origin+vec)
.......
[/lua]
Draw a Mesh around the point with a transparent material.
[QUOTE=Feihc;29759207]Draw a Mesh around the point with a transparent material.[/QUOTE]
So something like [b][url=http://wiki.garrysmod.com/?title=Mesh.QuadEasy]Mesh.QuadEasy [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] ?
Is there a simple way to draw the Mesh around the origin, at a size?
Bumping this; still really need something better to draw these. I tried messing with meshes, but I ended up getting cubes drawn in odd places, moving around the screen based on eye angles, etc, etc.
My current method, although is working, is very hacky. It also can get confusing, as the lines, when penetrating the world, draw directly over it, giving the impression the boxes are closer than where they actually are. I'd actually settle if I could stop the lines drawing over the "floor" of the world. Any way to get the bottom most, flat boundry of the world, ToScreen() it, and then only draw the lines up to that point?
What I'm really looking for is a function that I could simply throw an origin, and size at, and it'd draw a cube?
You could try this, it's got two parts, first is server side for adding boxes to the world.
[lua]
svDrawBoxes = {}
function drawBox( Min, Max, name, clr)
if Min == nil then return end
if Max == nil then return end
local boxName = name or tostring("TheBox" .. tostring(math.random(-12456, 123456)))
for k, v in pairs(player.GetHumans()) do
umsg.Start("AddDrawBox", v)
umsg.String(boxName)
umsg.Vector( Min )
umsg.Vector( Max )
umsg.String( ColorToString( clr ) )
umsg.End()
end
if !(svDrawBoxes[boxName] == nil) then
svDrawBoxes[boxName] = {Min, Max, clr}
else
table.Add(svDrawBoxes, boxName)
svDrawBoxes[boxName] = {Min, Max, clr}
end
end
[/lua]
And this is the client part that draws the boxes
[lua]
DrawBoxes = {}
function AddDrawBox( msgData )
local strID = msgData:ReadString()
local Min = msgData:ReadVector()
local Max = msgData:ReadVector()
local clr = msgData:ReadString()
clr = StringToColor(clr)
if !table.HasValue(DrawBoxes, strID) then
table.Add(DrawBoxes, strID)
DrawBoxes[strID] = { Min, Max, clr }
else
DrawBoxes[strID] = { Min, Max, clr }
end
end
usermessage.Hook("AddDrawBox", AddDrawBox)
function RenderBoxes( )
if DrawBoxes == nil then return end
if DrawBoxes == {} then return end
if table.Count(DrawBoxes) < 1 then return end
local matBeam = Material( "cable/new_cable_lit")
render.SetMaterial( matBeam )
for k, v in pairs(DrawBoxes) do
Min = v[1]
Max = v[2]
boxColor = v[3]
-- Top of the box
render.DrawBeam( Vector(Max.x, Max.y, Max.z), Vector(Max.x, Max.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Max.x, Max.y, Min.z), Vector(Min.x, Max.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Max.y, Min.z), Vector(Min.x, Max.y, Max.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Max.y, Max.z), Vector(Max.x, Max.y, Max.z), 5, 0, 0, boxColor)
--Bottom of the box
render.DrawBeam( Vector(Max.x, Min.y, Max.z), Vector(Max.x, Min.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Max.x, Min.y, Min.z), Vector(Min.x, Min.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Min.y, Min.z), Vector(Min.x, Min.y, Max.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Min.y, Max.z), Vector(Max.x, Min.y, Max.z), 5, 0, 0, boxColor)
-- Uprights of the box
render.DrawBeam( Vector(Max.x, Max.y, Max.z), Vector(Max.x, Min.y, Max.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Max.x, Max.y, Min.z), Vector(Max.x, Min.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Max.y, Min.z), Vector(Min.x, Min.y, Min.z), 5, 0, 0, boxColor)
render.DrawBeam( Vector(Min.x, Max.y, Max.z), Vector(Min.x, Min.y, Max.z), 5, 0, 0, boxColor)
end
end
hook.Add("PreDrawOpaqueRenderables", "fanRenderBoxes", RenderBoxes)
[/lua]
If i'm remembering right, this will not draw through the world, if a line goes underground it's no longer visible, I made this for visualizing and invisible brush entity in my meteor defense game mode. You will need to call drawBox on the server with the same name to update a boxes location, or a new name to draw a new box, I had a function to remove a box from the world, but I seem to have lost it, but it shouldn't be to hard to recreate.
Just realized it calls two custom functions these should be shared
[lua]
function StringToColor( str )
elements = string.Explode(".",str)
return Color(elements[1], elements[2], elements[3], elements[4])
end
function ColorToString( clr )
return tostring(clr.r .."." .. clr.g .."." .. clr.b .."." .. clr.a)
end
[/lua]
[b][url=http://wiki.garrysmod.com/?title=Debugoverlay.Box]Debugoverlay.Box [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
Note that it needs developer set to 1 or higher.
Thanks very much for sharing that :)
Exactly the kind of thing I was after.
Sorry, you need to Log In to post a reply to this thread.