unity 顶点shader制作流光

面片的顶点不多,所以判断不能精确的使用==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);
        }
    }
}

在这里插入图片描述

发布了67 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Icecoldless/article/details/103962119