Unity Shader learning (eight) mixed use of texture images

Effect:insert image description here

shader code:

Shader "Unlit/shader10"
{
    
     
    ///鼠标移动正方形
    Properties
    {
    
    
        _TextureA("TextureA",2D)="white"{
    
    }
        _TextureB("TextureB",2D)="white"{
    
    }
        _Duration("Duration",float)=6.0
        _StartTime("StartTime",float)=0
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

        Pass
        {
    
    

            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag


            #include "UnityCG.cginc"
        struct v2f{
    
    
            float4 vertex:SV_POSITION;
            float4 position:TEXCOORD1;
            float2 uv:TEXCOORD;
            float4 screenPos:TEXCOORD2;
        };
        v2f vert(appdata_base v){
    
    
            v2f o;
            o.vertex=UnityObjectToClipPos(v.vertex);
            o.position=v.vertex;
            o.uv=v.texcoord;
            o.screenPos=ComputeScreenPos(o.vertex);
            return o;
        }
    
            sampler2D _TextureA;
            sampler2D _TextureB;
            float _Duration;
            float _StartTime;
            fixed4 frag (v2f i) : SV_Target
            {
    
    
             float time=_Time.y-_StartTime;
             float2 p=-1.0+2.0*i.uv;
             float len=length(p);
             float2 ripple=i.uv+(p/len)*cos(len*12.0-time*4.0)*0.03;
             float delta=time/_Duration;
             float2 uv=lerp(ripple,i.uv,delta);
             fixed3 col1=tex2D(_TextureA,uv).rgb;
             fixed3 col2=tex2D(_TextureB,uv).rgb;
             float fade=smoothstep(delta*1.4,delta*2.5,len);
             fixed3 color=lerp(col2,col1,fade);

             return fixed4(color,1.0);
            }
            ENDCG
        }
    }
}

UI control code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIHandler : MonoBehaviour
{
    
    
    public GameObject quad;
    public List<Texture> images;

    private int index;
    private Material material;
    void Start()
    {
    
    
        index = 0;
        if (quad != null)
        {
    
    
            material=quad.GetComponent<MeshRenderer>().material;

        }
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }

    public void NetxtClicked()
    {
    
    
        if (material==null)
        {
    
    
            return;
        }
        index++;
        if (index>=images.Count)
        {
    
    
            index = 0;
        }
        if (index==0)
        {
    
    
            material.SetTexture("_TextureA", images[images.Count - 1]);
            material.SetTexture("_TextureB", images[index]);
        }
        else
        {
    
    
            material.SetTexture("_TextureA", images[index - 1]);
            material.SetTexture("_TextureB", images[index]);
        }
        material.SetFloat("_StartTime", Time.time);
    }
}

interface:
insert image description here

Guess you like

Origin blog.csdn.net/qq_14942529/article/details/126278209