-
-
Save unitycoder/6378bbf237378911734bf00f89cd4da0 to your computer and use it in GitHub Desktop.
Simple Fog Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| [RequireComponent(typeof(Camera))] | |
| public class FogComponent : MonoBehaviour | |
| { | |
| public Shader myFogShader; | |
| public Color fogColor = Color.red; | |
| private Material myFogMaterial; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth; | |
| myFogMaterial = new Material(myFogShader); | |
| myFogMaterial.hideFlags = HideFlags.HideAndDontSave; | |
| } | |
| private void OnRenderImage(RenderTexture src, RenderTexture dest) { | |
| myFogMaterial.SetColor("_FogColor", fogColor); | |
| Graphics.Blit(src, dest, myFogMaterial); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Shader "Hidden/FogShader" | |
| { | |
| Properties | |
| { | |
| _MainTex ("Texture", 2D) = "white" {} | |
| } | |
| SubShader | |
| { | |
| // No culling or depth | |
| Cull Off ZWrite Off ZTest Always | |
| Pass | |
| { | |
| CGPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #include "UnityCG.cginc" | |
| struct appdata | |
| { | |
| float4 vertex : POSITION; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| struct v2f | |
| { | |
| float2 uv : TEXCOORD0; | |
| float4 vertex : SV_POSITION; | |
| }; | |
| v2f vert (appdata v) | |
| { | |
| v2f o; | |
| o.vertex = UnityObjectToClipPos(v.vertex); | |
| o.uv = v.uv; | |
| return o; | |
| } | |
| sampler2D _MainTex; | |
| sampler _CameraDepthTexture; | |
| float3 _FogColor; | |
| fixed4 frag (v2f i) : SV_Target | |
| { | |
| float4 depthColor = tex2D(_CameraDepthTexture, i.uv); | |
| float linearDepth = Linear01Depth(depthColor.r); | |
| float fogDensity = linearDepth; | |
| float4 mainTexColor = tex2D(_MainTex, i.uv); | |
| float4 outColor = mainTexColor; | |
| outColor.rgb = lerp(mainTexColor.rgb, _FogColor, fogDensity); | |
| return outColor; | |
| } | |
| ENDCG | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment