From a6868ff3042c420155b7bf180260b2dc1cd64823 Mon Sep 17 00:00:00 2001 From: Reinier Vens Date: Sun, 30 May 2021 05:54:28 +0100 Subject: [PATCH] Volumetric Redux Fancied up the volumetrics --- Asset Sources/Zhestokost.png | 4 +- Asset Sources/Zhestokost.psd | 4 +- Assets/Materials/Fog Marching Main Menu.mat | 6 +- Assets/Materials/Fog Marching.mat | 22 +-- Assets/Materials/UI/Map Gravity Lines.mat | 2 +- .../Fog Patch Displacement.renderTexture | 1 + Assets/Resources/Fog Patch.renderTexture | 1 + .../Fog Surface Displacement.renderTexture | 1 + .../Shaders/Raymarching/VolumeRender.shader | 95 ++---------- Assets/Shaders/Volumetric.cginc | 146 ++++++++++++++++++ Assets/Shaders/Volumetric.cginc.meta | 3 + GameData/PlayerSettings.msgpack | 4 +- 12 files changed, 188 insertions(+), 101 deletions(-) create mode 100644 Assets/Shaders/Volumetric.cginc create mode 100644 Assets/Shaders/Volumetric.cginc.meta diff --git a/Asset Sources/Zhestokost.png b/Asset Sources/Zhestokost.png index 5f35ba9ba..5fbab5b63 100644 --- a/Asset Sources/Zhestokost.png +++ b/Asset Sources/Zhestokost.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:31a93ba3d727b46489acdc9318f03a35108d7d05b4c2fd43adf00728c1c13dc8 -size 8084300 +oid sha256:571141b35738b6eb00b75296ddcc25aeaccbc2287a3c53266c4bfa73095e2a72 +size 8192324 diff --git a/Asset Sources/Zhestokost.psd b/Asset Sources/Zhestokost.psd index e1f572e80..5d9f2e997 100644 --- a/Asset Sources/Zhestokost.psd +++ b/Asset Sources/Zhestokost.psd @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:67bc6406e4967401e0315c55e8083bd86f0f7120d55bb3a0521964163d9e0fc3 -size 47844837 +oid sha256:7df45066244805c7ab431df872df4af18e22bb2f5c2f1ec63487af2d48758ea2 +size 44499927 diff --git a/Assets/Materials/Fog Marching Main Menu.mat b/Assets/Materials/Fog Marching Main Menu.mat index 8cc39728f..49fd25a36 100644 --- a/Assets/Materials/Fog Marching Main Menu.mat +++ b/Assets/Materials/Fog Marching Main Menu.mat @@ -43,6 +43,7 @@ Material: - _AlphaFloor: 0 - _DepthBlend: 512 - _DepthCeiling: 4096 + - _FlowSpeed: 1 - _Gamma: 0.25 - _GridCeilingBlend: -300 - _GridCeilingDensity: 0.01 @@ -54,6 +55,7 @@ Material: - _GridFloorOffset: 27.55 - _GridPatchBlend: 200 - _GridPatchDensity: 3 + - _NoiseExponent: 2 - _NoiseFrequency: 0.01 - _NoiseSpeed: 0.01 - _NoiseStrength: 150 @@ -64,6 +66,8 @@ Material: - _ScatteringMinDist: 1 - _StepExponent: 2 - _TintExponent: 12 + - _TurbulenceAmplitude: 1 + - _TurbulenceScale: 1 m_Colors: - - _Tint: {r: 0.3932651, g: 0.3932651, b: 0.3932651, a: 1} + - _Tint: {r: 0.39215687, g: 0.39215687, b: 0.39215687, a: 1} m_BuildTextureStacks: [] diff --git a/Assets/Materials/Fog Marching.mat b/Assets/Materials/Fog Marching.mat index c20f146c9..6a62d9c3e 100644 --- a/Assets/Materials/Fog Marching.mat +++ b/Assets/Materials/Fog Marching.mat @@ -43,27 +43,31 @@ Material: - _AlphaFloor: 0.01 - _DepthBlend: 512 - _DepthCeiling: 4096 + - _FlowSpeed: 2.5 - _Gamma: 0.5 - _GridCeilingBlend: -300 - _GridCeilingDensity: 0.01 - _GridCeilingFollow: 0 - _GridCeilingOffset: 0 - - _GridFillDensity: 0.000002 + - _GridFillDensity: 0.00002 - _GridFloorBlend: 50 - - _GridFloorDensity: 1 + - _GridFloorDensity: 3 - _GridFloorOffset: 0 - - _GridPatchBlend: 5 - - _GridPatchDensity: 0.2 - - _NoiseFrequency: 0.01 - - _NoiseSpeed: 0.02 - - _NoiseStrength: 100 + - _GridPatchBlend: 50 + - _GridPatchDensity: 0.5 + - _NoiseExponent: 1 + - _NoiseFrequency: 0.02 + - _NoiseSpeed: 0 + - _NoiseStrength: 250 - _SafetyDistance: 53.73 - _Scattering: 1.25 - _ScatteringDensityExponent: 0.5 - _ScatteringDistExponent: 0.5 - _ScatteringMinDist: 500 - _StepExponent: 2 - - _TintExponent: 8 + - _TintExponent: 9 + - _TurbulenceAmplitude: 25 + - _TurbulenceScale: 100 m_Colors: - - _Tint: {r: 0.29799998, g: 0.29799998, b: 0.29799998, a: 1} + - _Tint: {r: 6.525489, g: 6.525489, b: 6.525489, a: 1} m_BuildTextureStacks: [] diff --git a/Assets/Materials/UI/Map Gravity Lines.mat b/Assets/Materials/UI/Map Gravity Lines.mat index 4ac4d5dfd..1841af6a4 100644 --- a/Assets/Materials/UI/Map Gravity Lines.mat +++ b/Assets/Materials/UI/Map Gravity Lines.mat @@ -86,7 +86,7 @@ Material: - _Mode: 0 - _OcclusionStrength: 1 - _Parallax: 0.02 - - _Scale: 0.7614405 + - _Scale: 4.091124 - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 - _SrcBlend: 1 diff --git a/Assets/Resources/Fog Patch Displacement.renderTexture b/Assets/Resources/Fog Patch Displacement.renderTexture index fecd192fe..b9df14340 100644 --- a/Assets/Resources/Fog Patch Displacement.renderTexture +++ b/Assets/Resources/Fog Patch Displacement.renderTexture @@ -12,6 +12,7 @@ RenderTexture: Hash: 00000000000000000000000000000000 m_ForcedFallbackFormat: 4 m_DownscaleFallback: 0 + m_IsAlphaChannelOptional: 0 serializedVersion: 3 m_Width: 256 m_Height: 256 diff --git a/Assets/Resources/Fog Patch.renderTexture b/Assets/Resources/Fog Patch.renderTexture index edf748309..4cbd4c474 100644 --- a/Assets/Resources/Fog Patch.renderTexture +++ b/Assets/Resources/Fog Patch.renderTexture @@ -12,6 +12,7 @@ RenderTexture: Hash: 00000000000000000000000000000000 m_ForcedFallbackFormat: 4 m_DownscaleFallback: 0 + m_IsAlphaChannelOptional: 0 serializedVersion: 3 m_Width: 512 m_Height: 512 diff --git a/Assets/Resources/Fog Surface Displacement.renderTexture b/Assets/Resources/Fog Surface Displacement.renderTexture index 18056a915..f2c565f70 100644 --- a/Assets/Resources/Fog Surface Displacement.renderTexture +++ b/Assets/Resources/Fog Surface Displacement.renderTexture @@ -12,6 +12,7 @@ RenderTexture: Hash: 00000000000000000000000000000000 m_ForcedFallbackFormat: 4 m_DownscaleFallback: 0 + m_IsAlphaChannelOptional: 0 serializedVersion: 3 m_Width: 512 m_Height: 512 diff --git a/Assets/Shaders/Raymarching/VolumeRender.shader b/Assets/Shaders/Raymarching/VolumeRender.shader index afb5d9279..60af55114 100644 --- a/Assets/Shaders/Raymarching/VolumeRender.shader +++ b/Assets/Shaders/Raymarching/VolumeRender.shader @@ -20,7 +20,11 @@ _DepthBlend("Depth Blend", float) = 100 _NoiseStrength("Noise Strength", float) = 1 _NoiseFrequency("Noise Frequency", float) = 1 + _NoiseExponent("Noise Exponent", float) = 2 _NoiseSpeed("Noise Speed", float) = 1 + _TurbulenceScale("Turbulence Scale", float) = 1 + _TurbulenceAmplitude("Turbulence Amplitude", float) = 1 + _FlowSpeed("Flow Speed", float) = 1 _SafetyDistance("Safety Distance", float) = 20 _Scattering("Scattering", float) = 1 _ScatteringMinDist("ScatteringMinDist", float) = 1 @@ -32,14 +36,9 @@ uniform sampler2D _MainTex; uniform sampler2D _CameraDepthTexture; - - uniform sampler2D _Surface; - uniform sampler2D _Patch; - uniform sampler2D _Displacement; - uniform sampler2D _TintTexture; - //uniform sampler3D _NoiseTex; #include "UnityCG.cginc" + #include "Assets/Shaders/Volumetric.cginc" // the number of volume samples to take #define SAMPLE_COUNT 128 @@ -56,28 +55,12 @@ uniform float _StepExponent; - uniform float3 _GridTransform; - - uniform half4 _Tint; - - uniform half _GridFillDensity, - _GridFloorDensity, - _GridPatchDensity, - _GridFloorOffset, - _GridFloorBlend, - _GridPatchBlend, - _Gamma, - _TintExponent, + uniform half _Gamma, _DepthCeiling, - _DepthBlend, - _NoiseFrequency, - _NoiseStrength, - _NoiseSpeed, _Scattering, _ScatteringDensityExponent, _ScatteringDistExponent, - _ScatteringMinDist, - _SafetyDistance; + _ScatteringMinDist; // Dithering sampler2D _DitheringTex; @@ -88,69 +71,13 @@ return frac(sin(ScreenUVs.x * 12.9898 + ScreenUVs.y * 78.233) * 43758.5453); } - float tri(in float x){return abs(frac(x)-.5);} - float3 tri3(in float3 p){return float3( tri(p.z+tri(p.y*1.)), tri(p.z+tri(p.x*1.)), tri(p.y+tri(p.x*1.)));} - - float triNoise3d(in float3 p) - { - float z=1.4; - float rz = 0.; - float3 bp = p; - for (float i=0.; i<=2.; i++ ) - { - float3 dg = tri3(bp * 2.); - p += dg + _Time.y * _NoiseSpeed; - - bp *= 1.8; - z *= 1.5; - p *= 1.2; - - rz+= (tri(p.z+tri(p.x+tri(p.y))))/z; - bp += 0.14; - } - return rz; - } - - float4 VolumeSampleColor(float3 pos) - { - float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5); - - float surface = tex2Dlod(_Surface, half4(uv, 0, 0)).r; - float dist = pos.y + surface - _GridFloorOffset; - float4 lightTint = tex2Dlod(_TintTexture, half4(uv, 0, dist/30)); - float fillDensity = saturate(-pos.y * _GridFillDensity); - - if(dist < _SafetyDistance) - { - float patch = tex2Dlod(_Patch, half4(uv, 0, 0)).r; - float displacement = tex2Dlod(_Displacement, half4(uv, 0, 0)).r; - - //float noise = tex3D(_NoiseTex, pos*_NoiseFrequency); - float noise = pow(triNoise3d(pos*_NoiseFrequency),2)* _NoiseStrength;// + triNoise3d(pos*_NoiseFrequency*2, _NoiseSpeed * 2) * .5; - pos.y += noise; - float patchDensity = saturate((-abs(pos.y+displacement)+patch)/_GridPatchBlend)*_GridPatchDensity; - float floorDist = -pos.y-surface+_GridFloorOffset; - float floorDensity = floorDist/_GridFloorBlend*_GridFloorDensity; - float fogDensity = patchDensity + max(0,floorDensity); - float alpha = min(max(fogDensity, 0) + fillDensity, .99); - float albedo = pow(1-alpha, _TintExponent) * smoothstep(0,-250,pos.y); - return float4((albedo*_Tint*lightTint).rgb, alpha); - } - - float albedo = pow(1-fillDensity, _TintExponent); - return float4((albedo*_Tint*lightTint).rgb, fillDensity); - } - void RaymarchStep( in float3 pos, in float stepSize, in float weight, inout float4 sum, in float scatter, inout float scatterSum) { - if( sum.a <= 0.99 ) - { - float4 col = VolumeSampleColor( pos ); - col.rgb *= weight * (1.0 - sum.a); + float4 col = VolumeSampleColor( pos ); + col.rgb *= weight * (1.0 - sum.a); - sum += stepSize * col * col.a; - scatterSum += pow(col.a, _ScatteringDensityExponent) * scatter; - } + sum += stepSize * col * col.a; + scatterSum += pow(col.a, _ScatteringDensityExponent) * scatter; } float4 RayMarch( in float3 origin, in float3 direction, in float zbuf, in float2 screenUV, out float scatterSum ) diff --git a/Assets/Shaders/Volumetric.cginc b/Assets/Shaders/Volumetric.cginc new file mode 100644 index 000000000..3bf0114fa --- /dev/null +++ b/Assets/Shaders/Volumetric.cginc @@ -0,0 +1,146 @@ +// +// Value Noise 3D Deriv +// Return value range of 0.0->1.0, with format float4( value, xderiv, yderiv, zderiv ) +// +float4 Value3D_Deriv( float3 P ) +{ + // https://github.com/BrianSharpe/Wombat/blob/master/Value3D_Deriv.glsl + + // establish our grid cell and unit position + float3 Pi = floor(P); + float3 Pf = P - Pi; + float3 Pf_min1 = Pf - 1.0; + + // clamp the domain + Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0; + float3 Pi_inc1 = step( Pi, float3( (69.0 - 1.5).xxx ) ) * ( Pi + 1.0 ); + + // calculate the hash + float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy; + Pt *= Pt; + Pt = Pt.xzxz * Pt.yyww; + float2 hash_mod = float2( 1.0 / ( 635.298681 + float2( Pi.z, Pi_inc1.z ) * 48.500388 ) ); + float4 hash_lowz = frac( Pt * hash_mod.xxxx ); + float4 hash_highz = frac( Pt * hash_mod.yyyy ); + + // blend the results and return + float3 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0); + float3 blendDeriv = Pf * Pf * (Pf * (Pf * 30.0 - 60.0) + 30.0); + float4 res0 = lerp( hash_lowz, hash_highz, blend.z ); + float4 res1 = lerp( res0.xyxz, res0.zwyw, blend.yyxx ); + float4 res3 = lerp( float4( hash_lowz.xy, hash_highz.xy ), float4( hash_lowz.zw, hash_highz.zw ), blend.y ); + float2 res4 = lerp( res3.xz, res3.yw, blend.x ); + return float4( res1.x, 0.0, 0.0, 0.0 ) + ( float4( res1.yyw, res4.y ) - float4( res1.xxz, res4.x ) ) * float4( blend.x, blendDeriv ); + return float4(1,1,1,1); +} + +uniform sampler2D _Surface; +uniform sampler2D _Patch; +uniform sampler2D _Displacement; +uniform sampler2D _TintTexture; + +uniform float3 _GridTransform; + +uniform half4 _Tint; + +uniform half _GridFillDensity, + _GridFloorDensity, + _GridPatchDensity, + _GridFloorOffset, + _GridFloorBlend, + _GridPatchBlend, + _TintExponent, + _DepthBlend, + _NoiseFrequency, + _NoiseExponent, + _NoiseStrength, + _NoiseSpeed, + _TurbulenceScale, + _TurbulenceAmplitude, + _FlowSpeed, + _SafetyDistance; + +float tri(in float x){return abs(frac(x)-.5);} +float3 tri3(in float3 p){return float3( tri(p.z+tri(p.y*1.)), tri(p.z+tri(p.x*1.)), tri(p.y+tri(p.x*1.)));} + +float triNoise3d(in float3 p) +{ + float z=1.4; + float rz = 0.; + float3 bp = p; + for (float i=0.; i<=2.; i++ ) + { + float3 dg = tri3(bp * 2.); + p += dg + _Time.y * _NoiseSpeed; + + bp *= 1.8; + z *= 1.5; + p *= 1.2; + + rz+= (tri(p.z+tri(p.x+tri(p.y))))/z; + bp += 0.14; + } + return rz; +} + +float parabola( float x, float k ) +{ + return pow( 4.0*x*(1.0-x), k ); +} + +float f(float3 pos) +{ + float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5); + const float surface = tex2Dlod(_Surface, half4(uv, 0, 0)).r; + const float dist = pos.y + surface - _GridFloorOffset; + + const float fillDensity = saturate(-pos.y * _GridFillDensity); + + // if(dist < _SafetyDistance) + // { + const float patch = tex2Dlod(_Patch, half4(uv, 0, 0)).r; + const float displacement = tex2Dlod(_Displacement, half4(uv, 0, 0)).r; + + const float patchDensity = saturate((-abs(pos.y+displacement)+patch)/_GridPatchBlend)*_GridPatchDensity; + const float floorDist = -pos.y-surface+_GridFloorOffset; + const float floorDensity = floorDist/_GridFloorBlend*_GridFloorDensity; + const float fogDensity = patchDensity + max(0,floorDensity); + return min(max(fogDensity, 0) + fillDensity, .99); + // } + + // return fillDensity; + +} + +float3 calcNormal( float3 p ) // for function f(p) +{ + const float h = 0.0001; // replace by an appropriate value + const float2 k = float2(1,-1); + return normalize( k.xyy*f( p + k.xyy*h ) + + k.yyx*f( p + k.yyx*h ) + + k.yxy*f( p + k.yxy*h ) + + k.xxx*f( p + k.xxx*h ) ); +} + +float d(float3 pos) +{ + const float4 simplex = Value3D_Deriv( pos / _TurbulenceScale - float3(0,_Time.x,0) ); + const float3 normal = calcNormal(pos); + const float3 turbulence = cross(normalize(simplex.yzw), normal) * _TurbulenceAmplitude; + //const float3 turbulence = (simplex.yzw) * _TurbulenceAmplitude; + const float lerp1 = frac(_Time.x * _FlowSpeed); + const float lerp2 = frac(_Time.x * _FlowSpeed + .5); + const float noise1 = pow(triNoise3d((pos+turbulence * lerp1)*_NoiseFrequency),_NoiseExponent) * _NoiseStrength * parabola(lerp1, 2); + const float noise2 = pow(triNoise3d((pos+turbulence * lerp2)*_NoiseFrequency),_NoiseExponent) * _NoiseStrength * parabola(lerp2, 2); + //pos.y += noise1 + noise2; + return f(pos - normalize(turbulence) * (noise1 + noise2)); +} + +float4 VolumeSampleColor(float3 pos) +{ + float2 uv = -(pos.xz-_GridTransform.xy)/_GridTransform.z + float2(.5,.5); + float density = d(pos); + const float4 tint = tex2Dlod(_TintTexture, half4(uv, 0, 1 / density)) * density; + const float albedo = pow(1-density, _TintExponent) * smoothstep(0,-250,pos.y); + return float4((albedo*_Tint*tint).rgb, density) ; +} diff --git a/Assets/Shaders/Volumetric.cginc.meta b/Assets/Shaders/Volumetric.cginc.meta new file mode 100644 index 000000000..931368647 --- /dev/null +++ b/Assets/Shaders/Volumetric.cginc.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a7177a0485da4145b781163b20f8234f +timeCreated: 1622333679 \ No newline at end of file diff --git a/GameData/PlayerSettings.msgpack b/GameData/PlayerSettings.msgpack index 0ae79375c..7e6d92003 100644 --- a/GameData/PlayerSettings.msgpack +++ b/GameData/PlayerSettings.msgpack @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7c83abaebc4b7fdfb0b53cb926bee1d42413eaf8c142fe992d016619e9804859 -size 28519 +oid sha256:1478a630cae13020cca64ce4d079128be00d6b0183b453ee928841f6ca9747ce +size 46017