美文网首页
矩阵变换_C#_解释矩阵变换旋转原理

矩阵变换_C#_解释矩阵变换旋转原理

作者: Rayson | 来源:发表于2020-06-30 17:11 被阅读0次

    C#

    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 () {
            Matrix4x4 RM =new Matrix4x4();
            RM[0,0] =Mathf.Cos (Time.realtimeSinceStartup);
            RM[0,2] =Mathf.Sin(Time.realtimeSinceStartup);
            RM[1,1] = 1;
    
            RM[2,0] =-Mathf.Sin (Time.realtimeSinceStartup);
            RM[2,2] =Mathf.Cos(Time.realtimeSinceStartup);
            RM[3,3] = 1;
    
    
    
            Matrix4x4 mvp =  Camera.main.projectionMatrix* Camera.main.worldToCameraMatrix *transform.localToWorldMatrix*RM ;
            GetComponent<Renderer>().material.SetMatrix("mvp",mvp);
            
        
            
        }
    }
    
    

    shader

    
    Shader "Unlit/V2f"
    {
        SubShader{
                pass{
                    
                    CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
                float4x4 mvp;
    
    
                struct a2v
                {
                     float4 vertex : POSITION;
                    float3 normal : NORMAL;
                    float4 texcoord : TEXCOORD0;
                };
                
                struct v2f {
                    
                    float4 pos:POSITION;
                };          
    
                v2f vert(a2v v)
                {
                    v2f o;
                    //o.pos = UnityObjectToClipPos(v.vertex);       //核心: 矩阵变换 当前模型*视图*投影矩阵
                    o.pos= mul(mvp,v.vertex);
                    return o;
    
                }   
    
                fixed frag():SV_TARGET
                {
                    return fixed4 (1,1,1,1);
    
                }
    
                ENDCG
     
                }
    
    
    
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:矩阵变换_C#_解释矩阵变换旋转原理

          本文链接:https://www.haomeiwen.com/subject/ddwxqktx.html