- Home /
Wireframe Shader With Shared Lines Removed
I am working on this wireframe shader:
It uses some adapted code from various shaders, and I am wanting to get rid of the lines that cut through the quads. I really do not know how to go about this in HLSL and was wondering if someone could share their insight.
Shader "WireframeShader"
{
Properties
{
[HDR] _EmissionColor("Color", Color) = (0,0,0)
_EdgeThickness ("Edge Threshold", float) = .9
_WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma multi_compile _ USEDISCARD_ON
#include "UnityCG.cginc"
float _WireThickness;
fixed4 _EmissionColor;
float _EdgeThickness;
struct appdata
{
float4 vertex : POSITION;
};
struct v2g
{
float4 projectionSpaceVertex : SV_POSITION;
float4 worldSpacePosition : TEXCOORD1;
};
struct g2f
{
float4 projectionSpaceVertex : SV_POSITION;
float4 worldSpacePosition : TEXCOORD0;
float4 dist : TEXCOORD1;
};
v2g vert (appdata v)
{
v2g o;
o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
return o;
}
[maxvertexcount(3)]
void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
{
float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;
float2 edge0 = p2 - p1;
float2 edge1 = p2 - p0;
float2 edge2 = p1 - p0;
float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
float wireThickness = 800 - _WireThickness;
g2f o;
o.worldSpacePosition = i[0].worldSpacePosition;
o.projectionSpaceVertex = i[0].projectionSpaceVertex;
o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
o.dist.w = 1.0 / o.projectionSpaceVertex.w;
triangleStream.Append(o);
o.worldSpacePosition = i[1].worldSpacePosition;
o.projectionSpaceVertex = i[1].projectionSpaceVertex;
o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
o.dist.w = 1.0 / o.projectionSpaceVertex.w;
triangleStream.Append(o);
o.worldSpacePosition = i[2].worldSpacePosition;
o.projectionSpaceVertex = i[2].projectionSpaceVertex;
o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
o.dist.w = 1.0 / o.projectionSpaceVertex.w;
triangleStream.Append(o);
}
fixed4 frag (g2f i) : SV_Target
{
float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
if(minDistanceToEdge > _EdgeThickness)
{
discard;
return (0,0,0,0);
}
return _EmissionColor;
}
ENDCG
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How is _WorldSpaceLightPos0 calculated for a directional light? 0 Answers
Mathf.Repeat in ShaderLab? 1 Answer
Why color not rendering in my shader? 0 Answers
Shader shows odd Simplex Noise behavior on Android 0 Answers
(Shader)How to find the cordinate of a given color inside a ramp texture? 0 Answers