shader知识点整理

利用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
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值