here is my modified globalfog.It works transparent objects fine.
Shader "Hidden/MyGlobalFogShader" {
Properties {
_MainTex ("Base (RGB)", 2D) = "black" {}
}
CGINCLUDE
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _CameraDepthTexture;
uniform float _GlobalDensity;
uniform float4 _FogColor;
uniform float4 _StartDistance;
uniform float4 _Y;
uniform float4 _MainTex_TexelSize;
// for fast world space reconstruction
uniform float4x4 _FrustumCornersWS;
uniform float4 _CameraWS;
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float4 interpolatedRay : TEXCOORD1;
float2 uv2: TEXCOORD2;
};
bool eq(float a,float b)
{
return abs(a-b) < 0.01;
}
v2f vert( appdata_img v )
{
v2f o;
float xx = v.texcoord.x;
float yy = v.texcoord.y;
float2 uv = v.texcoord.xy;
o.uv = uv;
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
{
uv.y = 1-uv.y;
}
else
{
yy = 1 - yy;
}
#endif
int index = (int)floor(xx+yy*2 + 0.5);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv2 = uv;
o.interpolatedRay = _FrustumCornersWS[(int)index];
o.interpolatedRay.w = index;
return o;
}
float ComputeFogForYAndDistance (in float3 camDir, in float3 wsPos)
{
float fogInt = saturate(length(camDir) * _StartDistance.x-1.0) * _StartDistance.y;
float fogVert = max(0.0, (wsPos.y-_Y.x) * _Y.y);
fogVert *= fogVert;
return (1-exp(-_GlobalDensity*fogInt)) * exp (-fogVert);
}
half4 fragAbsoluteYAndDistance (v2f i) : COLOR
{
float dpth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture,i.uv2)));
float4 wsDir = dpth * i.interpolatedRay;
float4 wsPos = _CameraWS + wsDir;
return lerp(tex2D(_MainTex, i.uv), _FogColor, ComputeFogForYAndDistance(wsDir.xyz,wsPos.xyz));
}
half4 fragRelativeYAndDistance (v2f i) : COLOR
{
float dpth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture,i.uv2)));
float4 wsDir = dpth * i.interpolatedRay;
return lerp(tex2D(_MainTex, i.uv), _FogColor, ComputeFogForYAndDistance(wsDir.xyz, wsDir.xyz));
}
half4 fragAbsoluteY (v2f i) : COLOR
{
float dpth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture,i.uv2)));
float4 wsPos = (_CameraWS + dpth * i.interpolatedRay);
float fogVert = max(0.0, (wsPos.y-_Y.x) * _Y.y);
fogVert *= fogVert;
fogVert = (exp (-fogVert));
return lerp(tex2D( _MainTex, i.uv ), _FogColor, fogVert);
}
half4 fragDistance (v2f i) : COLOR
{
float dpth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture,i.uv2)));
float4 camDir = ( dpth * i.interpolatedRay);
float fogInt = saturate(length( camDir ) * _StartDistance.x - 1.0) * _StartDistance.y;
return lerp(_FogColor, tex2D(_MainTex, i.uv), exp(-_GlobalDensity*fogInt));
}
ENDCG
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment fragAbsoluteYAndDistance
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment fragAbsoluteY
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment fragDistance
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment fragRelativeYAndDistance
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
}
Fallback off
}
using UnityEngine;
using System.Collections;
public class MyGlobalFog : MonoBehaviour {
void Start () {
}
[ImageEffectOpaque]
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
DrawFog(source, destination);
}
//fog script
public enum FogMode
{
AbsoluteYAndDistance = 0,
AbsoluteY = 1,
Distance = 2,
RelativeYAndDistance = 3,
}
public bool bEnableFog = true;
public FogMode fogMode = FogMode.AbsoluteYAndDistance;
private float CAMERA_NEAR = 0.5f;
private float CAMERA_FAR = 50.0f;
private float CAMERA_FOV = 60.0f;
private float CAMERA_ASPECT_RATIO = 1.333333f;
public float startDistance = 200.0f;
public float globalDensity = 1.0f;
public float heightScale = 100.0f;
public float height = 0.0f;
public Color globalFogColor = Color.grey;
public Shader fogShader;
private Material fogMaterial = null;
void DrawFog(RenderTexture source, RenderTexture destination)
{
if (!bEnableFog)
{
Graphics.Blit(source, destination);
return;
}
if (fogMaterial == null)
{
fogMaterial = new Material(fogShader);
}
CAMERA_NEAR = camera.nearClipPlane;
CAMERA_FAR = camera.farClipPlane;
CAMERA_FOV = camera.fieldOfView;
CAMERA_ASPECT_RATIO = camera.aspect;
Matrix4x4 frustumCorners = Matrix4x4.identity;
//Vector4 vec;
//Vector3 corner;
float fovWHalf = CAMERA_FOV * 0.5f;
Vector3 toRight = camera.transform.right * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad) * CAMERA_ASPECT_RATIO;
Vector3 toTop = camera.transform.up * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad);
Vector3 topLeft = (camera.transform.forward * CAMERA_NEAR - toRight + toTop);
float CAMERA_SCALE = topLeft.magnitude * CAMERA_FAR / CAMERA_NEAR;
topLeft.Normalize();
topLeft *= CAMERA_SCALE;
Vector3 topRight = (camera.transform.forward * CAMERA_NEAR + toRight + toTop);
topRight.Normalize();
topRight *= CAMERA_SCALE;
Vector3 bottomRight = (camera.transform.forward * CAMERA_NEAR + toRight - toTop);
bottomRight.Normalize();
bottomRight *= CAMERA_SCALE;
Vector3 bottomLeft = (camera.transform.forward * CAMERA_NEAR - toRight - toTop);
bottomLeft.Normalize();
bottomLeft *= CAMERA_SCALE;
frustumCorners.SetRow(0, topLeft);
frustumCorners.SetRow(1, topRight);
frustumCorners.SetRow(2, bottomRight);
frustumCorners.SetRow(3, bottomLeft);
fogMaterial.SetMatrix("_FrustumCornersWS", frustumCorners);
fogMaterial.SetVector("_CameraWS", camera.transform.position);
fogMaterial.SetVector("_StartDistance", new Vector4(1.0f / startDistance, (CAMERA_SCALE - startDistance)));
fogMaterial.SetVector("_Y", new Vector4(height, 1.0f / heightScale));
fogMaterial.SetFloat("_GlobalDensity", globalDensity * 0.01f);
fogMaterial.SetColor("_FogColor", globalFogColor);
CustomGraphicsBlit(source, destination, fogMaterial, (int)fogMode);
}
static void CustomGraphicsBlit(RenderTexture source, RenderTexture dest, Material fxMaterial, int passNr)
{
Graphics.Blit(source, dest, fxMaterial, passNr);
/*
RenderTexture.active = dest;
fxMaterial.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
fxMaterial.SetPass(passNr);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 3.0f); // BL
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 2.0f); // BR
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f); // TR
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f); // TL
GL.End();
GL.PopMatrix();
*/
}
}