- Home /
Problem with outling intersection using shader programming
I am trying to develop a shader that outlines objects. The issue I am having is that it manipulates the vertex color and cannot be seen from top view. What I want is to have an outline on all objects cut at a specific height of a fixed length,height,width, i.e.: 1cm. I want a shader to be applied either to all objects or only the plane that cuts the objects. I want to see the outline in the TOP view and preferably SIDE view.,
Here is a random perspective view (you can see the outline)
Here is the top view where you cannot see the outline on all shapes.
I am using the Volume Highlight Shader from @Glurth (https://answers.unity.com/questions/1487473/geometry-intersection-shader.html)
Shader "Unlit/highlightVolume"
{
Properties
{
_Color("Color", Color) = (1,1,1,.2)
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
Pass
{
Stencil
{
Ref 172
Comp Always
Pass Replace
ZFail Zero
}
Blend Zero One
Cull Front
ZTest GEqual
ZWrite Off
}// end stencil pass
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Stencil
{
Ref 172
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float3 normal: NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return _Color;
}
ENDCG
}//end color pass
}
}
I have also included the Shader that is on top of the objects that are cut at a specific height.
Shader "Custom/Clip Plane/Surface"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Main Texture", 2D) = "white" {}
_PlaneVector("Plane Vector", Vector) = (0,0,0,10)
_OutlineWidth("Outline Width", Range(0,0.05)) = 0.01
_OutlineColor("Outline Color", Color) = (0.5,0.5,0.5,1)
}
SubShader
{
// Drawing actual object
Pass
{
Cull Off // Turn off Face Culling
CGPROGRAM
#include "UnityCG.cginc"
#include "./CustomClipPlaneFunctions.cginc"
#pragma vertex vert
#pragma fragment frag
uniform fixed4 _LightColor0;
float4 _Color;
float4 _MainTex_ST; // For the Main Tex UV transform
sampler2D _MainTex; // Texture used for the line
float4 _PlaneVector;
float _DoClip;
struct v2f
{
float4 pos : POSITION;
float4 col : COLOR;
float2 uv : TEXCOORD0;
float doclip : TEXCOORD1;
float4 worldSpacePos : TEXCOORD2;
};
float4 _OutlineColor;
float _OutlineWidth;
v2f vert(appdata_base v)
{
v2f o;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
// Calculate clip value
float3 wp = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldSpacePos = mul(unity_ObjectToWorld, v.vertex);
o.doclip = isClipped(_PlaneVector, wp);
// Lighting
float4 LightDirection = normalize(_WorldSpaceLightPos0);
float3 normalDirection = normalize(mul(unity_ObjectToWorld, v.normal).xyz);
float4 DiffuseLight = saturate(dot(LightDirection, normalDirection)) * _LightColor0;
o.col = float4(UNITY_LIGHTMODEL_AMBIENT + DiffuseLight);
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
float4 frag(v2f i) : COLOR
{
clip(i.doclip);
float4 col = _Color * tex2D(_MainTex, i.uv);
float4 worldPos = ComputeScreenPos(i.pos);
return col * i.col;
}
ENDCG
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612225627im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need help with outline shader 0 Answers
someone knows how to create an anisotropic shader? HELP!!! 0 Answers
Shader Problem 0 Answers
calculating 1 Answer