返回介绍

GL.GetGPUProjectionMatrix 获取GPU投影矩阵

发布于 2019-12-18 15:37:43 字数 3426 浏览 1271 评论 0 收藏 0

JavaScript => public static function GetGPUProjectionMatrix(proj: Matrix4x4, renderIntoTexture: bool): Matrix4x4;
C# => public static Matrix4x4 GetGPUProjectionMatrix(Matrix4x4 proj, bool renderIntoTexture);

Description 描述

Compute GPU projection matrix from camera's projection matrix.

从相机的GPU投影矩阵计算GPU投影矩阵。

In Unity, projection matrices follow OpenGL convention. However on some platforms they have to be transformed a bit to match the native API requirements. Use this function to calculate how the final projection matrix will be like. The value will match what comes as UNITY_MATRIX_P matrix in a shader.

在Unity,投影矩阵遵循OpenGL公约,然而某些平台它们需要变换位来匹配本地API需要。使用此功能来计算最终的投影矩阵。这个值匹配shader里的UNITY_MATRIX_P matrix

The renderIntoTexture value should be set to true if you intend to render into a RenderTexture with this projection matrix. On some platforms it affects how the final matrix will look like.

如果你打算这个投影矩阵渲染到渲染纹理,renderIntoTexture值应该设置为true。在某些平台它影响到最终的矩阵的样子。

C#:

	public GameObject debugObject;
	// Use this for initialization
	void Start () {
		Matrix4x4 P = GL.GetGPUProjectionMatrix(Camera.main.projectionMatrix, false);
		Matrix4x4 V = Camera.main.worldToCameraMatrix;
		Matrix4x4 M = debugObject.GetComponent<Renderer>().localToWorldMatrix;
		Matrix4x4 MVP = P * V * M;
		debugObject.GetComponent<Renderer>().material.SetMatrix("_MATRIX_MVP", MVP);
 
	}

把debugObject对象给个材质用这个shader

Shader "Custom/Test" {
    Properties {
        _MainTex("Texture", 2D) = "white" { }  
    }
 
    SubShader{ 
 
    Pass{
 
    Cull Back
 
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
 
    #include "UnityCG.cginc"
 
    sampler2D _MainTex;
    float4x4 _MATRIX_MVP;
 
    struct v2f{
        float4  pos : SV_POSITION;
        float2  uv : TEXCOORD0;
    };
 
    v2f vert(appdata_base v){
 
        v2f o;
        float2 screenSpacePos;
        float4 clipPos;
 
        //Convert position from world space to clip space.
        //Only the UV coordinates should be frozen, so use a different matrix
        clipPos = mul(_MATRIX_MVP, v.vertex);
 
        //Convert position from clip space to screen space.
        //Screen space has range x=-1 to x=1
        screenSpacePos.x = clipPos.x / clipPos.w;
        screenSpacePos.y = clipPos.y / clipPos.w;
 
        //the screen space range (-1 to 1) has to be converted to
        //the UV range 0 to 1
        o.uv.x = (0.5f*screenSpacePos.x) + 0.5f;
        o.uv.y = (0.5f*screenSpacePos.y) + 0.5f;
 
        //The position of the vertex should not be frozen, so use
        //the standard UNITY_MATRIX_MVP matrix
        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
        return o;
    }
 
    half4 frag(v2f i) : COLOR{
 
        half4 texcol = tex2D(_MainTex, i.uv);
        return texcol;
    }
 
    ENDCG
 
    }
    }
} 

GL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文