diff options
| -rw-r--r-- | 2ner.cginc | 6 | ||||
| -rw-r--r-- | 2ner.shader | 6 | ||||
| -rw-r--r-- | Editor/generate_3d_noise.cs | 160 | ||||
| -rw-r--r-- | features.cginc | 4 | ||||
| -rw-r--r-- | fog.cginc | 16 | ||||
| -rw-r--r-- | globals.cginc | 6 |
6 files changed, 186 insertions, 12 deletions
@@ -189,7 +189,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.tangent = UnityObjectToWorldNormal(i.tangent);
i.binormal = UnityObjectToWorldNormal(i.binormal);
-#if defined(_RAYMARCHED_FOG)
+#if defined(_RAYMARCHED_FOG) && defined(FORWARD_BASE_PASS)
{
FogParams fog_params = {
_Raymarched_Fog_Steps,
@@ -199,6 +199,10 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace _Raymarched_Fog_Dithering_Noise_TexelSize,
_Raymarched_Fog_Density_Noise,
_Raymarched_Fog_Density_Noise_Scale,
+ _Raymarched_Fog_Velocity.xyz,
+ #if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT)
+ _Raymarched_Fog_Density_Exponent,
+ #endif
#if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY)
_Raymarched_Fog_Height_Density_Min,
_Raymarched_Fog_Height_Density_Max,
diff --git a/2ner.shader b/2ner.shader index bff1cb0..471c84c 100644 --- a/2ner.shader +++ b/2ner.shader @@ -826,6 +826,12 @@ Shader "yum_food/2ner" _Raymarched_Fog_Density_Noise("Density noise", 3D) = "black" {} _Raymarched_Fog_Density_Noise_Scale("Density noise scale", Vector) = (1, 1, 1, 0) _Raymarched_Fog_Y_Cutoff("Y cutoff", Float) = -1000 + _Raymarched_Fog_Velocity("Velocity", Vector) = (1, -.2, 0, 0) + + [HideInInspector] m_start_Raymarched_Fog_Density_Exponent("Density exponent", Float) = 0 + [ThryToggle(_RAYMARCHED_FOG_DENSITY_EXPONENT)] _Raymarched_Fog_Density_Exponent_Enabled("Enable", Float) = 0 + _Raymarched_Fog_Density_Exponent("Exponent", Float) = 1 + [HideInInspector] m_end_Raymarched_Fog_Density_Exponent("Density exponent", Float) = 0 [HideInInspector] m_start_Raymarched_Fog_Height_Density("Height density", Float) = 0 [ThryToggle(_RAYMARCHED_FOG_HEIGHT_DENSITY)] _Raymarched_Fog_Height_Density_Enabled("Enable", Float) = 0 diff --git a/Editor/generate_3d_noise.cs b/Editor/generate_3d_noise.cs index 437b0aa..1a0dca0 100644 --- a/Editor/generate_3d_noise.cs +++ b/Editor/generate_3d_noise.cs @@ -19,6 +19,12 @@ public class WhiteNoiseTextureGenerator : EditorWindow 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() @@ -35,6 +41,19 @@ public class WhiteNoiseTextureGenerator : EditorWindow 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")) { @@ -52,6 +71,43 @@ public class WhiteNoiseTextureGenerator : EditorWindow { 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++) @@ -67,14 +123,102 @@ public class WhiteNoiseTextureGenerator : EditorWindow } texture.SetPixels(colors); - texture.Apply(); - - string path = $"Assets/{textureName}.asset"; - AssetDatabase.CreateAsset(texture, path); - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - - EditorUtility.DisplayDialog("Success", $"White noise texture generated and saved at {path}", "OK"); + } + + 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() diff --git a/features.cginc b/features.cginc index 59a3ff3..79bed6c 100644 --- a/features.cginc +++ b/features.cginc @@ -319,5 +319,9 @@ #pragma shader_feature_local _RAYMARCHED_FOG_HEIGHT_DENSITY //endex +//ifex _Raymarched_Fog_Density_Exponent_Enabled==0 +#pragma shader_feature_local _RAYMARCHED_FOG_DENSITY_EXPONENT +//endex + #endif // __FEATURES_INC @@ -16,6 +16,10 @@ struct FogParams { float4 dithering_noise_texelsize; texture3D density_noise; float4 density_noise_scale; + float3 velocity; +#if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT) + float density_exponent; +#endif #if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY) float height_density_min; float height_density_max; @@ -68,8 +72,15 @@ FogResult raymarched_fog(v2f i, FogParams p) [loop] for (uint ii = 0; ii < p.steps; ++ii) { pp += step_size * rd; + + float3 noise_coord = (pp + _Time[0] * p.velocity) * p.density_noise_scale.xyz; + float cur_d = p.density_noise.SampleLevel(linear_repeat_s, - pp * p.density_noise_scale.xyz, 0); + noise_coord, 0); + +#if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT) + cur_d = pow(cur_d, p.density_exponent); +#endif cur_d *= p.density * step_size; @@ -77,8 +88,7 @@ FogResult raymarched_fog(v2f i, FogParams p) // Apply height-based density (branchless) float t = saturate((pp.y - p.height_density_min) / (p.height_density_max - p.height_density_min)); - float height_factor = pow(1.0 - t, p.height_density_power); - cur_d *= height_factor; + cur_d *= 1.0 - t; #endif cur_d = saturate(cur_d); diff --git a/globals.cginc b/globals.cginc index 341e08b..2768202 100644 --- a/globals.cginc +++ b/globals.cginc @@ -544,9 +544,15 @@ float4 _Raymarched_Fog_Dithering_Noise_TexelSize; texture3D _Raymarched_Fog_Density_Noise;
float4 _Raymarched_Fog_Density_Noise_Scale;
float _Raymarched_Fog_Y_Cutoff;
+float3 _Raymarched_Fog_Velocity;
+#if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT)
+float _Raymarched_Fog_Density_Exponent;
+#endif
+#if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY)
float _Raymarched_Fog_Height_Density_Min;
float _Raymarched_Fog_Height_Density_Max;
float _Raymarched_Fog_Height_Density_Power;
+#endif
#endif // _RAYMARCHED_FOG
#endif // __GLOBALS_INC
|
