Unlit Shader下的Texture切换


今天要实现一个贴图切换过渡的一个效果,Unlit的性能比较好,所以参考了网上一些Texture的shader之后,写了一下。

具体内容:MixValue 来完成渐变效果, MixValue = 1时为下面的Texture, MixValue = 0时为上面的Texutrue
Shader
下面是shader的代码:主要是frag 部分,通过mixvalue去进行两个贴图的颜色混合

Shader "Unlit/SwapTexture"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    _SubText("Texture", 2D) = "white"{}
    _MixValue("MixValue (Range)",Range(0,1)) = 0.5
  }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
      sampler2D _SubText;
      float _MixValue;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 main = tex2D(_MainTex, i.uv);
        fixed4 sub = tex2D(_SubText, i.uv);
                // apply fog
        main = main * (1 - _MixValue);
        sub = sub * _MixValue;
        fixed4 col = main + sub;
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

一个配套使用的脚本:挂在换贴图的物体上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class TextureSwapComp : MonoBehaviour {
  Renderer target;
  Shader shader;
  bool is_swaping;
  public int texture_index;
  // Use this for initialization
  private void Awake()
  {
    shader = Shader.Find("Unlit/SwapTexture");
    target = this.GetComponent<Renderer>();
    is_swaping = false;
  }

  private void Start()
  {
    ThemeManager.AddTextureSwap(this);
  }

  public void SwapTexture(Texture to, float time, Action action)
  {
    if (is_swaping) StopAllCoroutines();
    StartCoroutine(__Swap(to, time, action));
  }


  IEnumerator __Swap(Texture to, float time, Action action)
  {
    is_swaping = true;
    float timer = 0;
    float mix_value = 0;
    Material swaping_mat =  new Material(shader);
    Material currect_mat = target.material;

    swaping_mat.SetTexture("_MainTex", currect_mat.mainTexture);
    swaping_mat.SetTexture("_SubTex", to);
    swaping_mat.SetFloat("_MixValue", 0);
    target.material = swaping_mat;

    while (time >= timer)
    {
      timer += Time.deltaTime;
      mix_value = Mathf.Lerp(mix_value, 1, timer / time);
      swaping_mat.SetFloat("_MixValue", mix_value);
      yield return null;
    }

    currect_mat.SetTexture("_MainTex", to);
    target.material = currect_mat;

    currect_mat = null;
    swaping_mat = null;
    is_swaping = false;
    if (action != null) action();
  }

  private void OnDestroy()
  {
    StopAllCoroutines();
    ThemeManager.RemoveTextureSwap(this);
  }

  private void OnDisable()
  {
    StopAllCoroutines();
    ThemeManager.RemoveTextureSwap(this);
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在本篇文章中,我们将从一个简单的 Unlit Shader 开始,了解如何在 Unity 的 URP 中编写 ShaderUnlit Shader 是最基本的 Shader 之一,它不考虑任何光照,只对物体进行纯色渲染。这是一个非常适合初学者的 Shader,因为它可以让我们更好地理解 Shader 的基本结构和语法。 首先,我们需要创建一个新的 Shader,选择 URP 中的 Universal Render Pipeline/Lit Shader,并将其重命名为 Unlit。 接下来,打开 Shader 文件,我们可以看到 Shader 的基本结构如下: ``` Shader "Custom/Unlit" { Properties { // 定义 Shader 的属性 } SubShader { // 定义 SubShader 的标签和 Tags Pass { // 定义 Pass 的标签和 Tags CGPROGRAM // Shader 代码 ENDCG } } } ``` 如上所述,Shader 由多个 SubShader 组成,每个 SubShader 又由多个 Pass 组成。在这里,我们只需要关注一个 SubShader 和一个 Pass。 首先,我们需要在 Properties 中定义 Shader 的属性。这些属性可以在材质面板中进行编辑和调整。对于 Unlit Shader,我们只需要定义颜色属性即可,代码如下: ``` Properties { _Color ("Color", Color) = (1,1,1,1) } ``` 上述代码定义了一个名为 _Color 的属性,类型为 Color,初始值为白色。在这里,我们使用了 Unity 的 ShaderLab 语言来定义属性。 接下来,我们需要在 SubShader 中定义标签和 Tags。标签和 Tags 是用来控制 Shader 的渲染和使用的,我们需要根据实际情况进行定义。 对于 Unlit Shader,我们只需要定义一个名为 "RenderType" 的标签,值为 "Opaque",表示这个 Shader 用于不透明的渲染。代码如下: ``` Tags { "RenderType"="Opaque" } ``` 最后,我们需要在 Pass 中编写 Shader 代码。对于 Unlit Shader,我们只需要将颜色值直接输出即可,代码如下: ``` CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; }; float4 _Color; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { return _Color; } ENDCG ``` 在上述代码中,我们定义了两个结构体 appdata 和 v2f,分别代表输入和输出数据。在 vert 函数中,我们将输入的顶点位置转换为屏幕空间的位置,并将其保存到输出中。在 frag 函数中,我们直接返回颜色值,这样就能够实现纯色渲染了。 最后,我们需要将 Shader 保存,并将其赋给一个材质。在材质面板中,我们可以看到 _Color 属性,可以通过这个属性来修改材质的颜色。 这就是从一个简单的 Unlit Shader 开始,在 Unity 的 URP 中编写 Shader 的全部过程。希望本文对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值