用户工具

站点工具


侧边栏



Unity3D中文文档

script:unityengine:classes:gl:gl.getgpuprojectionmatrix

GL.GetGPUProjectionMatrix 获取GPU投影矩阵

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

script/unityengine/classes/gl/gl.getgpuprojectionmatrix.txt · 最后更改: 2016/08/02 05:24 (外部编辑)