利用shader来做图片颜色变化和旋转大小变化
using System.Collections;
using System.Collections.Generic;using UnityEngine;
public class Transform2 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//GetComponent<Renderer>().material.SetVector("mvp",);
//GetComponent<Renderer>().material.SetVector("mainColor",new Vector4(1,0,0,1));
Matrix4x4 RM= new Matrix4x4();
//绕z轴旋转
//RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);
//RM[0, 1] = Mathf.Sin(Time.realtimeSinceStartup);
//RM[1, 0] = -Mathf.Sin(Time.realtimeSinceStartup);
//RM[1, 1] = Mathf.Cos(Time.realtimeSinceStartup);
//RM[2, 2] = 1;
//RM[3, 3] = 1;
print((new Vector2(1, 5)).magnitude);
RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);
RM[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);
RM[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);
RM[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);
RM[1, 1] = 1;
RM[3, 3] = 1;
Matrix4x4 scaleM = new Matrix4x4();
scaleM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup)/8+0.5f;
scaleM[1, 1] = 1; //Mathf.Sin(Time.realtimeSinceStartup)/6+0.5f;
scaleM[2, 2] = 1;//Mathf.Cos(Time.realtimeSinceStartup)/4+0.5f;
scaleM[3, 3] = 1;
Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix* transform.localToWorldMatrix*RM* scaleM;//模型到世界
GetComponent<Renderer>().material.SetMatrix("mvp",mvp);
}
}
、、、、、、、、
shader文件
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/VerTexShader" {
SubShader {
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unityCG.cginc"
struct v2f
{
fixed4 col:COLOR;
float4 pos:POSITION;
};
float4x4 mvp;
fixed4 mainColor;
v2f vert(appdata_base v)//返回值类型为结构体 函数不需要添加语义
{
//pos=float4(objpos,0,1);
//col=pos;
//if(objpos.x<0)
//{
// col=fixed4(1,0,0,1);
//}else
//{
// col=fixed4(0,0,1,1);
//}
//if(objpos.y>0)
//{
// col=fixed4(0.5,0,0.5,1);
//}
//else
//{
// col=fixed4(0,1,0,1);
//}
//if(objpos.x==-0.5&&objpos.y==-0.5){
// col=fixed4(1,0,0,1);
//}
//else if(objpos.x==-0.5&&objpos.y==0.5)
//{
// col=fixed4(0,1,0,1);
//}
//else if(objpos.x==0.5&&objpos.y==-0.5)
//{
// col=fixed4(0,0,1,1);
//}
//else if(objpos.x==0.5&&objpos.y==0.5)
//{
// col=fixed4(0.5,0.5,0,1);
//}
//if( objpos.x>-0.5&&objpos.x<0&&objpos.y>-0.5&&objpos.y<0){
// col=fixed4(1,0,0,1);
//}
//else
//{
// col=fixed4(1,1,1,1);
//}
v2f o;
//o.pos=UnityObjectToClipPos(v.vertex);
o.pos=mul(mvp,v.vertex);
o.col=o.pos;
return o;
}
fixed4 frag(v2f v):COLOR//如果是函数返回值加语义 ,语义要加载函数小括号的后面
{
// //col=fixed4(1,0,0,1);
// float4 temp=float4(1,1,1,1);
// float3 temp2=float3(0,0,0);
// //col=float4(temp.xyzw);
// col=float4(temp.rgba);
// col=float4(temp2.xy,temp.xy);
// col=float4(temp.rg,temp.zy);
// // col=float4(temp.rgxy);//这句是有误的 rgba 和xyzw 不能一起出现 如果是float3的化没有w这个分量
// col=float4(temp.xxxx);
fixed4 col=mainColor;
return v.col;
}
ENDCG
}
}
}