Version: Unity 6.0 (6000.0)
언어 : 한국어
URP에서 셰이더에 텍스처 드로우
Write a depth-only pass in a Universal Render Pipeline shader

URP에서 셰이더의 노멀 벡터 시각화

이 예제의 Unity 셰이더는 메시의 노멀 벡터 값을 시각화합니다.

URP unlit basic shader 섹션의 Unity 셰이더 소스 파일을 사용하고__ ShaderLab__셰이더 오브젝트의 구조를 정의하기 위한 Unity 언어입니다. 자세한 정보
See in Glossary
코드에 다음 변경 사항을 적용하십시오.

  1. 이 예제의 버텍스 셰이더에 대한 입력 구조인 struct Attributes에서 각 버텍스의 노멀 벡터가 포함되어 있는 변수를 선언합니다.

    struct Attributes
    {
        float4 positionOS   : POSITION;
        // Declaring the variable containing the normal vector for each vertex.
        half3 normal        : NORMAL;
    };
    
  2. 이 예제의 프래그먼트 셰이더에 대한 입력 구조인 struct Varyings에서 각 프래그먼트의 노멀 벡터 값을 저장하기 위한 변수를 선언합니다.

    struct Varyings
    {
        float4 positionHCS  : SV_POSITION;
        // The variable for storing the normal vector values.
        half3 normal        : TEXCOORD0;
    };
    

    이 예제는 각 프래그먼트의 RGB 컬러 값으로 노멀 벡터의 세 가지 컴포넌트를 사용합니다.

  3. 메시의 노멀 벡터 값을 렌더링하기 위해 다음 코드를 프래그먼트 셰이더로 사용합니다.

    half4 frag(Varyings IN) : SV_Target
    {
        half4 color = 0;
        color.rgb = IN.normal;
        return color;
    }
    
  4. Unity가 메시의 노멀 벡터 값을 렌더링합니다.

    압축 없이 노멀 렌더링
    압축 없이 노멀 렌더링

    캡슐의 일부가 검게 보이는데, 이는 해당 포인트에서 노멀 벡터 컴포넌트의 세 가지 컴포넌트가 모두 음수이기 때문입니다. 다음 단계에서 이 영역의 값을 렌더링하는 방법도 확인할 수 있습니다.

  5. 음의 노멀 벡터 컴포넌트를 렌더링하려면 압축 기법을 사용합니다. 노멀 컴포넌트 값 (-1..1) 범위를 컬러 값 범위 (0..1)로 압축하려면 다음 줄을 변경합니다.

    color.rgb = IN.normal;
    

    위 줄을 아래 줄로 변경하십시오.

    color.rgb = IN.normal * 0.5 + 0.5;
    

이제 Unity가 메시에서 노멀 벡터 값을 컬러로 렌더링합니다.

압축으로 노멀 렌더링
압축으로 노멀 렌더링

아래는 이 예제의 완전한 ShaderLab 코드입니다.

// This shader visuzlizes the normal vector values on the mesh.
Shader "Example/URPUnlitShaderNormal"
{
    Properties
    { }

    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;
                // Declaring the variable containing the normal vector for each
                // vertex.
                half3 normal        : NORMAL;
            };

            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
                half3 normal        : TEXCOORD0;
            };

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                // Use the TransformObjectToWorldNormal function to transform the
                // normals from object to world space. This function is from the
                // SpaceTransforms.hlsl file, which is referenced in Core.hlsl.
                OUT.normal = TransformObjectToWorldNormal(IN.normal);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half4 color = 0;
                // IN.normal is a 3D vector. Each vector component has the range
                // -1..1. To show all vector elements as color, including the
                // negative values, compress each value into the range 0..1.
                color.rgb = IN.normal * 0.5 + 0.5;
                return color;
            }
            ENDHLSL
        }
    }
}
URP에서 셰이더에 텍스처 드로우
Write a depth-only pass in a Universal Render Pipeline shader
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961