Unity Shader学习:流体2

Unity Shader学习:流体2

参考:https://zhuanlan.zhihu.com/p/340842666
在这里插入图片描述
c#部分:

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

public class ZzcSmoke : MonoBehaviour
{
   
    public Material smokeMat;

    private RenderTexture DivergenceRT;
    private RenderTexture CurlRT;
    private RenderTexture DensityRT;
    private RenderTexture DensityRT2;
    private RenderTexture VelocityRT;
    private RenderTexture VelocityRT2;
    private RenderTexture PressureRT;
    private RenderTexture PressureRT2;

    [Range(0.95f, 1.0f)]
    public float DensityDiffusion = 0.995f;//密度消失速度,此值越大则粘度越小,越容易看到烟雾效果
    [Range(0.95f, 1.0f)]
    public float VelocityDiffusion = 0.995f;//速度扩散速度,此值越大则粘度越小,越容易看到烟雾效果
    [Range(1, 60)]
    public int Iterations = 50;//泊松方程迭代次数
    [Range(0, 60)]
    public float Vorticity = 50f;//控制漩涡缩放
    [Range(0.0001f, 0.005f)]
    public float SplatRadius = 0.001f;//鼠标点击产生烟雾的半径
    [Range(1, 15)]
    public float MouseForceScale = 10.0f;//鼠标拖动力度缩放

    private float dt = 0.01f;
    private float MouseDX = 0.0f, MouseDY = 0.0f, MouseX, MouseY;

    void Start()
    {
   
        int TexWidth = Screen.width;
        int TexHeight = Screen.height;
        DivergenceRT = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RHalf); DivergenceRT.Create();
        CurlRT = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RGHalf); CurlRT.Create();
        DensityRT = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.ARGBHalf); DensityRT.Create();
        DensityRT2 = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.ARGBHalf); DensityRT2.Create();
        VelocityRT = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RGHalf); VelocityRT.Create();
        VelocityRT2 = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RGHalf); VelocityRT2.Create();
        PressureRT = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RHalf); PressureRT.Create();
        PressureRT2 = new RenderTexture(TexWidth, TexHeight, 0, RenderTextureFormat.RHalf); PressureRT2.Create();
    }

    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
   
        //鼠标输入相关
        MouseDX = (Input.mousePosition.x - MouseX) * MouseForceScale;
        MouseDY = (Input.mousePosition.y - MouseY) * MouseForceScale;
        MouseX = Input.mousePosition.x;
        MouseY = Input.mousePosition.y;

        //第一步:平流速度
        smokeMat.SetTexture("VelocityTex", VelocityRT2);
        smokeMat.SetTexture("VelocityDensityTex", VelocityRT2);
        smokeMat.SetFloat("dt", dt);
        smokeMat.SetFloat("dissipation", VelocityDiffusion);
        Graphics.Blit(VelocityRT2, VelocityRT, smokeMat,0);
        Graphics.Blit(VelocityRT, VelocityRT2);

        if (Input.GetMouseButton(0))
        {
   
            //第二步:鼠标拖动会影响速度
            smokeMat.SetTexture(
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值