Unity Shader学习:ShaderToy扩散扭曲

本文主要探讨了如何在Unity中实现ShaderToy上的扩散扭曲效果,通过C#代码和Shader的结合,为游戏场景增添独特的视觉体验。
摘要由CSDN通过智能技术生成

Unity Shader学习:ShaderToy扩散扭曲

算法来自:https://www.shadertoy.com/view/4dcGW2
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
c#部分:

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

public class Diffusion : MonoBehaviour
{
   
    public Material mat;
 
    private RenderTexture BufferA;
    private RenderTexture BufferB;
    private RenderTexture BufferC;
    private RenderTexture BufferD;
    private RenderTexture BufferAA;
    private RenderTexture BufferBB;
    private RenderTexture BufferCC;
    private RenderTexture BufferDD;

    private float startTime;
    private bool isClicking = false;
    private int count;
    private float mouseX, mouseY,mouseXX,mouseYY;

    private void Start()
    {
   
        BufferA = new RenderTexture(1920, 1080, 0);
        BufferB = new RenderTexture(1920, 1080, 0);
        BufferC = new RenderTexture(1920, 1080, 0);
        BufferD = new RenderTexture(1920, 1080, 0);
        BufferAA = new RenderTexture(1920, 1080, 0);
        BufferBB = new RenderTexture(1920, 1080, 0);
        BufferCC = new RenderTexture(1920, 1080, 0);
        BufferDD = new RenderTexture(1920, 1080, 0);
        mouseXX = -1000f;
        mouseYY = -1000f;
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
   
        startTime += Time.deltaTime;
        mat.SetFloat("_StartTime", startTime);
        mat.SetTexture("_iChannel0", BufferAA);
        mat.SetTexture("_iChannel1", BufferCC);
        mat.SetTexture("_iChannel2", BufferDD);
        Graphics.Blit(source, BufferA, mat, 0);
        Graphics.Blit(BufferA, BufferAA);
        mat.SetTexture("_iChannel0", BufferAA);
        Graphics.Blit(BufferA, BufferB, mat, 1);
        Graphics.Blit(BufferB, BufferBB);
        mat.SetTexture("_iChannel0", BufferBB);
        Graphics.Blit(BufferB, BufferC, mat, 2);
        Graphics.Blit(BufferC, BufferCC);
        mat.SetTexture("_iChannel0", BufferAA);
        Graphics.Blit(BufferC, BufferD, mat, 3);
        Graphics.Blit(BufferD, BufferDD);
        mat.SetTexture("_iChannel0", BufferAA);
        Graphics.Blit(BufferD, destination, mat, 4);
    }

    private void Update()
    {
   
        if (Input.GetMouseButton(0))
        {
   
            mat.SetVector("_MousePos", new Vector4(Input.mousePosition.x, Input.mousePosition.y, 0, 0));
            mouseXX = Input.mousePosition.x;
            mouseYY= Input.mousePosition.y;
        }
        else
            mat.SetVector("_MousePos", new Vector4(mouseXX,mouseYY,0,0));        

        if (Input.GetMouseButtonDown(0))
            isClicking = true;

        if (Input.GetMouseButtonUp(0))
        {
   
            isClicking = false;
            count = 0;
        }

        if (isClicking)
        {
   
            if (count < 1)
            {
   
                mouseX = Input.mousePosition.x;
                mouseY = Input.mousePosition.y
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值