Built-in shader helper functions 内置shader帮助函数
本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html
Unity has a number of built-in utility functions designed to make writing shaders simpler and easier.
Unity有许多内置的实用程序函数,旨在使书写着色器更简单和容易。
Functions declared in UnityCG.cginc UnityCG.cginc中的函数声明
See Built-in shader include files for an overview of shader include files provided with Unity.
Vertex transformation functions in UnityCG.cginc
- float4 UnityObjectToClipPos(float3 pos) Transforms a point from object space to the camera’s clip space in homogeneous coordinates. This is the equivalent of mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), and should be used in its place. 将点从对象空间转换为摄像机裁剪空间在齐次坐标系中。这相当于mul(UNITY_MATRIX_MVP, float4(pos, 1.0)),应该替代后者。
- float3 UnityObjectToViewPos(float3 pos) Transforms a point from object space to view space. This is the equivalent of mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz, and should be used in its place. 将点从对象空间转换为视图空间。这相当于mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz,应该替代后者。
Generic helper functions in UnityCG.cginc
- float3 WorldSpaceViewDir (float4 v) Returns world space direction (not normalized) from given object space vertex position towards the camera. 从给定的对象空间顶点位置到摄像机,作为返回世界空间方向(未归一化)。
- float3 ObjSpaceViewDir (float4 v) Returns object space direction (not normalized) from given object space vertex position towards the camera. 从给定的对象空间顶点位置到摄像机,作为返回对象空间方向(未归一化)。
- float2 ParallaxOffset (half h, half height, half3 viewDir) calculates UV offset for parallax normal mapping. 计算视差法线贴图的UV偏移
- fixed Luminance (fixed3 c) Converts color to luminance (grayscale).将颜色转换为亮度(灰度)。
- fixed3 DecodeLightmap (fixed4 color) Decodes color from Unity lightmap (RGBM or dLDR depending on platform). 从Unity 光照图(基于平台的RGBM或dLDR)解码颜色。
- float4 EncodeFloatRGBA (float v) Encodes [0…1) range float into RGBA color, for storage in low precision render target. 编码[0…1)范围浮点值到RGBA颜色,存储在低精度渲染目标。
- float DecodeFloatRGBA (float4 enc) Decodes RGBA color into a float. 将RGBA颜色解码为一个浮点数。
- float2 EncodeFloatRG (float v) Encodes [0…1) range float into a float2. 编码[0…1)范围浮点数到低精度float2。
- float DecodeFloatRG (float2 enc) Decodes a previously-encoded RG float. 解码先前编码的RG浮点数到float。
- float2 EncodeViewNor