-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGeometryOctahedronSphereMultiVertexInstancing.cs
More file actions
80 lines (63 loc) · 2.36 KB
/
GeometryOctahedronSphereMultiVertexInstancing.cs
File metadata and controls
80 lines (63 loc) · 2.36 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GeometryOctahedronSphereMultiVertexInstancing : MonoBehaviour {
struct OctahedronSphereData
{
public Vector3 position;
public Quaternion rotation;
public float scale;
public int level;
}
const int THREAD_GROUP_X = 8;
public int num = 1024;
public float emitRange = 10f;
public Material renderMaterial;
public float noiseScale = 0.1f;
public float noiseSpeed = 0.1f;
public ComputeShader cs;
ComputeBuffer _particleBuffer;
int _particleNum = 0;
// Use this for initialization
void Start()
{
_particleNum = Mathf.CeilToInt((float)num / THREAD_GROUP_X) * THREAD_GROUP_X;
Debug.Log("Particle Num " + _particleNum);
_particleBuffer = new ComputeBuffer(_particleNum, System.Runtime.InteropServices.Marshal.SizeOf(typeof(OctahedronSphereData)));
OctahedronSphereData[] octahedronSphereData = new OctahedronSphereData[_particleNum];
for (int i = 0; i < _particleNum; i++)
{
octahedronSphereData[i].position = Random.insideUnitSphere * emitRange;
octahedronSphereData[i].rotation = Random.rotation;
octahedronSphereData[i].scale = Random.Range(0.1f, 0.5f);
octahedronSphereData[i].level = 1;
}
_particleBuffer.SetData(octahedronSphereData);
//// Init
//cs.Dispatch(0, _particleNum / THREAD_GROUP_X, 1, 1);
}
// Update is called once per frame
void Update()
{
cs.SetFloat("_Time", Time.time);
cs.SetFloat("_NoiseScale", noiseScale);
cs.SetFloat("_NoiseSpeed", noiseSpeed);
cs.SetInt("_SubDivisionNum", 9);
cs.SetBuffer(0, "_particleBuffer", _particleBuffer);
cs.Dispatch(0, _particleNum / THREAD_GROUP_X, 1, 1);
}
void OnRenderObject()
{
//renderMaterial.SetFloat("_Time", Time.time);
renderMaterial.SetFloat("_NoiseScale", noiseScale);
renderMaterial.SetFloat("_NoiseSpeed", noiseSpeed);
renderMaterial.SetBuffer("_particleBuffer", _particleBuffer);
renderMaterial.SetPass(0);
Graphics.DrawProcedural(MeshTopology.Points, _particleNum * 8);
}
private void OnDestroy()
{
_particleBuffer.Release();
_particleBuffer = null;
}
}