- Home /
Question by
Shinobinya · Jun 18, 2016 at 05:42 AM ·
animationshadereffect
How can i divide mesh into single polygons(tris) and then just make them fly randomly out(making like breakup effect)
I mean something like this asset https://www.assetstore.unity3d.com/en/#!/content/8264 I know it's not easy when it costs 20 bucks and is made by professional but i would still like to know some basics.
Thanks for everything
Comment
Best Answer
Answer by Cherno · Jun 18, 2016 at 12:13 PM
Use this script, it's one I found online and enhanced it to support different kinds of renderers. It's be wise to implement object pooling if it's used regularly during game play to avoid memory leaks.
Use it with
StartCoroutine(objectToEplode.GetComponent<TriangleExplosion>().SplitMesh(true));
123
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);
}
}
}
Your answer
Follow this Question
Related Questions
Aura effect, need tips/advice 1 Answer
Old TV Set post effect 2 Answers
How can I create glowing trail effect? 0 Answers
Creating this light effect 1 Answer
Material Animation from -1 to 1 0 Answers