Hybrid Volumetric Fog + some swamp area test
This commit is contained in:
84
Assets/Jbl_Shaders/Fog/FogLightTest.shader
Normal file
84
Assets/Jbl_Shaders/Fog/FogLightTest.shader
Normal file
@@ -0,0 +1,84 @@
|
||||
Shader "Custom/VolumetricFog_Full"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_Density("Fog Density", Range(0,1)) = 0.1
|
||||
_LightContribution("Light Contribution", Color) = (1,1,1,1)
|
||||
_LightScattering("Scattering", Range(-1,1)) = 0.0
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderPipeline"="UniversalPipeline" }
|
||||
Pass
|
||||
{
|
||||
HLSLPROGRAM
|
||||
#pragma vertex Vert
|
||||
#pragma fragment Frag
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
||||
|
||||
struct Attributes
|
||||
{
|
||||
float4 positionOS : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct Varyings
|
||||
{
|
||||
float4 positionCS : SV_POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
Varyings Vert(Attributes IN)
|
||||
{
|
||||
Varyings OUT;
|
||||
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
|
||||
OUT.uv = IN.uv;
|
||||
return OUT;
|
||||
}
|
||||
|
||||
float _Density;
|
||||
float4 _LightContribution;
|
||||
float _LightScattering;
|
||||
|
||||
// Henyey-Greenstein phase function
|
||||
float henyey_greenstein(float cosTheta, float g)
|
||||
{
|
||||
float g2 = g * g;
|
||||
return (1.0 - g2) / pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5);
|
||||
}
|
||||
|
||||
half4 Frag(Varyings IN) : SV_Target
|
||||
{
|
||||
// Raymarching setup (tu wstawiasz swój kod raymarchingu)
|
||||
float3 rayPosWS = float3(0,0,0); // przykładowa pozycja w świecie
|
||||
float3 rayDir = float3(0,0,1); // przykładowy kierunek
|
||||
|
||||
float3 fogCol = 0;
|
||||
|
||||
// === Main Light ===
|
||||
Light mainLight = GetMainLight();
|
||||
float phaseMain = henyey_greenstein(dot(rayDir, mainLight.direction), _LightScattering);
|
||||
fogCol += mainLight.color.rgb * _LightContribution.rgb * phaseMain;
|
||||
|
||||
// === Additional Lights ===
|
||||
int addCount = GetAdditionalLightsCount();
|
||||
for (int li = 0; li < addCount; li++)
|
||||
{
|
||||
Light addLight = GetAdditionalLight(li, rayPosWS);
|
||||
float3 dir = normalize(addLight.direction);
|
||||
float atten = addLight.distanceAttenuation * addLight.shadowAttenuation;
|
||||
float phase = henyey_greenstein(dot(rayDir, dir), _LightScattering);
|
||||
|
||||
fogCol += addLight.color.rgb * _LightContribution.rgb * phase * atten;
|
||||
}
|
||||
|
||||
// Final fog color
|
||||
return float4(fogCol * _Density, 1);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Assets/Jbl_Shaders/Fog/FogLightTest.shader.meta
Normal file
9
Assets/Jbl_Shaders/Fog/FogLightTest.shader.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdafb493989dec34a90c23c178dc535b
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
167
Assets/Jbl_Shaders/Fog/Volumetric Fog 2.0 — kopia.txt
Normal file
167
Assets/Jbl_Shaders/Fog/Volumetric Fog 2.0 — kopia.txt
Normal file
@@ -0,0 +1,167 @@
|
||||
Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_ColorLow("Fog Color Low", Color) = (0.6, 0.6, 0.7, 1)
|
||||
_ColorHigh("Fog Color High", Color) = (0.8, 0.8, 0.9, 1)
|
||||
_AmbientFogColor("Ambient Fog Color", Color) = (0.3, 0.3, 0.35, 1)
|
||||
|
||||
_MaxDistance("Max Distance", Float) = 100
|
||||
_StepSize("Step Size", Range(0.1, 20)) = 1
|
||||
_MaxSteps("Max Steps", Range(8, 256)) = 64
|
||||
|
||||
_DensityMultiplier("Density Multiplier", Range(0, 10)) = 1
|
||||
_DensityThreshold("Density Threshold", Range(0, 1)) = 0.1
|
||||
_HeightFalloff("Height Falloff", Range(0, 2)) = 0.2
|
||||
|
||||
_NoiseOffset("Noise Offset", Float) = 0
|
||||
_NoiseTiling("Noise Tiling", Float) = 1
|
||||
_FogNoise("Fog Noise", 3D) = "white" {}
|
||||
|
||||
_NoiseScrollSpeed("Noise Scroll Speed (XYZ)", Vector) = (0.02, 0, 0.015, 0)
|
||||
_NoisePulseSpeed("Noise Pulse Speed", Float) = 0.2
|
||||
_NoisePulseAmount("Noise Pulse Amount", Float) = 0.1
|
||||
|
||||
[HDR]_LightContribution("Light Contribution", Color) = (1, 1, 1, 1)
|
||||
_LightScattering("Light Scattering (g)", Range(-1, 1)) = 0.2
|
||||
|
||||
_DepthFade("Depth Fade Strength", Range(0,1)) = 0.3
|
||||
|
||||
// ✅ Soft clip dla near fade
|
||||
_NearStart("Near Fade Start", Float) = 1.0
|
||||
_NearEnd("Near Fade End", Float) = 3.0
|
||||
|
||||
_DayColor("Day Fog Tint", Color) = (0.7,0.75,0.8,1)
|
||||
_NightColor("Night Fog Tint", Color) = (0.3,0.35,0.4,1)
|
||||
_DayFactor("Day/Night Blend", Range(0,1)) = 0.5
|
||||
_GodRayStrength("God Ray Strength", Range(0,1)) = 0.2
|
||||
|
||||
_DitherStrength("Dither Strength", Range(0,1)) = 0.3
|
||||
_ColorSteps("Color Steps", Range(2,32)) = 8
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
|
||||
|
||||
Pass
|
||||
{
|
||||
HLSLPROGRAM
|
||||
#pragma vertex Vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
|
||||
#pragma multi_compile _ _ADDITIONAL_LIGHTS
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
|
||||
|
||||
float4 _ColorLow, _ColorHigh, _AmbientFogColor;
|
||||
float _MaxDistance, _StepSize; int _MaxSteps;
|
||||
float _DensityMultiplier, _DensityThreshold, _HeightFalloff;
|
||||
float _NoiseOffset, _NoiseTiling; TEXTURE3D(_FogNoise);
|
||||
float3 _NoiseScrollSpeed; float _NoisePulseSpeed, _NoisePulseAmount;
|
||||
float4 _LightContribution; float _LightScattering;
|
||||
|
||||
float _DepthFade;
|
||||
float _NearStart, _NearEnd;
|
||||
float4 _DayColor, _NightColor; float _DayFactor, _GodRayStrength;
|
||||
float _DitherStrength; float _ColorSteps;
|
||||
|
||||
float henyey_greenstein(float cosTheta, float g)
|
||||
{
|
||||
float g2 = g * g;
|
||||
return (1.0 - g2) / (4.0 * PI * pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5));
|
||||
}
|
||||
|
||||
float get_density(float3 worldPos)
|
||||
{
|
||||
float pulse = 1.0 + sin(_Time.y * _NoisePulseSpeed) * _NoisePulseAmount;
|
||||
float3 noiseCoords = worldPos * 0.01 * _NoiseTiling * pulse;
|
||||
noiseCoords += _NoiseScrollSpeed * _Time.y;
|
||||
|
||||
float4 noise = _FogNoise.SampleLevel(sampler_TrilinearRepeat, noiseCoords, 0);
|
||||
float density = noise.r;
|
||||
density = saturate(density - _DensityThreshold) * _DensityMultiplier;
|
||||
density *= exp(-max(0, worldPos.y) * _HeightFalloff);
|
||||
return density;
|
||||
}
|
||||
|
||||
half4 frag(Varyings IN) : SV_Target
|
||||
{
|
||||
float4 col = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, IN.texcoord);
|
||||
float depth = SampleSceneDepth(IN.texcoord);
|
||||
float3 worldPos = ComputeWorldSpacePosition(IN.texcoord, depth, UNITY_MATRIX_I_VP);
|
||||
|
||||
float3 entryPoint = _WorldSpaceCameraPos;
|
||||
float3 viewDir = worldPos - _WorldSpaceCameraPos;
|
||||
float viewLength = length(viewDir);
|
||||
float3 rayDir = normalize(viewDir);
|
||||
|
||||
float2 pixelCoords = IN.texcoord * _BlitTexture_TexelSize.zw;
|
||||
float distLimit = min(viewLength, _MaxDistance);
|
||||
float distTravelled = InterleavedGradientNoise(pixelCoords, (int)(_Time.y / max(HALF_EPS, unity_DeltaTime.x))) * _NoiseOffset;
|
||||
|
||||
float transmittance = 1.0;
|
||||
float3 fogAccum = 0;
|
||||
|
||||
[loop]
|
||||
for (int i = 0; i < _MaxSteps && distTravelled < distLimit; i++)
|
||||
{
|
||||
float3 rayPos = entryPoint + rayDir * distTravelled;
|
||||
float density = get_density(rayPos);
|
||||
|
||||
if (density > 0)
|
||||
{
|
||||
Light mainLight = GetMainLight(TransformWorldToShadowCoord(rayPos));
|
||||
float phase = henyey_greenstein(dot(rayDir, mainLight.direction), _LightScattering);
|
||||
float3 lightCol = mainLight.color.rgb * _LightContribution.rgb * phase * mainLight.shadowAttenuation;
|
||||
|
||||
#if defined(_ADDITIONAL_LIGHTS)
|
||||
uint lightCount = GetAdditionalLightsCount();
|
||||
[loop]
|
||||
for (uint li = 0; li < lightCount; li++)
|
||||
{
|
||||
Light addLight = GetAdditionalLight(li, rayPos);
|
||||
float phaseAdd = henyey_greenstein(dot(rayDir, addLight.direction), _LightScattering);
|
||||
lightCol += addLight.color.rgb * _LightContribution.rgb * phaseAdd * addLight.distanceAttenuation;
|
||||
}
|
||||
#endif
|
||||
|
||||
float3 fogColor = lerp(_ColorLow.rgb, _ColorHigh.rgb, saturate(rayPos.y * 0.01));
|
||||
float3 dayNightTint = lerp(_NightColor.rgb, _DayColor.rgb, _DayFactor);
|
||||
fogColor = lerp(fogColor, dayNightTint, 0.5);
|
||||
fogColor = lerp(fogColor, _AmbientFogColor.rgb, 0.25);
|
||||
|
||||
fogColor += lightCol * _GodRayStrength;
|
||||
|
||||
// ✅ Depth fade
|
||||
float fade = exp(-distTravelled * _DepthFade);
|
||||
|
||||
// ✅ Soft clip near fade
|
||||
float nearFactor = saturate((distTravelled - _NearStart) / (_NearEnd - _NearStart));
|
||||
|
||||
density *= fade * nearFactor;
|
||||
|
||||
fogAccum += (fogColor + lightCol) * density * _StepSize;
|
||||
transmittance *= exp(-density * _StepSize);
|
||||
|
||||
if (transmittance < 0.01) break;
|
||||
}
|
||||
|
||||
distTravelled += _StepSize;
|
||||
}
|
||||
|
||||
float3 finalFog = fogAccum * transmittance;
|
||||
|
||||
float dither = frac(sin(dot(pixelCoords, float2(12.9898,78.233))) * 43758.5453);
|
||||
finalFog += (dither - 0.5) * _DitherStrength.xxx;
|
||||
finalFog = floor(finalFog * _ColorSteps) / _ColorSteps;
|
||||
|
||||
return float4(lerp(col.rgb, finalFog, 1.0 - saturate(transmittance)), 1.0);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ede0440c54f24247bdf517431d7493d
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -27,7 +27,6 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
|
||||
_DepthFade("Depth Fade Strength", Range(0,1)) = 0.3
|
||||
|
||||
// ✅ Soft clip dla near fade
|
||||
_NearStart("Near Fade Start", Float) = 1.0
|
||||
_NearEnd("Near Fade End", Float) = 3.0
|
||||
|
||||
@@ -50,7 +49,6 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
#pragma vertex Vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
|
||||
#pragma multi_compile _ _ADDITIONAL_LIGHTS
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
||||
@@ -69,6 +67,12 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
float4 _DayColor, _NightColor; float _DayFactor, _GodRayStrength;
|
||||
float _DitherStrength; float _ColorSteps;
|
||||
|
||||
// ✅ Custom additional lights (z C# feature)
|
||||
int _CustomAdditionalLightCount;
|
||||
float4 _CustomAdditionalLightPos[16];
|
||||
float4 _CustomAdditionalLightColor[16];
|
||||
float _CustomAdditionalLightRange[16];
|
||||
|
||||
float henyey_greenstein(float cosTheta, float g)
|
||||
{
|
||||
float g2 = g * g;
|
||||
@@ -114,21 +118,25 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
|
||||
if (density > 0)
|
||||
{
|
||||
// Main Light
|
||||
Light mainLight = GetMainLight(TransformWorldToShadowCoord(rayPos));
|
||||
float phase = henyey_greenstein(dot(rayDir, mainLight.direction), _LightScattering);
|
||||
float3 lightCol = mainLight.color.rgb * _LightContribution.rgb * phase * mainLight.shadowAttenuation;
|
||||
|
||||
#if defined(_ADDITIONAL_LIGHTS)
|
||||
uint lightCount = GetAdditionalLightsCount();
|
||||
[loop]
|
||||
for (uint li = 0; li < lightCount; li++)
|
||||
// ✅ Custom Additional Lights
|
||||
for (int li = 0; li < _CustomAdditionalLightCount; li++)
|
||||
{
|
||||
Light addLight = GetAdditionalLight(li, rayPos);
|
||||
float phaseAdd = henyey_greenstein(dot(rayDir, addLight.direction), _LightScattering);
|
||||
lightCol += addLight.color.rgb * _LightContribution.rgb * phaseAdd * addLight.distanceAttenuation;
|
||||
}
|
||||
#endif
|
||||
float3 toLight = _CustomAdditionalLightPos[li].xyz - rayPos;
|
||||
float dist = length(toLight);
|
||||
float3 dir = normalize(toLight);
|
||||
|
||||
float atten = saturate(1.0 - dist / _CustomAdditionalLightRange[li]);
|
||||
float phaseAdd = henyey_greenstein(dot(rayDir, dir), _LightScattering);
|
||||
|
||||
lightCol += _CustomAdditionalLightColor[li].rgb * _LightContribution.rgb * phaseAdd * atten;
|
||||
}
|
||||
|
||||
// Fog color blending
|
||||
float3 fogColor = lerp(_ColorLow.rgb, _ColorHigh.rgb, saturate(rayPos.y * 0.01));
|
||||
float3 dayNightTint = lerp(_NightColor.rgb, _DayColor.rgb, _DayFactor);
|
||||
fogColor = lerp(fogColor, dayNightTint, 0.5);
|
||||
@@ -136,10 +144,7 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
|
||||
fogColor += lightCol * _GodRayStrength;
|
||||
|
||||
// ✅ Depth fade
|
||||
float fade = exp(-distTravelled * _DepthFade);
|
||||
|
||||
// ✅ Soft clip near fade
|
||||
float nearFactor = saturate((distTravelled - _NearStart) / (_NearEnd - _NearStart));
|
||||
|
||||
density *= fade * nearFactor;
|
||||
@@ -160,6 +165,8 @@ Shader "Custom/VolumetricFog_ExtendedFX"
|
||||
finalFog = floor(finalFog * _ColorSteps) / _ColorSteps;
|
||||
|
||||
return float4(lerp(col.rgb, finalFog, 1.0 - saturate(transmittance)), 1.0);
|
||||
return float4(_CustomAdditionalLightColor[0].rgb, 1.0);
|
||||
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
|
||||
198
Assets/Jbl_Shaders/Fog/Volumetric Fog_Hybrid.shader
Normal file
198
Assets/Jbl_Shaders/Fog/Volumetric Fog_Hybrid.shader
Normal file
@@ -0,0 +1,198 @@
|
||||
Shader "Custom/VolumetricFog_Hybrid"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_ColorLow("Fog Color Low", Color) = (0.6, 0.6, 0.7, 1)
|
||||
_ColorHigh("Fog Color High", Color) = (0.8, 0.8, 0.9, 1)
|
||||
_AmbientFogColor("Ambient Fog Color", Color) = (0.3, 0.3, 0.35, 1)
|
||||
|
||||
_MaxDistance("Max Distance", Float) = 100
|
||||
_StepSize("Step Size", Range(0.1, 20)) = 1
|
||||
_MaxSteps("Max Steps", Range(8, 256)) = 64
|
||||
|
||||
_DensityMultiplier("Density Multiplier", Range(0, 10)) = 1
|
||||
_DensityThreshold("Density Threshold", Range(0, 1)) = 0.1
|
||||
_HeightFalloff("Height Falloff", Range(0, 2)) = 0.2
|
||||
|
||||
_NoiseOffset("Noise Offset", Float) = 0
|
||||
_NoiseTiling("Noise Tiling", Float) = 1
|
||||
_FogNoise("Fog Noise", 3D) = "white" {}
|
||||
|
||||
_NoiseScrollSpeed("Noise Scroll Speed (XYZ)", Vector) = (0.02, 0, 0.015, 0)
|
||||
_NoisePulseSpeed("Noise Pulse Speed", Float) = 0.2
|
||||
_NoisePulseAmount("Noise Pulse Amount", Float) = 0.1
|
||||
|
||||
[HDR]_LightContribution("Light Contribution", Color) = (1, 1, 1, 1)
|
||||
_LightScattering("Light Scattering (g)", Range(-1, 1)) = 0.2
|
||||
|
||||
_DepthFade("Depth Fade Strength", Range(0,1)) = 0.3
|
||||
|
||||
_NearStart("Near Fade Start", Float) = 1.0
|
||||
_NearEnd("Near Fade End", Float) = 3.0
|
||||
|
||||
_DayColor("Day Fog Tint", Color) = (0.7,0.75,0.8,1)
|
||||
_NightColor("Night Fog Tint", Color) = (0.3,0.35,0.4,1)
|
||||
_DayFactor("Day/Night Blend", Range(0,1)) = 0.5
|
||||
_GodRayStrength("God Ray Strength", Range(0,1)) = 0.2
|
||||
|
||||
_DitherStrength("Dither Strength", Range(0,1)) = 0.3
|
||||
_ColorSteps("Color Steps", Range(2,32)) = 8
|
||||
|
||||
// Sterowanie zależnością od wysokości
|
||||
_HeightModeBlend("Height Mode Blend (0=World,1=Camera)", Range(0,1)) = 1
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
|
||||
|
||||
Pass
|
||||
{
|
||||
HLSLPROGRAM
|
||||
#pragma vertex Vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
|
||||
|
||||
float4 _ColorLow, _ColorHigh, _AmbientFogColor;
|
||||
float _MaxDistance, _StepSize; int _MaxSteps;
|
||||
float _DensityMultiplier, _DensityThreshold, _HeightFalloff;
|
||||
float _NoiseOffset, _NoiseTiling; TEXTURE3D(_FogNoise);
|
||||
float3 _NoiseScrollSpeed; float _NoisePulseSpeed, _NoisePulseAmount;
|
||||
float4 _LightContribution; float _LightScattering;
|
||||
|
||||
float _DepthFade;
|
||||
float _NearStart, _NearEnd;
|
||||
float4 _DayColor, _NightColor; float _DayFactor, _GodRayStrength;
|
||||
float _DitherStrength; float _ColorSteps;
|
||||
|
||||
int _CustomAdditionalLightCount;
|
||||
float4 _CustomAdditionalLightPos[16];
|
||||
float4 _CustomAdditionalLightColor[16];
|
||||
float _CustomAdditionalLightRange[16];
|
||||
|
||||
float _HeightModeBlend;
|
||||
|
||||
// ——— Helpers ———
|
||||
float henyey_greenstein(float cosTheta, float g)
|
||||
{
|
||||
float g2 = g * g;
|
||||
return (1.0 - g2) / (4.0 * PI * pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5));
|
||||
}
|
||||
|
||||
// Hybrydowa gęstość mgły: miks world height i camera height + relY
|
||||
float get_density(float3 worldPos)
|
||||
{
|
||||
// 3D noise coords pozostawiamy w world space (topologia nadal „rzeźbi” mgłę),
|
||||
// ale wysokościowy spadek gęstości kontrolujemy hybrydą.
|
||||
float pulse = 1.0 + sin(_Time.y * _NoisePulseSpeed) * _NoisePulseAmount;
|
||||
float3 noiseCoords = worldPos * 0.01 * _NoiseTiling * pulse;
|
||||
noiseCoords += _NoiseScrollSpeed * _Time.y;
|
||||
|
||||
float4 noise = _FogNoise.SampleLevel(sampler_TrilinearRepeat, noiseCoords, 0);
|
||||
float density = noise.r;
|
||||
density = saturate(density - _DensityThreshold) * _DensityMultiplier;
|
||||
|
||||
// World vs Camera height falloff
|
||||
float camY = _WorldSpaceCameraPos.y;
|
||||
float relY = worldPos.y - camY; // wysokość względem kamery
|
||||
float worldFactor = exp(-max(0, worldPos.y) * _HeightFalloff);
|
||||
float camFactor = exp(-max(0, camY) * _HeightFalloff); // stały względem ray’a
|
||||
float relFactor = exp(-abs(relY) * _HeightFalloff); // „chmura” wokół kamery
|
||||
|
||||
// Hybryda: najpierw łączymy world i relY (wygląda kamerowo), potem mieszamy z camY stałym
|
||||
float camLike = lerp(worldFactor, relFactor, _HeightModeBlend);
|
||||
float hybrid = lerp(worldFactor, camLike, _HeightModeBlend); // przy 1.0 = relY (kamerowo), przy 0.0 = world
|
||||
|
||||
density *= hybrid;
|
||||
return density;
|
||||
}
|
||||
|
||||
half4 frag(Varyings IN) : SV_Target
|
||||
{
|
||||
float4 col = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, IN.texcoord);
|
||||
float depth = SampleSceneDepth(IN.texcoord);
|
||||
float3 worldPos = ComputeWorldSpacePosition(IN.texcoord, depth, UNITY_MATRIX_I_VP);
|
||||
|
||||
float3 entryPoint = _WorldSpaceCameraPos;
|
||||
float3 viewDir = worldPos - _WorldSpaceCameraPos;
|
||||
float viewLength = length(viewDir);
|
||||
float3 rayDir = normalize(viewDir);
|
||||
|
||||
float2 pixelCoords = IN.texcoord * _BlitTexture_TexelSize.zw;
|
||||
float distLimit = min(viewLength, _MaxDistance);
|
||||
float distTravelled = InterleavedGradientNoise(pixelCoords, (int)(_Time.y / max(HALF_EPS, unity_DeltaTime.x))) * _NoiseOffset;
|
||||
|
||||
float transmittance = 1.0;
|
||||
float3 fogAccum = 0;
|
||||
|
||||
[loop]
|
||||
for (int i = 0; i < _MaxSteps && distTravelled < distLimit; i++)
|
||||
{
|
||||
float3 rayPos = entryPoint + rayDir * distTravelled;
|
||||
float density = get_density(rayPos);
|
||||
|
||||
if (density > 0)
|
||||
{
|
||||
// Lighting
|
||||
Light mainLight = GetMainLight(TransformWorldToShadowCoord(rayPos));
|
||||
float phase = henyey_greenstein(dot(rayDir, mainLight.direction), _LightScattering);
|
||||
float3 lightCol = mainLight.color.rgb * _LightContribution.rgb * phase * mainLight.shadowAttenuation;
|
||||
|
||||
for (int li = 0; li < _CustomAdditionalLightCount; li++)
|
||||
{
|
||||
float3 toLight = _CustomAdditionalLightPos[li].xyz - rayPos;
|
||||
float dist = length(toLight);
|
||||
float3 dir = normalize(toLight);
|
||||
|
||||
float atten = saturate(1.0 - dist / _CustomAdditionalLightRange[li]);
|
||||
float phaseAdd = henyey_greenstein(dot(rayDir, dir), _LightScattering);
|
||||
|
||||
lightCol += _CustomAdditionalLightColor[li].rgb * _LightContribution.rgb * phaseAdd * atten;
|
||||
}
|
||||
|
||||
// Fog color: przestajemy wiązać gradient z worldPos.y — używamy wysokości względnej do kamery
|
||||
float camY = _WorldSpaceCameraPos.y;
|
||||
float relY = rayPos.y - camY;
|
||||
float worldColorF = saturate(rayPos.y * 0.01);
|
||||
float camColorF = saturate(relY * 0.01); // ten daje „stały” wygląd niezależnie od poziomu mapy
|
||||
float colorF = lerp(worldColorF, camColorF, _HeightModeBlend);
|
||||
|
||||
float3 fogColor = lerp(_ColorLow.rgb, _ColorHigh.rgb, colorF);
|
||||
float3 dayNightTint = lerp(_NightColor.rgb, _DayColor.rgb, _DayFactor);
|
||||
fogColor = lerp(fogColor, dayNightTint, 0.5);
|
||||
fogColor = lerp(fogColor, _AmbientFogColor.rgb, 0.25);
|
||||
|
||||
fogColor += lightCol * _GodRayStrength;
|
||||
|
||||
// Fading
|
||||
float fade = exp(-distTravelled * _DepthFade);
|
||||
float nearFactor = saturate((distTravelled - _NearStart) / (_NearEnd - _NearStart));
|
||||
density *= fade * nearFactor;
|
||||
|
||||
fogAccum += (fogColor + lightCol) * density * _StepSize;
|
||||
transmittance *= exp(-density * _StepSize);
|
||||
|
||||
if (transmittance < 0.01) break;
|
||||
}
|
||||
|
||||
distTravelled += _StepSize;
|
||||
}
|
||||
|
||||
float3 finalFog = fogAccum * transmittance;
|
||||
|
||||
// Dither + quantize
|
||||
float dither = frac(sin(dot(pixelCoords, float2(12.9898,78.233))) * 43758.5453);
|
||||
finalFog += (dither - 0.5) * _DitherStrength.xxx;
|
||||
finalFog = floor(finalFog * _ColorSteps) / _ColorSteps;
|
||||
|
||||
return float4(lerp(col.rgb, finalFog, 1.0 - saturate(transmittance)), 1.0);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Assets/Jbl_Shaders/Fog/Volumetric Fog_Hybrid.shader.meta
Normal file
9
Assets/Jbl_Shaders/Fog/Volumetric Fog_Hybrid.shader.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cab4a81dc81edb48beccc00c14366f4
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -8,7 +8,7 @@ Material:
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Volumetric Light
|
||||
m_Shader: {fileID: 4800000, guid: 6934182509f924641a5a75ad544128c9, type: 3}
|
||||
m_Shader: {fileID: 4800000, guid: 3cab4a81dc81edb48beccc00c14366f4, type: 3}
|
||||
m_Parent: {fileID: 0}
|
||||
m_ModifiedSerializedProperties: 0
|
||||
m_ValidKeywords: []
|
||||
@@ -49,6 +49,10 @@ Material:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _FoamTex:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _FogMask:
|
||||
m_Texture: {fileID: 2800000, guid: 122f172c549967c4cbc3ca3e80298b5f, type: 3}
|
||||
m_Scale: {x: 0, y: 0}
|
||||
@@ -65,6 +69,10 @@ Material:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _NormalMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _OcclusionMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
@@ -91,43 +99,57 @@ Material:
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Ints: []
|
||||
m_Floats:
|
||||
- _AbsorptionStrength: 0.6
|
||||
- _AddPrecomputedVelocity: 0
|
||||
- _AlphaClip: 0
|
||||
- _AlphaToMask: 0
|
||||
- _AmbientScattering: 0.1
|
||||
- _Blend: 0
|
||||
- _BlendModePreserveSpecular: 1
|
||||
- _BubbleDensity: 8
|
||||
- _BubbleFade: 2
|
||||
- _BubbleFlow: 0.5
|
||||
- _BubbleStrength: 1.5
|
||||
- _BumpScale: 1
|
||||
- _ClearCoatMask: 0
|
||||
- _ClearCoatSmoothness: 0
|
||||
- _ColorSteps: 6
|
||||
- _ColorSteps: 8
|
||||
- _Cull: 2
|
||||
- _Cutoff: 0.5
|
||||
- _DayFactor: 0.091
|
||||
- _DebugLights: 0
|
||||
- _Density: 0.1
|
||||
- _DensityMultiplier: 2.2
|
||||
- _DensityThreshold: 0.18
|
||||
- _DepthFade: 0.01
|
||||
- _DepthStrength: 0.6
|
||||
- _DetailAlbedoMapScale: 1
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DitherStrength: 0.04
|
||||
- _DitherStrength: 0.036
|
||||
- _DstBlend: 0
|
||||
- _DstBlendAlpha: 0
|
||||
- _EnvironmentReflections: 1
|
||||
- _FoamBias: 0
|
||||
- _FoamStrength: 0.6
|
||||
- _FresnelStrength: 0.2
|
||||
- _GlossMapScale: 0
|
||||
- _Glossiness: 0
|
||||
- _GlossyReflections: 0
|
||||
- _GodRayStrength: 0.25
|
||||
- _HG_G: 0.2
|
||||
- _HeightFalloff: 0.4
|
||||
- _HeightFalloff: 1.21
|
||||
- _HeightModeBlend: 0.528
|
||||
- _Intensity: 1
|
||||
- _LavaCrackGlow: 0.8
|
||||
- _LavaEdgeGlow: 0.5
|
||||
- _LightScattering: 0.35
|
||||
- _LightScatteringG: 0.2
|
||||
- _MaskTiling: 0.03
|
||||
- _MaxDistance: 60
|
||||
- _MaxDistance: 5.84
|
||||
- _MaxSteps: 64
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _NearEnd: 3
|
||||
- _NearEnd: 4
|
||||
- _NearFade: 0
|
||||
- _NearStart: 0.05
|
||||
- _NoiseJitter: 0.02
|
||||
@@ -135,11 +157,15 @@ Material:
|
||||
- _NoisePulseAmount: 0.07
|
||||
- _NoisePulseSpeed: 0.15
|
||||
- _NoiseTiling: 0.8
|
||||
- _NormalStrength: 0.6
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.005
|
||||
- _QueueOffset: 0
|
||||
- _ReceiveShadows: 1
|
||||
- _RefractionStrength: 0.015
|
||||
- _ShadowStrength: 1
|
||||
- _ShorelineSharpness: 6
|
||||
- _ShorelineStrength: 1
|
||||
- _Smoothness: 0.5
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
@@ -148,7 +174,16 @@ Material:
|
||||
- _StepScale: 4
|
||||
- _StepSize: 1
|
||||
- _Surface: 0
|
||||
- _SurfaceDetail: 0.4
|
||||
- _SurfaceType: 0
|
||||
- _UVSec: 0
|
||||
- _VertexDistortSpeed: 1.5
|
||||
- _VertexDistortStrength: 0
|
||||
- _VoronoiScale: 5
|
||||
- _VoronoiThreshold: 0.3
|
||||
- _WaveAmplitude: 0.12
|
||||
- _WaveLength: 12
|
||||
- _WaveSpeed: 1
|
||||
- _WorkflowMode: 1
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
@@ -159,8 +194,10 @@ Material:
|
||||
- _ColorLow: {r: 0.45, g: 0.48, b: 0.5, a: 1}
|
||||
- _DayColor: {r: 0.6, g: 0.65, b: 0.7, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
- _FoamColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _FoamScrollSpeed: {r: 0.02, g: 0.01, b: 0, a: 0}
|
||||
- _FogBaseColor: {r: 0.78, g: 0.82, b: 0.9, a: 1}
|
||||
- _LightContribution: {r: 18.618134, g: 14.44776, b: 12.382816, a: 1}
|
||||
- _LightContribution: {r: 28.982248, g: 28.982248, b: 28.982248, a: 1}
|
||||
- _MainLightColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _MainLightDir: {r: 0, g: -1, b: 0, a: 0}
|
||||
- _MaskOffset: {r: -0.23, g: -0.32, b: 0, a: 0}
|
||||
@@ -169,6 +206,10 @@ Material:
|
||||
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
|
||||
- _UserMainLightColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _UserMainLightDir: {r: 0, g: -1, b: 0, a: 0}
|
||||
- _WaterNormal1Speed: {r: 0.05, g: 0, b: 0, a: 0}
|
||||
- _WaterNormal2Speed: {r: 0, g: -0.03, b: 0, a: 0}
|
||||
- _WaterTint: {r: 0.09999997, g: 0.54999995, b: 0.7, a: 1}
|
||||
- _WaveDirection: {r: 1, g: 0.2, b: 0, a: 0}
|
||||
m_BuildTextureStacks: []
|
||||
m_AllowLocking: 1
|
||||
--- !u!114 &6285378627783397592
|
||||
|
||||
@@ -2,7 +2,7 @@ fileFormatVersion: 2
|
||||
guid: 6dad1b85c9bf7884e8a1671ebcd5d1ab
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
mainObjectFileID: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
57
Assets/Jbl_Shaders/Fog/VolumetricFogLightsFeature.cs
Normal file
57
Assets/Jbl_Shaders/Fog/VolumetricFogLightsFeature.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
public class VolumetricFogLightsFeature : ScriptableRendererFeature
|
||||
{
|
||||
class LightsPass : ScriptableRenderPass
|
||||
{
|
||||
static readonly int _CountID = Shader.PropertyToID("_FogLightCount");
|
||||
static readonly int _PosID = Shader.PropertyToID("_FogLightPos");
|
||||
static readonly int _ColorID = Shader.PropertyToID("_FogLightColor");
|
||||
static readonly int _RangeID = Shader.PropertyToID("_FogLightRange");
|
||||
|
||||
const int MAX = 16;
|
||||
Vector4[] pos = new Vector4[MAX];
|
||||
Vector4[] color = new Vector4[MAX];
|
||||
float[] range = new float[MAX];
|
||||
|
||||
public override void Execute(ScriptableRenderContext ctx, ref RenderingData data)
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var vl in data.lightData.visibleLights)
|
||||
{
|
||||
if (count >= MAX) break;
|
||||
if (vl.lightType == LightType.Point || vl.lightType == LightType.Spot)
|
||||
{
|
||||
pos[count] = vl.light.transform.position;
|
||||
color[count] = vl.finalColor;
|
||||
range[count] = vl.range;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
var cmd = CommandBufferPool.Get("FogLights");
|
||||
cmd.SetGlobalInt(_CountID, count);
|
||||
cmd.SetGlobalVectorArray(_PosID, pos);
|
||||
cmd.SetGlobalVectorArray(_ColorID, color);
|
||||
cmd.SetGlobalFloatArray(_RangeID, range);
|
||||
ctx.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
|
||||
Debug.Log($"[FogLights] Passed {count} lights to shader");
|
||||
}
|
||||
}
|
||||
|
||||
LightsPass pass;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
pass = new LightsPass { renderPassEvent = RenderPassEvent.BeforeRendering };
|
||||
}
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data)
|
||||
{
|
||||
renderer.EnqueuePass(pass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9a1a02d6aceacf4183afbdb4fcb58e5
|
||||
Reference in New Issue
Block a user