我正在尝试制作一个单一的着色器,检查它是否与网格物体(使用相同的着色器)发生碰撞,如果有,则根本不应该渲染该碰撞部分。对于两个网格。
这是着色器代码:
// 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
然而,对象的其余部分应该是可见的,我的意思是说我不希望不相交的部分是不可见的(当从不可见的部分查看时),因此模板着色器不会或不是不要这么做。像这样的东西:
我想知道解决这个问题的正确方法是什么。我对着色器编程很新,我不知道要解决这个问题。任何帮助将不胜感激。
谢谢!