Shader methods in the Built-In Render Pipeline reference
Use built-in functions to transform positions and other values.
読み終わるまでの所要時間 5 分最終更新 11日前
Unity has a number of built-in utility functions in UnityCG.cginc designed to make writing shaders simpler and easier.
Transform positions
Function: | Description: |
|---|---|
| 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. |
| 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. |
Transform other values
Function: | Description: |
|---|---|
| Returns world space direction (not normalized) from given object space vertex position towards the camera. |
| Returns object space direction (not normalized) from given object space vertex position towards the camera. |
| calculates UV offset for parallax normal mapping. |
| Converts color to luminance (grayscale). |
| Decodes color from Unity lightmap (RGBM or dLDR depending on platform). |
| Encodes (0..1) range float into RGBA color, for storage in low precision render target. |
| Decodes RGBA color into a float. |
| Encodes (0..1) range float into a float2. |
| Decodes a previously-encoded RG float. |
| Encodes view space normal into two numbers in 0..1 range. |
| Decodes view space normal from enc4.xy. |
Forward rendering helper functions
These functions are only useful when using forward rendering (ForwardBase or ForwardAdd pass types).
Function: | Description: |
|---|---|
| Computes world space direction (not normalized) to light, given object space vertex position. |
| Computes object space direction (not normalized) to light, given object space vertex position. |
| Computes illumination from four point lights, with light data tightly packed into vectors. Forward rendering uses this to compute per-vertex lighting. |
Screen-space helper functions
The following functions are helpers to compute coordinates used for sampling screen-space textures. They return where the final coordinate to sample texture with can be computed via perspective division (for example ).
float4xy/wThe functions also take care of platform differences in render texture coordinates.
Function: | Description: |
|---|---|
| Computes texture coordinate for doing a screenspace-mapped texture sample. Input is clip space position. |
| Computes texture coordinate for sampling a GrabPass texure. Input is clip space position. |
Vertex-lit helper functions
These functions are only useful when using per-vertex lit shaders ("Vertex" pass type).
Function: | Description: |
|---|---|
| Computes illumination from four per-vertex lights and ambient, given object space position & normal. |
Texture and sampler macros
Macro: | Use: |
|---|---|
| Declares a Texture and Sampler pair. |
| Declares a Texture without a Sampler. |
| Declares a Texture array Sampler variable. |
| Sample from a Texture and Sampler pair, using given Texture coordinate. |
| Sample from Texture (name), using a Sampler from another Texture (samplername). |
| Sample from a Texture array with a float3 UV; the z component of the coordinate is array element index. |
| Sample from a Texture array with an explicit mipmap level. |
Shadow mapping macros
Declaring and sampling shadow maps can be very different depending on the platform. Unity has several macros to help with this:
Macro: | Use: |
|---|---|
| Declares a shadowmap Texture variable with name "tex". |
| Samples shadowmap Texture "tex" at given "uv" coordinate (XY components are Texture location, Z component is depth to compare with). Returns single float value with the shadow term in 0..1 range. |
| Similar to above, but does a projective shadowmap read. "uv" is a float4, all other components are divided by .w for doing the lookup. |
The format of must be RenderTextureFormat.Shadowmap.
texDepth Texture helper macros
Most of the time, Depth Texture are used to render Depth from the Camera. The UnityCG.cginc include file contains some macros to deal with the above complexity in this case:
- COMPUTE_EYEDEPTH(i): computes eye space depth of the vertex and outputs it in o. Use it in a vertex program when not rendering into a depth texture.
- DECODE_EYEDEPTH(i)/LinearEyeDepth(i): given high precision value from depth texture i, returns corresponding eye space depth.
- Linear01Depth(i): given high precision value from depth texture i, returns corresponding linear depth in range between 0 and 1.
- UNITY_TRANSFER_DEPTH(o): Deprecated. Computes the eye space depth of the vertex and outputs it in o (which must be a float2). On platforms with native depth textures this macro does nothing, because the Z buffer value is rendered implicitly.
- UNITY_OUTPUT_DEPTH(i): Deprecated. Returns eye space depth from i (which must be a float2). On platforms with native depth textures this macro always returns zero, because the Z buffer value is rendered implicitly.
For example, this shader would render depth of its GameObjects:
Shader "Render Depth" { SubShader { Tags { "RenderType"="Opaque" } Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float2 depth : TEXCOORD0; }; v2f vert (appdata_base v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); UNITY_TRANSFER_DEPTH(o.depth); return o; } half4 frag(v2f i) : SV_Target { UNITY_OUTPUT_DEPTH(i.depth); } ENDHLSL } }}