1.shader代码
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Custom/OpaVertColor"
{
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 vcol : COLOR;
};
v2f vert (appdata v)
{
v2f o;
float4 wpos = mul(unity_ObjectToWorld,v.vertex);
if(wpos.x > 0){
o.vcol = fixed4(1,0,0,1);
}else{
o.vcol = fixed4(0,0,1,1);
}
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.vcol;
}
ENDCG
}
}
}
2.c#代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCube : MonoBehaviour
{
void Start()
{
}
void Update()
{
transform.position = new Vector3(Mathf.Sin(Time.realtimeSinceStartup * 2.0f) * 2.0f, 0, 0);
}
}
3.效果: