- Home /
DrawMesh / Stencil / RendureTexture topic
Hello!
In my slot game I am using DrawMesh calls to render thick win lines like shown in the screenshot. First I am rendering the background of the boxes with stencil write on, then I render the boxes with stencil write on, then I am rendering the lines with stencil testing, so the lines get not rendered where the boxes are. ( see screenshot )
The problem is that I have no control over the z position of the win lines. In the frame debugger they always are rendered at the end of the camera render loop job. The z values in my meshes are ignored. I played around with zwrite on/off in the shaders, different queues but the result will always be the same: my lines will be the last steps render jobs.
I tried to render the winlines into a RenderTexture and display that texture as sprite. That does not work because the RenderTexture does not support stencil operations ( RenderTexture. SupportsStencil returns false for my render texture, and true for main screen )
I want to have other sprites rendered in front of the winlines, but the lines need to rendered by the same camera, because I am using a post effects to blur the screen in some situations when I display overlay windows.
The shader I use for writing to stencil looks like this:
Shader "Zentronic/Color/MaskStencil"
{
Properties
{
[PerRendererData] _Color ("Main Color", Color) = (1,1,1,1)
_Stencil ("Stencil ID", Float) = 2
}
SubShader
{
Tags
{
//"RenderType" = "Transparent"
//"Queue" = "Transparent"
"IgnoreProjector" = "True"
"ForceNoShadowCasting" = "True"
}
Pass
{
ZWrite On
Cull Off
Lighting Off
Blend One OneMinusSrcAlpha
ColorMask 0
Stencil
{
Ref [_Stencil]
Comp equal
Pass decrWrap
Fail keep
}
CGPROGRAM
#pragma vertex vert_shader
#pragma fragment frag_shader
#include "UnityCG.cginc"
fixed4 _Color;
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert_shader (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
return o;
}
fixed4 frag_shader (v2f i) : COLOR
{
i.color.rgb*=i.color.a;
return i.color;
}
ENDCG
}
}
}
the other shader that renders the lines looks like so:
Shader "Zentronic/Color/TestStencil"
{
Properties
{
_Stencil ("Stencil ID", Float) = 2
}
SubShader
{
Tags
{
"IgnoreProjector" = "True"
"ForceNoShadowCasting" = "True"
}
Pass
{
Blend One OneMinusSrcAlpha
ZWrite On
Cull Off
Lighting Off
Stencil
{
Ref [_Stencil]
Comp equal
Pass keep
Fail keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : COLOR
{
i.color.rgb*=i.color.a;
return i.color;
}
ENDCG
}
}
}
So my questions are
1) How can I get control of z ordering of my DrawMesh calls ?
2) Can anyone here figure out how to solve this scenario ( perhaps with MeshFilter / Mesh Renderer ) and point me in the right direction how to implement this.
3) Can anyone figure out a better solution to render those boxes/lines without stencil operations ? ( This would make the render texture aproach possible )
Thank you in advance for your help and thoughts!
[1]: /storage/temp/81238-bildschirmfoto-2016-10-31-um-142016.png
Your answer
![](https://koobas.hobune.stream/wayback/20220612095748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with transparency and stencil shader 1 Answer
Shader that renders pixel with highest alpha value? 0 Answers
Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 0 Answers
Adding a Stencil to Particle Standard Surface shader 0 Answers
Standard Shader Still Visible through Stencil Shader 0 Answers