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 /
  • Help Room /
avatar image
13
Question by kendavistoo · Nov 29, 2017 at 10:33 PM · submesh

Small Submesh Example

After three days of frustration, I finally got a subMesh example working. May not be the cleanest code, and if you see any improvements, please reply with them. An empty GameObject is created C# script is added. A Resources folder is added to the assets, and two images are placed into it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 
 
 //---------- class SubMeshScript
 
 public class SubMeshScript : MonoBehaviour {
 
     Material[] mat = new Material[2];
 
     Vector3[] vertices    = new Vector3[8];
     Vector2[] uvs        = new Vector2[8];
     Vector2[] uvs2        = new Vector2[8];
 
     Renderer rend;
 
     Mesh mesh;
 
     int[][] triDex = new int[2][];
 
     //---------- Start
 
     void Start () {
 
         mesh = GetComponent<MeshFilter> ().mesh;
         mesh.Clear ();
 
         rend = GetComponent<Renderer> ();
 
         mat [0] = new Material (Shader.Find("Unlit/Texture"));
         mat [1] = new Material (Shader.Find("Unlit/Texture"));
 
         mat[0].mainTexture = Resources.Load ("colorTest") as Texture2D;
         mat[1].mainTexture = Resources.Load ("MyTest") as Texture2D;
 
         rend.materials = mat;
 
         //----------
 
         vertices [0] = new Vector3 (0f, 0f, 0f);
         vertices [1] = new Vector3 (0f, 0f, 8f);
         vertices [2] = new Vector3 (0f, 8f, 0f);
         vertices [3] = new Vector3 (0f, 8f, 8f);
 
         vertices [4] = new Vector3 (8f, 0f, 0f);
         vertices [5] = new Vector3 (0f, 0f, 0f);
         vertices [6] = new Vector3 (8f, 8f, 0f);
         vertices [7] = new Vector3 (0f, 8f, 0f);
 
         //----------
 
         uvs [0] = new Vector2 (0f, 0f);
         uvs [1] = new Vector2 (1f, 0f);
         uvs [2] = new Vector2 (0f, 1f);
         uvs [3] = new Vector2 (1f, 1f);
 
         uvs [4] = new Vector2 (0f, 0f);
         uvs [5] = new Vector2 (1f, 0f);
         uvs [6] = new Vector2 (0f, 1f);
         uvs [7] = new Vector2 (1f, 1f);

      //                   y   mesh0
      //                  7   2 ------ 3
      //mesh1      / |   |          |
      //               /   |   |          |
      //           6       5   0 ------ 1      z
      //          |    /
      //          | /
      //          4
      //       x
 
         mesh.subMeshCount = 2;
 
         mesh.vertices = vertices;
         mesh.uv  = uvs;
 
         triDex [0] = new int[6] { 0, 3, 1, 0, 2, 3 };
         triDex [1] = new int[6] { 4, 7, 5, 4, 6, 7 };
 
         mesh.SetTriangles (triDex[0], 0);
         mesh.SetTriangles (triDex[1], 1);
 
         mesh.RecalculateNormals();
 
     }//Start
 }//class SubMeshScript
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 Glurth · Nov 29, 2017 at 11:12 PM 0
Share

looks fine. Couple suggestions:

Shader.Find("Unlit/Texture") only needs to called once. Store the value in a variable, and pass it to both your material creation functions.

Also, I would avoid using constant values (for flexibility), and would try to use named shortcuts, when possible (for readability). e.g.

 public float size=8.0f;  //default value of 8, but can be changed.
 
 //now we can write vertices [1] = new Vector3 (0f, 0f, 8f);  as
 vertices [1] = Vector3.forward * size;

(https://docs.unity3d.com/ScriptReference/Vector3-forward.html)

(Then again, I would also use size=1.0f, and ins$$anonymous$$d let the $$anonymous$$eshFilter/$$anonymous$$eshRederer object's transform do the scaling.)

Same for the UV's

 new Vector2 (1f, 0f);

is equivalent to Vector2.right

FYI: This Q would probabalybe better suited for the forum. UA is generally for finding solutions, rather than code review.

avatar image mexicano · Aug 15, 2021 at 10:16 PM 0
Share

Thank you so much. Your answer is the actual useful one on the entire internet on this subject. Love you my boy =).

avatar image MShahbazKharal · Apr 08 at 07:12 AM 0
Share

thanks man, I got stuck too and it helped me understand the idea.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by zeropage · Dec 15, 2018 at 09:49 PM

@kendavistoo - thanks for posting this. Super helpful.

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

76 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

Related Questions

Trouble with submeshes at runtime 0 Answers

Failed Getting triangles & Failed setting Triangles - Using Quixel Voxel Terrain 0 Answers

Vertex coloring multiple submeshes different colors 0 Answers

How to apply shader to all submeshes of a gameobject? 0 Answers

How can i assgin a 'subMesh' to 'MeshCollider' 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