• A 3D Scope - How Do?
    5 replies, posted
Hi everyone. im trying to code a 3D scope for some of my weapons. I want to make it kind of like a zoomed-in render target, but i honestly dont have half a clue how im going to code it. Anyone have any suggestions or tips?
Use a render target, convert to material, create poly in the shape of the circle. Similar to how gangwars has it's 3D radar, you can look at how they do it.
I have the render target material and everything, but i dont know how to make a poly or make it a circle. Can you give me an example of code?
[lua] local Radar = {} Radar.Targets = {} // Create map object local RadarMaps = {} // Make maps // Flatgrass local tmap = {} tmap.name = "gm_flatgrass" tmap.texture = surface.GetTextureID("radar/gw_downtown"); tmap.minx = -13600 tmap.maxx = 7800 tmap.miny = 17331 tmap.maxy = -4600 RadarMaps[tmap.name] = tmap // Downtown local tmap = {} tmap.name = "gw_downtown_v3" tmap.texture = surface.GetTextureID("radar/rpw_downtown_v3"); tmap.minx = -3600 tmap.maxx = 5900 tmap.miny = 5400 tmap.maxy = -4200 RadarMaps[tmap.name] = tmap // Cityway local tmap = {} tmap.name = "rp_cityway_v1" tmap.texture = surface.GetTextureID("radar/rp_cityway_v1"); tmap.minx = -14700 tmap.maxx = 4200 tmap.miny = 17900 tmap.maxy = -950 RadarMaps[tmap.name] = tmap // Crime city local tmap = {} tmap.name = "gw_crimecity_rc" tmap.texture = surface.GetTextureID("radar/gw_crimecity_rc"); tmap.minx = -11049 tmap.maxx = 13673 tmap.miny = 12542 tmap.maxy = -11895 RadarMaps[tmap.name] = tmap // left (x, y, z) // -10975.9580 2833.7151 546.2639] // right // 4852.1069 158.8308 -88.1052 // bottom // -5074.4663 -8886.6885 176.7886 // top // -5.4860 6800.8823 227.4823 // GangWars Downtown local tmap = {} tmap.name = "gw_downtown_beta_v5" tmap.texture = surface.GetTextureID("radar/gw_downtown"); tmap.minx = -10975 + 4600 tmap.maxx = 4852 + 4600 tmap.miny = 6800 tmap.maxy = -8886 RadarMaps[tmap.name] = tmap // pick radar map function SetRadarMap() local map = string.lower(game.GetMap( )) if RadarMaps[map] then Radar.map = RadarMaps[map] Msg("Set radar map for "..map.."\n") else Msg("ERROR! Could not find an appropriate radar map\n") end end hook.Add("InitPostEntity", "SetRadarMap", SetRadarMap) // props /* RadarTarget = {} RadarTarget.func = function() local List = {} List = ents.FindByClass("prop_physics") for k,v in pairs(List) do v.icon = surface.GetTextureID( "gui/silkicons/anchor" ) end return List end table.insert(Radar.Targets, RadarTarget) */ // NPC target RadarTarget = {} RadarTarget.func = function(targ) local List = {} List = ents.FindByClass("npc_*") for k,v in pairs(List) do v.icon = surface.GetTextureID( "gui/silkicons/user" ) if v.RadarName then v.text = v:RadarName() end end return List end table.insert(Radar.Targets, RadarTarget) // Building target RadarTarget = {} RadarTarget.func = function(targ) local List = {} List = ents.FindByClass("building") for k,v in pairs(List) do v.icon = surface.GetTextureID( "radar/gw_house" ) //if ValidEntity(v) then v.text = v:GetNWString("name") end end return List end table.insert(Radar.Targets, RadarTarget) // Territory target RadarTarget = {} RadarTarget.func = function(targ) local List = {} List = ents.FindByClass("gang_territory") for k,v in pairs(List) do v.icon = surface.GetTextureID( "gui/silkicons/shield" ) end return List end table.insert(Radar.Targets, RadarTarget) // Mission pickup target RadarTarget = {} RadarTarget.func = function(targ) local List = {} List = GangWars.CLPickups for k,v in pairs(List) do v.icon = surface.GetTextureID( "gui/silkicons/star" ) end return List end table.insert(Radar.Targets, RadarTarget) // Contraband Target RadarTarget = {} RadarTarget.func = function(targ) local List = ScannerContraband if List then for k,v in pairs(List) do v.icon = surface.GetTextureID( "gui/silkicons/bomb" ) end end return List end table.insert(Radar.Targets, RadarTarget) // Player targets RadarTarget = {} RadarTarget.func = function(targ) local LP = LocalPlayer() local List2 = {} local List = ents.FindByClass("player") for k,v in pairs(List) do if ValidEntity(v) and v:IsPlayer() and not v:GetNWBool("cloaked") then // Police see wanted players if (LP:Team() == TEAM_POLICE or LP:Team() == TEAM_CHIEF) and v:GetNWBool("wanted") then v.icon = surface.GetTextureID( "gui/silkicons/exclamation" ) v.text = v:Nick() table.insert(List2, v) end // Hitman sees his target if (LP:Team() == TEAM_HITMAN) and LP:GetNWInt("hit_target") == v:UniqueID() then v.icon = surface.GetTextureID( "gui/silkicons/exclamation" ) v.text = "TARGET" table.insert(List2, v) end // Targets see hitman if LP:GetNWInt("hitman_id") == v:UniqueID() then v.icon = surface.GetTextureID( "gui/silkicons/exclamation" ) v.text = "HITMAN" table.insert(List2, v) end // People see gang members if LP:GetNWString("gang_name") == v:GetNWString("gang_name") and v != LP then v.icon = surface.GetTextureID( "gui/silkicons/emoticon_smile" ) v.text = v:Nick() table.insert(List2, v) end end end return List2 end table.insert(Radar.Targets, RadarTarget) // load materials Radar.background = surface.GetTextureID("gangwars/hud2/radar_base"); Radar.deagle = surface.GetTextureID("gangwars/hud2/hit"); Radar.lights = {} Radar.lights[1] = surface.GetTextureID("gangwars/hud2/want_off"); Radar.lights[2] = surface.GetTextureID("gangwars/hud2/want_redon"); Radar.lights[3] = surface.GetTextureID("gangwars/hud2/want_blueon"); Radar.lights[4] = surface.GetTextureID("gangwars/hud2/want_allon"); Radar.light = 1 Radar.hazard = {} Radar.hazard[1] = surface.GetTextureID("gangwars/hud2/hazard_1"); Radar.hazard[2] = surface.GetTextureID("gangwars/hud2/hazard_2"); Radar.hazard[3] = surface.GetTextureID("gangwars/hud2/hazard_3"); Radar.hazard[4] = surface.GetTextureID("gangwars/hud2/hazard_4"); Radar.hazard[5] = surface.GetTextureID("gangwars/hud2/hazard_5"); Radar.hazard[6] = surface.GetTextureID("gangwars/hud2/hazard_6"); Radar.hazard[7] = surface.GetTextureID("gangwars/hud2/hazard_off"); Radar.haz_anim = 7 Radar.lastSeekTime = CurTime(); // size of radar on screen Radar.radarRadius = 100; // position of radar on screen Radar.radarPos = { x = 99; y = 99; } // zoom for radar Radar.zoom = 0.1 // circle polygon data Radar.circle= {} Radar.res = 32 for i=1,Radar.res do Radar.circle[i] = {} Radar.circle[i].x = Radar.radarPos.x + math.cos(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99 Radar.circle[i].y = Radar.radarPos.y + math.sin(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99 Radar.circle[i].u = (math.cos(math.rad( (i*360)/Radar.res )) + 1)*0.5 Radar.circle[i].v = -(math.sin(math.rad( (i*360)/Radar.res )) + 1)*0.5 end local function normalizeMapPos(pos) // limit pos to inside map pos.x = math.Clamp(pos.x, Radar.map.minx, Radar.map.maxx) pos.y = math.Clamp(pos.y, Radar.map.maxy, Radar.map.miny) // normalize pos pos.x = (pos.x - Radar.map.minx) / (Radar.map.maxx - Radar.map.minx) pos.y = (pos.y - Radar.map.miny) / (Radar.map.maxy - Radar.map.miny) return pos end local function calcCircle() local orient = LocalPlayer():EyeAngles().y + 270 local pos = normalizeMapPos( LocalPlayer():GetPos() ) for i=1,Radar.res do Radar.circle[i].u = ( (math.cos(math.rad( -orient + (i*360)/Radar.res )) ) * Radar.zoom ) + (pos.x) Radar.circle[i].v = ( (math.sin(math.rad( -orient + (i*360)/Radar.res )) ) * Radar.zoom ) + (pos.y) end end // Radar think local lightsTimer = CurTime() local hazardTimer = CurTime() local function RadarThink() if ( CurTime() > Radar.lastSeekTime + 2 ) then Radar.lastSeekTime = CurTime(); for k, v in pairs(Radar.Targets) do v.List = v.func() end end // animate hazard if LocalPlayer():GetNWBool("hazard") then if hazardTimer < CurTime() then Radar.haz_anim = Radar.haz_anim + 1 if Radar.haz_anim > 7 then Radar.haz_anim = 1 end hazardTimer = CurTime() + 0.1 end else Radar.haz_anim = 7 end if LocalPlayer():GetNWBool("wanted") then if lightsTimer < CurTime() then Radar.light = Radar.light + 1 if Radar.light > 4 then Radar.light = 1 end lightsTimer = CurTime() + 0.1 end else Radar.light = 1 end end hook.Add( "Think", "RadarThink", RadarThink ) // Renders a radar blip func
[code] surface.SetTexture(Radar.map.texture) surface.SetDrawColor(255, 255, 255, 255) surface.DrawPoly(Radar.circle) [/code] Surface is for huds and such. How can i make this a 3D entity to go where the scope model is on my gun? [editline]21st September 2012[/editline] oh. Okay, This is what i have so far: [lua] if CLIENT then function SWEP:DrawHUD() local Radar = {} Radar.circle= {} Radar.res = 32 Radar.radarRadius = 1000; Radar.radarPos = { x = 99; y = 99; } for i=1,Radar.res do Radar.circle[i] = {} Radar.circle[i].x = Radar.radarPos.x + math.cos(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99 Radar.circle[i].y = Radar.radarPos.y + math.sin(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99 Radar.circle[i].u = (math.cos(math.rad( (i*360)/Radar.res )) + 1)*0.5 Radar.circle[i].v = -(math.sin(math.rad( (i*360)/Radar.res )) + 1)*0.5 end cam.Start3D() local pos, ang = self:GetViewModelPosition() surface.SetTexture("negrs") surface.SetDrawColor(255, 255, 255, 255) surface.DrawPoly(Radar.circle) cam.End3D() end end [/lua] draws a huge screwed up looking thing wherever you are, and doesn't parent to your viewmodel at all. Edit: GetViewModelPosition apparently doesn't work, Changed the scale to 100, now it draws a circle in the middle of the map. On a print test, GetViewModelPosition returns "nil nil", While the function is defined in my base as a ironsights function. Still returns nothing. [editline]21st September 2012[/editline] [lua] SWEP:ViewModelDrawn() old = render.GetRenderTarget( ) ply = LocalPlayer() --ang2 = ang - aim --ang:RotateAroundAxis(ang:Up(), -ang2.y) CamData = {} CamData.angles = ang CamData.origin = ply:GetShootPos() CamData.x = 0 CamData.y = 0 CamData.w = 512 CamData.h = 512 CamData.fov = 4 CamData.drawviewmodel = false CamData.drawhud = false --render.SetRenderTarget( self.AcogRT ) render.SetViewPort( 0, 0, 512, 512 ) cam.Start2D() render.RenderView(CamData) //surface.SetDrawColor(255, 255, 255, 255) -- for GMod 13 //surface.SetTexture(ScopeTexture) //surface.DrawTexturedRect(-256, -256, 1024, 1024) cam.End2D() render.SetViewPort( 0, 0, ScrW( ), ScrH( ) ) render.SetRenderTarget( old ) end [/lua] Taken from LEETNOOB's Customizeable Weaponry. Crashes the game as soon as i spawn the weapon.
-snip-
Sorry, you need to Log In to post a reply to this thread.