• 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 corpsinheretoo · Feb 25, 2017 at 05:54 AM · texture atlas

Find set of all pixel coords on texture atlas actually used by a given model

Render texture camera points at "dirt" texture in front of model texture. Alpha of dirt texture is manipulated to simulate cleaning of the model. I would like to keep track of the ratio of remaining dirt pixels to the starting dirt pixels; this would be easy if I could just use the texture dimensions - but a model usually does not take up 100% of its texture atlas. How would I find the coordinates of all pixels of a texture atlas used by a given model? alt text

textureatlasexample.png (73.7 kB)
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

2 Replies

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

Answer by hrgchris · Feb 25, 2017 at 11:32 AM

Hi

That's an interesting problem. Before going into a solution, worth mentioning that reading from a render texture on CPU is very expensive (it flushes the GPU). If you are doing so, that'll be a consideration you may need to take into account if doing it every frame.

Anyway, onto a solution. There's no entirely simple way of doing this, however it is possible. You will have to iterate over all the triangles in each submesh of your mesh. This will basically mean reading the indices from each submesh. Then you'll need to use those to read the UVs of each triangle on your mesh.

From that work you'll be able to extract a list of triangles, and for each one the UV value assigned to each vertex. Now if you allocate an array integers of the same dimensions as your texture, you can use the UVs to iterate over each texel in each triangle, and increment its corresponding element in your array by 1.

Once complete, your array will be a count of how many triangles use each texel.

The tricky bit will probably be using the 3 UV values for your triangle to work out which texels it uses. This is a problem similar to rasterizing a triangle for which there's lots of info online available.

One thing I would recommend is whilst you develop this, implement the ability to easily copy that array into a texture and render it into a quad on screen. That'll help you visualise what's going on and make it easier to debug.

-Chris

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 corpsinheretoo · Feb 25, 2017 at 12:23 PM 0
Share

Ty @hrgchris - your excellent and daunting answer has motivated me to find an approach with a simpler solution to my specific problem. I was being sloppy and using a dirt texture covering the entire texture atlas; as these need to be custom made anyway - I will simply make my dirt texture only have "dirt" (alpha > 0) pixels in an area that is being used by my model. Then read the sum of all alpha values for the dirt texture in Start() - and re-sum during Update to get my running ratio.

avatar image
2

Answer by hexagonius · Feb 25, 2017 at 11:16 AM

the mesh has UV coordiantes on each vertex pointing into the texture in a normalized way.
this means a UV.x of .5 points to 512th pixel if the texture is 1024 wide. the UVs are interpolated when covering the mesh surface

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 Bunny83 · Feb 25, 2017 at 01:43 PM 0
Share

Note that "0.5" is actually between 511 and 512 ^^ Depending on your texture filtering it's either a mix of the two (or four in 2d) or the closest pixel (in case of point filtering).

I would probably "rasterize" the mesh manually in UV space to get all pixels used. Though in case of linear interpolation it might get tricky at borders. So some neighboring pixels might only contribute with a small fraction. Though you could always simply round "outwards" to include all factional pixels as well.

I'm not an artist and never really used modelling tools, but i would guess there should be a function in those tools which let you create a mask texture for all the used UV space of a mesh. If not, it's not that hard to do that yourself.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

67 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

Related Questions

is possible to tile within a texture atlas? 1 Answer

Optimizing Character Textures/Atlases for Zombie crowds 1 Answer

In an open world game, how big should texture atlases be? 1 Answer

How to apply texture to gameobject with texture atlas? 0 Answers

Tile Game slow. Lots of sprites. Texture atlas? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges