- Home /
Question by
RuzgarBayindir · Aug 24, 2020 at 09:08 AM ·
c#shaderunityeditorintersection
How do I paint the part of a model that is embedded in another model?,
Picture 1.1
I want the part of the cube inside the wall to be painted green like this, as seen in photo 1.1. I found Intersection shader and I used it to create this example. However, when I change the angle of the camera, it becomes what you see in photo 1.2.
Picture 1.2
Even if the camera position and angle changes, I always need the form in photo 1.1. How can I do that?
Shader Code:
// MIT License
//
// Copyright (c) 2017 Dustin Whirle
Shader "Custom/Unlit/Zex"
{
Properties
{
_MainColor("Main Color", Color) = (1, 1, 1, 0.25)
_IntersectionColor("Intersection Color", Color) = (1, 1, 1, 1)
_IntersectionMax("Intersection Max", Range(0.01,5)) = 1
_IntersectionDamper("Intersection Damper", Range(0,1)) = 0
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 100
// for use in other shaders
// called with -> UsePass "Custom/Unlit/Intersection_Color/PASS"
Pass
{
Name "PASS"
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
// Cull Off //if you want to see back faces
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _CameraDepthTexture; //<- built-in
half4 _MainColor;
half4 _IntersectionColor;
half _IntersectionMax;
half _IntersectionDamper;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 screenPos : TEXCOORD0;
float4 vertex : SV_POSITION;
// fog
UNITY_FOG_COORDS(1)
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
// fog
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float bufferZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)).r);
float pixelZ = i.screenPos.z;
float intersection_dist = sqrt(pow(bufferZ - pixelZ,2));
half highlight_mask = max(0, sign(_IntersectionMax - intersection_dist));
highlight_mask *= 1 - intersection_dist / _IntersectionMax * _IntersectionDamper;
fixed4 col = _MainColor;
highlight_mask *= _IntersectionColor.a;
col *= (1-highlight_mask);
col += _IntersectionColor * highlight_mask;
col.a = max(highlight_mask, col.a);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
FallBack "Unlit/Color"
}
,
screenshot-35.png
(18.8 kB)
screenshot-37.png
(27.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do I get this stylized look in unity? 1 Answer
How to whirl effect in shader graphs? 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer