最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity3D Shader實現(xiàn)貼圖切換效果

 更新時間:2019年03月01日 08:38:33   作者:星空不語  
這篇文章主要為大家詳細介紹了Unity3D Shader實現(xiàn)貼圖切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了shader實現(xiàn)基于世界坐標的貼圖置換效果。

效果如下:

設置面板如下:

可在面板上設置切換方向,與切換對象,及其切換速度。

shader實現(xiàn)如下:

Shader "XM/Effect/SwapTexture" {
 Properties {
 _Color ("Color", Color) = (1,1,1,1)
 _MainTex ("Albedo (RGB)", 2D) = "white" {}
 _TargetTex ("Target Tex", 2D) = "white" {}//目標貼圖
 [KeywordEnum(Up, Down, Left, Right, Forward, Back)] _mode ("Mode", Int) = 0//切換方向
 _SwapBlend ("Blend", Range(0,1)) = 0//0-1混合值
 _SwapMin("Min Value", Float) = 0//最小世界坐標
 _SwapMax("Max Value", Float) = 0//最大世界坐標
 _Glossiness ("Smoothness", Range(0,1)) = 0.5
 _Metallic ("Metallic", Range(0,1)) = 0.0
 }
 SubShader {
 Tags { "RenderType"="Opaque" }
 LOD 200

 CGPROGRAM
 // Physically based Standard lighting model, and enable shadows on all light types
 #pragma surface surf Standard fullforwardshadows vertex:vert

 // Use shader model 3.0 target, to get nicer looking lighting
 #pragma target 3.0

 sampler2D _MainTex;
 sampler2D _TargetTex;

 struct Input {
  float2 uv_MainTex;
  float3 worldPos;
 };

 half _mode;
 half _SwapBlend;
 float _SwapMin;
 float _SwapMax;
 half _Glossiness;
 half _Metallic;
 fixed4 _Color;

 void vert (inout appdata_full v, out Input o) {
  UNITY_INITIALIZE_OUTPUT(Input,o);
 }

 void surf (Input IN, inout SurfaceOutputStandard o) {
  half useTarget = 0;
  float targetValue = 0;
  switch(_mode)//模式選擇
  {
  case 0://up
   targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
   useTarget = IN.worldPos.y > targetValue?0:1;
   break;
  case 1://down
   targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
   useTarget = IN.worldPos.y < targetValue?0:1;
   break;
  case 2://left
   targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
   useTarget = IN.worldPos.x < targetValue?0:1;
   break;
  case 3://right
   targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
   useTarget = IN.worldPos.x > targetValue?0:1;
   break;
  case 4://forward
   targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
   useTarget = IN.worldPos.z > targetValue?0:1;
   break;
  case 5://back
   targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
   useTarget = IN.worldPos.z < targetValue?0:1;
   break;
  }

  // Albedo comes from a texture tinted by color
  fixed4 c;
  if(useTarget == 1)
  {
  c = tex2D (_TargetTex, IN.uv_MainTex);
  }
  else
  {
  c = tex2D (_MainTex, IN.uv_MainTex);
  }


  c *= _Color;
  o.Albedo = c.rgb;
  // Metallic and smoothness come from slider variables
  o.Metallic = _Metallic;
  o.Smoothness = _Glossiness;
  o.Alpha = c.a;
 }
 ENDCG
 }
 FallBack "Diffuse"
}

配合使用的腳本如下:

using System;
using System.Collections;
using UnityEngine;

namespace XM.Effect
{
 public class SwapTexture : MonoBehaviour
 {
  public enum SwapDirection
  {
   Up,
   Down,
   Left,
   Right,
   Forward,
   Back
  }

  public Shader _shader;//"XM/Effect/SwapTexture” shader
  public SwapDirection _swapDir;//切換方向
  public Renderer _target;//目標對象
  [Range(0, 1)]
  public float _speed;//速度

  private Material _matOld;
  private Material _matNew;

  public void Swap(Texture tex, Action<bool> onComplete)
  {
   if (_matOld != null)
   {
    StopAllCoroutines();
    if (null != onComplete)
    {
     onComplete(false);
    }

    _target.material = _matOld;
   }

   _matOld = _target.material;

   _matNew = new Material(_shader);
   _matNew.SetTexture("_MainTex", _target.material.GetTexture("_MainTex"));
   _matNew.SetTexture("_TargetTex", tex);
   _matNew.SetInt("_mode", (int)_swapDir);
   _matNew.SetFloat("_SwapBlend", 0);

   StartCoroutine("_StartChange", onComplete);
  }

  private IEnumerator _StartChange(Action<bool> onComplete)
  {
   float deltaVal = 0;

   _target.material = _matNew;

   Vector3 vtMin;
   Vector3 vtMax;
   float minVal = 0;
   float maxVal = 0;

   while (deltaVal != 1)
   {
    vtMin = _target.bounds.min;
    vtMax = _target.bounds.max;

    switch (_swapDir)
    {
     case SwapDirection.Up:
     case SwapDirection.Down:
      minVal = vtMin.y;
      maxVal = vtMax.y;
      break;
     case SwapDirection.Left:
     case SwapDirection.Right:
      minVal = vtMin.x;
      maxVal = vtMax.x;
      break;
     case SwapDirection.Forward:
     case SwapDirection.Back:
      minVal = vtMin.z;
      maxVal = vtMax.z;
      break;
    }

    minVal -= 0.01f;
    maxVal += 0.01f;

    _matNew.SetFloat("_SwapMin", minVal);
    _matNew.SetFloat("_SwapMax", maxVal);

    deltaVal = Mathf.Clamp01(deltaVal + _speed * Time.deltaTime);
    _matNew.SetFloat("_SwapBlend", deltaVal);
    yield return null;
   }

   _matOld.SetTexture("_MainTex", _matNew.GetTexture("_TargetTex"));
   _target.material = _matOld;

   _matNew = null;
   _matOld = null;

   if (null != onComplete)
   {
    onComplete(true);
   }
  }

  public void OnDrawGizmos()
  {
   if (_target != null)
   {
    Gizmos.DrawWireCube(_target.bounds.center, _target.bounds.size);
    Gizmos.DrawWireSphere(_target.bounds.min, 0.1f);
    Gizmos.DrawWireSphere(_target.bounds.max, 0.1f);
   }
  }

  //test
  public Texture testTex;
  private void OnGUI()
  {
   if (GUILayout.Button("SwapTexture"))
   {
    Swap(testTex, (t) =>
    {
     Debug.Log("Swap>" + t);
    });
   }
  }
  //
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • c# 循環(huán)語句的使用方法

    c# 循環(huán)語句的使用方法

    這篇文章主要介紹了c# 循環(huán)語句的使用方法,文中代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下
    2020-06-06
  • 詳解C#代碼生成器實現(xiàn)原理

    詳解C#代碼生成器實現(xiàn)原理

    這篇文章主要為大家詳細介紹了C#代碼生成器實現(xiàn)原理的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • C# WinForm窗口最小化到系統(tǒng)托盤

    C# WinForm窗口最小化到系統(tǒng)托盤

    C#編寫最小化時隱藏為任務欄圖標的 Window appllication.
    2008-11-11
  • C# List<T>的用法小結(jié)

    C# List<T>的用法小結(jié)

    本篇文章主要是對C#中List<T>的用法進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#數(shù)組學習相關資料整理

    C#數(shù)組學習相關資料整理

    最近開始學習c#,并有幸接觸到了數(shù)組方便的操作,感覺確實不錯,這里簡單的整理下c#相關的學習資料,方便大家學習
    2012-09-09
  • Unity Blend Tree動畫混合樹使用入門教程

    Unity Blend Tree動畫混合樹使用入門教程

    這篇文章主要為大家詳細介紹了Unity Blend Tree動畫混合樹使用入門教程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法

    VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法

    這篇文章主要介紹了VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • C#給圖片加水印的簡單實現(xiàn)方法

    C#給圖片加水印的簡單實現(xiàn)方法

    這篇文章主要介紹了C#給圖片加水印的簡單實現(xiàn)方法,涉及C#操作圖片的相關技巧,非常具有實用價值,需要的朋友可以參考下
    2015-05-05
  • C#多線程編程中的鎖系統(tǒng)(三)

    C#多線程編程中的鎖系統(tǒng)(三)

    這篇文章主要介紹了C#多線程編程中的鎖系統(tǒng)(三),本本文主要說下基于內(nèi)核模式構(gòu)造的線程同步方式、事件、信號量以及WaitHandle、AutoResetEvent、ManualResetEvent等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • 圖文詳解C#中的協(xié)變與逆變

    圖文詳解C#中的協(xié)變與逆變

    “協(xié)變”是指能夠使用與原始指定的派生類型相比,派生程度更大的類型,“逆變”則是指能夠使用派生程度更小的類型,這篇文章主要給大家介紹了關于C#中協(xié)變與逆變的相關資料,需要的朋友可以參考下
    2022-02-02

最新評論

南康市| 丽江市| 蓬莱市| 晋江市| 襄垣县| 富锦市| 花莲市| 平昌县| 康保县| 正蓝旗| 广水市| 黑河市| 凌源市| 甘肃省| 双鸭山市| 通榆县| 靖远县| 肥乡县| 凤阳县| 恩施市| 庆安县| 阿合奇县| 汝州市| 兰州市| 五莲县| 浦县| 黄石市| 青河县| 宜君县| 合山市| 永年县| 长武县| 临朐县| 蒲江县| 集贤县| 南投市| 信宜市| 霍州市| 太仓市| 新巴尔虎左旗| 洪湖市|