Unity 具有许多内置实用函数,旨在使编写着色器更简单,更轻松。
请参阅内置着色器 include 文件来大致了解 Unity 随附的着色器 include 文件。
功能: | 描述: |
---|---|
float4 UnityObjectToClipPos(float3 pos) |
将对象空间中的点变换到齐次坐标中的摄像机裁剪空间。这等效于 mul(UNITY_MATRIX_MVP, float4(pos, 1.0)),应该在适当的位置使用。 |
float3 UnityObjectToViewPos(float3 pos) |
将对象空间中的点变换到视图空间。这等效于 __mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz__,应该在适当的位置使用。 |
功能: | 描述: |
---|---|
float3 WorldSpaceViewDir (float4 v) |
返回从给定对象空间顶点位置朝向摄像机的世界空间方向(未标准化)。 |
float3 ObjSpaceViewDir (float4 v) |
返回从给定对象空间顶点位置朝向摄像机的对象空间方向(未标准化)。 |
float2 ParallaxOffset (half h, half height, half3 viewDir) |
计算视差法线贴图的 UV 偏移。 |
fixed Luminance (fixed3 c) |
将颜色转换为亮度(灰阶)。 |
fixed3 DecodeLightmap (fixed4 color) |
从 Unity 光照贴图(RGBM 或 dLDR,具体取决于平台)解码颜色。 |
float4 EncodeFloatRGBA (float v) |
将 [0..1) 范围浮点数编码为 RGBA 颜色,用于存储在低精度渲染目标中。 |
float DecodeFloatRGBA (float4 enc) |
将 RGBA 颜色解码为浮点数。 |
float2 EncodeFloatRG (float v) |
将 [0..1) 范围浮点数编码为 float2。 |
float DecodeFloatRG (float2 enc) |
解码先前编码的 RG 浮点数。 |
float2 EncodeViewNormalStereo (float3 n) |
将视图空间法线编码为 0 到 1 范围内的两个数字。 |
float3 DecodeViewNormalStereo (float4 enc4) |
从 enc4.xy 解码视图空间法线。 |
仅当使用前向渲染(ForwardBase 或 ForwardAdd 通道类型)时,这些函数才有用。
功能: | 描述: |
---|---|
float3 WorldSpaceLightDir (float4 v) |
根据给定的对象空间顶点位置计算朝向光源的世界空间方向(未标准化)。 |
float3 ObjSpaceLightDir (float4 v) |
根据给定对象空间顶点位置计算朝向光源的对象空间方向(未标准化)。 |
float3 Shade4PointLights (...) |
计算四个点光源的光照,将光源数据紧密打包到矢量中。前向渲染使用它来计算每顶点光照。 |
以下 helper 函数可计算用于采样屏幕空间纹理的坐标。它们返回 float4
,其中用于纹理采样的最终坐标可以通过透视除法(例如 xy/w
)计算得出。
这些函数还处理渲染纹理坐标中的平台差异。
功能: | 描述: |
---|---|
float4 ComputeScreenPos (float4 clipPos) |
计算用于执行屏幕空间贴图纹理采样的纹理坐标。输入是裁剪空间位置。 |
float4 ComputeGrabScreenPos (float4 clipPos) |
计算用于 GrabPass 纹理采样的纹理坐标。输入是裁剪空间位置。 |
仅当使用每顶点光照着色器(“Vertex”通道类型)时,这些函数才有用。
功能: | 描述: |
---|---|
float3 ShadeVertexLights (float4 vertex, float3 normal) |
根据给定的对象空间位置和法线计算四个每顶点光源和环境光的光照。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.