Last active
January 12, 2024 12:39
-
-
Save unitycoder/dec585ff22cc65b5f9024d847e96eb95 to your computer and use it in GitHub Desktop.
Wireframe Geometry Shader Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://forum.unity.com/threads/program-a-shader-that-only-displays-the-visible-edges.773534/#post-5158760 | |
Shader "Wireframe (Geometry Shader)" | |
{ | |
SubShader | |
{ | |
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } | |
Pass | |
{ | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex VSMain | |
#pragma geometry GSMain | |
#pragma fragment PSMain | |
#pragma target 5.0 | |
struct Data | |
{ | |
float4 vertex : SV_Position; | |
float2 barycentric : BARYCENTRIC; | |
}; | |
void VSMain(inout float4 vertex:POSITION) { } | |
[maxvertexcount(3)] | |
void GSMain( triangle float4 patch[3]:SV_Position, inout TriangleStream<Data> stream) | |
{ | |
Data GS; | |
for (uint i = 0; i < 3; i++) | |
{ | |
GS.vertex = UnityObjectToClipPos(patch[i]); | |
GS.barycentric = float2(fmod(i,2.0), step(2.0,i)); | |
stream.Append(GS); | |
} | |
stream.RestartStrip(); | |
} | |
float4 PSMain(Data PS) : SV_Target | |
{ | |
float3 coord = float3(PS.barycentric, 1.0 - PS.barycentric.x - PS.barycentric.y); | |
coord = smoothstep(fwidth(coord)*0.1, fwidth(coord)*0.1 + fwidth(coord), coord); | |
return float4(0..xxx, 1.0 - min(coord.x, min(coord.y, coord.z))); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// with colors and other options, uses UV now | |
Shader "Wireframe 2 (Geometry Shader)" | |
{ | |
Properties | |
{ | |
_Color ("Outline Color", Color) = (1,1,1,1) | |
_OutlineWidth ("Outline Width", float) = 0.1 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" "Queue"="Geometry" } | |
Cull Off | |
Pass | |
{ | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex VSMain | |
#pragma geometry GSMain | |
#pragma fragment PSMain | |
#pragma target 5.0 | |
struct appdata_t { | |
float4 vertex : POSITION; | |
fixed4 color : COLOR; | |
float2 uv : TEXCOORD0; | |
}; | |
struct Data | |
{ | |
float4 vertex : SV_Position; | |
float2 barycentric : BARYCENTRIC; | |
float2 uv : TEXCOORD0; | |
fixed4 color : COLOR; | |
}; | |
float4 _Color; | |
float _OutlineWidth; | |
void VSMain(in appdata_t v, out Data o) | |
{ | |
o.vertex = v.vertex; | |
o.color = v.color; | |
o.uv = v.uv; | |
} | |
[maxvertexcount(3)] | |
void GSMain(triangle Data patch[3], inout TriangleStream<Data> stream) | |
{ | |
Data GS; | |
for (uint i = 0; i < 3; i++) | |
{ | |
GS.vertex = UnityObjectToClipPos(patch[i].vertex); | |
GS.color = patch[i].color; | |
GS.uv = patch[i].uv; | |
GS.barycentric = float2(fmod(i,2.0), step(2.0,i)); | |
stream.Append(GS); | |
} | |
stream.RestartStrip(); | |
} | |
float4 PSMain(Data PS) : SV_Target | |
{ | |
// bary based (triangle) | |
//float3 coord = float3(PS.barycentric, 1.0 - PS.barycentric.x - PS.barycentric.y); | |
//coord = smoothstep(fwidth(coord)*0.1, fwidth(coord)*0.1 + fwidth(coord), coord); | |
//float o = 1-_OutlineWidth - min(coord.x, min(coord.y, coord.z)); | |
// uv based (quad) | |
float2 coord = PS.uv; | |
coord = smoothstep(fwidth(coord)*0.9, fwidth(coord)*0.9 + fwidth(coord), coord); | |
float o = 1-_OutlineWidth - min(coord.x, coord.y); | |
float4 col = float4(_Color.rgb*o, 1); | |
// works, with black color and 0 Width | |
return lerp(PS.color,_Color,o); | |
//return PS.color; | |
//return float4(PS.uv.xy,0,1); | |
//return PS.barycentric.xyxy; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment