Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Runewake · Dec 18, 2014 at 03:21 AM · texturemesh

Get Mesh Data From Texture Positions

I am working on generating a texture that updates in real time based on a few different parameters. To accomplish this I am looking to find information about the mesh the texture is applied to and use that information to do my calculations. The issue I've run into is that there doesn't appear to be a means to accomplish this.

I am looking to find the Normal and Position of the point on a model from texture coordinates but all I have been able to accomplish is to pull the raw data from the mesh (using the uv, normal and other properties of the Mesh object). This doesn't accomplish what I am looking for as my algorithm needs complete coverage to work, that is, I can't skip over a point on the texture.

Is there some way to efficiently get these values or am I going to need another solution?

Edit: Currently I am using a precalculation to figure this out. This calculation takes approximately a minute to run on my computer for a 16x16 texture. That is hardly optimal. While I could thread this I think that'd would just serve to hide a less than optimal solution. My code is as follows (WrappingGrid is a custom class for calculating nearby points based on a grid which wraps (like UV cords do)):

 void Start () {
     grid = new WrappingGrid(1, 1);
     nodes = new MeshTextureNode[heatmap.width, heatmap.height];
     for (int x = 0; x < heatmap.width; ++x)
     {
         for (int y = 0; y < heatmap.height; ++y)
         {
             float u = (float)x / (float)heatmap.width;
             float v = (float)y / (float)heatmap.height;
             int closest = 0;

             for (int i = 0; i < mesh.uv.Length; ++i)
             {
                 closest = CalculateClosest(u, v, closest, i);
             }

             nodes[x, y] = new MeshTextureNode()
             {
                 normal = mesh.normals[closest],
                 localPosition = mesh.vertices[closest]
             };
         }
     }
 }

 private int CalculateClosest(float u, float v, int closest, int i)
 {
     Vector2 thisDiff = mesh.uv[i];
     Vector2 closestDiff = mesh.uv[closest];
     if (grid.NearerThanPoint(new Vector2(u, v), thisDiff, closestDiff)) closest = i;
     return closest;
 }
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 Bunny83 · Dec 18, 2014 at 04:15 PM 0
Share

If this is going to be a heatmap, why mapped onto a mesh? Do you have caves and different levels? Heatmaps usually are simple overview maps and you simply map the worldspace coordinate to the map. or do you have a "round" world like Spore planets?

avatar image Runewake · Dec 19, 2014 at 12:07 AM 0
Share

I am mapping it to a planetoid. Specifically, this. Obviously it still has issues though.

2 Replies

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

Answer by Bunny83 · Dec 18, 2014 at 04:02 PM

So if i got you right you want a method that takes a mesh, the texture for that mesh and a uv position in that texture and get back a world / local space position on the mesh and the points normal vector, right?

First of all you have to understand that the uv to world point projection is not 1 to 1 it's a "1 to n" mapping where "n" even could be 0 (this is the case when that portion of the texture isn't mapped to the mesh at all). "n" could also be greater than 1. This is the case when you have multiple triangles mapped to the same portion of the texture.

So naturally a method like this would return an array of position / normal pairs which can also contain 0 elements.

What you have to do is iterating through all triangles, get the corresponding uv coordinates of the 3 corners. Calculate the barycentric coordinate of that triangle from the given uv coordinates. use those coordinates to test if your point is inside this triangle. If it's inside, just use the barycentric coordinates to interpolate the position and normal values of the 3 corners.

Finally if you need those positions / normals in worldspace you have to use transform.TransformPoint / TransformDirection of the transform that contains your mesh.

We had a similar question over here. There i only gather the position on the mesh. If you want a method that returns both, you should use a struct with two Vector3 (position and normal).

Also related (it's crosslinked from the other answer):

http://answers.unity3d.com/questions/46788/painting-on-a-texture-without-crossing-mesh-triang.html#answer-46816

ps: I just fixed the code formatting on those answers. It seems that (once more) due to some migration of UA the code formatting got messed up and there were tons of &lt;, &gt; and &amp; instead of <, > and &. Maybe i've missed some if you find something, please leave a comment.

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 Runewake · Dec 19, 2014 at 12:12 AM 0
Share

Well, the sample code I have in my question is close to this, however, it stopped at the Barycentric coordinates bit. Even had the struct as you describe. I'll work on implementing this, but your description solves the problem. Thanks for the related questions!

avatar image
0

Answer by Kiwasi · Dec 18, 2014 at 03:23 AM

Is it just me or are you simply describing a shader?

Comment
Add comment · Show 5 · 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 Runewake · Dec 18, 2014 at 03:31 AM 0
Share

Indeed, but I need to update the Texture and have it have a persistent state. There will be fading between the current frame and previous. If I can do this with a shader I'd love that solution! If you have a link or suggestion that will help here that could be handy. I had discarded shaders as not allowing me to persist the state, perhaps I was wrong?

avatar image Kiwasi · Dec 18, 2014 at 04:07 AM 0
Share

No idea, I'm not fully up to speed on shaders and how they work.

avatar image Runewake · Dec 18, 2014 at 04:12 AM 0
Share

Alas, nor am I. At the moment I am exploring a preprocessing route since an actual solution isn't apparent, once I get it close I'll update my question with the code, maybe someone will be able to help me refine it further.

avatar image MrSoad · Dec 18, 2014 at 04:14 AM 1
Share

@Bunny83 may be able to offer some advice :)

avatar image Runewake · Dec 18, 2014 at 06:21 AM 0
Share

I have edited in my current work around, it misses points and has a series of other issues (mostly it's hugely inefficient).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers

Realtime mesh deformation or texturing 0 Answers

Does micropoly / tinypoly texture mapping have disadvantages? 1 Answer

[ problem ] transparent shader !!! 1 Answer

Apply PNG as texture to 3d object 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