I have a shader that should do two passes that will render the back one front once. See the shader code below:
Shader "Custom/Geometry/Wireframe"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[PowerSlider(3.0)]
_WireframeVal ("Wireframe width", Range(0., 0.34)) = 0.05
_FrontColor ("Front color", color) = (1., 1., 1., 1.)
_BackColor ("Back color", color) = (1., 1., 1., 1.)
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#include "UnityCG.cginc"
struct v2g {
float4 pos : SV_POSITION;
};
struct g2f {
float4 pos : SV_POSITION;
float3 bary : TEXCOORD0;
};
v2g vert(appdata_base v) {
v2g o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
[maxvertexcount(3)]
void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
g2f o;
o.pos = IN[0].pos;
o.bary = float3(1., 0., 0.);
triStream.Append(o);
o.pos = IN[1].pos;
o.bary = float3(0., 0., 1.);
triStream.Append(o);
o.pos = IN[2].pos;
o.bary = float3(0., 1., 0.);
triStream.Append(o);
}
float _WireframeVal;
fixed4 _FrontColor;
fixed4 frag(g2f i) : SV_Target {
if(!any(bool3(i.bary.x < _WireframeVal, i.bary.y < _WireframeVal, i.bary.z < _WireframeVal)))
discard;
return _FrontColor;
}
ENDCG
}
Pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#include "UnityCG.cginc"
struct v2g {
float4 pos : SV_POSITION;
};
struct g2f {
float4 pos : SV_POSITION;
float3 bary : TEXCOORD0;
};
v2g vert(appdata_base v) {
v2g o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
[maxvertexcount(3)]
void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
g2f o;
o.pos = IN[0].pos;
o.bary = float3(1., 0., 0.);
triStream.Append(o);
o.pos = IN[1].pos;
o.bary = float3(0., 0., 1.);
triStream.Append(o);
o.pos = IN[2].pos;
o.bary = float3(0., 1., 0.);
triStream.Append(o);
}
float _WireframeVal;
fixed4 _BackColor;
fixed4 frag(g2f i) : SV_Target {
if(!any(bool3(i.bary.x < _WireframeVal, i.bary.y < _WireframeVal, i.bary.z < _WireframeVal)))
discard;
return _BackColor;
}
ENDCG
}
}
}
But the problem i am having is the fact that the second pass in the code is never executing. Because when i set the color for the back -pass That color never becomes visible in the on the mesh the shader (material) is applied to.
Also if i swap the passes so that the back-pass comes before the front-pass the opposite problem occurs.
EDIT
See here a picture of the front of the mesh with the material applied and a picture from the back of the same mesh with material:
Front:
Back:
This is my very first time trying to make such a shader so all help is very much appreaciated!