My first HLSL shader - messed up UVs

Hello mates!

I’m new to the forums, read my introductions here if you like: My introduction post

So first shader, first problem - first post, here goes!

I’ve been playing around with some basic HLSL and I’ve figured out how to apply textures to shaders - not very complicated I was pleasantly surprised to find out. And I’m using 3ds max to preview my shaders, very nice! But there was one problem - the UVs was all messed up! :?: But when I applied the same texture with a standard material it showed up perfectly, no problem with the UV channel or anything. So I worked around with the UVW unwrap a little and I found a solution to the problem. I offset the all the UVs downwards by one UV-space and the texture fitted like a glove! Observe though that it ONLY worked if I moved them downwards, no other direction worked. Here is an image that illustrate the problem as well as the solution.

So, it isn’t really a problem here I guess since I did find a solution. But I would like to know why this is happening - any suggestions?

Cheers mates!

so to fix it without having to change your UVS:


texture DiffuseMap : DiffuseMap
<
	string ResourceName = "axe.tga";
	string ResourceType = "2D";
	string UIName = "Diffuse Map";
>;

sampler2D DiffuseSampler = sampler_state
{
	texture = <DiffuseMap>;
	AddressU = WRAP;
	AddressV = WRAP;
	AddressW = WRAP;
};

I don’t know what quadrant in Max is UV positive, but a texture sampler, when faced with “AddressU = Clamp” will behave much as you show there when it hits values that are not from zero to one. You can think of it as inside any tex2d() it is taking your address and calling saturate() on it, if it is set to clamp.

Here is the D3D10 help on the subject

Also worthy of note, is that traditionally in Computer Graphics, Positive along the vertical is generally downward. Positive along the horizontal is to the right, so the image you show makes perfect sense as your offset UVs are in the traditional positive location. Hopefully a Max specialist can come in and clarify exactly what data Max is shooting out to your shader, because by convention it looks like where your texture is should be UV Positive. You could try adding 1 to V in the vertex shader to confirm this. =/ .

Cheers! -Lith

Max’s UV’s in the V direction are offset by 1 (it is a bug, nothing more nothing less). You need to add (or subtract? I think it is add) one to your V, so like this in your vertex shader:
In.texcoord.y += 1
Then you should get the proper results with your UV’s in the regular 0 to 1 space.

Thanks mates, very interesting. For How long has that Max bug been around? Sounds like something worth fixing over at Autodesk :p: Thanks for clearing that out Rob.

And thanks Lithium for that texture wrapping trick and the link, I’m going to need that later :):

Cheers!

  1. UV map offset bug in Max as described here.

  2. Viewcube etc. renders using Clamped and so your shader will inherit this - you need to turn it off. Or explicitly set “wrap” in all your shaders, otherwise tiling will not work.

[QUOTE=Nanne;3033]Thanks mates, very interesting. For How long has that Max bug been around? Sounds like something worth fixing over at Autodesk :p: Thanks for clearing that out Rob.

And thanks Lithium for that texture wrapping trick and the link, I’m going to need that later :):

Cheers![/QUOTE]

If they fixed it, it would break backwards compatibility for any shader (especially with the Viewcube clamp behaviour in new versions). So it is a bug everyone will have to live with.

[QUOTE=Rob Galanakis;3035]If they fixed it, it would break backwards compatibility for any shader (especially with the Viewcube clamp behaviour in new versions). So it is a bug everyone will have to live with.[/QUOTE]

Aha, I see. Thanks Rob.

[QUOTE=Rob Galanakis;3035]If they fixed it, it would break backwards compatibility for any shader (especially with the Viewcube clamp behaviour in new versions). So it is a bug everyone will have to live with.[/QUOTE]

I think they should fix the UV offset 99% of shaders that don’t use clamp will work, just as they do without the offset. Which is why a lot of people didnt know about it until the viewcube came along and thus a lot of shaders are written assuming its correct. There is the odd shader that might use UVs for other reasons, but not many.

What they do with the viewcube shader is legitimate, setting clamping state. Its just annoying because many shaders don’t set these states, but its easy for people to add.

They could add another set of ParamIDs for the fix if they don’t want to upset anyone.

山东烟台市都程塑料有限公司成立于1996年,是胶东半岛最早从事塑料周转箱生产的企业之一。[b]塑料托盘[/b]公司的主导产品有[b]塑料托盘[/b]、卡板箱、塑料周转箱、塑料周转筐、物流箱、各种塑料盒、垃圾桶等,产品品种有300多种,能够满足各行业货物的仓储和运输的需要。公司已通过ISO9001国际质量体系认证。 公司现拥有国内最先进锁模力120-1500吨精密微电脑注塑机30余台,年生产能力达一万吨;同时拥有制造精密注塑模具的加工车间,形成了产品开发?D?D模具制造?D?D注塑成型?D?D销售服务等较完善的一体化服务体系, 也可以为[b]塑料托盘[/b]客户进行个性化的产品的设计与制造。 “都程”牌塑料周转箱已广泛应用于机械电子业、禽蛋、渔业、果蔬食品、服装业、化工、仓储等众多行业所用。在业界已拥有较高的信誉度。 公司本着“品质、诚信、创新、速度”的发展理念,竭诚为广大[b]塑料托盘[/b]客户提供更加优质的服务!