Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active November 18, 2019 11:33
Show Gist options
  • Save unitycoder/3ab99661e1574a061fa9ba0e4989ea14 to your computer and use it in GitHub Desktop.
Save unitycoder/3ab99661e1574a061fa9ba0e4989ea14 to your computer and use it in GitHub Desktop.
Fix Sceneview Outline for Vertex Extrusion Shader using SceneSelectionPass (unity)
// vertex extrude shader example, with sceneviewpass (to fix editor outline)
// https://forum.unity.com/threads/scene-outline-bug-or-feature-change-doesnt-follow-shader-extruded-vertices.770273/#post-5149322
Shader "Unlit/vertexextrude"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Amount("Extrusion Amount", Range(-1,1)) = 0.5
}
SubShader
{
// sceneview pass (same as regular pass but cleaned this up a bit)
Pass
{
Name "SceneSelectionPass"
Tags{"LightMode" = "SceneSelectionPass"}
ColorMask 0
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define SCENESELECTIONPASS
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal: NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
float _Amount;
v2f vert(appdata v)
{
v2f o;
v.vertex.xyz += v.normal * _Amount;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return 1;
}
ENDCG
} // pass
// regular pass
Pass
{
Tags { "RenderType" = "Opaque" }
LOD 100
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Amount;
v2f vert(appdata v)
{
v2f o;
v.vertex.xyz += v.normal * _Amount;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
} // pass
} // subshader
} // shader
// this is the example from unity support
Shader "Unlit/vertexextrude"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Amount("Extrusion Amount", Range(-1,1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Name "SceneSelectionPass"
Tags{"LightMode" = "SceneSelectionPass"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Amount;
v2f vert(appdata v)
{
v2f o;
v.vertex.xyz += v.normal * _Amount;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Amount;
v2f vert (appdata v)
{
v2f o;
v.vertex.xyz += v.normal * _Amount;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment