Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created June 30, 2018 07:17
Show Gist options
  • Save hakanai/0d749d8a589820e9e21909122134e96d to your computer and use it in GitHub Desktop.
Save hakanai/0d749d8a589820e9e21909122134e96d to your computer and use it in GitHub Desktop.
Quick implementation of the first step documented in http://prideout.net/blog/?p=51
#include "UnityCG.cginc"
fixed4 _Color;
float _DepthScale;
float _Sigma;
struct VertexInput
{
float3 objectPos : POSITION;
};
struct FragmentInput
{
float4 clipPos : POSITION;
float3 viewPos : TEXCOORD0;
float3 objectPos : TEXCOORD1;
};
FragmentInput Vertex(VertexInput input)
{
FragmentInput output;
output.clipPos = UnityObjectToClipPos(input.objectPos);
output.viewPos = UnityObjectToViewPos(input.objectPos);
output.objectPos = input.objectPos;
return output;
}
float4 Fragment(FragmentInput input, fixed facing : VFACE) : SV_Target
{
#if UNITY_VFACE_FLIPPED
facing = -facing;
#endif
//TODO: How do we get a sensible gradient?
//float density = pow(0.5 - input.objectPos.y * 0.5, 2);
// float density = 1.0;
float density = 0.5 + input.objectPos.y * 0.5;
density = exp(-_Sigma * density);
float depth = _DepthScale * density * input.viewPos.z * sign(facing);
return depth * _Color;
}
Shader "Unlit/TempShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_DepthScale ("Depth Scale", Range(0.001, 2.0)) = 1.0
_Sigma ("Sigma", Range(0.1, 100.0)) = 30.0
}
SubShader
{
Tags
{
"RenderType" = "Transparent"
"Queue" = "Transparent+1"
}
LOD 100
Blend One One
Cull Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
#pragma target 3.0 // for VFACE
#include "TempShader.cginc"
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment