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