- Home /
tex2D(_texture, float2(col/(width-1), row/(height-1))) is not returning correct values
In Unity I have a hexagon grid using offset coordinates; I create a texture2d composed of data concerning their texture. So at (0,1) in the texture I may have a rgba value of (1,1,1,1) which tells me it's a plains texture.
I pass this texture to my shader and I check it's location per every tile like so:
col = tex2D(_texture, float2(col/(width-1), row/(height-1)));
And I notice something weird.
The above doesn't seem to work, but if I put in manual coordinates like so:
col = tex2D(_texture, float2(0, 0.9));
This will get me the top left value correctly, but not (0,1). (0,1) seems to wrap back around.
In Unity my texture2D is set to point filtering. The texture however isn't a power of 2, does it have to be? Even so, it seems strange for specific manual coordinates to work but not a simple division to find the approximate coordinate.
Should I be clamping or approximating my values somehow? Is there a technical aspect to this I am missing?
Here is a example of such a texture, it's much larger than the one I'm currently using but I'm at work and can't link the current one. But the principle is the same, a square texture with each pixel being a particular colour:
Are you sure the math is correct? Looks a bit odd.
col = tex2D(_texture, float2(col/(width-1), row/(height-1)));
Does col mean Color or Column? col is being used for both dividing the width and storing the results.
one more guess. I don't know about shaders exact syntax, but with C# a division with only integers needs at least one cast to a floating point value to result in one. if col and height both are that is.
Answer by TheProfessor · Sep 09, 2015 at 06:56 PM
I figured it out!
@Jessespike is mostly correct in surmising the problem is in my formula; just not the formula posted above, to demonstrate:
// Zerothly, assume some variable giving me the pivot point of
// the current object in worldspace: HexOrigin
// Firstly, convert given x,y,z coordinates in worldspace to axial/cube coordinates.
float mSize = 1;
// Below we perform matrix operations to convert the worldspace
// coordinates to axial coordinates.
float column = ((HexOrigin.x * (float(sqrt(3)) / float(3))) - (float(HexOrigin.z) / float(3))) / mSize;
float row = (HexOrigin.z * (float(2) / float(3))) / mSize;
// Convert axial to cube coordinates
float3 HexOriginCubeCoords = float3(column,-column-row,row);
// now we round
int rx = round(HexOriginCubeCoords.x);
int ry = round(HexOriginCubeCoords.y);
int rz = round(HexOriginCubeCoords.z);
// But this doesn't gaurantee that X+Y+Z=0 constraint is met
// So we need to reset it so it is.
float x_diff = abs(rx - HexOriginCubeCoords.x);
float y_diff = abs(ry - HexOriginCubeCoords.y);
float z_diff = abs(rz - HexOriginCubeCoords.z);
if (x_diff > y_diff && x_diff > z_diff)
{
rx=-ry-rz;
}
else if (y_diff > z_diff)
{
ry = -rx-rz;
}
else
{
rz = -rx-ry;
}
// Now we have our proper cube coordinates
int3 myHex = int3(rx,ry,rz);
// convert back to even-r offset coordinates
float q = myHex.x + ceil(float(myHex.z + (int(myHex.z) % 1)) / 2);
float r = myHex.z;
Now the line above was wrong, it was myHex.x instead of myHex.z, fixed now. Additionally I was round(...) instead of ceil(...) not sure why this is the case as my tests it should never happen but okay.
// We want to lookup the pixel at q,r (column,row)2
float2 mHexCoord = float2((q/(MapWidth - 1)),
1 - (-r/(MapHeight - 1)));
// Now sample the given pixel at loc mHexCoord
float3 _color = tex2D(_texture,mHexCoord);
Accounting for the offset of the hexagons in comparison to a 2D graph it appears to be correct!
Also, @Hexagonius is correct in that part of my problem was also that in some cases 3/4 returns an int and not a lot, so 3/4 and 0.75 aren't the same. So it has to be float(3)/4.
Additionally I set my texture to Clamp instead of Wrapping around, perhaps it helped, I didn't test if turning that off makes a difference.
Upvoted both you gentlemen.
Your answer
Follow this Question
Related Questions
How to adapt this shader into a transparent one (alpha blend) 0 Answers
Fog not working in my Shader~ 0 Answers
Header for material properties in inspector? 2 Answers
Addition to Standard Shader stopping it from batching? 0 Answers
someone knows how to create an anisotropic shader? HELP!!! 0 Answers