이 예제는 기본 URP 호환 셰이더를 보여줍니다. 이 셰이더는 셰이더 코드에 사전 정의된 컬러로 메시 모양을 채웁니다.
셰이더를 시험 삼아 사용해 보려면 다음__ ShaderLab__셰이더 오브젝트의 구조를 정의하기 위한 Unity 언어입니다. 자세한 정보
See in Glossary 코드를 복사하여 셰이더 에셋에 붙여 넣으십시오.
// This shader fills the mesh shape with a color predefined in the code.
Shader "Example/URPUnlitShaderBasic"
{
// The properties block of the Unity shader. In this example this block is empty
// because the output color is predefined in the fragment shader code.
Properties
{ }
// The SubShader block containing the Shader code.
SubShader
{
// SubShader Tags define when and under which conditions a SubShader block or
// a pass is executed.
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
// The HLSL code block. Unity SRP uses the HLSL language.
HLSLPROGRAM
// This line defines the name of the vertex shader.
#pragma vertex vert
// This line defines the name of the fragment shader.
#pragma fragment frag
// The Core.hlsl file contains definitions of frequently used HLSL
// macros and functions, and also contains #include references to other
// HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// The structure definition defines which variables it contains.
// This example uses the Attributes structure as an input structure in
// the vertex shader.
struct Attributes
{
// The positionOS variable contains the vertex positions in object
// space.
float4 positionOS : POSITION;
};
struct Varyings
{
// The positions in this struct must have the SV_POSITION semantic.
float4 positionHCS : SV_POSITION;
};
// The vertex shader definition with properties defined in the Varyings
// structure. The type of the vert function must match the type (struct)
// that it returns.
Varyings vert(Attributes IN)
{
// Declaring the output object (OUT) with the Varyings struct.
Varyings OUT;
// The TransformObjectToHClip function transforms vertex positions
// from object space to homogenous clip space.
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// Returning the output.
return OUT;
}
// The fragment shader definition.
half4 frag() : SV_Target
{
// Defining the color variable and returning it.
half4 customColor = half4(0.5, 0, 0, 1);
return customColor;
}
ENDHLSL
}
}
}
프래그먼트 셰이더는 게임 오브젝트에 암적색(RGB 값(0.5, 0, 0))을 지정합니다.
다음 섹션에서는 기본 Unity 셰이더의 구조에 대해 소개합니다.
Unity 셰이더는 ShaderLab이라는 Unity 전용 언어로 작성됩니다.
이 예제의 Unity 셰이더에는 다음의 블록이 포함되어 있습니다.
ShaderLab 코드는 Shader 선언으로 시작합니다.
Shader "Example/URPUnlitShaderBasic"
이 선언의 경로는 머티리얼의 셰이더 메뉴에 있는 Unity 셰이더의 표시 이름과 위치를 결정합니다. Shader.Find 메서드도 이 경로를 사용합니다.
프로퍼티 블록에는 사용자가 머티리얼의 인스펙터 창에서 설정할 수 있는 프로퍼티의 선언이 포함되어 있습니다.
이 예제의 프로퍼티 블록은 비어 있는데, 이는 Unity 셰이더가 사용자가 정의할 수 있는 Material 프로퍼티를 노출하지 않기 때문입니다.
Unity 셰이더 소스 파일에는 하나 이상의 서브셰이더 블록이 포함되어 있습니다. 메시를 렌더링할 때 Unity는 타겟 디바이스의 GPU와 호환되는 첫 번째 서브셰이더를 선택합니다.
서브셰이더 블록은 선택적으로 서브셰이더 태그 블록을 포함할 수 있습니다. Tags 키워드를 사용하여 서브셰이더 태그 블록을 선언합니다.
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
이름이 RenderPipeline인 서브셰이더 태그는 이 서브셰이더를 사용할 렌더 파이프라인을 Unity에 알려 주며, UniversalPipeline 값은 Unity가 이 서브셰이더를 URP에 사용해야 함을 나타냅니다.
다른 렌더 파이프라인에서 동일한 셰이더를 실행하려면 다른 RenderPipeline 태그 값이 있는 여러 개의 서브셰이더 블록을 생성하십시오. HDRP에서 서브셰이더 블록을 실행하려면 RenderPipeline 태그를 HDRenderPipeline으로 설정하고, 빌트인 렌더 파이프라인에서 실행하려면 RenderPipeline을 빈 값으로 설정하십시오.
서브셰이더 태그에 대한 상세 내용은 ShaderLab: 서브셰이더 태그를 참조하십시오.
이 예시에는 HLSL 프로그램 코드가 포함된 하나의 패스 블록이 있습니다. 패스 블록에 대한 상세 내용은 ShaderLab: 패스를 참조하십시오.
패스 블록은 선택적으로 패스 태그 블록을 포함할 수 있습니다. 자세한 내용은 URP ShaderLab 패스 태그를 참조하십시오.
이 블록에는 HLSL 프로그램 코드가 포함되어 있습니다.
참고: HLSL 언어는 URP 셰이더에 선호되는 언어입니다.
참고: URP는 CG 언어를 지원합니다. 셰이더에 CGPROGRAM/ENDCGPROGRAM 블록을 추가하면 Unity는 빌트인 렌더 파이프라인 라이브러리의 셰이더를 자동으로 포함합니다. SRP 셰이더 라이브러리의 셰이더를 포함하면 일부 SRP 셰이더 매크로 및 함수가 빌트인 렌더 파이프라인 셰이더 함수와 충돌할 수 있습니다. CGPROGRAM 블록이 포함된 셰이더는 SRP 배처와 호환됩니다.
이 블록에는 Core.hlsl 파일을 참조하는 #include 선언이 포함되어 있습니다.
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
Core.hlsl 파일에는 HLSL 매크로와 함수에서 자주 사용되는 정의뿐만 아니라 다른 HLSL 파일에 대한 레퍼런스(예: Common.hlsl 및 SpaceTransforms.hlsl)도 포함되어 있습니다.
예를 들어 HLSL 코드의 버텍스 셰이더는 SpaceTransforms.hlsl 파일의 TransformObjectToHClip 함수를 사용합니다. 이 함수는 오브젝트 공간의 버텍스 위치를 균일한 공간으로 변환합니다.
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
return OUT;
}
이 기본 HLSL 코드의 프래그먼트 셰이더는 코드에 사전 정의된 단일 컬러를 출력합니다.
half4 frag() : SV_Target
{
half4 customColor;
customColor = half4(0.5, 0, 0, 1);
return customColor;
}
컬러 입력을 제공하는 URP 언릿 셰이더 섹션에서는 머티리얼의 인스펙터 창에 편집 가능한 컬러 프로퍼티를 추가하는 방법을 설명합니다.