85 lines
2.8 KiB
Plaintext
85 lines
2.8 KiB
Plaintext
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
|
|
}
|
|
}
|
|
}
|