unity实现透明水波纹扭曲

 更新时间:2020年05月04日 09:43:07   作者:请输入姓名  
这篇文章主要为大家详细介绍了unity实现透明水波纹扭曲,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity实现透明水波纹扭曲的具体代码,供大家参考,具体内容如下

需要挂一个摄像机把脚本挂在一个物体上

可随意在物体上面点击

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# 序列化与反序列化几种格式的转换

    下面小编就为大家带来一篇浅谈C# 序列化与反序列化几种格式的转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • c# 自定义泛型链表类的详解

    c# 自定义泛型链表类的详解

    本篇文章是对c#中自定义泛型链表类进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Unity ScrollView实现自动吸附效果

    Unity ScrollView实现自动吸附效果

    这篇文章主要为大家详细介绍了Unity ScrollView实现自动吸附效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • .NET/C#实现识别用户访问设备的方法

    .NET/C#实现识别用户访问设备的方法

    这篇文章主要介绍了.NET/C#实现识别用户访问设备的方法,结合实例形式分析了C#识别用户访问设备的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • 如何用C#获取计算机详细的软件和硬件信息

    如何用C#获取计算机详细的软件和硬件信息

    我们应该都知道System.Management提供的类可以用于读取本地计算机设备的各种数据,下面这篇文章主要给大家介绍了关于如何用C#获取计算机详细的软件和硬件信息的相关资料,需要的朋友可以参考下
    2022-12-12
  • Unity实现物体沿自身的任意轴向旋转

    Unity实现物体沿自身的任意轴向旋转

    这篇文章主要为大家详细介绍了Unity实现物体沿自身的任意轴向旋转,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C#配置log4net实现将日志分类记录到不同的日志文件中

    C#配置log4net实现将日志分类记录到不同的日志文件中

    log4net是.Net下一个非常优秀的开源日志记录组件,log4net记录日志的功能非常强大,它可以将日志分不同的等级,以不同的格式,输出到不同的媒介,下面我们就来看看C#如何配置log4net让日志分类记录到不同的日志文件吧
    2024-02-02

最新评论