Compute Shader生成噪声


噪声生成工具

尝试一下ta的另一个方向:写Tools,正好也写写computeshader ,去做一个噪声生成工具。

Window、GUI布局

脚本继承自EditorWindow之后,使用一些API去把窗口需要的选项做出来就好。

1
2
3
4
5
6
7
[MenuItem("Tools/Noise Tool")]
static void CreateWindow()
{
Rect size = new Rect(0, 0, 400, 600);
NoiseGenTool window = (NoiseGenTool)EditorWindow.GetWindowWithRect(typeof(NoiseGenTool), size, true);
window.Show();
}

窗口中的选择选项布局:

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
public enum NoiseType { Worley, Perlin, Simplex};
public enum TextureSize { x64 = 64, x128 = 128, x256 = 256, x512 = 512, x1024 = 1024, x2048 = 2048 };

private ComputeShader noiseShader;
private NoiseType noiseType = NoiseType.Perlin;
private RenderTextureFormat format = RenderTextureFormat.ARGB32;
private TextureSize size = TextureSize.x512;
private float noiseScale = 10f;

//在void OnGUI里面
noiseShader = EditorGUILayout.ObjectField("Compute Noise Shader:", noiseShader, typeof(ComputeShader), true) as ComputeShader;
noiseType = (NoiseType)EditorGUILayout.EnumPopup("Noise Type:", noiseType);
format = (RenderTextureFormat)EditorGUILayout.EnumPopup("Texture Format:", format);
size = (TextureSize)EditorGUILayout.EnumPopup("Texture Size:", size);
noiseScale = EditorGUILayout.Slider("Noise Scale:", noiseScale, 20f, 200f);

if (GUILayout.Button("Generate Noise Texture"))
{
if (noiseShader == null)
{
ShowNotification(new GUIContent("Noise Shader is null!!"));
}
else
{
Generate();
}
}

if (GUILayout.Button("Save Texture"))
{
if (renderTexture == null)
{
ShowNotification(new GUIContent("Texture is null!!"));
}
else
{
SaveTexture();
AssetDatabase.Refresh();
ShowNotification(new GUIContent("Successfully saved!!"));
}
}

Compute Shader

这里最重要的就是如何使用compute shader,所有的计算逻辑我们都写在Compute shader里面,可以通过传入一个整数来确定具体是生成什么噪声:

具体生成各种噪声的函数直接写好了直接调用就好,后面可以拓展更多类型的噪声生成,

关于save的逻辑:

生成一个RenderTexture,然后启动compute shader计算:

后面会继续拓展,做成一个能生2D、3D的各种噪声的噪声生成工具,并且让美术能有更多调变的参数,目前的工作进度:

后面抽个时间找些Paper来看看,专门研究一下各种噪声的生成,然后完成这个Tool


Author: cdc
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source cdc !
  TOC