summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Effects/ShurikenBakedMeshEmitter.cs
blob: 65a36d9da2a0e4de8286e2eb576c133e55eb9a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using UnityEngine;
using System.Collections;

public class ShurikenBakedMeshEmitter : MonoBehaviour
{
    [Tooltip("パーティクルで使用したいメッシュ")]
    public SkinnedMeshRenderer skin;
    //使用するメッシュの現在の形を保存
    Mesh baked;
    //再生に使用するパーティクルシステム
    ParticleSystem particle;
    ParticleSystemRenderer render;
    [Tooltip("パーティクルの再生/停止")]
    public bool emit;
    [Tooltip("パーティクルを放出する間隔(秒)")]
    public float coolDown = 0.5f;
    //現在の待機時間
    float interval = 0;

    // Use this for initialization
    void Start()
    {
        //元となるメッシュが指定されていなければ本スクリプトを停止
        if (!skin)
            this.enabled = false;
        //使用するパーティクルシステムへのアクセス
        particle = GetComponent<ParticleSystem>();
        render = GetComponent<ParticleSystemRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        //emitが[true]なら...
        if (emit)
        {
            //待機時間の計算
            interval -= Time.deltaTime;
            //必要分の待機時間が経過したなら...
            if (interval < 0)
            {
                //シーンに新たなGameObjectを再生(本GameObjectのコピー)
                GameObject newEmitter = Instantiate(gameObject, transform.position, transform.rotation) as GameObject;
                //再生したGameObjectにEmitMesh()を指示
                newEmitter.GetComponent<ShurikenBakedMeshEmitter>().EmitMesh();
                //待機時間のリセット
                interval = coolDown;
            }
            //emitが[false]なら...
        }
        else
        {
            //待機時間のリセット
            interval = coolDown;
        }
    }

    //外部からのアクセスでパーティクルの再生をさせる
    public void EmitMesh()
    {
        //Update ()させない為に[false]
        emit = false;
        //メッシュの型をリセット
        baked = new Mesh();
        //使用するメッシュの現在の形を保存
        skin.BakeMesh(baked);

        //自身のパーティクルシステムへのアクセス
        particle = GetComponent<ParticleSystem>();
        render = GetComponent<ParticleSystemRenderer>();

        Mesh mainMesh = baked;
        Mesh meshToCopy = new Mesh();
        meshToCopy.vertices = mainMesh.vertices;
        meshToCopy.triangles = mainMesh.triangles;
        meshToCopy.normals = mainMesh.normals;
        meshToCopy.uv = mainMesh.uv;
        int amount = 8;
        Matrix4x4[] matrix = new Matrix4x4[amount];
        for (int i = 0; i < amount; i++)
        {
            matrix[i].SetTRS(new Vector3(i * 10, 0, 0), Quaternion.Euler(new Vector3(Random.Range(-5, 5), Random.Range(0, 360), Random.Range(-5, 5))), Vector3.one);
        }

        CombineInstance[] ci = new CombineInstance[amount];
        for (int i = 0; i < amount; i++)
        {
            ci[i] = new CombineInstance();
            ci[i].mesh = meshToCopy;
            ci[i].transform = matrix[i];
        }

        Mesh batchedMesh = gameObject.AddComponent<MeshFilter>().mesh = new Mesh();
        batchedMesh.CombineMeshes(ci);

        int[] mainSubTri;
        int[] newSubTri;
        batchedMesh.subMeshCount = mainMesh.subMeshCount;

        for (int i = 0; i < mainMesh.subMeshCount; i++)
        {
            mainSubTri = mainMesh.GetTriangles(i);

            newSubTri = new int[mainSubTri.Length * amount];

            for (int ii = 0; ii < amount; ii++)
            {
                for (int iii = 0; iii < mainSubTri.Length; iii++)
                {
                    newSubTri[(ii * mainSubTri.Length) + iii] = mainSubTri[iii] + (ii * mainMesh.vertexCount);
                }
            }
            batchedMesh.SetTriangles(newSubTri, i);
        }


        //パーティクルシステムで使用するメッシュを指定
        render.mesh = meshToCopy;
        //パーティクルの再生
        particle.Play();
        //本GameObjectの破棄の指示
        Destroy(gameObject, particle.duration);
    }
}