thegrb93 / StarfallEx

Starfall, but with active development and more features. Write Garry's mod chips similar to E2, but in lua
https://discord.gg/yFBU8PU
Other
199 stars 111 forks source link

Rendertargets getting cut off when drawn to screen and screen height is below 1024 #1629

Open ZH-Hristov opened 9 months ago

ZH-Hristov commented 9 months ago

Tested with this code:

--@client

local HUDRT = render.createRenderTarget("HUDRT")

local w, h = render.getResolution()
local clr = Color(255, 0, 0)
local clearclr = Color(0, 0, 0, 0)

enableHud(player(), true)

hook.add("drawhud", "HUD", function()
    render.selectRenderTarget("HUDRT")
    render.clear(clearclr)
    render.setColor(clr)
    render.drawRect(1024 * 0.5 - 25, 0, 50, 1024) -- this should reach the bottom of the screen
    render.selectRenderTarget()

    render.setRenderTargetTexture("HUDRT")
    render.drawTexturedRect(w * 0.5 - (h * 0.5), 0, h, h)
end)

Creates a centered RT and tries to draw a red rectangle from the top to the bottom of the screen.

For example: The rectangle will be properly fully drawn at 1080p, but will be cut off about 3/4 of the way down at 720p. 1080p 720p

This could be a GMod issue for all I know, so its place might not be here.

thegrb93 commented 9 months ago

I think the drawHud hook clips rendering within the screen resolution. Maybe just need to put a DisableClipping call in it.

adamnejm commented 9 months ago

If disabling the clipping won't work, maybe using RT_SIZE_LITERAL could.

ZH-Hristov commented 9 months ago

SF render.enableClipping doesn't change it.

thegrb93 commented 9 months ago

that function is for 3d clip planes. it won't do it

thegrb93 commented 8 months ago

I tried this

-- TEXTUREFLAGS_PROCEDURAL + TEXTUREFLAGS_EIGHTBITALPHA + TEXTUREFLAGS_RENDERTARGET + TEXTUREFLAGS_DEPTHRENDERTARGET
return GetRenderTargetEx("Starfall_CustomRT_" .. i, 1024, 1024, RT_SIZE_LITERAL, MATERIAL_RT_DEPTH_SEPARATE, bit.bor(2048, 8192, 32768, 65536), 0, 0 )

I'm pretty sure I tried it before too and it didn't work. Not sure it can be fixed

ZH-Hristov commented 4 months ago

@thegrb93 Got a reply from rubat. Apparently drawing on it in 2D mode when it's bigger than the screen requires it to be wrapped in cam.start2D

Will give it a try when I can later.

thegrb93 commented 4 months ago

The selectRenderTarget function does use cam.start2d

ZH-Hristov commented 4 months ago

The selectRenderTarget function does use cam.start2d

This has to be a issue in Starfall somewhere. I recreated the exact same thing in normal GLua and it works fine there.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

render.PushRenderTarget( exampleRT )
    cam.Start2D()
        render.Clear(0, 0, 0, 0)
        surface.SetDrawColor( 255, 0, 0, 255 )
        surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
    cam.End2D()
render.PopRenderTarget()

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
    ["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
    ["$translucent"] = 1,
    ["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( customMaterial )
    surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

Tested at 720p. SF one is still cut off.

ZH-Hristov commented 4 months ago

@thegrb93 Got any clues? I tried DisableClipping in the render library but that didn't change anything.

The GLua snippet I sent works fine at low height resolutions, despite seemingly not doing anything different functionally?

thegrb93 commented 4 months ago

I'll try in a bit

thegrb93 commented 4 months ago

@ZH-Hristov It's because of the HUDPaint hook. If you do this code, then you see the same clipping starfall has.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
    ["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
    ["$translucent"] = 1,
    ["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
    render.PushRenderTarget( exampleRT )
        cam.Start2D()
            render.Clear(0, 0, 0, 0)
            surface.SetDrawColor( 255, 0, 0, 255 )
            surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
        cam.End2D()
    render.PopRenderTarget()

    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( customMaterial )
    surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )
thegrb93 commented 4 months ago

Seems most gmod render hooks suffer from this. I tried a few of them in starfall

--@name Untitled
--@author Sparky
--@client

enableHud(nil, true)

render.createRenderTarget("hi")
hook.add("predrawtranslucentrenderables","",function()
    render.selectRenderTarget("hi")
    render.clear()
    render.setColor(Color(255,0,0))
    render.drawRect(0,0,1024,1024)
    render.selectRenderTarget()

    render.pushViewMatrix({type="2D"})

    render.setColor(Color(255,255,255))
    render.setRenderTargetTexture("hi")
    render.drawTexturedRect(100,200,200,200)

    render.popViewMatrix()
end)

image

ZH-Hristov commented 4 months ago

@ZH-Hristov It's because of the HUDPaint hook. If you do this code, then you see the same clipping starfall has.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
  ["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
  ["$translucent"] = 1,
  ["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
  render.PushRenderTarget( exampleRT )
      cam.Start2D()
          render.Clear(0, 0, 0, 0)
          surface.SetDrawColor( 255, 0, 0, 255 )
          surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
      cam.End2D()
  render.PopRenderTarget()

  surface.SetDrawColor( 255, 255, 255, 255 )
  surface.SetMaterial( customMaterial )
  surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

Adding DisableClipping in the RT context here works:

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
    ["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
    ["$translucent"] = 1,
    ["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
    render.PushRenderTarget( exampleRT )
    DisableClipping(true)
        cam.Start2D()
            render.Clear(0, 0, 0, 0)
            surface.SetDrawColor( 255, 0, 0, 255 )
            surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
        cam.End2D()
        DisableClipping(false)
    render.PopRenderTarget()

    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( customMaterial )
    surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

So I suppose we need that in the render lib

ZH-Hristov commented 4 months ago

Weird though, since the prepare render hook in SF has a surface.DisableClipping(true) call. It's apparently deprecated but it's an alias so it shouldn't matter. Maybe pushing an RT or a cam enables clipping? Idk.

thegrb93 commented 4 months ago

Idk if its an alias. You should try adding it

ZH-Hristov commented 4 months ago

I have no damn clue why this refuses to work in SF. I tried adding it in PrepareRender, I tried adding it as a render library function and I tried adding it right before cam.Start2D in the selectRenderTarget function.

It still gets cut off.

kill me

thegrb93 commented 4 months ago

I'll check it out again

thegrb93 commented 4 months ago

@ZH-Hristov Ok I got it working with render.drawRectFast

--@name Untitled
--@author Sparky
--@client

enableHud(nil, true)

render.createRenderTarget("hi")
hook.add("drawhud","",function()
    render.selectRenderTarget("hi")
    render.clear()
    render.setColor(Color(255,0,0))
    render.drawRectFast(0,0,1024,1024)
    render.selectRenderTarget()

    render.pushViewMatrix({type="2D"})

    render.setColor(Color(255,255,255))
    render.setRenderTargetTexture("hi")
    render.drawTexturedRect(100,200,200,200)

    render.popViewMatrix()
end)

It looks like the 3D rendering functions in the 2D context, such as render.DrawQuad suffers from clipping still, but surface render functions don't.

ZH-Hristov commented 3 months ago

I didn't know the SF rect functions used DrawQuad instead. I guess I'll just use the "fast" ones.