Vertex Shader几何变换---MVP矩阵变换2

遇到的问题:运行时mul()函数总是出错


1、

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

public class MVPTransform : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //4x4矩阵旋转矩阵
        Matrix4x4 RM = new Matrix4x4();
        RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);                    //第1行第1列
        RM[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);                    //第1行第3列
        RM[1, 1] = 1;                                                       //第1行第2列
        RM[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);                   //第3行第1列
        RM[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);                    //第3行第3列
        RM[3, 3] = 1;                                                       //第4行第4列


        //transform.localToWorldMatrix:物体的模型到世界矩阵的包装
        //Camera.main.worldToCameraMatrix:从世界到摄像机的矩阵
        //Camera.main.projectionMatrix:投影的矩阵
        //矩阵相乘是有顺序的,如果顺序不对,则有可能出错
        Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix*RM;

        //给mvp赋值
        GetComponent<Renderer>().material.SetMatrix("mvp",mvp);

    }
}



// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/s_eight" {

	SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"

		float4x4 mvp;

		struct v2f {
			float4 pos:POSITION;
		};



		v2f vert(appdata_base v) {
			v2f o;
			//o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			//使用自定义的新的矩阵来影响矩阵
			o.pos = mul(mvp, v.vertex);
			return o;
		}


		fixed4 frag():COLOR{
			return fixed4(1,1,1,1);
		}
		ENDCG
	}
	}
}


矩阵在旋转:





2、将矩阵作为参数传递

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

public class MVPTransform : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //4x4矩阵旋转矩阵
        Matrix4x4 RM = new Matrix4x4();
        RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);                    //第1行第1列
        RM[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);                    //第1行第3列
        RM[1, 1] = 1;                                                       //第1行第2列
        RM[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);                   //第3行第1列
        RM[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);                    //第3行第3列
        RM[3, 3] = 1;                                                       //第4行第4列


        //transform.localToWorldMatrix:物体的模型到世界矩阵的包装
        //Camera.main.worldToCameraMatrix:从世界到摄像机的矩阵
        //Camera.main.projectionMatrix:投影的矩阵
        //矩阵相乘是有顺序的,如果顺序不对,则有可能出错
        Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;

        //给矩阵rm赋值
        GetComponent<Renderer>().material.SetMatrix("rm",mvp);

    }
}


// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/s_eight" {

	SubShader{
		pass {
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"

			float4x4 mvp;
		float4x4 rm;

		struct v2f {
			float4 pos:POSITION;
		};



		v2f vert(appdata_base v) {
			v2f o;
			//与rm矩阵相乘得到一个新的旋转矩阵
			float4x4 m = mul(UNITY_MATRIX_MVP,rm);
			//再用新的旋转矩阵相乘得到最终的矩阵变换顶点
			o.pos = mul(m,v.vertex);
			//使用自定义的新的矩阵来影响矩阵
			//o.pos = mul(mvp, v.vertex);
			return o;
		}


		fixed4 frag() :COLOR{
			return fixed4(1,1,1,1);
		}
			ENDCG
	}
	}
}



运行后的结果:沿y轴旋转


3、缩放矩阵

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

public class MVPTransform : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //4x4矩阵旋转矩阵
        Matrix4x4 RM = new Matrix4x4();
        RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);                    //第1行第1列
        RM[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);                    //第1行第3列
        RM[1, 1] = 1;                                                       //第1行第2列
        RM[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);                   //第3行第1列
        RM[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);                    //第3行第3列
        RM[3, 3] = 1;                                                       //第4行第4列


        //缩放矩阵
        Matrix4x4 SM = new Matrix4x4();
        SM[0, 0] = Mathf.Sin(Time.realtimeSinceStartup);
        SM[1, 1] = Mathf.Cos(Time.realtimeSinceStartup);
        SM[2, 2] = Mathf.Sin(Time.realtimeSinceStartup);
        SM[3, 3] = 1;


        //transform.localToWorldMatrix:物体的模型到世界矩阵的包装
        //Camera.main.worldToCameraMatrix:从世界到摄像机的矩阵
        //Camera.main.projectionMatrix:投影的矩阵
        //矩阵相乘是有顺序的,如果顺序不对,则有可能出错
        Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;

        //给矩阵赋值
        GetComponent<Renderer>().material.SetMatrix("sm", SM);

    }
}


// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/s_eight" {

	SubShader{
		pass {
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"

		float4x4 mvp;
		float4x4 rm;
		float4x4 sm;

		struct v2f {
			float4 pos:POSITION;
		};



		v2f vert(appdata_base v) {
			v2f o;
			//与rm矩阵相乘得到一个新的旋转矩阵
			float4x4 m = mul(UNITY_MATRIX_MVP,sm);
			//再用新的旋转矩阵相乘得到最终的矩阵变换顶点
			o.pos = mul(m,v.vertex);
			//使用自定义的新的矩阵来影响矩阵
			//o.pos = mul(mvp, v.vertex);
			return o;
		}


		fixed4 frag() :COLOR{
			return fixed4(1,1,1,1);
		}
			ENDCG
	}
	}
}

运行后的结果:


4、分别控制不同轴向上的变换



5、

6、

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值