-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathSplineToTexture.cs
More file actions
449 lines (373 loc) · 14.8 KB
/
SplineToTexture.cs
File metadata and controls
449 lines (373 loc) · 14.8 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System.IO;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
#if SPLINE_PACKAGE_INSTALLED
#if UNITY_EDITOR
using UnityEditor.Splines;
#endif
using UnityEngine.Splines;
#endif
[ExecuteInEditMode]
public class SplineToTexture : MonoBehaviour
{
#if SPLINE_PACKAGE_INSTALLED
public SplineContainer splineContainer;
#endif
public ComputeShader computeShader;
public WaterSurface waterSurface;
public Material waterDecalMaterial;
public float splineWidth = 20;
public float splineBlendWidth = 5;
public int searchStepsPerCurve = 8;
public Vector2Int resolution = new Vector2Int(256,256);
public int currentMapBlurSize = 8;
public int heightMapBlurSize = 8;
public Vector2 heightMapMinMax = new Vector2(0, 1);
public RenderTexture currentMap = null;
public RenderTexture heightMap = null;
private int currentHandle = -1;
private int heightHandle = -1;
private int blurHandle = -1;
private int fillWithNeutralHandle = -1;
private bool isInitialized = false;
#if SPLINE_PACKAGE_INSTALLED
private Bounds highestBounds = new Bounds();
#endif
private int curvesCount = 0;
ComputeBuffer buffer;
float4[] array;
void OnEnable()
{
Init();
}
void HookCallbacks()
{
#if SPLINE_PACKAGE_INSTALLED && UNITY_EDITOR
if (splineContainer != null)
foreach (Spline spline in splineContainer.Splines)
EditorSplineUtility.AfterSplineWasModified += OnAfterSplineWasModified;
#endif
}
void UnhookCallbacks()
{
#if SPLINE_PACKAGE_INSTALLED && UNITY_EDITOR
if (splineContainer != null)
foreach (Spline spline in splineContainer.Splines)
EditorSplineUtility.AfterSplineWasModified -= OnAfterSplineWasModified;
#endif
}
void OnDisable()
{
isInitialized = false;
UnhookCallbacks();
}
#if SPLINE_PACKAGE_INSTALLED
private void OnAfterSplineWasModified(Spline spline)
{
Refresh();
}
#endif
void Init()
{
#if SPLINE_PACKAGE_INSTALLED
if (splineContainer == null)
splineContainer = this.GetComponent<SplineContainer>();
#endif
HookCallbacks();
InitRenderTexture();
InitComputeBuffer(GetCurveCount());
isInitialized = true;
Refresh();
}
private int GetCurveCount()
{
// This is to avoid creating a zero length compute buffer when there's no spline package installed.
int c = 1;
#if SPLINE_PACKAGE_INSTALLED
for (int i = 0; i < splineContainer.Splines.Count; i++)
c += splineContainer[i].GetCurveCount();
#endif
return c;
}
private void InitComputeBuffer(int count)
{
// *16 because there's 4 vector4 per curve.
buffer = new ComputeBuffer(count, sizeof(float) * 16);
// *4 because there's 4 float4 to define a curve;
array = new float4[count * 4];
}
private void InitRenderTexture(bool current = true, bool height = true)
{
if (current)
{
if (currentMap != null)
currentMap.Release();
currentMap = new RenderTexture(resolution.x, resolution.y, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
currentMap.enableRandomWrite = true;
currentMap.Create();
}
if (height)
{
if (heightMap != null)
heightMap.Release();
heightMap = new RenderTexture(resolution.x, resolution.y, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
heightMap.enableRandomWrite = true;
heightMap.Create();
}
}
void Refresh()
{
if(!isInitialized) return;
if (currentMap != null)
{
if (currentMap.width != resolution.x || currentMap.height != resolution.y)
InitRenderTexture(true, false);
}
if (heightMap != null)
{
if (heightMap.width != resolution.x || heightMap.height != resolution.y)
InitRenderTexture(false, true);
}
UpdateSpline();
RunCompute();
}
void Update()
{
waterDecalMaterial?.SetTexture("_CurrentMap", currentMap);
waterDecalMaterial?.SetTexture("_Deformation_Texture", heightMap);
// Uncomment this to update everyframe
Refresh();
}
private void UpdateSpline()
{
int count = GetCurveCount();
if (count != curvesCount)
{
curvesCount = count;
InitComputeBuffer(curvesCount);
}
#if SPLINE_PACKAGE_INSTALLED
int currentIndex = 0;
// For each spline
for (int i = 0; i < splineContainer.Splines.Count; i++)
{
var spline = splineContainer[i];
int currentCurveCount = spline.GetCurveCount();
for (int j = 0; j < currentCurveCount; j++)
{
var curve = spline.GetCurve(j);
array[currentIndex + 0] = (Vector4)(Vector3)curve.P0;
array[currentIndex + 1] = (Vector4)(Vector3)curve.P1;
array[currentIndex + 2] = (Vector4)(Vector3)curve.P2;
array[currentIndex + 3] = (Vector4)(Vector3)curve.P3;
currentIndex += 4;
}
if(buffer == null)
Debug.Log("buffer is null");
if(array == null)
Debug.Log("array is null");
buffer.SetData(array);
}
// This is for heightMap only
float amplitudeOfTheSteepestSpline = 0;
highestBounds = new Bounds();
foreach (Spline spline in splineContainer.Splines)
{
Bounds b = spline.GetBounds();
float amplitude = b.max.y - b.min.y;
if (amplitude > amplitudeOfTheSteepestSpline)
{
amplitudeOfTheSteepestSpline = amplitude;
highestBounds = b;
}
}
#endif
}
public void RunCompute()
{
if (computeShader != null)
{
if(fillWithNeutralHandle < 0)
fillWithNeutralHandle = computeShader.FindKernel("FillWithNeutral");
if (currentHandle < 0)
currentHandle = computeShader.FindKernel("GenerateCurrent");
if (heightHandle < 0)
heightHandle = computeShader.FindKernel("GenerateHeight");
if (blurHandle < 0)
blurHandle = computeShader.FindKernel("MotionBlur");
if (currentMap == null || heightMap == null)
InitRenderTexture();
// Fill with neutral kernel
// First, a neutral color is set on each texture, #FF80FF for current map
computeShader.SetVector("neutralColor", new Vector4(1, 0.5f, 1, 0));
computeShader.SetTexture(fillWithNeutralHandle, "result", currentMap);
computeShader.Dispatch(fillWithNeutralHandle, resolution.x / 8, resolution.y / 8, 1);
#if SPLINE_PACKAGE_INSTALLED
// Generate Current kernel
// We need to pass all those information to this pass for the compute shader to be able to match the correct position and scale.
// Then, for each texture to generate, we iterate through each pixel to find the closest matching point on any of the splines.
// By evaluating the position and tangent of the closest point, we can calculate a color for the current pixel.
computeShader.SetVector("WaterSurfaceLocalScale", waterSurface != null ? waterSurface.transform.localScale : Vector3.one);
computeShader.SetVector("WaterSurfacePosition", waterSurface != null ? waterSurface.transform.position : Vector3.zero);
computeShader.SetVector("resolution", new Vector4(resolution.x, resolution.y, 0, 0));
computeShader.SetFloat("curvesCount", curvesCount);
computeShader.SetFloat("splineWidth", splineWidth);
computeShader.SetFloat("splineBlendWidth", splineBlendWidth);
computeShader.SetFloat("stepsPerCurve", searchStepsPerCurve);
computeShader.SetBuffer(currentHandle, "curvesData", buffer);
computeShader.SetTexture(currentHandle, "result", currentMap);
computeShader.Dispatch(currentHandle, resolution.x / 8, resolution.y / 8, 1);
// Directional Blur kernel
// Lastly, to avoid blocky artifacts, a blur pass is done in the direction of the current to smooth out the result.
if (currentMapBlurSize > 0)
{
computeShader.SetFloat("blurSize", currentMapBlurSize);
computeShader.SetTexture(blurHandle, "currentMap", currentMap);
computeShader.SetTexture(blurHandle, "result", currentMap);
computeShader.Dispatch(blurHandle, resolution.x / 8, resolution.y / 8, 1);
}
#endif
// Fill with neutral kernel
// First, a neutral color is set on each texture, #808080 for height map map
computeShader.SetVector("neutralColor", new Vector4(0.5f, 0.5f, 0.5f, 0.5f));
computeShader.SetTexture(fillWithNeutralHandle, "result", heightMap);
computeShader.Dispatch(fillWithNeutralHandle, resolution.x / 8, resolution.y / 8, 1);
#if SPLINE_PACKAGE_INSTALLED
// Generate height kernel
// We need to pass all those information to this pass for the compute shader to be able to match the correct position and scale.
// Then, for each texture to generate, we iterate through each pixel to find the closest matching point on any of the splines.
// By evaluating the position and tangent of the closest point, we can calculate a color for the current pixel.
computeShader.SetVector("centerBounds", highestBounds.center);
computeShader.SetVector("extentsBounds", highestBounds.extents);
computeShader.SetVector("maxBounds", highestBounds.max);
computeShader.SetVector("minBounds", highestBounds.min);
computeShader.SetVector("sizeBounds", highestBounds.size);
computeShader.SetVector("minMaxHeight", heightMapMinMax);
computeShader.SetBuffer(heightHandle, "curvesData", buffer);
computeShader.SetTexture(heightHandle, "result", heightMap);
computeShader.Dispatch(heightHandle, resolution.x / 8, resolution.y / 8, 1);
// Directional Blur kernel
// Lastly, to avoid blocky artifacts, a blur pass is done in the direction of the current to smooth out the result.
// In this example, the deformation map is not blurred because it specifically needs a steep dropoff at the waterfall.
if (heightMapBlurSize > 0)
{
computeShader.SetFloat("blurSize", heightMapBlurSize);
computeShader.SetTexture(blurHandle, "currentMap", currentMap);
computeShader.SetTexture(blurHandle, "result", heightMap);
computeShader.Dispatch(blurHandle, resolution.x / 8, resolution.y / 8, 1);
}
#endif
}
}
#if UNITY_EDITOR
public void OpenDialogAndSaveCurrentMap()
{
var path = EditorUtility.SaveFilePanel("Save current map", "","currentMap","png");
if (path.Length != 0)
SaveTextureOnDisk(currentMap, path);
}
public void OpenDialogAndSaveHeightMap()
{
var path = EditorUtility.SaveFilePanel("Save height map", "", "heightMap", "png");
if (path.Length != 0)
SaveTextureOnDisk(heightMap, path);
}
public static void SaveTextureOnDisk(RenderTexture renderTexture, string path)
{
Texture2D tex = ToTexture2D(renderTexture);
var bytesHeight = tex.EncodeToPNG();
File.WriteAllBytes(path, bytesHeight);
AssetDatabase.Refresh();
}
#endif
public static Texture2D ToTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.ARGB32, false, true);
// ReadPixels looks at the active RenderTexture.
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
return tex;
}
// This move the spline knots so that the spline gameobject is at the origin with a scale of 1 to simplify the calculations.
public void ApplyScaleAndPosition()
{
#if SPLINE_PACKAGE_INSTALLED
foreach (Spline spline in splineContainer.Splines)
{
BezierKnot[] array = spline.ToArray();
// Apply Scale
for (int i = 0; i < array.Length; i++)
{
array[i].Position *= this.transform.localScale;
array[i].TangentIn *= this.transform.localScale;
array[i].TangentOut *= this.transform.localScale;
}
// Apply position
for (int i = 0; i < array.Length; i++)
{
array[i].Position += new float3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
}
spline.Clear();
for (int i = 0; i < array.Length; i++)
spline.Add(array[i]);
}
this.transform.position = Vector3.zero;
this.transform.localScale = Vector3.one;
#endif
}
void OnValidate()
{
if (splineWidth < 0)
splineWidth = 0;
if (splineBlendWidth < 0)
splineBlendWidth = 0;
if (searchStepsPerCurve < 0)
searchStepsPerCurve = 0;
if (resolution.x < 0)
resolution.x = 0;
if (resolution.y < 0)
resolution.y = 0;
if (currentMapBlurSize < 0)
currentMapBlurSize = 0;
if (heightMapBlurSize < 0)
heightMapBlurSize = 0;
if (heightMapBlurSize < 0)
heightMapBlurSize = 0;
heightMapMinMax.x = Mathf.Clamp01(heightMapMinMax.x);
heightMapMinMax.y = Mathf.Clamp01(heightMapMinMax.y);
if (currentMap != null)
{
if (currentMap.width != resolution.x || currentMap.height != resolution.y)
InitRenderTexture(true, false);
}
if (heightMap != null)
{
if (heightMap.width != resolution.x || heightMap.height != resolution.y)
InitRenderTexture(false, true);
}
if (isInitialized)
Refresh();
}
// This is to ensure the script is called every update in Edit Mode.
void OnDrawGizmos()
{
#if UNITY_EDITOR
// Ensure continuous Update calls.
if (!Application.isPlaying)
{
UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
UnityEditor.SceneView.RepaintAll();
}
#endif
}
void OnDestroy()
{
currentMap?.Release();
heightMap?.Release();
buffer?.Dispose();
UnhookCallbacks();
}
}