Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 khos85 · Jul 13, 2015 at 07:18 PM · destruction

Script to break mesh into smaller pieces?

Hi,

I looked at assets such as https://www.assetstore.unity3d.com/en/#!/content/9411 but they are quite expensive, I would like to ask if there is a free or cheaper alternative to be able to spit a mesh into many small pieces/rigidbodies? Any advise would be appreciated.

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
13
Best Answer

Answer by Cherno · Jul 13, 2015 at 08:31 PM

Here is a script that will create seperate triangle rigidbodies from the gameObject's mesh and make them explode, for a nice shattering effect. Works great for low-poly object, but might lead to performancy issues with medium-to high poly count objects. I think I found it on the unity wiki.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class TriangleExplosion : MonoBehaviour {
 
     public IEnumerator SplitMesh (bool destroy)    {
 
         if(GetComponent<MeshFilter>() == null || GetComponent<SkinnedMeshRenderer>() == null) {
             yield return null;
         }
 
         if(GetComponent<Collider>()) {
             GetComponent<Collider>().enabled = false;
         }
 
         Mesh M = new Mesh();
         if(GetComponent<MeshFilter>()) {
             M = GetComponent<MeshFilter>().mesh;
         }
         else if(GetComponent<SkinnedMeshRenderer>()) {
             M = GetComponent<SkinnedMeshRenderer>().sharedMesh;
         }
 
         Material[] materials = new Material[0];
         if(GetComponent<MeshRenderer>()) {
             materials = GetComponent<MeshRenderer>().materials;
         }
         else if(GetComponent<SkinnedMeshRenderer>()) {
             materials = GetComponent<SkinnedMeshRenderer>().materials;
         }
 
         Vector3[] verts = M.vertices;
         Vector3[] normals = M.normals;
         Vector2[] uvs = M.uv;
         for (int submesh = 0; submesh < M.subMeshCount; submesh++) {
 
             int[] indices = M.GetTriangles(submesh);
 
             for (int i = 0; i < indices.Length; i += 3)    {
                 Vector3[] newVerts = new Vector3[3];
                 Vector3[] newNormals = new Vector3[3];
                 Vector2[] newUvs = new Vector2[3];
                 for (int n = 0; n < 3; n++)    {
                     int index = indices[i + n];
                     newVerts[n] = verts[index];
                     newUvs[n] = uvs[index];
                     newNormals[n] = normals[index];
                 }
 
                 Mesh mesh = new Mesh();
                 mesh.vertices = newVerts;
                 mesh.normals = newNormals;
                 mesh.uv = newUvs;
                 
                 mesh.triangles = new int[] { 0, 1, 2, 2, 1, 0 };
 
                 GameObject GO = new GameObject("Triangle " + (i / 3));
                 GO.layer = LayerMask.NameToLayer("Particle");
                 GO.transform.position = transform.position;
                 GO.transform.rotation = transform.rotation;
                 GO.AddComponent<MeshRenderer>().material = materials[submesh];
                 GO.AddComponent<MeshFilter>().mesh = mesh;
                 GO.AddComponent<BoxCollider>();
                 Vector3 explosionPos = new Vector3(transform.position.x + Random.Range(-0.5f, 0.5f), transform.position.y + Random.Range(0f, 0.5f), transform.position.z + Random.Range(-0.5f, 0.5f));
                 GO.AddComponent<Rigidbody>().AddExplosionForce(Random.Range(300,500), explosionPos, 5);
                 Destroy(GO, 5 + Random.Range(0.0f, 5.0f));
             }
         }
 
         GetComponent<Renderer>().enabled = false;
         
         yield return new WaitForSeconds(1.0f);
         if(destroy == true) {
             Destroy(gameObject);
         }
 
     }
 
 
 }


You can use it like this:

 gameObject.AddComponent<TriangleExplosion>();
 
 StartCoroutine(gameObject.GetComponent<TriangleExplosion().SplitMesh(true));
 
Comment
Add comment · Show 7 · 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 khos85 · Jul 13, 2015 at 10:07 PM 0
Share

Very cool, thanks! is there a way to control the mesh splitting complexity in this script? I am trying to figure it out if yes or not, but looks very nice.

avatar image Cherno · Jul 14, 2015 at 06:11 AM 0
Share

No, there isn't. You'd have to create that functionality yourself. Take a look at the $$anonymous$$esh class on the Unity Scripting API. The alternative is to create a prefab for each model that consist of several pieces, and instantiate that one when the explosion should occur.

How to $$anonymous$$ake an Object Shatter Into Smaller Fragments in Unity

avatar image Epiplon · Dec 30, 2016 at 04:18 AM 0
Share

Wow, this is awesome. Very easy to understand and with amazing result.

avatar image softwiz · Apr 13, 2017 at 12:49 PM 0
Share

Hi,

When i add the above code, i am getting the following error . Could you pls help?

'A game object can only be in one layer. The layer needs to be in the range [0...31] UnityEngine.GameObject:set_layer(Int32) c__Iterator0:$$anonymous$$oveNext() (at Assets/Scripts/TriangleExplosion.cs:64)"

avatar image Cherno softwiz · Apr 14, 2017 at 10:06 PM 0
Share
  1. Answers are for answers to the original question, not for answering to a comment, let alone to bump an old thread. Please use the comment function.

  2. You have to substitute your own layer name if you don't have a layer named "Particles".

       GO.layer = Layer$$anonymous$$ask.NameToLayer("YourLayerName");
     
    
avatar image Arctic_Evolution · Feb 18, 2018 at 11:50 PM 0
Share

This script increases the scale of your objects, a small, yet noticeable amount. Looks like you would have to offset that and reduce the scale before you broke it apart.

Show more comments

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

29 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

Related Questions

Simple Fracture ( Destruction ) 1 Answer

2D vs 3D mesh deformation. 0 Answers

Destroy Child Object(s) OnValidate? 6 Answers

Nvidia Techonology in Unity 3 Answers

How to split 3D array in to two - Breaking voxelized objects. 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