Plane이나 Quad같은 오브젝트 크기를 늘리면 자동으로 텍스처 UV 타일링되는 쉐이더입니다.
Shader "CatDarkGame/ScaleUVTiling"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" "Queue"="Geometry" "RenderPipeline" = "UniversalPipeline" }
Pass
{
Name "URPUnlit"
Tags {"LightMode" = "SRPDefaultUnlit"}
Cull back
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex vert
#pragma fragment frag
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
half4 _MainTex_ST;
CBUFFER_END
struct appdata
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata v)
{
v2f o = (v2f)0;
// Transform Scale 정보가져오기
float4x4 scaleMatrix;
float4 sx = float4(UNITY_MATRIX_M._m00, UNITY_MATRIX_M._m10, UNITY_MATRIX_M._m20, 0);
float4 sy = float4(UNITY_MATRIX_M._m01, UNITY_MATRIX_M._m11, UNITY_MATRIX_M._m21, 0);
float4 sz = float4(UNITY_MATRIX_M._m02, UNITY_MATRIX_M._m12, UNITY_MATRIX_M._m22, 0);
float scaleX = length(sx);
float scaleY = length(sy);
float scaleZ = length(sz);
float4 positionOS = v.positionOS;
float4 positionWS = mul(UNITY_MATRIX_M, float4(positionOS.xyz, 1));
float4 positionVS = mul(UNITY_MATRIX_V, positionWS);
float4 positionCS = mul(UNITY_MATRIX_P, positionVS);
o.positionCS = positionCS;
o.uv = v.uv * float2(scaleX, scaleZ); // UV Tiling
return o;
}
float4 frag (v2f i) : SV_Target
{
float2 mainTexUV = i.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, mainTexUV);
return col;
}
ENDHLSL
}
}
}
구현 과정
이전에 빌보드 쉐이더 R&D하면서, 오브젝트 Transform 정보를 버텍스 쉐이더에서 활용하는 부분이 있었습니다.
https://darkcatgame.tistory.com/137
해당 기능을 활용해, Transform Scale 값으로 UV Tiling하는 것이 핵심 원리입니다.
한계점
소개하는 쉐이더는 현재 Quad나 Plane같이 평면 모델에서만 동작합니다.
또한 모델의 가로 세로가 X,Y축으로 되어 있어야합니다.
그래서 위 예시처럼 입체적인 모델 & 터레인에서 사용하는 쉐이더가 아닙니다.
위 기술은 Triplaner Mapping이라고 하며 추후 R&D하면 포스팅해보겠습니다.
'Unity > Shader & VFX' 카테고리의 다른 글
Unity Triplanar Shader 분석 & StaticWorldPosition 응용 (0) | 2023.07.10 |
---|---|
[URP] HLSL SoftParticles Shader (0) | 2023.03.07 |
유니티 빌보드 쉐이더 구현 & 분석 (1) | 2022.11.04 |
털(Fur) 쉐이더 R&D - 로스트아크에서 털 표현하는 방법 (0) | 2022.11.01 |
URP 반투명 굴절 구현(Transparent Grabpass) (3) | 2022.03.22 |
WRITTEN BY
- CatDarkGame
Technical Artist dhwlgn12@gmail.com
,