Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
« Previous 1 2 3 4 5 6 … 18 Next »
UV issue in drawTexturedRect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pages (2): 1 2 Next »
Threaded Mode
UV issue in drawTexturedRect
11-07-2020, 11:03 PM, (This post was last modified: 12-07-2020, 05:55 AM by Tomeno. Edited 2 times in total.)
#1
Tomeno Offline
TSCM Member
***
Posts: 10
Threads: 3
Joined: Jul 2020
UV issue in drawTexturedRect
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/b....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)
Find
Reply
12-07-2020, 09:53 AM,
#2
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: UV issue in drawTexturedRect
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.
Find
Reply
12-07-2020, 10:13 AM,
#3
Tomeno Offline
TSCM Member
***
Posts: 10
Threads: 3
Joined: Jul 2020
RE: UV issue in drawTexturedRect
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.
Find
Reply
12-07-2020, 10:39 AM,
#4
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: UV issue in drawTexturedRect
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.
Find
Reply
12-07-2020, 10:43 AM,
#5
Tomeno Offline
TSCM Member
***
Posts: 10
Threads: 3
Joined: Jul 2020
RE: UV issue in drawTexturedRect
(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?
Find
Reply
12-07-2020, 10:48 AM,
#6
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: UV issue in drawTexturedRect
PNG materials can be tiled by specifying "noclamp" in the second parameter of Material().
Find
Reply
12-07-2020, 10:53 AM, (This post was last modified: 12-07-2020, 11:00 AM by Tomeno. Edited 1 time in total.)
#7
Tomeno Offline
TSCM Member
***
Posts: 10
Threads: 3
Joined: Jul 2020
RE: UV issue in drawTexturedRect
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!
Find
Reply
12-07-2020, 11:08 AM,
#8
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: UV issue in drawTexturedRect
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.
Find
Reply
12-07-2020, 01:22 PM,
#9
Tomeno Offline
TSCM Member
***
Posts: 10
Threads: 3
Joined: Jul 2020
RE: UV issue in drawTexturedRect
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
Find
Reply
12-07-2020, 01:35 PM,
#10
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: UV issue in drawTexturedRect
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?
Find
Reply
« Next Oldest | Next Newest »
Pages (2): 1 2 Next »


  • View a Printable Version
Forum Jump:


Users browsing this thread:
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.