UnityObjectToClipPos(use float4 instead of float3)

In the past, we uses mul(UNITY_MATRIX_MVP, v.vertex) to convert vertex position from local to world space. v.vertex is float4 which has w component.

But in most cases w is = 1. To make vertex shader run faster, Unity replaced it with UnityObjectToClipPos(float3 pos), which ignores w component even you pass a float4 position instead of float3.

For some advanced users who still need the w component in their custom shaders, here is a cheaper UnityObjectToClipPos() function which respects the w component!😄

// More efficient than computing M*VP matrix product
inline float4 UnityObjectToClipPosRespectW(in float4 pos)
{
    return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, pos));
}

This is provided by one of the Unity graphics developer, Yao.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s