커스텀 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
}
}
}