为主纹理添加旋转 - 统一着色器

问题描述 投票:0回答:1

我对着色器脚本一无所知。我想在我的着色器中放置一个浮点,并使用 UV 上的该值旋转我的纹理。我如何将此选项添加到我的着色器中? 我不想使用 Shader graph

这是我的着色器,我想旋转主纹理 我想要一个浮点值来将其设置为纹理旋转角度。

任何人都可以帮助我将此选项添加到我的着色器中吗?

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" {}
        _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;

        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) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _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"
}
unity-game-engine shader unityscript
1个回答
0
投票

部分答案-这适用于shadertoy。将其发布在这里,以便您可以调整代码并将其放入片段着色器中以使其与 hlsl 一起使用:

void mainImage( out vec4 oFragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    
    uv -= 0.5; // Move the "0,0" origin away from the corner, to center it
    
    float aspect = iResolution.x/iResolution.y;
    uv.x *= aspect; // Square it
    
    float angle = 45.0;    // In degrees (45 degrees). A negative number will cause it to spin in the other direction
    angle = (angle / 180.0) * PI;  // Convert to radians
    
    mat2 rotMat = mat2(
        cos(angle), -sin(angle),
        sin(angle),  cos(angle)
    );
    
    uv = rotMat * uv;  // Apply rotation matrix to texture coordinates
    uv.x /= aspect; // Unsquare it
    uv += 0.5;         // Move the "0,0" origin back into the corner after rotating it
    
    vec2 bottomLeft = step(vec2(0.0), uv);
    vec2 topRight   = step(vec2(0.0), 1.0 - uv);
    
    float quadAlphaMask = bottomLeft.x * bottomLeft.y * topRight.x * topRight.y;
    
    oFragColor = vec4(texture(iChannel0, uv).rgb * quadAlphaMask, 1.0);
}

基于 Shadertoy 上 AndreiDespinoiu 的平滑旋转纹理四边形:https://www.shadertoy.com/view/dsycWw

© www.soinside.com 2019 - 2024. All rights reserved.