面片的顶点不多,所以判断不能精确的使用==0来判断,只能粗略的判断一个<0.1f了,根据直线x+y+b=0在外部变化b的值只要在直线上的点就可以设计为红色。
Shader "Custom/vex2" {
SubShader{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
float b;
struct vf {
float4 pos: POSITION;
fixed4 color : COLOR;
};
vf vert(appdata_base v)
{
vf o;
o.pos = UnityObjectToClipPos(v.vertex); //用最后矩阵*mvp矩阵
float x = o.pos.x / o.pos.w;
float y = o.pos.y / o.pos.w;
float tmp = x + y + b;
if (tmp < 0)
tmp = -1 * tmp;
if (tmp<=0.1)
o.color = fixed4(1, 0, 0, 1);
else
o.color = fixed4(1, 1, 1, 1);
return o;
}
fixed4 frag(vf IN) :COLOR
{
return IN.color;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace demospace
{
public class vex2 : MonoBehaviour
{
void Start()
{
}
float b = 2f;
void Update()
{
b -= Time.deltaTime;
if (b < -2f)
b = 2f;
GetComponent<Renderer>().material.SetFloat("b",b);
}
}
}