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 ina · Nov 13, 2015 at 12:02 PM · texturemeshtexture2dprocedural meshbake

Baking vertices down to flat texture from shader

I'm trying to bake from a shader material down to a flat texture, but getting results that seem rather sparse...

Wanted result: alt text

What my below attempts are getting: alt text

Actually - I am not sure if I am looping through the constructs right to be able to get the color of each UV point to bake to texture?

     texBaked = new Texture2D (512,512);

     MeshFilter mf = goBakeMe.GetComponent<MeshFilter> ();
     Mesh mesh = mf.mesh;
     /*for(int i=0;i<mesh.vertices.Length;i++){
         Vector2 uv = mesh.uv[i];
         Color color = mesh.colors[i];
         int minx = Mathf.FloorToInt (uv.x * 512); int maxx = Mathf.CeilToInt (uv.x*512);
         int miny = Mathf.FloorToInt (uv.y * 512); int maxy = Mathf.CeilToInt (uv.y*512);
         for(int a=minx;a<=maxx;a++){
             for(int b=miny;b<=maxy;b++){
                 texBaked.SetPixel (a,b,color);
             }
         }
     }*/
     for(int i=0;i<mesh.triangles.Length;i++){
         Vector2 uv = mesh.uv[mesh.triangles[i]];
         Color color = mesh.colors[mesh.triangles[i]]; 
         int minx = Mathf.FloorToInt (uv.x * 512); int maxx = Mathf.CeilToInt (uv.x*512);
         int miny = Mathf.FloorToInt (uv.y * 512); int maxy = Mathf.CeilToInt (uv.y*512);
         for(int a=minx;a<=maxx;a++){
             for(int b=miny;b<=maxy;b++){
                 texBaked.SetPixel (a,b,color);
             }
         }
     }

     texBaked.Apply();

d48a169e60140110152bfb1843a47641.png (94.5 kB)
316d33bcfab36f18dbc18500054932b4.png (143.4 kB)
Comment
Add comment · Show 3
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 helarts · Nov 23, 2015 at 12:56 PM 0
Share

From what you say you are trying to do something like this http://wiki.unity3d.com/index.php/Bake_$$anonymous$$aterial_to_Texture

While in your code you are somehow baking your model vertex color into a texture. So I am a bit confused about what you really want to achieve here.

avatar image saschandroid · Nov 23, 2015 at 02:10 PM 0
Share

I think you are just setting 4 pixels ($$anonymous$$/max x and y) for each vertex to a specific color and not the entire triangle between 3 vertices of a face. You have to fill the area between the first, second and third uv-coordinate of a face.

avatar image ina · Dec 23, 2015 at 12:13 AM 0
Share

can you explain more about filling the area?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 23, 2015 at 01:41 AM

So if i got you right you want to bake vertex colors into a texture. This does only work when your mesh is already unwrapped, so each vertex already has a uv coordinate. Next thing is the same area of the texture could be mapped to several different triangles. So you would either choose one triange and pick the colors of that or interpolate between all triangles that are mapped to that area.

The best way to sample the pixels for the texture is to do the reverse of what you're doing at the moment. You don't pick a vertex and try to find out which pixels might be affected by that vertex, but instead you start by picking a pixel and then figure out which vertices affect that pixel.

All you need is what i posted over here on this question. You basically loop through all pixels in your texture, convert that pixel coordinate to uv coordinates and then for each triangle in the mesh you calculate the barycentric coordinate of the uv point in relation to each triangle. That allows you to determine if the pixel is inside one of those triangles and furthermore it allows you to sample the interpolated color of the 3 vertices of a triangle.

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

39 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

Related Questions

Apply Generated Texture to Mesh 1 Answer

Texture doesn't appear on created through code mesh,Texture disappears when I enter play mode 0 Answers

Procedual quad doesnt display texture - only single color 1 Answer

How to overlap tiles in procedural meshes? 1 Answer

Procedurally Generated Mesh Texturing 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