Render target's material turns dark and makes the viewmodel dissappear

So basically the title is pretty much self explanatory.

I’ve been working on a kind of security system for a while and came up with this:

These are the issues I’ve faced:
-the screen turns darker when picked up with the physgun (maybe its because of the halo)
-the viewmodel dissappears when the screen is updated (it updates every .25 seconds)
-the screen’s material is not drawn on the other screens

This is the code I’ve used:


include("shared.lua")

local TEXTURE_FLAGS_CLAMP_S = 0x0004
local TEXTURE_FLAGS_CLAMP_T = 0x0008

surface.CreateFont("CamFont", {
	font = "DebugFixed",
	size = 32,
	weight = 300,
	antialias = false,
})

surface.CreateFont("CamFont/2", {
	font = "DebugFixed",
	size = 16,
	weight = 300,
	antialias = false,
})

surface.CreateFont("CamFont/4", {
	font = "DebugFixed",
	size = 8,
	weight = 300,
	antialias = false,
})

function ENT:Initialize()
	self.Cams = {}
	
	for i = 0, 4 do
		table.insert(self.Cams, Cameras*)
	end
	
	self.ID = #ents.FindByClass(self:GetClass())
	
	self.ScreenRT = GetRenderTargetEx("CCTV_monitor"..self.ID,
		512,
		512,
		RT_SIZE_NO_CHANGE,
		MATERIAL_RT_DEPTH_SEPARATE,
		TEXTURE_FLAGS_CLAMP_S || TEXTURE_FLAGS_CLAMP_T,
		CREATERENDERTARGETFLAGS_UNFILTERABLE_OK,
		IMAGE_FORMAT_RGB565
	)
	
	self.ScreenMat = CreateMaterial("CCTV_Monitor"..self.ID,"UnlitGeneric",{
		["$basetexture"] = self.ScreenRT:GetName()
	})
	
	self.DisplayMode = 2
end

function ENT:Draw()
	if !UpdateTime then
		UpdateTime = CurTime()
	end
	
	if UpdateTime < CurTime() and self.ScreenRT then
		InDraw = true
	
		render.PushRenderTarget(self.ScreenRT)
		
		render.Clear(0, 0, 0, 255)
		render.ClearDepth()
		render.ClearStencil()
		
		cam.Start2D()
			for k, v in pairs(self.Cams) do
				if self.DisplayMode == 1 then
					render.RenderView({
						angles = v.ang,
						origin = v.pos,
						x = 0,
						y = 0,
						w = 512,
						h = 512,
						fov = 90,
					})
					
					surface.DrawRect(0, 0, 4, 4)
					
					draw.SimpleText("Camera"..((k<10 and "0"..k) or k), "CamFont", 3*128, 3.5*128, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
					draw.SimpleText(os.date("!%d-%m-%Y %a %H:%M:%S", os.time() + 10800), "CamFont/2", 0.125*128, 0.15*128, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
					
					break
				end
				
				if self.DisplayMode == 2 then
					render.RenderView({
						angles = v.ang,
						origin = v.pos,
						x = k%2*256,
						y = 2%k*128,
						w = 256,
						h = 256,
						fov = 90,
					})
					
					draw.SimpleText("Camera"..((k<10 and "0"..k) or k), "CamFont/4", k%2*256 + 3*64, 2%k*128 + 3.5*64, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
					draw.SimpleText(os.date("!%d-%m-%Y %a %H:%M:%S", os.time() + 10800), "CamFont/2", k%2*256 + 0.125*64, 2%k*128 + 0.15*64, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
					
					if k >= 4 then
						break
					end
				end
			end
		cam.End2D()
		
		render.PopRenderTarget()
		
		InDraw = false
		
		UpdateTime = CurTime() + 0.25
	end
	
	render.MaterialOverrideByIndex(1, self.ScreenMat)
		self:DrawModel()
	render.MaterialOverrideByIndex()
end


hook.Add("ShouldDrawLocalPlayer", "Camera_DrawLocalPlayer", function()
	return InDraw
end)

Note: I’ve meaningly used the worst image format I could for the render target.

I think its an issue with halo’s, not 100% certain though.