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 DayyanSisson · Aug 13, 2012 at 10:07 PM · meshrendermesh renderer

Combining Mesh's For Mesh Deformation

I'm working on a script that takes several mesh's combines them to make one mesh, then renders that one mesh instead of rendering it separately. Here's what I tried doing:

     public List<MeshFilter> terrainSegments;
 
     private MeshFilter terrainMesh;
     private Transform thisTerrain;
 
     void Awake () {
 
         thisTerrain = transform;
 
         int i = 0;
 
         while(i < thisTerrain.childCount){
             MeshFilter terrChild = thisTerrain.GetChild(i).GetComponent<MeshFilter>();
             if(terrChild && thisTerrain.GetChild(i).CompareTag("Terrain Segment")){
                 terrChild.mesh.name = "Terrain Mesh " + i.ToString();
                 terrainSegments.Add(terrChild);
                 terrChild.transform.renderer.enabled = false;
             }
             i++;
         }
 
         CombineInstance[] combine = new CombineInstance[terrainSegments.Count];
         terrainMesh = thisTerrain.GetComponent<MeshFilter>();
         terrainMesh.mesh.CombineMeshes(combine, false);
         thisTerrain.GetComponent<MeshFilter>().mesh = terrainMesh.mesh;
     }

Basically once all the mesh's have been combined, the mesh renderer for the parent object should render the combined meshs. But it doesn't. It has a mesh renderer, and a mesh filter, and once this code runs, the mesh filter's mesh is the combined mesh. Why doesn't it render anything?

Edit

I'm apparently using CombineMeshes() wrong. Can't seem to find how to actually combine the meshes.

Edit

I've re-written the script and it works. It combines the mesh's, but unfortunately it doesn't suit what I need it to do. What I need is to have one renderer that renders the entire mesh as one, and keep the other mesh filter's intact. The player responds to the separate mesh filters, but only see's what the combined mesh renderer shows (although what he sees and what he's interacting with are two different things). Since the original mesh's are being deformed, the mesh renderer needs to create a new combined mesh whenever it's deformed. Unfortunately, whenever I run the new code I wrote, it creates a completely new mesh on top of the old one, without replacing the old one. It also doesn't render the deforms all the time. Here's the code I used:

         Component[] meshFilters = GetComponentsInChildren<MeshFilter>();
         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
 
         int i = 0;
         while (i < meshFilters.Length) {
             combine[i].mesh = meshFilters[i].GetComponent<MeshFilter>().sharedMesh;
             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
             i++;
         }
 
 
         terrain.GetComponent<MeshFilter>().mesh = new Mesh();
         terrain.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
         terrain.gameObject.active = true;

Here's a webplayer showing what's supposed to happen (click to deform).
Here's a webplayer showing what's actually happening (keep clicking after glitch happens).

Anyone know why this is happening and how to fix it?

Comment
Add comment · Show 2
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 ScroodgeM · Aug 13, 2012 at 10:13 PM 0
Share

mesh renderer has a material assigned?

no warnings in console?

clicking on mesh from meshfilter points to mesh with visible geometry, verts and tris count in inspector?

avatar image DayyanSisson · Aug 13, 2012 at 10:24 PM 0
Share

That seems to be the problem. No warnings, and there's a material assigned. I did a couple debugs and I found that the mesh doesn't have any vertices or triangles. So it says that the mesh has been combined, and a new mesh is in the mesh filter, but it doesn't have any mesh data. What did I do wrong when combining it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by ThermalFusion · Aug 13, 2012 at 11:24 PM

You were not adding anything into the CombineInstances.

private List terrainSegments; private List allMaterials;

private MeshFilter terrainMesh; private MeshRenderer terrainRenderer; private Transform thisTerrain;

void Awake () { terrainSegments = new List(); allMaterials = new List(); thisTerrain = transform;

foreach (Transform child in thisTerrain){ MeshFilter terrChild = child.GetComponent(); MeshRenderer terrRender = child.GetComponent(); if(terrChild && terrRender && child.CompareTag("Terrain Segment")){ terrainSegments.Add(terrChild); foreach (Material m in terrRender.materials) { allMaterials.Add(m); }

   terrChild.renderer.enabled = false;
 }

}

CombineInstance[] combine = new CombineInstance[terrainSegments.Count]; Matrix4x4 worldToLocalMatrix; worldToLocalMatrix = thisTerrain.worldToLocalMatrix; for(int c = 0; c < terrainSegments.Count; c ++) { combine[c].mesh = terrainSegments[c].sharedMesh; combine[c].transform = worldToLocalMatrix * terrainSegments[c].transform.localToWorldMatrix; Debug.Log(terrainSegments.Count.ToString()); } terrainMesh = thisTerrain.GetComponent(); terrainMesh.mesh.Clear(); terrainMesh.mesh.name = "Name"; terrainMesh.mesh.CombineMeshes(combine, false, true); Debug.Log(terrainMesh.mesh.vertexCount.ToString()); terrainRenderer = thisTerrain.GetComponent(); terrainRenderer.materials = allMaterials.ToArray(); }

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 ThermalFusion · Aug 13, 2012 at 11:31 PM 0
Share

How in the world do you format this correctly, plase change if you can.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

combining meshes 0 Answers

Mesh Renderer and Memory 2 Answers

Turn an objects mesh renderer on upon collision. 1 Answer

Changing colour in a mesh? 0 Answers

Mesh render flicker 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