• Draw render target to matproxy?
    8 replies, posted
Hello guys! I'm wondering how I could draw a render target to the matproxy. The basic .vmt definition is as follows: [CODE] "Proxies" { "Scope" { "textureScrollVar" "$baseTextureTransform" "fieldOfView" "10" "scopeGeometry" "24" // 1 / sin( arctan( radius / ( 2 * length ) ) ) } }[/CODE] The thing is, although I know how RT Scopes work using just a normal texture, I don't know how to write the proper code for rendering the view to the matproxy. Any lua you could give me would be great, as I don't really know where to start. This is what I have so far: [CODE] matproxy.Add( { name = "Scope", init = function( self, mat, values ) -- Store the name of the variable we want to set self.Mat_Scope_FOV = values.fieldOfView self.Mat_RT = GetRenderTarget("ins_mat_rt",1024,1024,false) mat:SetTexture("$basetexture",self.Mat_RT) end, bind = function( self, mat, ent ) -- If the target ent has a function called GetPlayerColor ) then use that -- The function SHOULD return a Vector with the chosen player's colour. -- In sandbox this function is created as a network function, -- in player_sandbox.lua in SetupDataTables local oldrt oldrt = render.GetRenderTarget( ) render.SetRenderTarget( self.Mat_RT ) render.SetViewPort( 0, 0, 1024, 1024 ) render.Clear( 255, 000, 000, 255, true ) local RTScopeCam = {} RTScopeCam.x = 0 RTScopeCam.y = 0 RTScopeCam.w = 1024 RTScopeCam.h = 1024 RTScopeCam.angles = LocalPlayer():EyeAngles() RTScopeCam.origin = LocalPlayer():GetShootPos() RTScopeCam.fov = self.Mat_Scope_FOV RTScopeCam.drawhud = false RTScopeCam.ortho = false RTScopeCam.drawviewmodel = false RTScopeCam.dopostprocess = false cam.Start2D() render.RenderView(RTScopeCam) cam.End2D() render.SetViewPort( 0, 0, ScrW( ), ScrH( ) ) render.SetRenderTarget( oldrt ) end } )[/CODE] Which oddly crashes the game.
You must call render.RenderView in RenderScene hook, without the cam2D nonsense.
Thank you Robotboy! I was actually basing my code off some earlier code you posted a long time ago, so don't call it nonsense :P Anyways, is there a way I can store the variable from Matproxy as a variable accessible by the rest of the SWEP I'm putting this in?
I don't think I have ever posted any code with render.RenderView because I never was able to get it to work until recently. You could access the SWEP via LocalPlayer():GetActiveWeapon() or LocalPlayer():GetWeapon("weapon_class") in dev branch/after next update.
For some reason, the RT texture doesn't appear to be overriding properly. Instead, I get a white plane. This is my code: [CODE] SWEP.Mat_RT = nil function SWEP:InitRTScope() --[[ OLD CODE if CLIENT then self.RenderTarget = GetRenderTarget( "insurrection_rt_scope", 1024, 1024, false ) local mat mat = Material( "models/weapons/optics/lense_rt" ) mat:SetTexture( "$basetexture", self.RenderTarget ) --mat = Material( "models/m41a/pulse_rifle_ammo_zero" ) --mat:SetMaterialTexture( "$basetexture", SWEP.RenderTarget ) --mat = Material( "avp_pulserifle_ammo" ) --mat:SetMaterialVector( "$tint", Vector( 1.2, 0, 0 ) ) local color_red = Color( 189, 000, 000, 255 ) end ]]-- self.Mat_RT = GetRenderTarget("ins_mat_rt",1024,1024,false) matproxy.Add( { name = "Scope", init = function( self, mat, values ) -- Store the name of the variable we want to set if values.fieldOfView then LocalPlayer():GetActiveWeapon().Scope_FOV = values.fieldOfView else LocalPlayer():GetActiveWeapon().Scope_FOV = 30 end mat:SetTexture("$basetexture",LocalPlayer():GetActiveWeapon().Mat_RT) end, bind = function( self, mat, ent ) -- If the target ent has a function called GetPlayerColor ) then use that -- The function SHOULD return a Vector with the chosen player's colour. -- In sandbox this function is created as a network function, -- in player_sandbox.lua in SetupDataTables end } ) end // Called in Initialise in the SWEP code hook.Add("RenderScene","InsurrectionRTScope", function( tmporigin, tmpviewangles, tmpfov) local tmprt = LocalPlayer():GetActiveWeapon().Mat_RT if IsValid(tmprt) then local oldrt = render.GetRenderTarget( ) render.SetRenderTarget( tmprt ) render.SetViewPort( 0, 0, 1024, 1024 ) render.Clear( 255, 000, 000, 255, true ) RTScopeCam = {} RTScopeCam.x = 0 RTScopeCam.y = 0 RTScopeCam.w = 1024 RTScopeCam.h = 1024 RTScopeCam.fov = LocalPlayer():GetActiveWeapon().Mat_Scope_FOV RTScopeCam.drawhud = false RTScopeCam.ortho = false RTScopeCam.drawviewmodel = false RTScopeCam.dopostprocess = false RTScopeCam.angles = tmpviewangles RTScopeCam.origin = tmporigin render.RenderView(RTScopeCam) render.SetViewPort( 0, 0, ScrW( ), ScrH( ) ) render.SetRenderTarget( oldrt ) end end) [/CODE] Edit: Nevermind, I'll move it to renderscene and see what happens. Missed that. Edit: Didn't work. See updated code.
Move matproxy.Init outside of any functions. Also, I believe you are supposed to update $basetexture every time you render to rendertarget.
[QUOTE=Robotboy655;44829854]Move matproxy.Init outside of any functions. Also, I believe you are supposed to update $basetexture every time you render to rendertarget.[/QUOTE] I can't actually find a definition for matproy.Init. How exactly do I use it?
I meant matproxy.Add, sorry. Move it outside of any functions.
Alright, tested it by moving the matproxy.Add to an autorun function. Will report back with results in an edit post. Edit: For some reason, the skybox is only being partially rendered . It's being filled with the red background in certain areas when I look upwards with my gun. Any idea what could be causing this? Otherwise, perfect. My draw hook: [CODE]hook.Add("RenderScene","InsurrectionRTScope", function( tmporigin, tmpviewangles, tmpfov) local tmprt = Insurgency_Mat_RT if tmprt then local oldrt = render.GetRenderTarget( ) render.SetRenderTarget( tmprt ) render.SetViewPort( 0, 0, 1024, 1024 ) render.Clear( 255, 000, 000, 255, true ) local RTScopeCam = {} RTScopeCam.x = 0 RTScopeCam.y = 0 RTScopeCam.w = 1024 RTScopeCam.h = 1024 RTScopeCam.fov = LocalPlayer():GetActiveWeapon().Scope_FOV RTScopeCam.drawhud = false RTScopeCam.ortho = false RTScopeCam.drawviewmodel = false RTScopeCam.dopostprocess = false RTScopeCam.angles = tmpviewangles RTScopeCam.origin = tmporigin render.RenderView(RTScopeCam) render.SetViewPort( 0, 0, ScrW( ), ScrH( ) ) render.SetRenderTarget( oldrt ) end end)[/CODE] I'll bring this up in a different thread and mark this as solved.
Sorry, you need to Log In to post a reply to this thread.