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 GioLeta · May 18, 2016 at 04:29 AM · shadershader programmingtexture-atlas

How to locate the correct texture within a texture atlas using a shader?

Hi, I am beginning to learn shader programming. I have been doing some stuff with vertex, geometry and computational shaders. But in god's name, I can't understand and couldn't find a clear, concise answer to that simple thing: how to locate a specific texture within a texture atlas, inside a shader?

I have found questions and answers about how to access a specific texture from an atlas outside shaders. I have found extensive discussions on tiling, wrapping, etc. But I did not find a simple explanation of simply how to locate a texture within an atlas inside the shader code.

For instance, suppose I have 4 textures of 128x128 each. Displayed in an atlas in a 2x2 fashion (so the atlas has 256x256). What is the correct way to make the shader access each of these 4 pieces of the atlas (let's call them A, B, C and D, starting at the left-up, and then gong clockwise).

Thanks for any help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by FortisVenaliter · May 18, 2016 at 04:16 PM

Generally, this is done through the texture coordinates in the vertex shader. You can shift and scale them as needed. So, in your example, you would pass in texture coordinates for the whole texture: (0,0) (1,0) (1,1) (0,1), then in the vertex shader, scale and offset them. For example, the top right image would be (0.5, 0) (1,0) (1,0.5) (0.5, 0.5). Or, it could be represented by the following equation:

 newX = (srcX / numImgX) + (offsetX * 1/numImgX);
 newY = (srcY / numImgY) + (offsetY * 1/numImgY);

But, you'll need to pass in that information about the texure atlas (how many images it contains, etc), as well as information telling the shader which image to use.

Comment
Add comment · Show 3 · 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 GioLeta · May 19, 2016 at 08:39 AM 0
Share

Thanks for the quick reply to my question! $$anonymous$$y main problem is exactly where at the shader I should inform that position of the image I want. I mean, I know how to inform the shader about the number of images in the atlas and which image in the atlas I want to use. However, my main problem is exactly how, i.e. in which command with which parameter should I inform the coordinates of the 4 corners of the image in the atlas? Because so far I have always used only one image (no atlas) with the command tex2D like in tex2D(_texname, input.uv). Which means, passing only one coordinate in a float2 as parameter, never 4 coordinates.

PS: in your example (0.5, 0) (1,0) (1,0.5) (0.5, 0.5), shouldn't it be the bottom-right image ins$$anonymous$$d of the top right, or did I get the coordinate system wrong?

avatar image FortisVenaliter GioLeta · May 19, 2016 at 04:57 PM 0
Share

Should be top right. 0 is top Y/V, 1 is bottom Y/V.

You would need to pass it in, not through the model, but as a global shader variable, like the way you pass in the texture. Then it will show up as a parameter in the regular shader inspector/code that you can set before drawing your object.

avatar image Vladislav-Hromyh · Oct 26, 2016 at 03:22 PM 0
Share

Are you found the solution finally ?

Could you share a shader if you did ?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unlit Transparent With Color Support 1 Answer

Transparency and texture atlases, shader calls affecting performance? 2 Answers

create a texture with shader code? ( without using a texture ) 1 Answer

How to add Emission to my custom shader? 2 Answers

The Best Way To Make Stylised Grass in Unity? 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