Search Unity

Question I'd like to merge the two shaders below. How to Script?

Discussion in 'Shaders' started by pk920930_unity, Jan 19, 2021.

  1. pk920930_unity

    pk920930_unity

    Joined:
    Jan 19, 2021
    Posts:
    2
    I'd like to merge the two shaders below.
    How to Script?



    Shader "Custom/UV rotation"
    {
    Properties
    {
    _MainTex ("Texture", 2D) = "white" {}
    _Angle ("Angle", Range(-5.0, 5.0)) = 0.0
    }
    SubShader
    {
    Tags { "RenderType"="Opaque" }

    Pass
    {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

    struct v2f {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
    };

    float _Angle;

    v2f vert(appdata_base v)
    {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

    // Pivot
    float2 pivot = float2(0.5, 0.5);
    // Rotation Matrix
    float cosAngle = cos(_Angle);
    float sinAngle = sin(_Angle);
    float2x2 rot = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);

    // Rotation consedering pivot
    float2 uv = v.texcoord.xy - pivot;
    o.uv = mul(rot, uv);
    o.uv += pivot;

    return o;
    }

    sampler2D _MainTex;

    fixed4 frag(v2f i) : SV_Target
    {
    // Texel sampling
    return tex2D(_MainTex, i.uv);
    }

    ENDCG
    }
    }
    }



    Shader "Mattatz/UVTransform" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _TexScale ("Scale of Tex", Float) = 1.0
    _TexRatio ("Ratio of Tex", Float) = 1.0
    _Theta ("Rotation of Tex", Float) = 0.0
    _PlaneScale ("Scale of Plane Mesh", Vector) = (1, 1, 0, 0)
    }
    SubShader {
    LOD 200
    CGINCLUDE
    #include "UnityCG.cginc"
    #pragma target 3.0
    sampler2D _MainTex;
    float _TexScale;
    float _TexRatio; // _MainTex.width / _MainTex.height
    float _Theta;
    float2 _PlaneScale;
    struct v2f {
    float4 pos : POSITION;
    float2 texcoord : TEXCOORD0;
    };
    float3x3 getXYTranslationMatrix (float2 translation) {
    return float3x3(1, 0, translation.x, 0, 1, translation.y, 0, 0, 1);
    }
    float3x3 getXYRotationMatrix (float theta) {
    float s = -sin(theta);
    float c = cos(theta);
    return float3x3(c, -s, 0, s, c, 0, 0, 0, 1);
    }
    float3x3 getXYScaleMatrix (float2 scale) {
    return float3x3(scale.x, 0, 0, 0, scale.y, 0, 0, 0, 1);
    }
    float2 applyMatrix (float3x3 m, float2 uv) {
    return mul(m, float3(uv.x, uv.y, 1)).xy;
    }
    v2f vert (appdata_full v) {
    v2f o;
    float2 offset = float2((1 - _PlaneScale.x) * 0.5, (1 - _PlaneScale.y) * 0.5);
    o.texcoord = applyMatrix(
    getXYTranslationMatrix(float2(0.5, 0.5)),
    applyMatrix( // scale
    getXYScaleMatrix(float2(1 / _TexRatio, 1) * _TexScale),
    applyMatrix( // rotate
    getXYRotationMatrix(_Theta),
    applyMatrix(
    getXYTranslationMatrix(float2(-0.5, -0.5) + offset),
    (v.texcoord.xy * _PlaneScale)
    )
    )
    )
    );
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    return o;
    }
    half4 frag (v2f IN) : COLOR {
    return tex2D(_MainTex, IN.texcoord);
    }
    ENDCG
    Pass {
    Blend SrcAlpha OneMinusSrcAlpha
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    ENDCG
    }
    }
    FallBack "Diffuse"
    }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Why? What is the first one doing that the second shader doesn't already do?

    -ch