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
0
Question by Soos621 · Nov 04, 2015 at 10:58 PM · unity 5submesh

Trouble with submeshes at runtime

Hello, i am having a bit of trouble with setting the triangles of a submesh at runtime. What i have is a sphere which i made in blender, what I'm doing is creating all the materials and submesh indexes at the beginning then i am creating regions which contain a list of the triangle indexes that are within that region. What the problem is, is that once the region is created and i attempt to set the triangles of that region to the correct submesh index, it just doesn't render the new material. I very well may be missing something but I've been going around in circles with this if someone could point me in the right direction it would be most appreciated. (as a note, i am not receiving any errors and the regions do contain all the triangles that they are supposed to)

Here is how I'm attempting to map out the sphere.

 void CreateAllSubmeshes (List<Material> mats){
     List<Material> curMats = this.GetComponent<Renderer> ().materials.ToList();
     curMats.AddRange (mats);
     int mi = 0;
     foreach (Material mat in mats) {
         materialDictionary.Add(mat.name, mat);
         submeshDictionary.Add(mat.name, mi);
         mi++;
     }
     currentMesh.mesh.subMeshCount = mi + 1;
     this.GetComponent<Renderer>().materials = curMats.ToArray();
     currentMesh.mesh.RecalculateNormals ();
     StartCoroutine (CreateLandscapes ());
 }

 IEnumerator CreateLandscapes (){
     List<Material> curmats = new List<Material> ();
     curmats = this.GetComponent<Renderer> ().materials.ToList ();
     for(int mi = 0; mi < curmats.Count; mi++){
         if(curmats[mi].name.Equals("Water (Instance)") || curmats[mi].name.Equals("Bedrock (Instance)") || curmats[mi].name.Equals("Default (Instance)")){
             curmats.RemoveAt(mi);
         }
     }
     int l = 0;
     while (l < curmats.Count) {
         Landscape newLandscape = new Landscape();
         newLandscape.submeshIndex = l;
         newLandscape.SetLandscapeMaterial(curmats[l]);
         landscapesList.Add(newLandscape);
         l++;
         yield return null;
     }
     StartCoroutine(CreateRegions ());
 }

IEnumerator CreateRegions (){ List triangles = currentMesh.sharedMesh.triangles.ToList(); int trisPerRegion = (currentMesh.sharedMesh.triangles.Length/3) / regionCount; int r = 0; while(r < regionCount){ int index = r (trisPerRegion 3); List regionTris = triangles.GetRange(index, trisPerRegion*3); CreateRegion (regionTris, landscapesList[Random.Range(0, landscapesList.Count - 1)]); r++; yield return null; }

     int totalmapedtriangles = 0;
     foreach (PlanetRegion reg in Regions) {
         totalmapedtriangles = totalmapedtriangles + reg.triangles.Count;
     }
     print ("Total Mapped Triangles: " + totalmapedtriangles + " and Total Mesh Triangles: "+              currentMesh.mesh.triangles.Length);
 }

 public void AddToSubMesh (string matName, int[] tris){
     Mesh curMesh = currentMesh.mesh;
     foreach (string n in submeshDictionary.Keys) {
         if(matName.Equals(n + " (Instance)")){
             List<int> currentSubmeshTris = curMesh.GetTriangles(submeshDictionary[n]).ToList();
             currentSubmeshTris.AddRange(tris);
             curMesh.SetTriangles(currentSubmeshTris.ToArray(), submeshDictionary[n]);
             print ("triangles mapped: " + currentSubmeshTris.Count + " as " + n);
         }
     }
     currentMesh.mesh = curMesh;
     currentMesh.mesh.triangles = curMesh.triangles;
     currentMesh.mesh.RecalculateNormals ();
 }

public class PlanetaryRegion { public void SetTris (List _triangles, PlanetaryGrid _pGrid){ triangles = _triangles; pGrid = _pGrid; pGrid.AddToSubMesh (landscape.landscapeMaterial.name, _triangles.ToArray()); StartCoroutine (CreateTris (triangles)); } }

I don't know if I am just calling something in the wrong order or when i call mesh.setTriangles i need to update the mesh somehow. Thanks for you help.

~Soos

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

0 Replies

· Add your reply
  • Sort: 

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

38 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

Related Questions

Material not rendering correctly on Submesh 0 Answers

How do i create sub meshes? 1 Answer

Android plugin - how to create a daily notification 0 Answers

Sprite didn't change more than once 0 Answers

Can't see GameObjects In Scene View 2 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