Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Created February 12, 2015 21:48
Show Gist options
  • Save thebeardphantom/c4b9de370425c8dbd7ea to your computer and use it in GitHub Desktop.
Save thebeardphantom/c4b9de370425c8dbd7ea to your computer and use it in GitHub Desktop.
Distance Aware Outline
// Shader created with Shader Forge v1.04
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.04;sub:START;pass:START;ps:flbk:Diffuse,lico:1,lgpr:1,nrmq:1,limd:1,uamb:True,mssp:True,lmpd:False,lprd:False,rprd:False,enco:False,frtr:True,vitr:True,dbil:False,rmgx:True,rpth:0,hqsc:True,hqlp:False,tesm:0,blpr:0,bsrc:0,bdst:1,culm:0,dpts:2,wrdp:True,dith:2,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,ofsf:0,ofsu:0,f2p0:False;n:type:ShaderForge.SFN_Final,id:2942,x:32753,y:32707,varname:node_2942,prsc:2|diff-1116-RGB,olwid-5129-OUT,olcol-2465-RGB;n:type:ShaderForge.SFN_Color,id:2465,x:32474,y:33133,ptovrint:False,ptlb:lineColor,ptin:_lineColor,varname:_outlineColor,prsc:2,glob:False,c1:1,c2:1,c3:0.5607843,c4:1;n:type:ShaderForge.SFN_Tex2d,id:1116,x:32519,y:32659,ptovrint:False,ptlb:texture,ptin:_texture,varname:_texture,prsc:2,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Time,id:2843,x:31429,y:32711,varname:node_2843,prsc:2;n:type:ShaderForge.SFN_Sin,id:2197,x:31772,y:32784,varname:node_2197,prsc:2|IN-3234-OUT;n:type:ShaderForge.SFN_Abs,id:5938,x:31930,y:32784,varname:node_5938,prsc:2|IN-2197-OUT;n:type:ShaderForge.SFN_ValueProperty,id:4176,x:31901,y:33343,ptovrint:False,ptlb:pulseDist,ptin:_pulseDist,cmnt:How far to pulse the outline,varname:_distance,prsc:2,glob:False,v1:0.09;n:type:ShaderForge.SFN_Multiply,id:3883,x:32096,y:33299,varname:node_3883,prsc:2|A-8599-OUT,B-4176-OUT;n:type:ShaderForge.SFN_ValueProperty,id:4830,x:31383,y:32912,ptovrint:False,ptlb:timeMult,ptin:_timeMult,cmnt:Speed of sine lerp,varname:_timeMultiplier,prsc:2,glob:False,v1:3;n:type:ShaderForge.SFN_Multiply,id:3234,x:31617,y:32784,varname:node_3234,prsc:2|A-2843-T,B-4830-OUT;n:type:ShaderForge.SFN_ValueProperty,id:19,x:32085,y:32987,ptovrint:False,ptlb:minLineWidth,ptin:_minLineWidth,cmnt:The minimum outline size during the lerp,varname:_minOutlineWidth,prsc:2,glob:False,v1:0.75;n:type:ShaderForge.SFN_Add,id:8599,x:32268,y:32777,varname:node_8599,prsc:2|A-7096-OUT,B-19-OUT;n:type:ShaderForge.SFN_FragmentPosition,id:4919,x:31554,y:33393,varname:node_4919,prsc:2;n:type:ShaderForge.SFN_ViewPosition,id:7551,x:31554,y:33509,varname:node_7551,prsc:2;n:type:ShaderForge.SFN_Distance,id:7373,x:31748,y:33455,varname:node_7373,prsc:2|A-4919-XYZ,B-7551-XYZ;n:type:ShaderForge.SFN_Divide,id:4532,x:31930,y:33455,varname:node_4532,prsc:2|A-7373-OUT,B-530-OUT;n:type:ShaderForge.SFN_Lerp,id:5129,x:32449,y:33360,varname:node_5129,prsc:2|A-5214-OUT,B-3883-OUT,T-7326-OUT;n:type:ShaderForge.SFN_Vector1,id:5214,x:32096,y:33249,cmnt:Represents 0-width outline.,varname:node_5214,prsc:2,v1:0;n:type:ShaderForge.SFN_Clamp01,id:8483,x:32096,y:33455,varname:node_8483,prsc:2|IN-4532-OUT;n:type:ShaderForge.SFN_OneMinus,id:7326,x:32262,y:33455,varname:node_7326,prsc:2|IN-8483-OUT;n:type:ShaderForge.SFN_ValueProperty,id:530,x:31748,y:33635,ptovrint:False,ptlb:falloffDist,ptin:_falloffDist,cmnt:How far in units until falloff.,varname:node_530,prsc:2,glob:False,v1:10;n:type:ShaderForge.SFN_Clamp01,id:7096,x:32085,y:32777,varname:node_7096,prsc:2|IN-5938-OUT;proporder:1116-4176-19-2465-530-4830;pass:END;sub:END;*/
Shader "Shader Forge/Distance Aware Outline" {
Properties {
_texture ("texture", 2D) = "white" {}
_pulseDist ("pulseDist", Float ) = 0.09
_minLineWidth ("minLineWidth", Float ) = 0.75
_lineColor ("lineColor", Color) = (1,1,0.5607843,1)
_falloffDist ("falloffDist", Float ) = 10
_timeMult ("timeMult", Float ) = 3
}
SubShader {
Tags {
"RenderType"="Opaque"
}
Pass {
Name "Outline"
Tags {
}
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcaster
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _TimeEditor;
uniform float4 _lineColor;
uniform float _pulseDist;
uniform float _timeMult;
uniform float _minLineWidth;
uniform float _falloffDist;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.posWorld = mul(_Object2World, v.vertex);
float4 node_2843 = _Time + _TimeEditor;
o.pos = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz + v.normal*lerp(0.0,((saturate(abs(sin((node_2843.g*_timeMult))))+_minLineWidth)*_pulseDist),(1.0 - saturate((distance(mul(_Object2World, v.vertex).rgb,_WorldSpaceCameraPos)/_falloffDist)))),1));
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
/////// Vectors:
return fixed4(_lineColor.rgb,0);
}
ENDCG
}
Pass {
Name "ForwardBase"
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform sampler2D _texture; uniform float4 _texture_ST;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz;
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
/////// Vectors:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 indirectDiffuse = float3(0,0,0);
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
float4 _texture_var = tex2D(_texture,TRANSFORM_TEX(i.uv0, _texture));
float3 diffuse = (directDiffuse + indirectDiffuse) * _texture_var.rgb;
/// Final Color:
float3 finalColor = diffuse;
return fixed4(finalColor,1);
}
ENDCG
}
Pass {
Name "ForwardAdd"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
Fog { Color (0,0,0,0) }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform sampler2D _texture; uniform float4 _texture_ST;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz;
o.posWorld = mul(_Object2World, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
/////// Vectors:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float4 _texture_var = tex2D(_texture,TRANSFORM_TEX(i.uv0, _texture));
float3 diffuse = directDiffuse * _texture_var.rgb;
/// Final Color:
float3 finalColor = diffuse;
return fixed4(finalColor * 1,0);
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment