Unity Shader屏幕空间纹理的重复、缩放与移动[unlit/surface shader]

[Surface Shaders版本]:

Shader "Custom/Sample"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
            float4 screenPos;//添加screenPos获取屏幕坐标。
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        float4 _MainTex_ST;//声明纹理的缩放及平移变量

        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {   
            float2 textureCoordinate = saturate(IN.screenPos.xy / IN.screenPos.w);//存储屏幕UV
            textureCoordinate = TRANSFORM_TEX(textureCoordinate, _MainTex);//将UV与材质球里的tiling和offset联系起来

            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D(_MainTex, textureCoordinate) * _Color;//以图片联系坐标,形成新的纹理,之后的color则为该图片纹理加上颜色。
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

解释:

  1. 在Input结构中添加screenPos获取屏幕坐标。
CGPROGRAM
#pragma surface surf Lambert alpha noambient nolightmap nodirlightmap novertexlights
struct Input{
       		 float4 screenPos; 
       		 }     
  1. 声明变量纹理的缩放及平移变量
float4 _MainMap_ST;
  1. 存储屏幕UV:
float2 textureCoordinate = saturate(IN.screenPos.xy / IN.screenPos.w);

4.将UV与材质球里的tiling和offset联系起来:

textureCoordinate = TRANSFORM_TEX(textureCoordinate, _MainTex);

4.以图片联系坐标,形成新的纹理,之后的color则为该图片纹理加上颜色。

fixed4 c = tex2D(_MainTex, textureCoordinate) * _Color;

[Unlit Shader版本]:

Shader "Unlit/Sample"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float4 screenPosition : TEXCOORD1;//新建屏幕空间的UV存储区
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.screenPosition = ComputeScreenPos(o.vertex);//获取齐次坐标下的屏幕坐标值
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float2 textureCoordinate = i.screenPosition.xy / i.screenPosition.w;//引入屏幕坐标值
                textureCoordinate = TRANSFORM_TEX(textureCoordinate, _MainTex);//将屏幕UV与材质球面板上的offset与tileing联系起来
                fixed4 col = tex2D(_MainTex, textureCoordinate);//将图像映射到屏幕UV
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }

解释:
1.新建屏幕空间的UV存储区

float4 screenPosition : TEXCOORD1;

2.获取齐次坐标下的屏幕坐标值

 o.screenPosition = ComputeScreenPos(o.vertex);//返回齐次坐标下的屏幕坐标值

3.引入屏幕坐标值

float2 textureCoordinate = i.screenPosition.xy / i.screenPosition.w;
  1. 将屏幕UV与材质球面板上的offset与tileing联系起来
textureCoordinate = TRANSFORM_TEX(textureCoordinate, _MainTex);

6.将屏幕UV与材质球面板上的offset与tileing联系起来

textureCoordinate = TRANSFORM_TEX(textureCoordinate, _MainTex);

7.将图像映射到屏幕UV

 fixed4 col = tex2D(_MainTex, textureCoordinate);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值