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.

MaterialPropertyDrawer in Shader – GUI without creating shaderGUI



Shader "MaterialPropertyDrawer"
_MainTex("Texture", 2D) = "white" {}

[HideInInspector] _MainTex2(“Hide Texture”, 2D) = “white” {}

[NoScaleOffset] _MainTex3(“No Scale/Offset Texture”, 2D) = “white” {}

[PerRendererData] _MainTex4(“PerRenderer Texture”, 2D) = “white” {}

[Normal] _MainTex5(“Normal Texture”, 2D) = “white” {}

_Color(“Color”, Color) = (1,0,0,1)

[HDR] _HDRColor(“HDR Color”, Color) = (1,0,0,1)

_Vector(“Vector”, Vector) = (0,0,0,0)

//Can’t go below zero
[Gamma] _GVector(“Gamma Vector”, Vector) = (0,0,0,0)

// Header creates a header text before the shader property.
[Header(A group of things)]

// Will set “_INVERT_ON” shader keyword when set
[Toggle] _Invert(“Auto keyword toggle”, Float) = 0

// Will set “ENABLE_FANCY” shader keyword when set.
[Toggle(ENABLE_FANCY)] _Fancy(“Keyword toggle”, Float) = 0

// Will show when ENABLE_FANCY is true //Feature request
//[ShowIf(ENABLE_FANCY)] _ShowIf(“Show If”, Float) = 0

// Blend mode values
[Enum(UnityEngine.Rendering.BlendMode)] _Blend(“Blend mode Enum”, Float) = 1

// A subset of blend mode values, just “One” (value 1) and “SrcAlpha” (value 5).
[Enum(One,1,SrcAlpha,5)] _Blend2(“Blend mode subset”, Float) = 1

// Each option will set _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY shader keywords.
[KeywordEnum(None, Add, Multiply)] _Overlay(“Keyword Enum”, Float) = 0
// …later on in CGPROGRAM code:
// …

// A slider with 3.0 response curve
[PowerSlider(3.0)] _Shininess(“Power Slider”, Range(0.01, 1)) = 0.08

// An integer slider for specified range (0 to 255)
[IntRange] _Alpha(“Int Range”, Range(0, 255)) = 100

// Default small amount of space.
[Space] _Prop1(“Small amount of space”, Float) = 0

// Large amount of space.
[Space(50)] _Prop2(“Large amount of space”, Float) = 0


📌Shader Tutorial Links

[This post would keep updated]
The tutorials are originally made for internal knowledge share. But because these knowledge are so useful to everyone who uses Unity, so I got the permission to share them.

The goal of this series of tutorials is not just teaching you how to write shaders — is to learn how important shader is and WHY & HOW IT AFFECTS YOUR GAME!

I was a game artist before and I understand the pain. I was always being questioned about how to optimize games and how to reduce draw calls. The concept is always the same and this is what I want to share to all of you.

Continue reading

A change for blogging

I havn’t been authoring any post for years as I had moved to United Kingdom in 2016, which is definitely a big change in my life and I enjoyed so much.

I’m working in Unity now and found that I gain new knowledge EVERYDAY.

To tackle the knowledge wave, I used to keep links or upload pics to my personal fb and share with my fds in game industry. But I found that it’s limiting my content sizes and hard for me to trace the notes.

And for the blog here, the dark theme was too unclear and spaces are too small. So now I changed the theme to see how it goes 😀


So for now on, I hope to share the knowledge I gained and this blog would also serve as my own note.


I’m currently working on Shader tutorials which are having lots of pictures and simple description so that people who are new to Unity can learn the techniques as well as the software itself.


Learning would never stop!