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

 

유니티 빌보드 쉐이더 구현 & 분석

빌보드(Billboard)는 3D 오브젝트가 카메라를 바라보는 기술입니다. 일반적으로 유니티 파티클 시스템을 생성하면 나오는 오브젝트들이 기본적으로 빌보드로 되어 있습니다. 이번 포스팅은 Vertex

darkcatgame.tistory.com

해당 기능을 활용해, Transform Scale 값으로 UV Tiling하는 것이 핵심 원리입니다.

 

 

한계점

소개하는 쉐이더는 현재 Quad나 Plane같이 평면 모델에서만 동작합니다.

또한 모델의 가로 세로가 X,Y축으로 되어 있어야합니다.

 

https://catlikecoding.com/unity/tutorials/advanced-rendering/triplanar-mapping/

그래서 위 예시처럼 입체적인 모델 & 터레인에서 사용하는 쉐이더가 아닙니다.

위 기술은 Triplaner Mapping이라고 하며 추후 R&D하면 포스팅해보겠습니다.

 

 

 

 

 


WRITTEN BY
CatDarkGame
Technical Artist dhwlgn12@gmail.com

,