커스텀 URP(유니버설 렌더 파이프라인) 셰이더에서 카메라를 사용하려면 다음 과정을 따르십시오.
HLSLPROGRAM 안에 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"을 추가합니다. Core.hlsl 파일이 ShaderVariablesFunction.hlsl 파일을 임포트합니다.ShaderVariablesFunction.hlsl 파일에서 다음 메서드 중 하나를 사용하십시오.| 메서드 | 구문 | 설명 |
|---|---|---|
GetCameraPositionWS |
float3 GetCameraPositionWS() |
카메라의 월드 공간 위치를 반환합니다. |
GetScaledScreenParams |
float4 GetScaledScreenParams() |
화면의 너비와 높이를 픽셀 단위로 반환합니다. |
GetViewForwardDir |
float3 GetViewForwardDir() |
월드 공간에서 뷰의 전방 방향을 반환합니다. |
IsPerspectiveProjection |
bool IsPerspectiveProjection() |
카메라 투영이 원근으로 설정된 경우 true를 반환합니다. |
LinearDepthToEyeDepth |
half LinearDepthToEyeDepth(half linearDepth) |
선형 뎁스 버퍼 값을 뷰 뎁스로 전환합니다. 자세한 내용은 카메라 및 뎁스 텍스처를 참조하십시오. |
TransformScreenUV |
void TransformScreenUV(inout float2 screenSpaceUV) |
Unity가 아래 위가 뒤집힌 좌표 공간을 사용하는 경우 스크린 공간 위치의 y 좌표를 반전합니다. uv와 화면 높이를 모두 float로 입력하여 메서드가 화면 크기에 맞춰 픽셀 단위로 조정된 위치를 출력하게 할 수 있습니다. |
다음 URP 셰이더는 표면에서 카메라까지의 방향을 나타내는 컬러로 오브젝트 표면을 드로우합니다.
Shader "Custom/DirectionToCamera"
{
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 viewDirection : TEXCOORD2;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
// Get the positions of the vertex in different coordinate spaces
VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS);
OUT.positionCS = positions.positionCS;
// Get the direction from the vertex to the camera, in world space
OUT.viewDirection = GetCameraPositionWS() - positions.positionWS.xyz;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// Set the fragment color to the direction vector
return float4(IN.viewDirection, 1);
}
ENDHLSL
}
}
}