IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 交互涟漪(水波) -> 正文阅读

[游戏开发]交互涟漪(水波)

原地址:【Unity教程】可实时交互的涟漪效果_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili

?思路的简单实现

点击物体的UV,传入shader,获得一张,根据点生生成的小圆球

将图片传入扩散shader,神奇的公式

 float3 offset = float3(_SourceTex_TexelSize.xy,0);
 float col12 = tex2D(_SourceTex,i.uv + offset.zy ).x;
 float col10 = tex2D(_SourceTex,i.uv - offset.zy ).x;
 float col01 = tex2D(_SourceTex,i.uv - offset.xz ).x;
 float col21 = tex2D(_SourceTex,i.uv + offset.xz ).x;
 float col11 = tex2D(_PrevTex,i.uv).x;
 float finel = (col10 + col12 + col01 + col21)*0.5 - col11;
 finel = finel*0.99;
 return float4(finel,0,0,1);

将生成的高度场贴图传入

根据高度场生成法线贴图

C#代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshUVRipple : MonoBehaviour
{
    public Camera camera;
    public float radius;
    private Shader _generateRippleHight;
    private RenderTexture _tempTexture;
    private RenderTexture _currentTexture;
    private RenderTexture _prevTexture;
    private Material _hightRipple;
    private Material _point_material;
    private Material material;
    private void Awake()
    {
        if (null == camera)
        {
            camera = Camera.main;
        }
        _currentTexture = CreatRT();
        _tempTexture = CreatRT();
        _prevTexture = CreatRT();

        _point_material = new Material(Shader.Find("Custom/GenerateRippleHight"));
        _hightRipple = new Material(Shader.Find("Custom/HightRipple"));

        material = GetComponent<MeshRenderer>().material;
    }
    private RenderTexture CreatRT()
    {
        var rt = new RenderTexture(1024, 1024, 0, RenderTextureFormat.RFloat);
        rt.Create();
        return rt;
    }
    private Vector2 lastTexCoord;
    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            RaycastHit hit;
            if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (Vector2.Distance(hit.textureCoord, lastTexCoord) >0.01f)
                {
                    lastTexCoord = hit.textureCoord;
                    DrawPoint(hit);
                }
            }
        }
        DrawRipple();
        material.SetTexture("_RippleHightTex", _currentTexture);
    }
    private void DrawPoint(RaycastHit hit)
    {


        _point_material.SetFloat("_Radius", radius);
        _point_material.SetVector("_HitPos", hit.textureCoord);
        _point_material.SetTexture("_SourceTex", _currentTexture);
        Graphics.Blit(null, _tempTexture, _point_material);

        RenderTexture rt = _currentTexture;
        _currentTexture = _tempTexture;
        _tempTexture = rt;
    }

    private void DrawRipple()
    {

        _hightRipple.SetTexture("_SourceTex", _currentTexture);
        _hightRipple.SetTexture("_PrevTex", _prevTexture);
        Graphics.Blit(null, _tempTexture, _hightRipple);
        Graphics.Blit(_tempTexture, _prevTexture);

        RenderTexture rt = _prevTexture;
        _prevTexture = _currentTexture;
        _currentTexture = rt;
    }

 
}

?初始涟漪生成shader

Shader "Custom/GenerateRippleHight"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            sampler2D _SourceTex;
            float4 _SourceTex_ST;
            float2 _HitPos;
            float _Radius;

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _SourceTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_SourceTex, i.uv);
                float factor =max(0,_Radius-length(i.uv-_HitPos)/_Radius);
                col.r += min( factor,1);
                return col;
            }
            ENDCG
        }
    }
}

?涟漪扩散shader

Shader "Custom/HightRipple"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };


            sampler2D _SourceTex;
            sampler2D _PrevTex;
            float4 _SourceTex_ST;
            float4 _SourceTex_TexelSize;

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float3 offset = float3(_SourceTex_TexelSize.xy,0);
                float col12 = tex2D(_SourceTex,i.uv + offset.zy ).x;
                float col10 = tex2D(_SourceTex,i.uv - offset.zy ).x;
                float col01 = tex2D(_SourceTex,i.uv - offset.xz ).x;
                float col21 = tex2D(_SourceTex,i.uv + offset.xz ).x;
                float col11 = tex2D(_PrevTex,i.uv).x;
                float finel = (col10 + col12 + col01 + col21)*0.5 - col11;
                finel = finel*0.99;
                return float4(finel,0,0,1);
            }
            ENDCG
        }
    }
}

最终应用shader,简单的用了Blinn-Phong光照模型

Shader "Unlit/MeshRipple"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Specular ("_Specular", Color) = (1,1,1,1)
        _Diffuse ("_Diffuse", Color) = (1,1,1,1)
        
        _heigth2normalFactor ("_heigth2normalFactor", Range(0.01,100)) = 0.1
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque"  "Queue" = "Transparent" }
        LOD 100
         Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float4 worldPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _RippleHightTex;
            float4 _RippleHightTex_TexelSize;
            float4 _Specular;
            float4 _Diffuse;
            float _heigth2normalFactor;

            float3 HigthToNormal(sampler2D heigthTex ,float2 heigth_TexelSize , float2 uv)
            {
                float3 offset = float3(heigth_TexelSize.xy,0);
                float factor = _heigth2normalFactor;
                float3 S = float3(1,0, (tex2D(heigthTex,uv + offset.xz).x - tex2D(heigthTex,uv - offset.xz).x)*factor);
                float3 T = float3(0,1, (tex2D(heigthTex,uv + offset.zy).x - tex2D(heigthTex,uv - offset.zy).x)*factor);
                float3 d =  cross(S,T)+float3(0.5,0.5,1);
                return d;
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float4 col = tex2D(_MainTex, i.uv)*_Diffuse;
 
                float3 normal = HigthToNormal(_RippleHightTex,_RippleHightTex_TexelSize.xy, i.uv);
                normal = normalize(normal);

                float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                float3 view = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				float3 halfDir = normalize(worldLight + view);
				float3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(normal, halfDir)), 4);


                col.rgb += specular;
                return col;
            }
            ENDCG
        }
    }
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-11-10 12:43:37  更:2021-11-10 12:43:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/28 0:48:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码