Separate definition of texture and sampler for URP Shader in Unity


Preface

In this article, we will learn how to implement Shader texture sampling under URP. (Texture sampling under URP is different from texture sampling under BRP)


1. Implementation of URP Shader texture sampling

1. Define a 2D variable in the properties panel to receive the texture.

_MainTex(“MainTex”,2D) = “white”{}

2. Declare texture

使用前在Pass中,我们申明纹理

  • The definition of texture, if compiled to the GLES2.0 platform, is equivalent to sample2D _MainTex; otherwise, it is equivalent to Texture2D _MainTex;

TEXTURE2D(_MainTex);

3. Declare the sampler

使用前在Pass中,我们申明着色器

  • The definition of the sampler, if compiled to the GLES2.0 platform, is equivalent to empty; otherwise it is equivalent to SamplerState sampler_MainTex;

SAMPLER(sampler_textureName):sampler+texture name. This definition form indicates the sampling method in the texture Inspector panel using textureName.

SAMPLER(sampler_MainTex);

4. Texture sampling

  • Texture sampling requires preparing uv information in advance, and the method is the same as under BRP

float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv);


2. Declaring the texture and declaring what is done inside the sampler

1. Declaration of texture

  • Under GLES2 (an older platform), the declared texture is consistent with the implementation under BRP: sample2D
    Insert image description here
  • Under other platforms, it is a new way of definition: Texture2D
    Insert image description here

2. Declare the sampler

  • Under GLES2 (an older platform), declaring a sampler is an empty statement. Because, sample2D contains textures and samplers

Insert image description here

  • On other platforms, it is a new way of definition: SampleState
    Insert image description here

3. Sampler settings

Sampler's incoming format

The definition of sampler (separate definition of texture and sampler), sampler refers to the filtering mode and repeating mode of texture. This function is not supported on OpenGL ES2.0, which is equivalent to not writing it.

  • filter:point/linear/triLinear
  • wrap:clamp/repeat/mirror/mirrorOnce

1. In the texture settings, you can see our sampler settings

Insert image description here

2. Default sampling input

SAMPLER(sampler_textureName):sampler+texture name. This definition form indicates the sampling method in the texture Inspector panel using textureName.

3. Modify the filter mode of the sampler

SAMPLER(_filter_wrap): For example, SAMPLER(sampler_linear_repeat) uses custom sampler settings. The custom sampler must include both filter mode and repeat mode settings. Note that many mobile platforms do not support this custom writing method! .

  • Before modifying filter mode:
    Insert image description here
  • After modifying the filter mode, use sampling pixel points and non-linear interpolation method

//SAMPLER(sampler name_filter mode_relay mode);
SAMPLER(SamplerState_Point_Repeat);

Insert image description here

4. Modify wrap mode

我们修改U方向平铺模式为 镜像,V方向平铺模式为 镜像

SAMPLER(SamplerState_linear_mirrorU_ClampV);

Remember to use the TRANSFORM_TEX function to apply the Tilling and Offset of the texture used in retile mode


4. Test Shader

1、Shader:

Shader "MyShader/URP/P3_1_5"
{
    Properties 
    {
        _Color("Color",Color) = (0,0,0,0)
        _MainTex("MainTex",2D) = "white"{}
    }
    SubShader
    {
        Tags
        {
            //告诉引擎,该Shader只用于 URP 渲染管线
            "RenderPipeline"="UniversalPipeline"
            //渲染类型
            "RenderType"="Opaque"
            //渲染队列
            "Queue"="Geometry"
        }
        Pass
        {
            Name "Universal Forward"
            Tags
            {
                // LightMode: <None>
            }

            Cull Back
            Blend One Zero
            ZTest LEqual
            ZWrite On
          
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // Pragmas
            #pragma target 2.0
            
            // Includes
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"

            CBUFFER_START(UnityPerMaterial)
            half4 _Color;
            CBUFFER_END

            
            //纹理的定义,如果是编译到GLES2.0平台,则相当于sample2D _MainTex;否则相当于 Texture2D _MainTex;
            TEXTURE2D(_MainTex);
            float4 _MainTex_ST;
            //采样器的定义,如果是编译到GLES2.0平台,就相当于空;否则相当于 SamplerState sampler_MainTex;
            //SAMPLER(sampler_MainTex);
            //修改纹理采样格式
            #define smp SamplerState_linear_mirrorU_ClampV
            SAMPLER(smp);
            
            //struct appdata
            //顶点着色器的输入
            struct Attributes
            {
                float3 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };
            //struct v2f
            //片元着色器的输入
            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
            //v2f vert(Attributes v)
            //顶点着色器
            Varyings vert(Attributes v)
            {
                Varyings o = (Varyings)0;
                float3 positionWS = TransformObjectToWorld(v.positionOS);
                o.positionCS = TransformWorldToHClip(positionWS);
                o.uv = TRANSFORM_TEX(v.uv,_MainTex);
                return o;
            }
            //fixed4 frag(v2f i) : SV_TARGET
            //片元着色器
            half4 frag(Varyings i) : SV_TARGET
            {
                half4 c;
                float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,smp,i.uv);
                c = _Color *  mainTex;
                return c;
            }
            ENDHLSL
        }
    }

    FallBack "Hidden/Shader Graph/FallbackError"
}

2. Test effect

Please add image description

3. Implemented in ShaderGraph

Insert image description here

Guess you like

Origin blog.csdn.net/qq_51603875/article/details/135030050