shader学习(二)顶点着色器

1、C#脚本设置shader参数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class class33 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Matrix4x4 SM = new Matrix4x4();//缩放矩阵 随时间动态变换
        SM[0, 0] = Mathf.Sin(Time.realtimeSinceStartup)/4+0.5f;
        SM[1, 1] = Mathf.Cos(Time.realtimeSinceStartup)/8+0.5f;
        SM[2, 2] = Mathf.Sin(Time.realtimeSinceStartup)/6+0.5f;
        SM[3, 3] = 1;
        Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;
        GetComponent<Renderer>().material.SetMatrix("sm", SM);
    }
}

2、在shader里做变换和颜色显示

Shader "Custom/shader33.1"
{
    SubShader
    {
        pass {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "unitycg.cginc"
         float4x4 mvp; 
        float4x4 rm;
        float4x4 sm;
        struct v2f
        {
           float4 pos:POSITION;
           fixed4 color : COLOR;
        };
        v2f vert(appdata_base v) {
            v2f o;
            // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
            float4 m = UnityObjectToClipPos(v.vertex);//顶点MVP变换
            o.pos = mul(m, sm);//控制缩放
            //1、控制顶点颜色
            if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5)
                o.color = fixed4(1, 0, 0, 1);
            else
                o.color = fixed4(0, 0, 1, 1);
            // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
            float4 wpos = mul(unity_ObjectToWorld, v.vertex);//转换模型坐标系到世界坐标系
			//2、改变物体位置时会改变颜色
            if (wpos.x > 0)
                o.color = fixed4(1, 0, 0, 1);
            else
                o.color = fixed4(0, 0, 1, 1);   
           //3、颜色周期变换
           if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5)
				o.color = fixed4(_SinTime.w/2+0.5,_CosTime.w/2+0.5, _SinTime.y/2+0.5, 1);
		   else
			    o.color = fixed4(0, 0, 1, 1);
            return o;    
            
        }
        fixed4 frag(v2f IN) :COLOR{
            return IN.color;
        }


        ENDCG
            }
    }
}

3、改变顶点的位置信息

Shader "Custom/shader35"
{
	properties{
		_R("R",range(0,5))=1
	}
	SubShader
	{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		float _R;
		struct v2f
		{
		   float4 pos:POSITION;
		   fixed4 color : COLOR;
		};
		v2f vert(appdata_base v) {
			v2f o;
			//float2 xy = v.vertex.xz;
			//float d =_R-length(xy);//计算模
			float4 wpos = mul(unity_ObjectToWorld, v.vertex);//转换到世界坐标系
			float2 xy = wpos.xz;
			float d = _R - length(xy);
			d= d < 0 ? 0 : d;
			float height = 1;
			float4 uppos = float4(v.vertex.x, height * d, v.vertex.z, v.vertex.w);
			o.pos = UnityObjectToClipPos(uppos);//透视
			o.color = fixed4(uppos.y,uppos.y,uppos.y,1);
			return o;
		}
		fixed4 frag(v2f IN) :COLOR{
			return IN.color;
		}
		ENDCG
		}
	}
}

你可能感兴趣的:(学习,着色器)