-
Notifications
You must be signed in to change notification settings - Fork 1
/
PendulumComputeMono.cs
131 lines (117 loc) · 4.31 KB
/
PendulumComputeMono.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PendulumComputeMono : MonoBehaviour
{
private RenderTexture rt;
private ComputeBuffer rtCompBuffer;
private int kernelIndex = -1;
private int fadeKernelIndex = -1;
private int renderKernelIndex = -1;
private ComputeBuffer readBuffer;
private ComputeBuffer writeBuffer;
public ComputeShader s;
public Material mat;
public Vector2 initialConditions;
private Vector2 oldInitialConditions;
public int resolution;
private int oldResolution;
public int objectCount;
private int oldObjectCount;
public float timeScale;
public float L1;
public float L2;
public float m1;
public float m2;
public float g;
public float fadeRate;
public float brightness;
private void OnDisable()
{
readBuffer.Release();
writeBuffer.Release();
rtCompBuffer.Release();
rt.Release();
}
[System.NonSerialized]
public float deltaTime;
void FadeTexture(float amount)
{
s.SetFloat("fadeAmount",amount);
s.Dispatch(fadeKernelIndex, resolution / 8, resolution / 8, 1);
}
void Update()
{
objectCount = Mathf.Max(objectCount, 1);
if (kernelIndex == -1)
{
kernelIndex = s.FindKernel("CSMain");
}
if (fadeKernelIndex == -1)
{
fadeKernelIndex = s.FindKernel("Fade");
}
if (renderKernelIndex == -1)
{
renderKernelIndex = s.FindKernel("Render");
}
if (resolution != oldResolution)
{
oldResolution = resolution;
if (rtCompBuffer != null)
rtCompBuffer.Release();
if (rt != null)
rt.Release();
rt = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGBFloat);
rt.enableRandomWrite = true;
rt.useMipMap = false;
rt.Create();
rtCompBuffer = new ComputeBuffer(resolution * resolution, 4);
s.SetBuffer(kernelIndex, "Result", rtCompBuffer);
s.SetBuffer(fadeKernelIndex, "Result", rtCompBuffer);
s.SetBuffer(renderKernelIndex, "Result", rtCompBuffer);
s.SetTexture(renderKernelIndex, "ResultTexture", rt);
s.SetTexture(fadeKernelIndex, "ResultTexture", rt);
//s.SetTexture(fadeKernelIndex, "Result", rt);
mat.SetTexture("_MainTex", rt);
}
if (objectCount != oldObjectCount || oldInitialConditions!= initialConditions)
{
oldInitialConditions = initialConditions;
oldObjectCount = objectCount;
if (readBuffer != null)
readBuffer.Release();
if (writeBuffer != null)
writeBuffer.Release();
readBuffer = new ComputeBuffer(objectCount, 16);
List<Vector4> initialValues = new List<Vector4>();
//Vector4 rand = new Vector4(Random.value * Mathf.PI * 2, 0, Random.value * Mathf.PI * 2, 0);
Vector4 rand = new Vector4(initialConditions.x, 0, initialConditions.y, 0);
for (int i = 0; i < objectCount; i++)
{
Vector4 v = rand;
v.z += i / 10000000.0f;
initialValues.Add(v);
}
readBuffer.SetData(initialValues);
writeBuffer = new ComputeBuffer(objectCount, 16);
}
FadeTexture(Mathf.Clamp01(fadeRate*Time.deltaTime));
s.SetBuffer(kernelIndex, "ping", readBuffer);
s.SetBuffer(kernelIndex,"pong", writeBuffer);
s.SetInt("objectCount", objectCount);
s.SetInt("resolution", resolution);
s.SetFloat("brightness", brightness);
s.SetFloat("L1", L1);
s.SetFloat("L2", L2);
s.SetFloat("m1",m1);
s.SetFloat("m2",m2);
s.SetFloat("g",g);
s.SetFloat("deltaTime", Time.deltaTime*timeScale);
var swap = readBuffer;
readBuffer = writeBuffer;
writeBuffer = swap;
s.Dispatch(kernelIndex, Mathf.CeilToInt(objectCount/32.0f), 1, 1);
s.Dispatch(renderKernelIndex, resolution / 8, resolution / 8, 1);
}
}