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
0
Question by KonstantinMaximus · Apr 30, 2015 at 06:46 PM · meshtexturingprogrammatically

How do I texture a mesh programatically

I am trying to generate a mesh that will be a rock and I would like to texture it. I looked around, but could not figure how to do this. I use this code to generate a mesh, it's kinda ugly, but works for now:

     void CreateRock() 
     {
         GameObject rockObject = new GameObject();
         rockObject.transform.position = new Vector3 ( 0, 2, 0 );
         
         MeshFilter mf = rockObject.AddComponent < MeshFilter >();
         MeshRenderer mr = rockObject.AddComponent < MeshRenderer > ();
         
         Vector3[] verts = new Vector3[ 12 ];
         int[] tri = new int[10 * 3 * 2];
         
         // bottom
         float size = 0.75f;
         float height = 0;
         verts[0] = new Vector3(  size, height, -size );
         verts[1] = new Vector3(  size, height,  size );
         verts[2] = new Vector3( -size, height,  size );
         verts[3] = new Vector3( -size, height, -size );
 
         // center
         size = 1.25f;
         height = 3;
         verts[4] = new Vector3(  size, height, -size );
         verts[5] = new Vector3(  size, height,  size );
         verts[6] = new Vector3( -size, height,  size );
         verts[7] = new Vector3( -size, height, -size );
 
         // top
         size = 0.5f;
         height = 4;
         verts[ 8] = new Vector3(  size, height, -size );
         verts[ 9] = new Vector3(  size, height,  size );
         verts[10] = new Vector3( -size, height,  size );
         verts[11] = new Vector3( -size, height, -size );
 
 
 
         //triangles (clockwise)
         // bottom
         // 0, 1, 2
         // 0, 2, 3
 
         tri[0] = 0;
         tri[1] = 1;
         tri[2] = 2;
         
         tri[3] = 0;
         tri[4] = 2;
         tri[5] = 3;
 
         // bottom right
         // 0, 5, 1
         // 0, 4, 5
 
         tri[6] = 0;
         tri[7] = 5;
         tri[8] = 1;
         
         tri[9] = 0;
         tri[10] = 4;
         tri[11] = 5;
 
         // bottom front
         // 0, 7, 4
         // 0, 3, 7
 
         tri[12] = 0;
         tri[13] = 7;
         tri[14] = 4;
 
         tri[15] = 0;
         tri[16] = 3;
         tri[17] = 7;
 
         // bottom left
         // 3, 2, 6
         // 3, 6, 7
 
         tri[18] = 3;
         tri[19] = 2;
         tri[20] = 6;
 
         tri[21] = 3;
         tri[22] = 6;
         tri[23] = 7;
 
         // bottom back
         // 2, 1, 5
         // 2, 5, 6
 
         tri[24] = 2;
         tri[25] = 1;
         tri[26] = 5;
 
         tri[27] = 2;
         tri[28] = 5;
         tri[29] = 6;
 
         // top right
         // 4, 8, 9
         // 4, 9, 5
 
         tri[30] = 4;
         tri[31] = 8;
         tri[32] = 9;
 
         tri[33] = 4;
         tri[34] = 9;
         tri[35] = 5;
 
         // top front
         // 7, 11, 8
         // 7, 8, 4
 
         tri[36] = 7;
         tri[37] = 11;
         tri[38] = 8;
 
         tri[39] = 7;
         tri[40] = 8;
         tri[41] = 4;
 
         // top left
         // 11, 6, 10
         // 11, 7, 6
 
         tri[42] = 11;
         tri[43] = 6;
         tri[44] = 10;
 
         tri[45] = 11;
         tri[46] = 7;
         tri[47] = 6;
 
         // top back
         // 6, 5, 9
         // 6, 9, 10
 
         tri[48] = 6;
         tri[49] = 5;
         tri[50] = 9;
 
         tri[51] = 6;
         tri[52] = 9;
         tri[53] = 10;
 
         // top
         // 10, 9, 8
         // 11, 10, 8
 
         tri[54] = 10;
         tri[55] = 9;
         tri[56] = 8;
 
         tri[57] = 11;
         tri[58] = 10;
         tri[59] = 8;
 
         
         Mesh mesh = new Mesh();
         mesh.name = "Rock";
         mesh.vertices = verts;
         mesh.triangles = tri;
         mesh.RecalculateNormals();
         mf.mesh = mesh;
         
         mr.material = this.defaultRockMaterial;
     }

This works, and I see this: alt text

I would like to apply this kind of texture to it. I would like the top to be generally green and bottom to be black. Maybe have some sort of wave effect where the color changes. So, here is a sample texture:

alt text

I guess I should be calculating UVs for the mesh, but not sure where to start. Halp please!

texture.png (4.1 kB)
screen-shot-2015-04-29-at-101236-pm.png (76.8 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Apr 30, 2015 at 06:51 PM

This should provide all the info you need.

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 KonstantinMaximus · May 01, 2015 at 09:59 PM 0
Share

This articles makes sense, and it works great if we are talking about a 2D object. But how does this work with 3D objects, where each vertex belongs to multiple faces.

Lets say we have a cube and a texture. We want each face to show hte texture exactly the same, but if we specify bottom right corner as (1,0) and then rotate the cube to the left this right bottom corner will become left bottom corner, but its coordinate will still be (1,0). What about pointy objects?

avatar image Sessional · May 01, 2015 at 10:17 PM 0
Share

If you want jump discontinuity when applying your texture to your vertices you need to double up at least to allow each vertex to have a different UV. If you are concerned about the texture not being their, it to some extent does some weird wrapping stuff.

So in the bottom left you can give it (0,0) and in the top right you can give it (1,1). This means that the top left of the next face will start at (1,1) and the bottom right could become (0,0). If this doesn't line up well you HAVE to implement the doubling up of vertices to allow UVs to be different from aech other.

avatar image KonstantinMaximus · May 01, 2015 at 11:36 PM 0
Share

So, to create this kind of textured cube, you have to double up all of the verts.... or tripple because there are three faces?

$$anonymous$$y crappy cube: alt text

Desired cube: alt text

screen-shot-2015-05-01-at-43240-pm.png (61.2 kB)
screen-shot-2015-05-01-at-43321-pm.png (14.1 kB)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Applying textures to meshes 0 Answers

Procedural mesh with texture atlas, how to get rid of material artifacts? 0 Answers

Terrain import detail texture 0 Answers

Solid color instead of a texture on a custom mesh 2 Answers

Mesh with texture atlas is "sparkling" 1 Answer


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