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 Benzoid · Jul 26, 2019 at 09:37 AM · texturemeshmaterialsubmesh

How to apply a texture to a submesh

I created a cube mesh because I need to apply two materials to it : one on 5 faces (basic colored material) and one on the remaining face (textured with an image)

Here's my code to create the submesh and apply the materials.

 public class CubeMesh : MonoBehaviour
 {
     public float size;
 
     public Material[] materials;
     // Start is called before the first frame update
     void Start()
     {
         createCube();
     }
 
     // Update is called once per frame
     void createCube()
     {
         Vector3[] vertices = {
             new Vector3(0, 0, 0)*size,
             new Vector3(1, 0, 0)*size,
             new Vector3(1, 1, 0)*size,
             new Vector3(0, 1, 0)*size,
             new Vector3(0, 1, 1)*size,
             new Vector3(1, 1, 1)*size,
             new Vector3(1, 0, 1)*size,
             new Vector3(0, 0, 1)*size,
         };
 
         int[] triangles = {
             0, 2, 1, //face front
             0, 3, 2,
             2, 3, 4, //face top
             2, 4, 5,
             1, 2, 5, //face right
             1, 5, 6,
             0, 7, 4, //face left
             0, 4, 3,
             5, 4, 7, //face back
             5, 7, 6,
             0, 6, 7, //face bottom
             0, 1, 6
         };
         print(triangles.Length);
 
         Mesh mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
 
         mesh.vertices = vertices;
         mesh.triangles = triangles;
 
         mesh.subMeshCount = 2;

         int[] subTriangles = new int[6];    // Front face submesh
         Array.Copy(triangles, 0, subTriangles, 0, 6);
         mesh.SetTriangles(subTriangles, 0);
 
         subTriangles = new int[30];
         Array.Copy(triangles, 6, subTriangles, 0, 30);
         mesh.SetTriangles(subTriangles, 1);
 
 
         mesh.RecalculateNormals();
 
         Renderer r = GetComponent<Renderer>();
         r.materials = materials;
     }
 }

I made the textured material by dropping the image in the Albedo grey square. It's working on regular cubes. However, after assigning the script to a GameObject and setting the materials, I cannot see the image on the face of the cube. I only get a white texture (the image is a logo which is mostly white)

Is there something wrong in what I'm doing?

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 Bunny83 · Jul 26, 2019 at 01:02 PM

You haven't created any UV coordinates for your vertices. So there's no texture mapping taking place. By default all vertices would have a UV coordinate of 0,0 so all faces would be mapped to the lower left corner of your image.


Also your mesh doesn't represent a cube. It's more like a lowpoly sphere since you have shared vertices everywhere. The vertices at the corners need to be split into 3, one for each face that meets at that corner. That's why a cube mesh needs 24 vertices instead of 8. You have 6 faces and each face needs 4 seperate vertices. In order to have a cube look like a cube the 4 vertices of a face need to have all the same vertex normal. However if you only have 8 vertices in total, you can not assign 3 different normals to the same vertex. Likewise the UV coordinates. To map a single face to a certain portion of a texture you need to specify a specific UV coordinate at each of the 4 corners. However if you share vertices between two or more faces you can only specify one UV coordinate which would be shared between all faces. This won't work.


Creating a UV mapped cube mesh procedurally is of course possible, but it's a bit more work as what you've done here.


The code example I've posted over here doesn't really create a cube but the frustom of a camera which is just a deformed cube. Though the concept would be similar. I also started with the initial 8 vertices but have created that "m_VertOrder" array which creates the 24 required vertices out of the initial 8. Though I do not generate any UVs in this example since there is not only one valid way how to unwrap a cube.


If you want to create an unwrapped cube (with UV coordinates) it might be simpler to just create each face one by one.

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 Benzoid · Jul 29, 2019 at 08:43 AM 0
Share

Hi, thank you for all the information and the help, it's been very useful. I followed your advice and made a 24 vertices cube and generated the UVs but the texture is split in 4 and the four parts of the image are respectively in the opposite corner it should be.

Here is how I generate UVs

 Vector2[] uvs = new Vector2[vertices.Length];
 for (int i = 0; i < uvs.Length; i++) {
     uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
 }
 mesh.uv = uvs;

I tried a few combinations of vertices and triangle order but can't fix it.

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

162 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 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 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

Layered Textures/Materials 0 Answers

Mesh wont accept material? 2 Answers

Applying a material to mesh parts 1 Answer

Smoothing mesh texture around edges 0 Answers

3D Mesh is broken thanks to normal map 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