Skip to content

Instantly share code, notes, and snippets.

@bitinn
Created May 16, 2018 04:01
Show Gist options
  • Save bitinn/40944d705be37de2703fd6509795d28a to your computer and use it in GitHub Desktop.
Save bitinn/40944d705be37de2703fd6509795d28a to your computer and use it in GitHub Desktop.
depth.shader
Shader "Hidden/BeginHQ/FogMachine" {
HLSLINCLUDE
#include "PostProcessing/Shaders/StdLib.hlsl"
/* effect parameters */
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
half _Blend;
float4x4 _MainCameraToWorld;
/* unity parameters */
float4x4 unity_MatrixInvV;
float4x4 unity_CameraToWorld;
float4x4 unity_CameraInvProjection;
// vertext input (vertex in object space)
struct VertexInput {
float3 vertex : POSITION;
};
// vertex to fragment input (vertex in clip space)
struct Vertex2Fragment {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
float3 ray : TEXCOORD1;
};
// create view space position
float3 CreateViewSpacePosition (float3 ray, float depth) {
// remap depth to linear [0..1] range, 1 being the far plane depth
return ray * Linear01Depth(depth);
}
// create world space position
float3 CreateWorldSpacePosition (float3 viewPosition) {
return mul(_MainCameraToWorld, float4(viewPosition, 1.0)).xyz;
}
// create color from input position
half4 CreateEffectColor (float3 position) {
return half4(pow(abs(cos(position * PI * 4)), 20), 1.0);
}
// blend color
half4 BlendEffectColor (half4 sourceColor, half4 effectColor) {
return half4(lerp(sourceColor.rgb, effectColor.rgb, _Blend), sourceColor.a);
}
// default vert
Vertex2Fragment FogMachineVert (VertexInput i) {
Vertex2Fragment o;
// object space
float2 vertexXY = i.vertex.xy;
// mesh input is actually a triangle that fills the screen
// object space to clip space, assume: far plane, orthographic view
// remember clip space xy is [-1..1]
o.vertex = float4(vertexXY, 1.0, 1.0);
// vertex.xy are (-1,-1), (-1,3), (3,-1)
// this hack just put uv back to [0..1] range on screen
o.texcoord = (o.vertex.xy + 1.0) * 0.5;
// directx and metal requires Y-axis flip and use (0,1) as origin
#if UNITY_UV_STARTS_AT_TOP
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
#endif
// view space vertex direction
o.ray = mul(unity_CameraInvProjection, o.vertex).xyz * _ProjectionParams.z;
return o;
}
// view space pass
half4 FogMachineFragVS (Vertex2Fragment i) : SV_Target {
half4 outColor;
// sample camera textures
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord);
// view position from ray and depth
float3 viewPosition = CreateViewSpacePosition(i.ray, depth);
// visual color
half4 visualColor = CreateEffectColor(viewPosition);
// blend visual with source color
outColor = BlendEffectColor(color, visualColor);
return outColor;
}
// world space pass
half4 FogMachineFragWS (Vertex2Fragment i) : SV_Target {
half4 outColor;
// sample camera textures
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord);
// view position from ray and depth
float3 viewPosition = CreateViewSpacePosition(i.ray, depth);
// world position from view position
float3 worldPosition = CreateWorldSpacePosition(viewPosition);
// visual color
half4 visualColor = CreateEffectColor(worldPosition);
// blend visual with source color
outColor = BlendEffectColor(color, visualColor);
return outColor;
}
ENDHLSL
SubShader {
Cull Off
ZWrite Off
ZTest Always
Pass {
HLSLPROGRAM
#pragma target 3.5
#pragma vertex FogMachineVert
#pragma fragment FogMachineFragVS
ENDHLSL
}
Pass {
HLSLPROGRAM
#pragma target 3.5
#pragma vertex FogMachineVert
#pragma fragment FogMachineFragWS
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment