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

unity實現(xiàn)透明水波紋扭曲

 更新時間:2020年05月04日 09:43:07   作者:請輸入姓名  
這篇文章主要為大家詳細介紹了unity實現(xiàn)透明水波紋扭曲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了unity實現(xiàn)透明水波紋扭曲的具體代碼,供大家參考,具體內容如下

需要掛一個攝像機把腳本掛在一個物體上

可隨意在物體上面點擊

shader:

Shader "Unlit/Water"
{
 Properties
 {
 _MainTex ("Texture", 2D) = "white" {}
 _WaterUV("WaterUV",2D)="while"{}
 _WaterIntensity("WaterIntensity",float)=500
 }
 SubShader
 {

 GrabPass{
 Name "BASE"
 Tags { "Mode" = "Always" }
 }

 Tags { "Queue"="Transparent+100" "RenderType"="Transparent" }
 

 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 
 #include "UnityCG.cginc"

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

 struct v2f
 {
 float2 uv : TEXCOORD0;
 float4 grabuv:TEXCOORD1;
 float4 vertex : SV_POSITION;
 float3 normal:Normal;
 };

 sampler2D _MainTex;
 float4 _MainTex_ST;
 sampler2D _GrabTexture;
 sampler2D _WaterUV;
 float4 _GrabTexture_TexelSize;
 float _WaterIntensity;

 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 o.uv = TRANSFORM_TEX(v.uv, _MainTex);

 UNITY_TRANSFER_FOG(o,o.vertex);
 o.grabuv=ComputeGrabScreenPos(o.vertex);

 o.normal=v.normal;


 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 {
 fixed4 col = tex2D(_MainTex, i.uv);
 float2 uv=tex2D(_WaterUV,i.uv).xy;

  uv= (uv-0.5)*2;

 float2 offset_ =uv * _GrabTexture_TexelSize.xy*_WaterIntensity;
 
 float4 grabuv=i.grabuv;
 grabuv.xy+=sin(offset_);

 fixed4 grabCol=tex2Dproj(_GrabTexture,UNITY_PROJ_COORD(grabuv));
 
 
 return col*grabCol;
 }
 ENDCG
 }
 }
}

C#:

using UnityEngine;
using System.Collections;
using System.Threading;

public class Water : MonoBehaviour
{


 public int m_texHeight = 512;

 public int m_texWidth = 512;

 public Texture2D m_waterTexture = null;

 private Material m_waterMatrial;

 private float[,] waveA;
 private float[,] waveB;

 public float decrement=0.025f;

 public Camera m_waterCam;

 private int time;
 void Start()
 {

  m_waterTexture = new Texture2D(m_texWidth, m_texHeight, TextureFormat.RGBA32, false);

  m_waterMatrial = GetComponent<MeshRenderer>().material;

  waveA = new float[m_texWidth, m_texHeight];
  waveB = new float[m_texWidth, m_texHeight];
 
  m_waterMatrial.SetTexture("_WaterUV", m_waterTexture);

  allColor = new Color[m_texWidth* m_texHeight];

  Thread t = new Thread(new ThreadStart(ThreadWave));
  t.Start();

  // m_waterMatrial.SetTexture("_MainTex", m_waterTexture);
 }

 public void PopWater(Vector2 pos)
 {
  float x=pos.x;
  float y=pos.y;
  waveA[(int)(m_texWidth * x) , (int)(m_texHeight * y)] = 1;
  //waveA[(int)(m_texWidth * x) - 1, (int)(m_texHeight * y) ] = 1;
  //waveA[(int)(m_texWidth * x) + 1, (int)(m_texHeight * y) ] = 1;
  //waveA[(int)(m_texWidth * x) , (int)(m_texHeight * y) - 1] = 1;
  //waveA[(int)(m_texWidth * x), (int)(m_texHeight * y) + 1] = 1;
  //waveA[(int)(m_texWidth * x) - 1, (int)(m_texHeight * y) - 1] = 1;
  //waveA[(int)(m_texWidth * x) - 1, (int)(m_texHeight * y) + 1] = 1;
  //waveA[(int)(m_texWidth * x) + 1, (int)(m_texHeight * y) - 1] = 1;
  //waveA[(int)(m_texWidth * x) + 1, (int)(m_texHeight * y) + 1] = 1;
 }
 public void PopWater()
 {
  waveA[(int)(m_texWidth / 2), (int)(m_texHeight / 2)] = 1;
  waveA[(int)(m_texWidth / 2) - 1, (int)(m_texHeight / 2)] = 1;
  waveA[(int)(m_texWidth / 2) + 1, (int)(m_texHeight / 2)] = 1;
  waveA[(int)(m_texWidth / 2), (int)(m_texHeight / 2) - 1] = 1;
  waveA[(int)(m_texWidth / 2), (int)(m_texHeight / 2) + 1] = 1;
  waveA[(int)(m_texWidth / 2) - 1, (int)(m_texHeight / 2) - 1] = 1;
  waveA[(int)(m_texWidth / 2) - 1, (int)(m_texHeight / 2) + 1] = 1;
  waveA[(int)(m_texWidth / 2) + 1, (int)(m_texHeight / 2) - 1] = 1;
  waveA[(int)(m_texWidth / 2) + 1, (int)(m_texHeight / 2) + 1] = 1;
 }


 void Update()
 {
  ComputeWave();


  if (Input.GetMouseButtonDown(0))
  {
   RaycastHit rh;
   if (Physics.Raycast(m_waterCam.ScreenPointToRay(Input.mousePosition), out rh))
   {
    

    PopWater(rh.textureCoord);
   }

  }

  time = (int)Time.deltaTime * 1000;
 
 }

 void OnDestroy()
 {
  f = false;

 }

 bool f = true;
 void ThreadWave()
 {
  while (f)
  {
   Thread.Sleep(time);
   for (int w = 1; w < m_texWidth - 1; w++)
   {

    for (int h = 1; h < m_texHeight - 1; h++)
    {
     waveB[w, h] =
      (waveA[w - 1, h] +
      waveA[w + 1, h] +
      waveA[w, h - 1] +
      waveA[w, h + 1] +
      waveA[w - 1, h - 1] +
      waveA[w + 1, h - 1] +
      waveA[w - 1, h + 1] +
      waveA[w + 1, h + 1]) / 4 - waveB[w, h];


     float value = waveB[w, h];
     if (value > 1)
     {
      waveB[w, h] = 1;
     }

     if (value < -1)
     {
      waveB[w, h] = -1;
     }


     if (value > -0.0001 && value < 0.0001)
     {
      waveB[w, h] = 0;
     }


     float offset_u = (waveB[w - 1, h] - waveB[w + 1, h]) / 2;

     float offset_v = ((waveB[w, h - 1]) - waveB[w, h + 1]) / 2;


     float r = offset_u / 2 + 0.5f;
     float g = offset_v / 2 + 0.5f;
     Color c = new Color(r, g, 0);
     

     waveB[w, h] -= waveB[w, h] * decrement;
     allColor[w + m_texWidth * h] = c;


    }
   }

   float[,] temp;
   temp = waveA;
   waveA = waveB;
   waveB = temp;
  }

 }

 private Color[] allColor;

 void ComputeWave()
 {
  m_waterTexture.SetPixels(allColor);
  m_waterTexture.Apply();



 }

}

效果圖:

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

相關文章

  • c#之事件用法

    c#之事件用法

    這篇文章介紹了c#中事件的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • c#定時器使用示例詳解

    c#定時器使用示例詳解

    這篇文章主要介紹了c#定時器的使用示例,大家參考使用吧
    2014-01-01
  • Unity的BuildPlayerProcessor實用案例深入解析

    Unity的BuildPlayerProcessor實用案例深入解析

    這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 淺談C# 序列化與反序列化幾種格式的轉換

    淺談C# 序列化與反序列化幾種格式的轉換

    下面小編就為大家?guī)硪黄獪\談C# 序列化與反序列化幾種格式的轉換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • c# 自定義泛型鏈表類的詳解

    c# 自定義泛型鏈表類的詳解

    本篇文章是對c#中自定義泛型鏈表類進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Unity ScrollView實現(xiàn)自動吸附效果

    Unity ScrollView實現(xiàn)自動吸附效果

    這篇文章主要為大家詳細介紹了Unity ScrollView實現(xiàn)自動吸附效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • .NET/C#實現(xiàn)識別用戶訪問設備的方法

    .NET/C#實現(xiàn)識別用戶訪問設備的方法

    這篇文章主要介紹了.NET/C#實現(xiàn)識別用戶訪問設備的方法,結合實例形式分析了C#識別用戶訪問設備的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • 如何用C#獲取計算機詳細的軟件和硬件信息

    如何用C#獲取計算機詳細的軟件和硬件信息

    我們應該都知道System.Management提供的類可以用于讀取本地計算機設備的各種數(shù)據(jù),下面這篇文章主要給大家介紹了關于如何用C#獲取計算機詳細的軟件和硬件信息的相關資料,需要的朋友可以參考下
    2022-12-12
  • Unity實現(xiàn)物體沿自身的任意軸向旋轉

    Unity實現(xiàn)物體沿自身的任意軸向旋轉

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)物體沿自身的任意軸向旋轉,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#配置log4net實現(xiàn)將日志分類記錄到不同的日志文件中

    C#配置log4net實現(xiàn)將日志分類記錄到不同的日志文件中

    log4net是.Net下一個非常優(yōu)秀的開源日志記錄組件,log4net記錄日志的功能非常強大,它可以將日志分不同的等級,以不同的格式,輸出到不同的媒介,下面我們就來看看C#如何配置log4net讓日志分類記錄到不同的日志文件吧
    2024-02-02

最新評論

上蔡县| 纳雍县| 水富县| 海阳市| 龙南县| 明星| 台中县| 临城县| 白银市| 平罗县| 绥宁县| 保定市| 白城市| 舟山市| 定安县| 龙山县| 万宁市| 湖口县| 乐清市| 晋江市| 长春市| 东阳市| 石柱| 邛崃市| 漳浦县| 洛浦县| 安仁县| 巴彦淖尔市| 全椒县| 榆中县| 天长市| 石泉县| 甘泉县| 沁源县| 武宁县| 甘谷县| 汾阳市| 永兴县| 内江市| 施甸县| 讷河市|