欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

效果如下:

設(shè)置面板如下:

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

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" {}//目標(biāo)貼圖
 [KeywordEnum(Up, Down, Left, Right, Forward, Back)] _mode ("Mode", Int) = 0//切換方向
 _SwapBlend ("Blend", Range(0,1)) = 0//0-1混合值
 _SwapMin("Min Value", Float) = 0//最小世界坐標(biāo)
 _SwapMax("Max Value", Float) = 0//最大世界坐標(biāo)
 _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;//目標(biāo)對象
  [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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# WINFORM 強制讓窗體獲得焦點的方法代碼

    C# WINFORM 強制讓窗體獲得焦點的方法代碼

    C# WINFORM 強制讓窗體獲得焦點的方法代碼,需要的朋友可以參考一下
    2013-04-04
  • C#獲取文件MD5值的實現(xiàn)示例

    C#獲取文件MD5值的實現(xiàn)示例

    文件的md5值,即文件簽名,為了驗證文件的正確性,是否被惡意篡改等。每個文件有一個唯一的md5。下面這篇文中就給大家介紹了如何利用C#獲取文件MD5值,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • C#/VB.NET實現(xiàn)創(chuàng)建PDF/UA文件的示例代碼

    C#/VB.NET實現(xiàn)創(chuàng)建PDF/UA文件的示例代碼

    PDF/UA,即Universally?Accessible?PDF,該格式的PDF文件是于2012年8月以ISO標(biāo)準(zhǔn)14289-1發(fā)布的、具有普遍可訪問的PDF文檔標(biāo)準(zhǔn)。本文將用C#實現(xiàn)DF/UA文件的創(chuàng)建,需要的可以參考一下
    2022-08-08
  • C#中的IEnumerable接口深入研究

    C#中的IEnumerable接口深入研究

    這篇文章主要介紹了.NET中的IEnumerable接口深入研究,分析出了它的實現(xiàn)原理和實現(xiàn)代碼,需要的朋友可以參考下
    2014-07-07
  • 深入解析C#中的abstract抽象類

    深入解析C#中的abstract抽象類

    這篇文章主要介紹了深入解析C#中的abstract抽象類,包括定義抽象類等C#面相對象編程中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-01-01
  • C# 數(shù)組刪除元素的實現(xiàn)示例

    C# 數(shù)組刪除元素的實現(xiàn)示例

    本文主要介紹了C# 數(shù)組刪除元素的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C#基于Extension Method(擴展方法)獲得文件大小的方法

    C#基于Extension Method(擴展方法)獲得文件大小的方法

    這篇文章主要介紹了C#基于Extension Method(擴展方法)獲得文件大小的方法,實例分析了C#擴展方法的定義與文件操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • WPF實現(xiàn)好看的Loading動畫的示例代碼

    WPF實現(xiàn)好看的Loading動畫的示例代碼

    這篇文章主要介紹了如何利用WPF實現(xiàn)好看的Loading動畫效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-08-08
  • 在C#中使用二叉樹實時計算海量用戶積分排名的實現(xiàn)詳解

    在C#中使用二叉樹實時計算海量用戶積分排名的實現(xiàn)詳解

    這篇文章主要介紹了在C#中使用二叉樹實時計算海量用戶積分排名的實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • C#生成Word文檔代碼示例

    C#生成Word文檔代碼示例

    這篇文章主要介紹了C#生成Word文檔代碼示例,本文直接給出代碼實例,需要的朋友可以參考下
    2015-06-06

最新評論