Advertisement
Staggart

SCPE Fog integration shader example

Jan 30th, 2023
2,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/SCPE_TransparentWithFog"
  2. {
  3.     SubShader
  4.     {
  5.         Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
  6.         Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
  7.         Cull Back
  8.         Zwrite Off
  9.        
  10.         Pass
  11.         {
  12.             HLSLPROGRAM
  13.             #pragma vertex vert
  14.             #pragma fragment frag
  15.  
  16.             //Uncomment to disable features that definitely aren't used for a performance gain
  17.            
  18.             //#define DISABLE_FOG_GRADIENT
  19.             //#define DISABLE_FOG_SKYBOXCOLOR
  20.             //#define DISABLE_FOG_HEIGHTNOISE
  21.             //#define DISABLE_FOG_DIRECTIONAL_COLOR
  22.            
  23.             //Source for the "ComputeFog" function and all its dependencies
  24.             #include "Assets/SC Post Effects/Runtime/Fog/Fog.hlsl"
  25.  
  26.             struct appdata
  27.             {
  28.                 float4 vertex : POSITION;
  29.             };
  30.  
  31.             struct v2f
  32.             {
  33.                 float4 vertex : SV_POSITION;
  34.                 float3 positionWS: TEXCOORD1;
  35.                 float4 screenPos : TEXCOORD2;
  36.             };
  37.  
  38.             v2f vert (appdata v)
  39.             {
  40.                 v2f o;
  41.                 o.vertex = UnityObjectToClipPos(v.vertex);
  42.                 o.positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
  43.                 o.screenPos = ComputeScreenPos(v.vertex);
  44.                
  45.                 return o;
  46.             }
  47.  
  48.             float4 frag (v2f i) : SV_Target
  49.             {
  50.                 float4 col = float4(0,0,0,1);
  51.  
  52.                 //The screen position input is used when the fog color source is set to "Skybox". If never in use, you can set it to 0
  53.                 float4 fogColor = ComputeTransparentFog(i.positionWS, i.screenPos);
  54.  
  55.                 //The alpha channel will hold the density of the fog, use it as a lerp factor
  56.                 col.rgb = lerp(fogColor.rgb, col.rgb, fogColor.a);
  57.  
  58.                 //Alternatively, apply this function also does the lerp operation
  59.                 //ApplyTransparencyFog(i.positionWS, i.screenPos, col.rgb);
  60.                
  61.                 return col;
  62.             }
  63.            
  64.             ENDHLSL
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement