カスタムユニバーサルレンダーパイプライン (URP) シェーダーで位置を変換するには、次の手順に従います。
HLSLPROGRAM 内に #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" を追加します。Core.hlsl ファイルは ShaderVariablesFunction.hlsl ファイルをインポートします。ShaderVariablesFunction.hlsl ファイルの以下のメソッドのいずれかを使用します。| メソッド | 構文 | 説明 |
|---|---|---|
GetNormalizedScreenSpaceUV |
float2 GetNormalizedScreenSpaceUV(float2 positionInClipSpace) |
クリップスペース内の位置をスクリーンスペースに変換します。 |
GetObjectSpaceNormalizeViewDir |
half3 GetObjectSpaceNormalizeViewDir(float3 positionInObjectSpace) |
オブジェクト空間内の位置をビューアーに向かう正規化された向きに変換します。 |
GetVertexNormalInputs |
VertexNormalInputs GetVertexNormalInputs(float3 normalInObjectSpace) |
オブジェクト空間の頂点の法線をワールド空間の接線、従接線、法線に変換します。オブジェクト空間での法線と float4 接線の両方を入力することもできます。 |
GetVertexPositionInputs |
VertexPositionInputs GetVertexPositionInputs(float3 positionInObjectSpace) |
オブジェクト空間の頂点の位置をワールド空間、ビュー空間、クリップスペース、正規化されたデバイス座標の位置に変換します。 |
GetWorldSpaceNormalizeViewDir |
half3 GetWorldSpaceNormalizeViewDir(float3 positionInWorldSpace) |
ワールド空間の位置からビューアーに向きを返し、向きを正規化します。 |
GetWorldSpaceViewDir |
float3 GetWorldSpaceViewDir(float3 positionInWorldSpace) |
ワールド空間の位置からビューアーに向きを返します。 |
GetVertexNormalInputs メソッドを使用してこの構造体を取得します。
| フィールド | 説明 |
|---|---|
float3 positionWS |
ワールド空間での位置 |
float3 positionVS |
ビュー空間での位置。 |
float4 positionCS |
クリップスペースでの位置。 |
float4 positionNDC |
正規化されたデバイス座標 (NDC) としての位置。 |
GetVertexNormalInputs メソッドを使用してこの構造体を取得します。
| フィールド | 説明 |
|---|---|
real3 tangentWS |
ワールド空間内の接線。 |
real3 bitangentWS |
ワールド空間での従接線。 |
float3 normalWS |
ワールド空間内の法線。 |
以下の URP シェーダーは、スクリーンスペースでの位置を表す色でオブジェクト面をレンダリングします。
Shader "Custom/ScreenSpacePosition"
{
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv: TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv: TEXCOORD0;
float3 positionWS : TEXCOORD2;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
// Get the position of the vertex in different spaces
VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS);
// Set positionWS to the screen space position of the vertex
OUT.positionWS = positions.positionWS.xyz;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// Set the fragment color to the screen space position vector
return float4(IN.positionWS.xy, 0, 1);
}
ENDHLSL
}
}
}