Shader "Unlit/vf2"
{
//in out 输入与输出
Properties
{
_MainColor("我是主颜色!",Color)=(1,0,0,1)
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//属性变量名 应与 Properties里的_MainColor相同
float4 _MainColor;
//通过C#脚本来改变赋值 (通过此方式声明的属性 在属性面板找不到)
uniform float4 _SecondColor;
//方式一
//输出结构体
struct v2f{
float4 pos:POSITION;
float2 objPos:TEXCOORD0;
float4 col:COLOR;
};
//in 输入结构体
struct inInfo{
float2 objPos:POSITION;
fixed4 col:COLOR;
};
//声明方式二 取别名
用结构体代替 vert里的out输出
// typedef struct {
// float4 pos:POSITION;
// float4 col:COLOR0;
// }v2f;
//vert 中in输入接收到物体传过来的 objPos物体坐标位置
// out将颜色信息 输出到frag中 进行颜色的下一步处理
//vert中可以有多个out输出 但语义不能重复 out float4 pos:POSITION,out float4 col:COLOR0
v2f vert(inInfo inInfo)
{
v2f o;
o.pos = float4(inInfo.objPos,0,1);
o.objPos = float2(1,0);
o.col = inInfo.col; //CG中 只要数据类型相同 就可以赋值 与语义无关
return o;
}
//frag 里in输入 接收vert里传过来的数据
// out输出到游戏物体上 看到最终效果 inout float4 col:COLOR
fixed4 frag (v2f IN):COLOR //方法函数后跟 :语义 (意思就是给函数返回值定义一个语义)
{
// return col * _MainColor * 3 * _SecondColor;
// In.col = float4(1,1,1,1);
// return IN.col ;
// return _MainColor * 0.5 + _SecondColor * 0.5;
return lerp(_MainColor,_SecondColor,0.6);
}
ENDCG
}
}
}
Uniform.cs 脚本的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Uniform : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Renderer>().material.SetVector("_SecondColor", new Vector4(1, 0, 0, 1));
}
}
效果: