- Home /
Unity 5.5: Mesh Intersection to be rendered invisible with shader? (Pictures)
I'm trying to make a single shader that checks if it has collided with a mesh (with the same shader), if it has, then it should not render that collided part at all. For both meshes.
I have used a crosssection shader to achieve this but it's not what I want:
This is the shader code:
// Upgrade NOTE: replaced 'glstate.matrix.invtrans.modelview[0]' with 'UNITY_MATRIX_IT_MV'
// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
// 2 Pass Edition
Shader "cross_section_v004a"
{
Properties
{
section_depth ("section depth (x, y, z, depth)", vector) = (0,0,0,0.15)
section_color ("section color", color) = (0.5,0.1, 0.1, 1)
color_map ("color map", 2D) = "white" {}
}
SubShader
{
Pass
{
CULL OFF
CGPROGRAM //--------------
//#pragma target 3.0
#pragma vertex vertex_shader
#pragma fragment fragment_shader
#include "UnityCG.cginc"
uniform float4 section_depth;
uniform float4 section_color;
uniform sampler2D color_map;
float4x4 rotate(float3 r)
{
float3 c, s;
sincos(r.x, s.x, c.x);
sincos(r.y, s.y, c.y);
sincos(r.z, s.z, c.z);
return float4x4( c.y*c.z, -s.z, s.y, 0,
s.z, c.x*c.z, -s.x, 0,
-s.y, s.x, c.x*c.y, 0,
0, 0, 0, 1 );
}
struct a2v
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD;
float3 normal : NORMAL;
};
struct v2f
{
float4 position : POSITION;
float2 texcoord : TEXCOORD0;
float4 normal : TEXCOORD1;
float4 vertex : TEXCOORD2;
float4 mask : TEXCOORD3;
};
v2f vertex_shader( a2v IN )
{
v2f OUT;
float4x4 r = rotate(radians(section_depth.xyz) +_SinTime.xyz);
float4 c = float4(IN.vertex.xyz,1);
OUT.mask = mul(r, c);
OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
r *= float4x4( 1,-1,-1, 0,
-1, 1,-1, 0,
-1,-1, 1, 0,
0, 0, 0, 1 ); // the section_depth.xyz need to be inverted !
OUT.normal = mul(r, float4(1,0,0,1));
OUT.vertex = IN.vertex;
return OUT;
}
void fragment_shader( v2f IN,
out float4 finalcolor : COLOR)
{
if(IN.mask.x > section_depth.w)
discard;
float3 N = IN.normal.xyz;
N = mul(UNITY_MATRIX_IT_MV, float4(N, 1));
//float diffuse = saturate(dot(glstate.light[0].position, N));
finalcolor = float4(0,0,0,1);
finalcolor.xyz = section_color *(0.6 +0.4);
}
ENDCG //--------------
} // Pass
//---------------------------------------------------------------------------------
Pass
{
CULL BACK
CGPROGRAM //--------------
#pragma vertex vertex_shader
#pragma fragment fragment_shader
#include "UnityCG.cginc"
uniform float4 section_depth;
uniform float4 section_color;
uniform sampler2D color_map;
float4x4 rotate(float3 r)
{
float3 c, s;
sincos(r.x, s.x, c.x);
sincos(r.y, s.y, c.y);
sincos(r.z, s.z, c.z);
return float4x4( c.y*c.z, -s.z, s.y, 0,
s.z, c.x*c.z, -s.x, 0,
-s.y, s.x, c.x*c.y, 0,
0, 0, 0, 1 );
}
struct a2v
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD;
float3 normal : NORMAL;
};
struct v2f
{
float4 position : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : TEXCOORD1;
float4 vertex : TEXCOORD2;
float4 mask : TEXCOORD3;
};
v2f vertex_shader( a2v IN )
{
v2f OUT;
float4x4 r = rotate(radians(section_depth.xyz) +_SinTime.xyz);
float4 c = float4(IN.vertex.xyz,1);
OUT.mask = mul(r, c);
OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.normal = IN.normal;
OUT.vertex = IN.vertex;
return OUT;
}
void fragment_shader( v2f IN,
out float4 finalcolor : COLOR)
{
if(IN.mask.x > section_depth.w)
discard;
float3 N = IN.normal;
N = mul(UNITY_MATRIX_IT_MV, float4(N, 1));
//float diffuse = saturate(dot(glstate.light[0].position, N));
finalcolor = float4(0,0,0,1);
finalcolor.xyz = tex2D(color_map, IN.texcoord).xyz *(0.6 +0.4);
}
ENDCG //--------------
} // Pass
} // SubShader
} // Shader
However, the rest of the object should be visible, I mean to say that I don't want the parts that are NOT getting intersected to be invisible (when viewed from the invisible parts) so Stencil shaders won't or aren't doing the trick. I mean to get something like this:
I want to know what is the correct way to approach to this problem. I'm fairly new to shader programming and I don't know to solve this issue. Any help would be appreciated.
Thanks!
Your answer
Follow this Question
Related Questions
Adding a Stencil to Particle Standard Surface shader 0 Answers
Add to existing shader at runtime 1 Answer
Standard Shader Still Visible through Stencil Shader 0 Answers
Problems importing plane mesh from blender to unity 1 Answer
How to show only the intersection between two meshes at runtime? 0 Answers