Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheProfessor · Sep 08, 2015 at 01:40 PM · shadershader programmingshaderlabshader writing

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: My Texture

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessespike · Sep 08, 2015 at 04:56 PM 1
Share

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.

avatar image hexagonius · Sep 08, 2015 at 08:30 PM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);

My hex grid

My grid texture mapping

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges