[ad_1]
Here’s a fragment shader I wrote that makes use of a seize move to copy an “common coloration” mix mode
Shader "2DSB/VertexColor"
{
Properties
{
[PerRendererData] _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Color ("Principal Coloration", Coloration) = (1, 1, 1, 1)
}
SubShader
{
Tags {"Queue"="Clear" "IgnoreProjector"="True" "RenderType"="Clear"}
ZWrite Off
Lighting Off
Cull Off
Fog { Mode Off }
//Earlier: Mix SrcAlpha OneMinusSrcAlpha
Mix SrcAlpha Zero
LOD 100
// Add GrabPass to seize the present body buffer
GrabPass { } // "_GrabTexture" is the title of the grabbed texture
Cross
{
CGPROGRAM
#pragma vertex vert_vct
#pragma fragment frag_mult
#pragma fragmentoption ARB_precision_hint_fastest
#embrace "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _GrabTexture;
float4 _GrabTexture_ST;
float4 _Color;
struct vin_vct
{
float4 vertex : POSITION;
float4 coloration : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f_vct
{
float4 vertex : SV_POSITION;
fixed4 coloration : COLOR;
float2 texcoord : TEXCOORD0;
float2 screenspaceUV : TEXCOORD1; // Add display screen area UV
};
v2f_vct vert_vct(vin_vct v)
{
v2f_vct o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.coloration = v.coloration;
o.texcoord = v.texcoord;
o.screenspaceUV = ComputeScreenPos(o.vertex).xy; // Compute display screen area place
return o;
}
fixed4 frag_mult(v2f_vct i) : SV_Target
{
fixed4 srcColor = tex2D(_MainTex, i.texcoord) * i.coloration * _Color;
fixed4 dstColor = tex2D(_GrabTexture, i.screenspaceUV); // Coloration from the body buffer; Use display screen area UV to pattern
// Customized logic to common the colours
fixed4 finalColor = srcColor;
if (any(dstColor.rgba > fixed4(0,0,0,0).rgba)) { // Why is that this by no means true, even when there's overlap between sport objects?
finalColor = (srcColor + dstColor) * 0.5; // Easy averaging
//Take a look at: finalColor = fixed4(1,0,0,1);
}
return finalColor;
}
ENDCG
}
}
}
The large drawback is that the sampled _GrabTexture is at all times empty. I do know this as a result of this block was by no means true.
if (any(dstColor.rgba > fixed4(0,0,0,0).rgba)) { // Why is that this by no means true, even when there's overlap between sport objects?
finalColor = (srcColor + dstColor) * 0.5; // Easy averaging
//Take a look at: finalColor = fixed4(1,0,0,1);
}
Nonetheless, this needs to be true for areas the place there are overlapping sport objects. What’s the challenge right here? How do I appropriately pattern the present body buffer’s texture utilizing GrabPass to implement a median colours mix mode?
[ad_2]