본문 바로가기
Shader

Unity URP HLSL Shader Basic

by 대마왕J 2024. 7. 31.

https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@12.0/manual/writing-shaders-urp-basic-unlit-structure.html

 

URP unlit basic shader | Universal RP | 12.0.0

URP unlit basic shader This example shows a basic URP-compatible shader. This shader fills the mesh shape with a color predefined in the shader code. To see the shader in action, copy and paste the following ShaderLab code into the Shader asset. // This sh

docs.unity3d.com

 

뭔 셰이더를 짜려면 
기본형부터 시작해야 하는데 자꾸 찾기 힘들어서 여기 백업. 

Shader "Example/URPUnlitShaderBasic"
{
    Properties
    { }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS   : POSITION;
            };

            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
            };

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                return OUT;
            }

            half4 frag() : SV_Target
            {
                half4 customColor = half4(0.5, 0, 0, 1);
                return customColor;
            }
            ENDHLSL
        }
    }
}

여기서부터 시작하면 됩니다. 빨강셰이더 . 기본형. 

하지만 흔히 쓰는건 또 다르죠. 
텍스쳐 한 장은 가지고 시작하는게 기본이지 않습니까? 
그래서 텍스쳐 한 장 가지고 시작하는걸 만들어 놓았슴다. 여기서 시작하시죠 

Shader "TextureBasic"
{
    Properties
    { 
        [MainTexture] _BaseMap("Base Map", 2D) = "white" {} //[MainTexture]는 없어도 되는데 옛날 _MainTex와의 자동호환을 위해 둔 것 같음
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            HLSLPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            CBUFFER_START(UnityPerMaterial)//C버퍼 시작
            float4 _BaseMap_ST;
            CBUFFER_END//C버퍼 끝
            
            TEXTURE2D(_BaseMap);//텍스쳐 오브젝트
            SAMPLER(sampler_BaseMap);//샘플러

            struct Attributes
            {
                float4 positionOS   : POSITION;
                float2 uv           : TEXCOORD0;
            };
            
            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
                float2 uv           : TEXCOORD0;
            };

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = TRANSFORM_TEX(IN.uv,_BaseMap);
                return OUT;
            }

            float4 frag(Varyings IN) : SV_Target
            {
                float4 final = SAMPLE_TEXTURE2D(_BaseMap,sampler_BaseMap,IN.uv);
                return final;
            }
            ENDHLSL
        }
    }
}

 

 

반응형

댓글