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();

 }

}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Unity Shader实现2D水流效果

    水流的模拟主要运用了顶点变换和纹理动画的结合: 顶点变换中,利用正弦函数模拟河流的大致形态,例如波长,振幅等. 纹理动画中,将纹理坐标朝某一方向持续滚动以形成流动的效果. 脚本如下: Shader "MyUnlit/ScrollWater" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color("Color Tint",color)=(1,1,1,1) //控制

  • Unity Shader实现水墨效果

    Unity Shader学习:水墨效果 偶然在网上看到9级铁甲蛹大神的水墨风格后处理觉得挺有意思,参照着实现一下,还是涉及到之前油画效果的算法,叫什么滤波暂时不清楚,应该用来处理手绘效果挺多的. 水墨风格基本原理:高斯模糊原始图像,用深度算出边缘进行描边,最后用画笔效果的滤波完成最终图像. 有需要可以用Post Proces改变颜色范围,更接近水墨的颜色. C#部分: //屏幕后处理基类 using UnityEngine; using System.Collections; //非运行时也触发

  • Unity Shader实现水波纹效果

    本文实例为大家分享了Unity Shader实现水波纹的具体代码,供大家参考,具体内容如下 效果: Shader代码: Shader "Custom/shuibowen"{ Properties{ _MainTex("Base (RGB)",2D)="white"{} _distanceFactor("Distancefactor",float)=1 _timeFactor("time factor",fl

  • unity实现透明水波纹扭曲

    本文实例为大家分享了unity实现透明水波纹扭曲的具体代码,供大家参考,具体内容如下 需要挂一个摄像机把脚本挂在一个物体上 可随意在物体上面点击 shader: Shader "Unlit/Water" { Properties { _MainTex ("Texture", 2D) = "white" {} _WaterUV("WaterUV",2D)="while"{} _WaterIntensity(&

  • Android自定义View 实现水波纹动画引导效果

    一.实现效果图 二.实现代码 1.自定义view package com.czhappy.showintroduce.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Pat

  • Android实现点击Button产生水波纹效果

    先上图,看看接下来我要向大家介绍的是个什么东西,如下图: 接下来要介绍的就是如何实现上述图中的波纹效果,这种效果如果大家没有体验过的话,可以看看百度手机卫士或者360手机卫士,里面的按钮点击效果都是这样的,另外Android 5.0以上的版本也出现了这种效果.不多说,下面聊聊具体的怎么实现. 首先大家看到的是三个button,水波纹的出现给我们的错觉是直接将波纹绘制在button上面的,但是这样能做到吗?首先button自己有background和src,如果把半透明的水波纹当作backgrou

  • Android自定义View实现水波纹引导动画

    一.实现效果图 关于贝塞尔曲线 二.实现代码 1.自定义view package com.czhappy.showintroduce.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.grap

  • Android实现兼容的水波纹效果

    先看看效果图 其实,要实现这一效果很简单,只要分drawable和drawablev21两个文件夹就好了. 普通情况下的selector: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@c

  • Android实现自定义华丽的水波纹效果

    先来看看效果 实现效果 模拟水波纹的效果:点击屏幕就有圆环出现,半径从小到大,透明度从大到小(0为透明) 实现思路 1.自定义类继承View. 2.定义每个圆环的实体类 Wave,并初始化绘制圆环的画笔的数据. 3.重写onTouchEvent方法,down时,获得坐标点,做为圆环圆心. 4.发送handler信息,对数据进行修改,刷新页面. 5.重写onDraw方法,绘制一个圆环. 1. 自定义类继承View 新建WaterWaveView2类继承View public class Water

  • Android5.0中多种水波纹效果的实现代码

    水波纹效果已经不是什么稀罕的东西了,用过5.0新控件的小伙伴都知道这个效果,可是如果使用一个TextView或者Button或者其它普通控件的话,你是否知道如何给它设置水波纹效果呢?OK,我们今天就来看看这个水波纹效果的实现.水波纹效果的实现有系统自带属性可以实现,我们也可以自定义实现效果. 1.系统自带水波纹实现方式 有界水波纹 水波纹效果大致上可以分为两种,一种是有界的,一种无界,我们先来看看有界水波纹效果: 效果: 代码: <TextView android:layout_width=&quo

  • Android RippleDrawable 水波纹/涟漪效果的实现

    一.效果图 二.RippleDrawable基本概念介绍 (1).RippleDrawable RippleDrawable可以实现上面效果图中的水波纹效果,它是在API 21 中添加的,所以,低于21的版本中不可使用.它的继承关系如下: 根据上面的继承关系,我们可知,我们可以用它来做背景:RippleDrawable是有层级的--LayerDrawable的特性. (2).xml属性 RippleDrawable在xml中对应的是 <ripple></ripple>,它只有两个属

  • Android自定义控件实现水波纹效果

    本文实例为大家分享了Android自定义控件实现水波纹的具体代码,供大家参考,具体内容如下 示例代码: MainActivity.java package com.example.mhy.shuibowen; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protec

随机推荐