summaryrefslogtreecommitdiffstats
path: root/Editor/generate_3d_noise.cs
blob: 1a0dca0f44152c6534286609ecac0cd5645471ff (plain)
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
// !! AI ARTIFACT !!
// This code was originally generated by Claude 3.5 Sonnet.
using UnityEngine;
using UnityEditor;

public enum NoiseType
{
    OneDimensional,
    TwoDimensional,
    ThreeDimensional,
    FourDimensional,
    NormalizedThreeDimensional
}

public class WhiteNoiseTextureGenerator : EditorWindow
{
    private int textureWidth = 32;
    private int textureHeight = 32;
    private int textureDepth = 32;
    private string textureName = "WhiteNoiseTexture";
    private NoiseType noiseType = NoiseType.ThreeDimensional;
    
    // Domain warping parameters
    private bool enableDomainWarping = false;
    private int domainWarpingOctaves = 2;
    private float domainWarpingStrength = 0.1f;
    private float domainWarpingScale = 0.5f;

    [MenuItem("Tools/yum_food/White Noise Texture Generator")]
    public static void ShowWindow()
    {
        GetWindow<WhiteNoiseTextureGenerator>("White Noise Texture Generator");
    }

    private void OnGUI()
    {
        GUILayout.Label("White Noise Texture Generator", EditorStyles.boldLabel);

        textureWidth = EditorGUILayout.IntField("Texture Width", textureWidth);
        textureHeight = EditorGUILayout.IntField("Texture Height", textureHeight);
        textureDepth = EditorGUILayout.IntField("Texture Depth", textureDepth);
        textureName = EditorGUILayout.TextField("Texture Name", textureName);
        noiseType = (NoiseType)EditorGUILayout.EnumPopup("Noise Type", noiseType);
        
        EditorGUILayout.Space();
        GUILayout.Label("Domain Warping", EditorStyles.boldLabel);
        enableDomainWarping = EditorGUILayout.Toggle("Enable Domain Warping", enableDomainWarping);
        
        if (enableDomainWarping)
        {
            EditorGUI.indentLevel++;
            domainWarpingOctaves = EditorGUILayout.IntSlider("Octaves", domainWarpingOctaves, 1, 10);
            domainWarpingStrength = EditorGUILayout.Slider("Strength", domainWarpingStrength, 0f, 10f);
            domainWarpingScale = EditorGUILayout.Slider("Scale", domainWarpingScale, 0.1f, 10f);
            EditorGUI.indentLevel--;
        }

        if (GUILayout.Button("Generate Texture"))
        {
            if (textureWidth <= 0 || textureHeight <= 0 || textureDepth <= 0)
            {
                EditorUtility.DisplayDialog("Error", "Texture dimensions must be greater than zero.", "OK");
                return;
            }

            GenerateWhiteNoiseTexture();
        }
    }

    private void GenerateWhiteNoiseTexture()
    {
        TextureFormat format = GetTextureFormat();
        Texture3D texture = new Texture3D(textureWidth, textureHeight, textureDepth, format, false);
        
        if (enableDomainWarping)
        {
            GenerateWithDomainWarping(texture);
        }
        else
        {
            GenerateSimpleNoise(texture);
        }

        texture.Apply();

        string path = $"Assets/{textureName}.asset";
        
        // Check if asset already exists
        Texture3D existingTexture = AssetDatabase.LoadAssetAtPath<Texture3D>(path);
        if (existingTexture != null)
        {
            // Copy new texture data to existing asset to preserve GUID
            EditorUtility.CopySerialized(texture, existingTexture);
            EditorUtility.SetDirty(existingTexture);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("Success", $"White noise texture updated at {path}", "OK");
        }
        else
        {
            // Create new asset
            AssetDatabase.CreateAsset(texture, path);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("Success", $"White noise texture generated and saved at {path}", "OK");
        }
    }
    
    private void GenerateSimpleNoise(Texture3D texture)
    {
        Color[] colors = new Color[textureWidth * textureHeight * textureDepth];

        for (int z = 0; z < textureDepth; z++)
        {
            for (int y = 0; y < textureHeight; y++)
            {
                for (int x = 0; x < textureWidth; x++)
                {
                    int index = x + y * textureWidth + z * textureWidth * textureHeight;
                    colors[index] = GenerateColor();
                }
            }
        }

        texture.SetPixels(colors);
    }
    
    private void GenerateWithDomainWarping(Texture3D texture)
    {
        // First pass: generate base noise
        Color[] baseColors = new Color[textureWidth * textureHeight * textureDepth];
        for (int i = 0; i < baseColors.Length; i++)
        {
            baseColors[i] = GenerateColor();
        }
        
        // Second pass: apply domain warping
        Color[] warpedColors = new Color[textureWidth * textureHeight * textureDepth];
        
        for (int z = 0; z < textureDepth; z++)
        {
            for (int y = 0; y < textureHeight; y++)
            {
                for (int x = 0; x < textureWidth; x++)
                {
                    Vector3 coord = new Vector3(
                        (float)x / textureWidth,
                        (float)y / textureHeight,
                        (float)z / textureDepth
                    );
                    
                    // Apply domain warping
                    for (int octave = 0; octave < domainWarpingOctaves; octave++)
                    {
                        Vector3 sampleCoord = coord * domainWarpingScale;
                        Color warpValue = SampleTexture(baseColors, sampleCoord);
                        
                        // Convert color to offset vector
                        Vector3 offset = new Vector3(
                            warpValue.r * 2f - 1f,
                            warpValue.g * 2f - 1f,
                            warpValue.b * 2f - 1f
                        ) * domainWarpingStrength;
                        
                        coord += offset;
                    }
                    
                    // Sample final color at warped coordinate
                    int index = x + y * textureWidth + z * textureWidth * textureHeight;
                    warpedColors[index] = SampleTexture(baseColors, coord);
                }
            }
        }
        
        texture.SetPixels(warpedColors);
    }
    
    private Color SampleTexture(Color[] colors, Vector3 coord)
    {
        // Wrap coordinates
        coord.x = Mathf.Repeat(coord.x, 1f);
        coord.y = Mathf.Repeat(coord.y, 1f);
        coord.z = Mathf.Repeat(coord.z, 1f);
        
        // Convert to texture space
        float fx = coord.x * (textureWidth - 1);
        float fy = coord.y * (textureHeight - 1);
        float fz = coord.z * (textureDepth - 1);
        
        // Trilinear interpolation
        int x0 = Mathf.FloorToInt(fx);
        int y0 = Mathf.FloorToInt(fy);
        int z0 = Mathf.FloorToInt(fz);
        int x1 = (x0 + 1) % textureWidth;
        int y1 = (y0 + 1) % textureHeight;
        int z1 = (z0 + 1) % textureDepth;
        
        float dx = fx - x0;
        float dy = fy - y0;
        float dz = fz - z0;
        
        // Sample 8 corners
        Color c000 = colors[x0 + y0 * textureWidth + z0 * textureWidth * textureHeight];
        Color c001 = colors[x0 + y0 * textureWidth + z1 * textureWidth * textureHeight];
        Color c010 = colors[x0 + y1 * textureWidth + z0 * textureWidth * textureHeight];
        Color c011 = colors[x0 + y1 * textureWidth + z1 * textureWidth * textureHeight];
        Color c100 = colors[x1 + y0 * textureWidth + z0 * textureWidth * textureHeight];
        Color c101 = colors[x1 + y0 * textureWidth + z1 * textureWidth * textureHeight];
        Color c110 = colors[x1 + y1 * textureWidth + z0 * textureWidth * textureHeight];
        Color c111 = colors[x1 + y1 * textureWidth + z1 * textureWidth * textureHeight];
        
        // Interpolate
        Color c00 = Color.Lerp(c000, c001, dz);
        Color c01 = Color.Lerp(c010, c011, dz);
        Color c10 = Color.Lerp(c100, c101, dz);
        Color c11 = Color.Lerp(c110, c111, dz);
        
        Color c0 = Color.Lerp(c00, c01, dy);
        Color c1 = Color.Lerp(c10, c11, dy);
        
        return Color.Lerp(c0, c1, dx);
    }

    private TextureFormat GetTextureFormat()
    {
        switch (noiseType)
        {
            case NoiseType.OneDimensional:
                return TextureFormat.R8;
            case NoiseType.TwoDimensional:
                return TextureFormat.RG16;
            case NoiseType.ThreeDimensional:
            case NoiseType.NormalizedThreeDimensional:
                return TextureFormat.RGB48;
            case NoiseType.FourDimensional:
                return TextureFormat.RGBA32;
            default:
                return TextureFormat.RGB24;
        }
    }

    private Color GenerateColor()
    {
        switch (noiseType)
        {
            case NoiseType.OneDimensional:
                return new Color(Random.value, 0, 0, 1);
            case NoiseType.TwoDimensional:
                return new Color(Random.value, Random.value, 0, 1);
            case NoiseType.ThreeDimensional:
                return new Color(Random.value, Random.value, Random.value, 1);
            case NoiseType.FourDimensional:
                return new Color(Random.value, Random.value, Random.value, Random.value);
            case NoiseType.NormalizedThreeDimensional:
                Vector3 normalizedColor = Random.insideUnitSphere.normalized;
                return new Color(normalizedColor.x * 0.5f + 0.5f, normalizedColor.y * 0.5f + 0.5f, normalizedColor.z * 0.5f + 0.5f, 1);
            default:
                return Color.white;
        }
    }
}