Advertisement
Guest User

Untitled

a guest
Jul 20th, 2020
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FluidSimulation : MonoBehaviour
  6. {
  7.  
  8.     public RenderTexture readA;
  9.     public RenderTexture readB;
  10.     public RenderTexture readC;
  11.     public RenderTexture readD;
  12.  
  13.     public RenderTexture writeA;
  14.     public RenderTexture writeB;
  15.     public RenderTexture writeC;
  16.     public RenderTexture writeD;
  17.  
  18.     public ComputeShader Compute;
  19.  
  20.     public Material OutputMaterial;
  21.  
  22.     [Range(8, 1024)] public int resolution = 8;
  23.  
  24.     private int stepKernel;
  25.  
  26.     private int nthreads = 1;
  27.  
  28.     private int threadresolution => (resolution / nthreads);
  29.  
  30.     private Camera cam;
  31.  
  32.     [Range(1,50)]public int stepMod = 1;
  33.  
  34.     [Range(0, 50)] public int stepsPerFrame = 0;
  35.  
  36.  
  37.     // Start is called before the first frame update
  38.     void Start()
  39.     {
  40.         cam = Camera.main;
  41.         Reset();
  42.     }
  43.  
  44.     private void Reset()
  45.     {
  46.         readA = CreateTexture(RenderTextureFormat.ARGBFloat);
  47.         readB = CreateTexture(RenderTextureFormat.ARGBFloat);
  48.         readC = CreateTexture(RenderTextureFormat.ARGBFloat);
  49.         readD = CreateTexture(RenderTextureFormat.ARGBFloat);
  50.  
  51.         writeA = CreateTexture(RenderTextureFormat.ARGBFloat);
  52.         writeB = CreateTexture(RenderTextureFormat.ARGBFloat);
  53.         writeC = CreateTexture(RenderTextureFormat.ARGBFloat);
  54.         writeD = CreateTexture(RenderTextureFormat.ARGBFloat);
  55.  
  56.         stepKernel = Compute.FindKernel("StepKernel");
  57.  
  58.         GPUResetKernel();
  59.     }
  60.  
  61.     void GPUResetKernel()
  62.     {
  63.         int k = Compute.FindKernel("ResetKernel");
  64.  
  65.         Compute.SetTexture(k, "readA", readA);
  66.         Compute.SetTexture(k, "readB", readB);
  67.         Compute.SetTexture(k, "readC", readC);
  68.         Compute.SetTexture(k, "readD", readD);
  69.  
  70.         Compute.SetTexture(k, "writeA", writeA);
  71.         Compute.SetTexture(k, "writeB", writeB);
  72.         Compute.SetTexture(k, "writeC", writeC);
  73.         Compute.SetTexture(k, "writeD", writeD);
  74.  
  75.         Compute.SetInt("nthreads", nthreads);
  76.  
  77.         Compute.SetInt("resolution", resolution);
  78.  
  79.         Compute.Dispatch(k, threadresolution, threadresolution, 1);
  80.     }
  81.  
  82.     // Update is called once per frame
  83.     void Update()
  84.     {
  85.         if (Time.frameCount % stepMod == 0)
  86.         {
  87.             for (int i = 0; i < stepsPerFrame; i++)
  88.             {
  89.                 Step();
  90.             }
  91.         }
  92.     }
  93.  
  94.     void Step()
  95.     {
  96.         if (readA == null || readB == null || readC == null || readD == null)
  97.         {
  98.             Reset();
  99.         }
  100.  
  101.         Compute.SetTexture(stepKernel, "readA", readA);
  102.         Compute.SetTexture(stepKernel, "readB", readB);
  103.         Compute.SetTexture(stepKernel, "readC", readC);
  104.         Compute.SetTexture(stepKernel, "readD", readD);
  105.  
  106.         Compute.SetTexture(stepKernel, "writeA", writeA);
  107.         Compute.SetTexture(stepKernel, "writeB", writeB);
  108.         Compute.SetTexture(stepKernel, "writeC", writeC);
  109.         Compute.SetTexture(stepKernel, "writeD", writeD);
  110.  
  111.         Compute.Dispatch(stepKernel, resolution/nthreads, resolution/nthreads, 1);
  112.  
  113.         SwapTex();
  114.  
  115.         OutputMaterial.SetTexture("_UnlitColorMap", readD);
  116.     }
  117.  
  118.     protected RenderTexture CreateTexture(RenderTextureFormat format)
  119.     {
  120.         RenderTexture tex = new RenderTexture(resolution, resolution, 0, format);
  121.  
  122.         //IMPORTANT FOR GPU SHADERS, allows random access (like gpus will do)
  123.         tex.enableRandomWrite = true;
  124.         tex.dimension = UnityEngine.Rendering.TextureDimension.Tex2D;
  125.         tex.volumeDepth = 1;
  126.         tex.filterMode = FilterMode.Point;
  127.         tex.wrapMode = TextureWrapMode.Repeat;
  128.         tex.useMipMap = false;
  129.         tex.Create();
  130.  
  131.         return tex;
  132.     }
  133.  
  134.     void SwapTex()
  135.     {
  136.         RenderTexture tmp = readA;
  137.         readA = writeA;
  138.         writeA = tmp;
  139.  
  140.         tmp = readB;
  141.         readB = writeB;
  142.         writeB = tmp;
  143.  
  144.         tmp = readC;
  145.         readC = writeC;
  146.         writeC = tmp;
  147.  
  148.         tmp = readD;
  149.         readD = writeD;
  150.         writeD = tmp;
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement