- Home /
How to highlight intersection of two meshes with a texture as the highlighting
Hi! I've been trying to make a shader that checks for intersection between two meshes and then highlights an area around it with a texture. It's been really frustrating because i tried many different shaders and none solved my problem. I made a drawing to illustrate what i need:
So, for now i have tried some solutions based on reading the depth texture from the camera to check for the intersection. The best result i've got as of now is highlighting the area with a color, using code i got from a video on youtube made by a guy called "許逸倫": (don't know if i'm allowed to post his video, am i?)
Shader "Unlit/ShieldFX"
{
Properties
{
_MainColor("MainColor", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_Fresnel("Fresnel Intensity", Range(0,200)) = 3.0
_FresnelWidth("Fresnel Width", Range(0,2)) = 3.0
_Distort("Distort", Range(0, 100)) = 1.0
_IntersectionThreshold("Highlight of intersection threshold", range(0,1)) = .1 //Max difference for intersections
_ScrollSpeedU("Scroll U Speed",float) = 2
_ScrollSpeedV("Scroll V Speed",float) = 0
//[ToggleOff]_CullOff("Cull Front Side Intersection",float) = 1
}
SubShader
{
Tags{ "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
GrabPass{ "_GrabTexture" }
Pass
{
Lighting Off ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
fixed4 vertex : POSITION;
fixed4 normal: NORMAL;
fixed3 uv : TEXCOORD0;
};
struct v2f
{
fixed2 uv : TEXCOORD0;
fixed4 vertex : SV_POSITION;
fixed3 rimColor :TEXCOORD1;
fixed4 screenPos: TEXCOORD2;
};
sampler2D _MainTex, _CameraDepthTexture, _GrabTexture;
fixed4 _MainTex_ST,_MainColor,_GrabTexture_ST, _GrabTexture_TexelSize;
fixed _Fresnel, _FresnelWidth, _Distort, _IntersectionThreshold, _ScrollSpeedU, _ScrollSpeedV;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//scroll uv
o.uv.x += _Time * _ScrollSpeedU;
o.uv.y += _Time * _ScrollSpeedV;
//fresnel
fixed3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
fixed dotProduct = 1 - saturate(dot(v.normal, viewDir));
o.rimColor = smoothstep(1 - _FresnelWidth, 1.0, dotProduct) * .5f;
o.screenPos = ComputeScreenPos(o.vertex);
COMPUTE_EYEDEPTH(o.screenPos.z);//eye space depth of the vertex
return o;
}
fixed4 frag (v2f i,fixed face : VFACE) : SV_Target
{
//intersection
fixed intersect = saturate((abs(LinearEyeDepth(tex2Dproj(_CameraDepthTexture,i.screenPos).r) - i.screenPos.z)) / _IntersectionThreshold);
fixed3 main = tex2D(_MainTex, i.uv);
//distortion
i.screenPos.xy += (main.rg * 2 - 1) * _Distort * _GrabTexture_TexelSize.xy;
fixed3 distortColor = tex2Dproj(_GrabTexture, i.screenPos);
distortColor *= _MainColor * _MainColor.a + 1;
//intersect hightlight
i.rimColor *= intersect * clamp(0,1,face);
main *= _MainColor * pow(_Fresnel,i.rimColor) ;
//lerp distort color & fresnel color
main = lerp(distortColor, main, i.rimColor.r);
main += (1 - intersect) * (face > 0 ? .03:.3) * _MainColor * _Fresnel;
return fixed4(main,.9);
}
ENDCG
}
}
}
The problem is that it makes my lava texture really transparent but need it to be opaque. I also wanted the texture of the lava to gradually turn into another texture when near the intersection area (like a gradient). If someone can help me atleast make it so i can highlight it with some color i would be really grateful, i've tried changing the code all by myself but i'm a total shader noob.
Bump. Does anyone know how i can do this or has some kind of source material i could read?
Your answer
![](https://koobas.hobune.stream/wayback/20220612153058im_/https://answers.unity.com/themes/thub/images/avi.jpg)