Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Beks_Omega · Aug 04, 2017 at 07:48 PM · shadertexturetexture2dcustom-shader

Normalizing World Coordinates in Shader & Looking at Reference Texture Returns Incorrect Color

I am creating a custom shader for a plane.

It's basic function is that it looks at a "reference texture", which tells it what texture to apply where. A more detailed explanation is that:

  1. It gets the worldPos of the pixel it is looking at.

  2. Normalizes that into a float2 between (0,0) and (1,1)

  3. Checks what color the reference texture has at that point.

  4. If that color is green it applies the grass texture to the mesh.

  5. Otherwise it applies the color red to the mesh.

This is almost completely working, the only problem is that there are two red lines where there shouldn't be any (the reference texture is completely green). If anyone knows what could be going on I would much appreciate it!

alt text

 //bottomLeftX declared & filled above
 //bottomLeftZ declared & filled above
 //textureSize declared & filled above
 
 void surf(Input IN, inout SurfaceOutput o) {
             uint distanceFromBottomLeftX = IN.worldPos.x - bottomLeftX;
         uint distanceFromBottomLeftZ = IN.worldPos.z - bottomLeftZ;
         uint mapTextureCoordX = (distanceFromBottomLeftX - (-textureSize / 2 /*min*/)) / ((textureSize / 2) - (-textureSize / 2)); //normalizes
         uint mapTextureCoordZ = (distanceFromBottomLeftZ - (-textureSize / 2 /*min*/)) / ((textureSize / 2) - (-textureSize / 2)); //normalizes
         float2 mapTextureCoords = float2(mapTextureCoordX, mapTextureCoordZ);
         float2 adjustedTextureCoords = float2(IN.worldPos.x - .5f, IN.worldPos.z - .5f); //this just makes sure the texture aligns with the squares of the mesh
 
         float3 mapTextureSample = tex2D(mapTexture, mapTextureCoords).rgb;
         if (mapTextureSample.x == 0 && mapTextureSample.y == 1 && mapTextureSample.z == 0) //(0,1,0) is green
         {
             o.Albedo = tex2D(_GrassTexture, adjustedTextureCoords).rgb;
         }
         else
         {
             o.Albedo = float3(1, 0, 0);
         }
 }

Comment
Add comment · Show 1
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 toddisarockstar · Aug 04, 2017 at 09:02 PM 0
Share

i cant see the top of your code. its seems it might be an alignment problem. either distance or texture coordinate starting at starting at 1 ins$$anonymous$$d of zero, or something like that. you might just need a simple subtraction or addition of 1 somewhere.

another thing to check... make sure your paint program is not slightly changing the green color value around the edges when you save it. i know it sounds silly but I have seen paint programs do this in effort to make edges look smoother.

im not sure why you need world coodinates. if you just need a shader that combines / blends two textures based on a map over an object i wrote one a couple months ago that i can share. it works well

1 Reply

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

Answer by Bunny83 · Aug 05, 2017 at 12:52 AM

There are several things very strange in your code. First of all, why do you use "uint" variables? This is most likely the main problem.

Since you use uint as type, your "mapTextureCoords" will always be a whole number (though due to floating point inaccuracy during conversion it might be slightly off for larger numbers). uint is an unsigned integer. That means assigning a negative value would wrap around to the largest uint value.

You really should use vector types whenever possible. Your code looks unnecessarily complicated. Also this:

 ... / ((textureSize / 2) - (-textureSize / 2))

is the same as

 ... / textureSize

Also if you want to offset a position by half of the available space it's easier to do the offset after you divide the value. All your code basically does is this:

 // untested but should work
 float2 mapTextureCoords = (IN.worldPos.xz - float2(bottomLeftX, bottomLeftZ)) / textureSize;
 mapTextureCoords -= .5f;
 
 float3 mapTextureSample = tex2D(mapTexture, mapTextureCoords).rgb;
 if (mapTextureSample.x == 0 && mapTextureSample.y == 1 && mapTextureSample.z == 0) 
 {
     o.Albedo = tex2D(_GrassTexture, IN.worldPos.xz -.5f).rgb;
 }
 else
 {
     o.Albedo = float3(1, 0, 0);
 }

However this time without using uint. If the use of uint should server an actual purpose you should be more clear as currently when using uint the first texture lookup would make no sense as it will always look up the same point.

Comment
Add comment · Show 1 · 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
avatar image Beks_Omega · Aug 05, 2017 at 02:29 PM 0
Share

Ty for the code critique! $$anonymous$$y script looks so much more elegant now. And it fixed the problem!

I will remember to use the floats whenever possible, makes everything alot simpler lol.

Also just wanted to mention for future peoples who may find this I found a better way to compare float3's which is using "all":

 if (all(mapTextureSample == float3(0,1,0))) //(0,1,0) is green
 {
 
 }

Ty again!

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

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Better shader effects on terrain - Bumpmapping or Specular? 2 Answers

Shader output to a texture 1 Answer

How to fill texture that would be avaliable to ither shaders? 1 Answer

Drawing a Texture on top of another Texture 2 Answers

Dynamic Shader to Outline Adjacent Textures? 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