본문 바로가기
셰이더 (Shader)/유니티 쉐이더 스타트업 - 정종필

[Unity/Shader] 파트14-4 : 버텍스 쉐이더를 이용한 오브젝트 확장

by Minkyu Lee 2023. 8. 20.

버텍스 쉐이더를 사용해본다.

버텍스를 노말 방향으로 이동시키는 것이 핵심이다.

 

코드

Shader "Custom/part14-4"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        cull front

        // 1st pass 검은색 출력
        CGPROGRAM
        #pragma surface surf Nolight vertex:vert noshadow noambient
        // vertex:vert 버텍스 쉐이더가 vert라는 함수 이름으로 가동된다는 것을 알려준다.
        // 라이팅 연산을 최소화하여 가볍게 만든다.

        #pragma target 3.0

        sampler2D _MainTex;

        void vert(inout appdata_full v) // appdata_full은 미리 선언되어 있는 구조체이다. 버텍스가 가진 정보를 구조체로 만든 것이다.
        {
            // 아래에서 필요한 것만 조작한다. 유니티 서피스 쉐이더에서는 조작하지 않은 것은 자동 변환해주어 편리하다.
            v.vertex.xyz += v.normal.xyz * 0.003; // 노말방향으로 버텍스 이동
        }

        struct Input
        {
            float2 uv_MainTex;
        };

        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {

        }

        float4 LightingNolight(SurfaceOutput s, float3 lightDir, float atten)
        {
            return float4(0,0,0,1); // 검은색 출력
        }
        ENDCG

        cull back // 컬링을 원래대로 되돌린다.
        // 2nd Pass 일반 오브젝트 출력
        CGPROGRAM
        #pragma surface surf Lambert

        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

댓글