Team Spartan Cookies & Milk Forums
UV issue in drawTexturedRect - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: UV issue in drawTexturedRect (/thread-3378.html)

Pages: 1 2


UV issue in drawTexturedRect - Tomeno - 11-07-2020

There's some sort of an issue with DrawTexturedRect and co. where UV coordinated of 0,0 aren't the same as 1,1 and so on on looping materials. Some sort of a transform seems to be applied, and 1 in UV coordinates doesn't actually correspond to 1 width/height of the texture. Based on some pixel counting and testing on a reliable 512x512 texture, the actually used starting UV seems to be shifted by 8 pixels down and right with the end UV being shifted up and left by 8 pixels.

UV position test case:

local last = nil
local function UVIssueShowcase(x,y,w,h)
local offset = math.floor(time.curTime()) % 3
if offset ~= last then last = offset print(offset) end
render.drawTexturedRectUV(x, y, w, h, offset, offset, 1 + offset, 1 + offset)
end

local texture = render.getTextureID("spacebuild/hazard2")
hook( "render", "render", function()
render.clear()
render.setTexture(texture)
render.setColor(255, 255, 255, 255)
UVIssueShowcase(0,0,512,512)
end)

It seems that StarfallEx has some code to deal with this bug.
https://github.com/thegrb93/StarfallEx/blob/master/lua/starfall/libs_cl/render.lua#L1094

Applying this "UVHack" to the test case seems to work, except that starting UVs are clamped to 0, so the starting position ends up being zoomed in.

local function UVHack(x,y,w,h,startU,startV,endU,endV)
startU = ( startU * 32 - 0.5 ) / 31
startV = ( startV * 32 - 0.5 ) / 31
endU = ( endU * 32 - 0.5 ) / 31
endV = ( endV * 32 - 0.5 ) / 31
render.drawTexturedRectUV(x,y,w,h,startU,startV,endU,endV)
end

local last = nil
local function UVIssueShowcase(x,y,w,h)
local offset = math.floor(time.curTime()) % 3
if offset ~= last then last = offset print(offset) end
--render.drawTexturedRectUV(x, y, w, h, offset, offset, 1 + offset, 1 + offset)
UVHack(x, y, w, h, offset, offset, 1 + offset, 1 + offset)
end

local texture = render.getTextureID("spacebuild/hazard2")
hook( "render", "render", function()
render.clear()
render.setTexture(texture)
render.setColor(255, 255, 255, 255)
UVIssueShowcase(0,0,512,512)
end)



RE: UV issue in drawTexturedRect - Person8880 - 12-07-2020

This issue is a long standing annoyance with the way the engine draws textures. Sadly there's no way to avoid some kind of correction to the input values, though adding a "UVHack" parameter isn't the best solution and it's not accounting for the fact that even properly initialised materials have an offset to their co-ordinates.

To help with this, I've added a helper function, render.computeTextureCoords( texture, u, v, endU, endV ) that applies appropriate corrections to the given values based on the passed in texture ID/material.

It should work for textures from render.getTextureID() and any existing static materials (without needing to be in a render hook), and will be available after the next restart.


RE: UV issue in drawTexturedRect - Tomeno - 12-07-2020

Thanks for the quick solution! I just have one question: will this work if I need to render the top/left portion of the texture? Some parts end up in negative UVs and are currently inaccessible, as UVs seem to be clamped to >=0.


RE: UV issue in drawTexturedRect - Person8880 - 12-07-2020

Negative UV values don't work, but you can just start at 1 instead, e.g. if you want (-0.5, -0.5) to (0.5, 0.5), you can just render (0.5, 0.5) to (1.5, 1.5) to get the same result (assuming the material permits tiling).

Nevermind, the clamping is actually being done in the binding, I've removed it. It should accept negative UV values now.


RE: UV issue in drawTexturedRect - Tomeno - 12-07-2020

(12-07-2020, 10:39 AM)Person8880 Wrote: Negative UV values don't work, but you can just start at 1 instead, e.g. if you want (-0.5, -0.5) to (0.5, 0.5), you can just render (0.5, 0.5) to (1.5, 1.5) to get the same result (assuming the material permits tiling).

In that case, is there any way to get tiling on .pngs?


RE: UV issue in drawTexturedRect - Person8880 - 12-07-2020

PNG materials can be tiled by specifying "noclamp" in the second parameter of Material().


RE: UV issue in drawTexturedRect - Tomeno - 12-07-2020

What if I'm rendering on a screen?
EDIT:
Ah, I see you've edited your post. That'll be enough then. Thanks a lot!


RE: UV issue in drawTexturedRect - Person8880 - 12-07-2020

There's no difference when rendering on a screen, other than some materials (including PNGs) needing the depth buffer to be cleared before they'll draw.

Currently this isn't done automatically, but I've changed it to be automatic before the first time they're rendered after the next restart to avoid people having to know about this quirk.


RE: UV issue in drawTexturedRect - Tomeno - 12-07-2020

It seems that there are a few regressions after the update. These are the ones that I've found so far. There might be more, and they could be caused by me.
render.getTextureID now requires explicit extensions for materials
render.getTextureID no longer works with .pngs


RE: UV issue in drawTexturedRect - Person8880 - 12-07-2020

For PNGs, you'd be better off using Material( "path/to/material.png", "<params>" ) as it lets you customise the way the material is setup.

Part of the changes means that the material passed to render.getTextureID() has to have a texture that can be resolved to a path. PNGs are handled in a bit of a weird way (thanks Garry), so I'm not surprised they no longer work with it. I'll fix it to make it handle them as an equivalent to Material( texture, "noclamp" ).

As for non-PNG materials, those should be unchanged. Are there non-PNG materials that aren't working with it?