I’m trying to write a shader that colors the intersection(s) of a mesh. I have it working for solid colors and alphas, but it’s basically just an optical illusion that falls apart whenever I try to do any sort of computed pattern.
Here’s my collider/mesh inspector:
Here’s the shader working with a base color:
Here’s the same shader with an alpha:
And here’s the problem with a computed world space pattern :
Is it possible to “flatten” the pattern somehow within the shader so it maintains the optical illusion? Or is there some other way to achieve the desired effect?
Here’s the shader I have so far:
Shader "Unlit/Intersect" {
Properties {
_Color("Color", Color) = (1,1,1,1)
_Position("Position", Vector) = (0,0,0,0)
}
SubShader {
Tags {
"Queue" = "Transparent" "RenderType" = "Transparent"
}
Pass {
Cull Front ZTest GEqual ZWrite Off
ColorMask 0
Stencil {
Ref 100
Comp Always
Pass Replace
}
}
Pass {
Blend SrcAlpha OneMinusSrcAlpha
Stencil {
Ref 100
Comp Equal
Pass Zero
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
float4 _Position;
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 vertex : SV_POSITION;
float4 screenPos : TEXCOORD0;
float4 worldPos : TEXCOORD1;
};
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target {
float dist = distance(i.worldPos, _Position);
float offset = 1 - saturate(round(abs(frac(dist * 2))));
return _Color * offset;
}
ENDCG
}
}
}
Any help would be appreciated, thanks!