Version: Unity 6.0 (6000.0)
언어 : 한국어
셰이더에서 뎁스 버퍼에 쓰기 비활성화
셰이더에서 블렌딩 모드 설정

셰이더에서 스텐실 버퍼 확인 또는 쓰기

스텐실 버퍼는 프레임 버퍼의 각 픽셀에 대해 8비트 정수 값을 저장합니다. 특정 픽셀에 대한 프래그먼트 셰이더를 실행하기 전에 GPU는 스텐실 버퍼의 현재 값을 특정 레퍼런스 값과 비교할 수 있습니다. 이를 스텐실 테스트라고 합니다. 스텐실 테스트를 통과하면 GPU가 뎁스 테스트를 수행합니다. 스텐실 테스트가 실패하면 GPU는 해당 픽셀에 대한 나머지 처리 작업을 건너뜁니다. 따라서 스텐실 버퍼를 마스크로 사용하여 드로우할 픽셀과 폐기할 픽셀을 GPU에 알릴 수 있습니다.

일반적으로 포털이나 미러 같은 특수 효과에 스텐실 버퍼를 사용합니다. 또한, 스텐실 버퍼는 하드 섀도우 또는 CSG(구조적 입체 지오메트리) 렌더링에 사용되는 경우가 있습니다.

Stencil 커맨드를 사용하여 스텐실 테스트를 설정하고 GPU가 스텐실 버퍼에 쓸 내용을 설정하는 두 가지 작업을 수행할 수 있습니다. 동일한 커맨드로 두 가지를 모두 수행할 수 있지만, 가장 일반적인 사용 사례는 다른 셰이더 오브젝트가 그릴 수 없는 화면 영역을 마스킹하는 하나의 셰이더 오브젝트를 생성하는 것입니다. 이렇게 하려면 첫 번째 셰이더 오브젝트가 항상 스텐실 테스트를 통과하고 스텐실 버퍼에 쓰도록 설정하고 나머지는 스텐실 테스트를 수행하고 스텐실 버퍼에 쓰지 않도록 설정해야 합니다.

스텐실 테스트를 설정하려면 Ref, ReadMask, Comp 파라미터를 사용합니다. 스텐실 쓰기 작업을 설정하려면 Ref, WriteMask, Pass, Fail, ZFail 파라미터를 사용합니다.

이 커맨드는 렌더 상태를 변경합니다. Pass 블록에서 사용하여 해당 패스의 렌더 상태를 설정하거나, SubShader 블록에서 사용하여 해당 서브셰이더에 있는 모든 패스의 렌더 상태를 설정할 수 있습니다.

스텐실 테스트 등식은 다음과 같습니다.

(ref & readMask) comparisonFunction (stencilBufferValue & readMask)

예제

Shader "Examples/CommandExample"
{
    SubShader
    {
         // The rest of the code that defines the SubShader goes here.

        Pass
        {    
             // All pixels in this Pass will pass the stencil test and write a value of 2 to the stencil buffer
             // You would typically do this if you wanted to prevent subsequent shaders from drawing to this area of the render target or restrict them to render to this area only
             Stencil
             {
                 Ref 2
                 Comp Always
                 Pass Replace
             }            

             // The rest of the code that defines the Pass goes here.
        }
    }
}

이 예제 코드는 SubShader 블록에서 이 커맨드를 사용하기 위한 구문을 나타냅니다.

Shader "Examples/CommandExample"
{
    SubShader
    {
             // All pixels in this SubShader pass the stencil test only if the current value of the stencil buffer is less than 2
             // You would typically do this if you wanted to only draw to areas of the render target that were not "masked out"
             Stencil
             {
                 Ref 2
                 Comp Less
             }  

         // The rest of the code that defines the SubShader goes here.        

        Pass
        {    
              // The rest of the code that defines the Pass goes here.
        }
    }
}

추가 리소스

셰이더에서 뎁스 버퍼에 쓰기 비활성화
셰이더에서 블렌딩 모드 설정