-
Notifications
You must be signed in to change notification settings - Fork 174
/
ParticleEngine.cs
160 lines (130 loc) · 5.06 KB
/
ParticleEngine.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UTJ
{
public class ParticleEngine : MonoBehaviour
{
[StructLayout(LayoutKind.Explicit)]
public struct peParticle
{
public const int size = 32;
[FieldOffset(0)]
public Vector3 position;
[FieldOffset(0)]
public Vector4 position4;
[FieldOffset(16)]
public Vector3 velocity;
[FieldOffset(16)]
public Vector4 velocity4;
}
public struct CSParams
{
public const int size = 32;
public int particle_count;
public float particle_size;
public float rcp_particle_size2;
public float pressure_stiffness;
public float wall_stiffness;
public float timestep;
float pad1, pad2;
};
const int KernelBlockSize = 256;
public int m_particle_count = 1024 * 4;
public float m_particle_size = 0.1f;
public float m_pressure_stiffness = 500.0f;
public float m_wall_stiffness = 1500.0f;
public ComputeShader m_cs_particle_core;
ComputeBuffer m_cb_params;
ComputeBuffer m_cb_particles;
ComputeBuffer m_cb_positions;
ComputeBuffer m_cb_velocities;
CSParams[] m_csparams;
Vector3[] m_buf_positions;
Vector3[] m_buf_velocities;
float m_elapsed;
public Vector3[] positionBuffer { get { return m_buf_positions; } }
public Vector3[] velocityBuffer { get { return m_buf_velocities; } }
public float elapsed { get { return m_elapsed; } }
void InitializeSimulation()
{
if (SystemInfo.supportsComputeShaders)
{
m_cb_params = new ComputeBuffer(1, CSParams.size);
m_cb_particles = new ComputeBuffer(m_particle_count, peParticle.size);
m_cb_positions = new ComputeBuffer(m_particle_count, 12);
m_cb_velocities = new ComputeBuffer(m_particle_count, 12);
m_buf_positions = new Vector3[m_particle_count];
m_buf_velocities = new Vector3[m_particle_count];
m_csparams = new CSParams[1];
}
{
UnityEngine.Random.InitState(0);
var tmp = new peParticle[m_particle_count];
for (int i = 0; i < tmp.Length; ++i)
{
tmp[i].position = new Vector3(
UnityEngine.Random.Range(-5.0f, 5.0f),
UnityEngine.Random.Range(-5.0f, 5.0f) + 5.0f,
UnityEngine.Random.Range(-5.0f, 5.0f));
}
m_cb_particles.SetData(tmp);
}
}
void ReleaseSimulation()
{
if (m_cb_params != null)
{
m_cb_params.Release(); m_cb_params = null;
m_cb_particles.Release(); m_cb_particles = null;
m_cb_positions.Release(); m_cb_positions = null;
m_cb_velocities.Release(); m_cb_velocities = null;
}
}
void UpdateSimulation(float dt)
{
if (!SystemInfo.supportsComputeShaders)
{
Debug.Log("ComputeShader is not available.");
return;
}
float begin_time = Time.realtimeSinceStartup;
m_csparams[0].particle_count = m_particle_count;
m_csparams[0].particle_size = m_particle_size;
m_csparams[0].rcp_particle_size2 = 1.0f / (m_particle_size * 2.0f);
m_csparams[0].pressure_stiffness = m_pressure_stiffness;
m_csparams[0].wall_stiffness = m_wall_stiffness;
m_csparams[0].timestep = dt;
m_cb_params.SetData(m_csparams);
for (int i = 0; i < 2; ++i)
{
m_cs_particle_core.SetBuffer(i, "g_params", m_cb_params);
m_cs_particle_core.SetBuffer(i, "g_particles", m_cb_particles);
m_cs_particle_core.SetBuffer(i, "g_positions", m_cb_positions);
m_cs_particle_core.SetBuffer(i, "g_velocities", m_cb_velocities);
m_cs_particle_core.Dispatch(i, m_particle_count / KernelBlockSize, 1, 1);
}
m_cb_positions.GetData(m_buf_positions);
m_cb_velocities.GetData(m_buf_velocities);
m_elapsed = Time.realtimeSinceStartup - begin_time;
Debug.Log("ParticleEngine.UpdateSimulation(): " + (m_elapsed * 1000.0f) + "ms");
}
void OnEnable()
{
InitializeSimulation();
}
void OnDisable()
{
ReleaseSimulation();
}
void Update()
{
UpdateSimulation(Time.deltaTime);
}
}
}