You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity非Shader Graph的Shader中添加主纹理旋转参数

为「Alpha Mask/Bumped Specular」Shader添加主纹理旋转功能

关键修改说明

我已经给原Shader添加了主纹理旋转功能,你可以在Inspector面板通过新增参数控制旋转角度,具体修改点如下:

  • 在Properties块中添加了旋转角度参数(单位为度数,默认值0)
  • 在CG代码中声明对应变量,接收面板传入的参数值
  • 在surf函数中对主纹理UV做旋转变换,实现纹理旋转效果

修改后的完整Shader代码

Shader "Alpha Mask/Bumped Specular" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _SpecCol ("Specular Color", Color) = (1,1,1,1)
        _Spec ("Specularity", Range (0,1)) = 0.8
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        // 新增:主纹理旋转角度参数,单位为度数
        _MainTexRotation ("Main Tex Rotation (Degrees)", Float) = 0.0
        _BumpMap ("Normal Map", 2D) = "bump" {}
        _Mask ("Mask (A)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
       
        CGPROGRAM
        #pragma surface surf Spec fullforwardshadows addshadow alpha

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

        sampler2D _MainTex;
        sampler2D _BumpMap;
        sampler2D _Mask;
        // 新增:声明旋转角度变量,对应Properties中的参数
        float _MainTexRotation;

        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_Mask;
        };

        half _Spec;
        fixed4 _SpecCol;
        fixed4 _Color;

        half4 LightingSpec (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
            s.Normal = normalize (s.Normal);

            half3 h = normalize (lightDir + viewDir);

            half diff = max (0, dot (s.Normal, lightDir));

            float nh = max (0, dot (s.Normal, h));
            float spec = pow (nh, 4096.0 * pow (s.Specular, 2)) * s.Specular * 2;

            half4 c;
            c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * _SpecCol) * atten;
            c.a = s.Alpha;
            return c;
        }

        void surf (Input IN, inout SurfaceOutput o) {
            // 新增:UV旋转逻辑
            // 1. 将UV原点移到纹理中心(从[0,1]转为[-0.5,0.5])
            float2 centeredUV = IN.uv_MainTex - 0.5;
            // 2. 将角度转为弧度(三角函数需要弧度值)
            float rad = _MainTexRotation * UNITY_PI / 180.0;
            float cosRad = cos(rad);
            float sinRad = sin(rad);
            // 3. 应用旋转矩阵
            float2 rotatedUV = float2(
                centeredUV.x * cosRad - centeredUV.y * sinRad,
                centeredUV.x * sinRad + centeredUV.y * cosRad
            );
            // 4. 将UV移回原范围(从[-0.5,0.5]转回[0,1])
            rotatedUV += 0.5;

            // Albedo使用旋转后的UV采样主纹理
            fixed4 c = tex2D (_MainTex, rotatedUV) * _Color;
            o.Albedo = c.rgb;
            o.Specular = _Spec;
            o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
            o.Alpha = tex2D (_Mask, IN.uv_Mask).a;
        }

        ENDCG
    }
    FallBack "Diffuse"
}

使用说明

把上述代码替换原Shader代码即可,之后在材质的Inspector面板就能看到Main Tex Rotation (Degrees) 参数:输入正数为顺时针旋转,负数为逆时针旋转,直接调整数值就能看到主纹理的旋转效果。

内容的提问来源于stack exchange,提问作者g4me club

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:12:04