Cg/HLSL 버텍스 프로그램에서
메시 버텍스 데이터는 버텍스 셰이더 함수에 입력으로
전달됩니다. 각 입력에는 입력에 대해 지정된 시맨틱이 있어야 합니다. 예를 들어, POSITION
입력은 버텍스 포지션이고 NORMAL
은 버텍스 노멀입니다.
버텍스 데이터 입력이 하나씩 나열되는 것이 아니라 구조체로 선언되어 있는 경우도 있습니다. 자주 사용되는 일반적인 버텍스 구조는 UnityCG.cginc 포함 파일 내에 정의되어 있으며 대부분의 경우에 이 구조만 사용해도 충분합니다. 다음과 같은 구조가 정의되어 있습니다:
appdata_base
: 포지션, 노멀, 하나의 텍스처 좌표appdata_tan
: 포지션, 탄젠트, 노멀, 하나의 텍스처 좌표appdata_full
: 포지션, 탄젠트, 노멀, 네 개의 텍스처 좌표, 컬러예제: 셰이더는 메시의 노멀에 기반하여 메시에 컬러를 칠하며, 버텍스 프로그램 입력으로 appdata_base
를 사용합니다.
Shader "VertexInputSimple" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.color.xyz = v.normal * 0.5 + 0.5;
o.color.w = 1.0;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
다른 버텍스 데이터에 접근하려면 버텍스 구조를 직접 선언하거나 버텍스 셰이더에 입력 파라미터를 추가해야 합니다. 버텍스 데이터는 Cg/HLSL 시맨틱에 의해 식별되며 다음 리스트에 속해야 합니다.
POSITION
는 버텍스 포지션이며 보통 float3
또 float4
입니다.NORMAL
은 버텍스 노멀이며 보통 float3
입니다.TEXCOORD0
는 첫 번째 UV 좌표이며 보통 float2
, float3
또 float4
입니다.TEXCOORD1
, TEXCOORD2
및 TEXCOORD3
은 각각 두 번째, 세 번째 및 네 번째 UV 좌표입니다.TANGENT
는 탄젠트 벡터(노멀 매핑에 사용됨)이며 보통 float4
입니다.COLOR
는 버텍스당 컬러이며 보통 float4
입니다.버텍스 셰이더 입력에서 요구되는 것보다 더 적은 컴포넌트만 메시 데이터에 포함되어
있을 경우 나머지는 모두 0으로 채워집니다(디폴트가 1인 .w
컴포넌트 제외). 예를 들어, 메시 텍스처 좌표는 대부분의 경우
x와 y 컴포넌트만 있는 2D 벡터입니다. 버텍스 셰이더가
TEXCOORD0
시맨틱이 붙은 float4
입력을 선언하면 버텍스 셰이더가
받게 되는 값은 (x,y,0,1)을 포함합니다.
다음 셰이더 예에서는 버텍스 포지션과 첫 번째 텍스처 좌표를 버텍스 셰이더 입력으로 사용합니다(appdata 구조에서 정의됨). 셰이더는 메시의 UV 좌표를 디버깅하는 데 매우 유용합니다.
Shader "Debug/UV 1" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, UV
struct appdata {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.uv = float4( v.texcoord.xy, 0, 0 );
return o;
}
half4 frag( v2f i ) : SV_Target {
half4 c = frac( i.uv );
if (any(saturate(i.uv) - i.uv))
c.b = 0.5;
return c;
}
ENDCG
}
}
}
여기서 UV 좌표는 빨간색과 초록색으로 시각화되며 01 범위 밖의 좌표에는 추가 파란색 색조가 적용되었습니다.
비슷한 방식으로, 셰이더는 모델의 두 번째 UV 세트를 시각화합니다.
Shader "Debug/UV 2" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, second UV
struct appdata {
float4 vertex : POSITION;
float4 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.uv = float4( v.texcoord1.xy, 0, 0 );
return o;
}
half4 frag( v2f i ) : SV_Target {
half4 c = frac( i.uv );
if (any(saturate(i.uv) - i.uv))
c.b = 0.5;
return c;
}
ENDCG
}
}
}
다음 셰이더는 버텍스 포지션 및 버텍스당 컬러를 버텍스 셰이더 입력으로 사용합니다(appdata 구조에 정의됨).
Shader "Debug/Vertex color" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, color
struct appdata {
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
다음 셰이더는 버텍스 포지션 및 노멀을 버텍스 셰이더 입력으로 사용합니다(appdata 구조에 정의됨). 노멀의 X, Y, Z 컴포넌트는 RGB 컬러로 시각화됩니다. 노멀 컴포넌트가 –11 범위이기 때문에 컴포넌트를 스케일 및 바이어스하여 출력 컬러가 표시 가능한 01 범위에 들어오도록 만듭니다.
Shader "Debug/Normals" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, normal
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.color.xyz = v.normal * 0.5 + 0.5;
o.color.w = 1.0;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
탄젠트 및 바이노멀 벡터는 노멀 매핑에 사용됩니다. Unity에서는 버텍스에 탄젠트 벡터만이 저장되며 바이노멀은 노멀과 탄젠트 값에서 파생됩니다.
다음 셰이더는 버텍스 포지션 및 탄젠트를 버텍스 셰이더 입력으로 사용합니다(appdata 구조에 정의됨). 탄젠트의 x, y, z 컴포넌트는 R, G, B 컬러로 시각화됩니다. 노멀 컴포넌트가 –11 범위이기 때문에 컴포넌트를 스케일 및 바이어스하여 출력 컬러가 표시 가능한 01 범위에 들어오도록 만듭니다.
Shader "Debug/Tangents" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, tangent
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
};
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.color = v.tangent * 0.5 + 0.5;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
다음 셰이더는 바이노멀을 시각화합니다. 버텍스 포지션, 노멀, 탄젠트 값을 버텍스 입력으로 사용합니다. 바이탄젠트(바이노멀이라고도 함)는 노멀 및 탄젠트 값에서 계산됩니다. 표시 가능한 01 범위로 스케일하고 바이어스해야 합니다.
Shader "Debug/Bitangents" {
SubShader {
Pass {
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, normal, tangent
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f {
float4 pos : SV_POSITION;
float4 color : COLOR;
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
// calculate bitangent
float3 bitangent = cross( v.normal, v.tangent.xyz ) * v.tangent.w;
o.color.xyz = bitangent * 0.5 + 0.5;
o.color.w = 1.0;
return o;
}
fixed4 frag (v2f i) : SV_Target { return i.color; }
ENDCG
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.