Unity Shader学习(八)纹理图像的混合使用

效果:在这里插入图片描述

shader代码:

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控制代码:

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);
    }
}

界面:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToDoNothing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值