Version: Unity 6.0 (6000.0)
언어 : 한국어
커스텀 URP 셰이더에서 위치 변환
커스텀 URP 셰이더에서 조명 사용

커스텀 URP 셰이더에서 카메라 사용

커스텀 URP(유니버설 렌더 파이프라인) 셰이더에서 카메라를 사용하려면 다음 과정을 따르십시오.

  1. 셰이더 파일의 HLSLPROGRAM 안에 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"을 추가합니다. Core.hlsl 파일이 ShaderVariablesFunction.hlsl 파일을 임포트합니다.
  2. 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
        }
    }
}

추가 리소스

커스텀 URP 셰이더에서 위치 변환
커스텀 URP 셰이더에서 조명 사용
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961