Advertisement
Guest User

script and compute shader

a guest
Apr 26th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | Source Code | 0 0
  1.  
  2.  
  3.  
  4.  
  5. using UnityEngine;
  6.  
  7. public class ConwayCompute : MonoBehaviour
  8. {
  9.     public ComputeShader computeShader;
  10.     public int width = 300;
  11.     public int height = 300;
  12.     public RenderTexture renderTexture1;
  13.     public RenderTexture renderTexture2;
  14.     private bool useRenderTexture1 = true;
  15.  
  16.     void Start()
  17.     {
  18.         InitializeTextures();
  19.         RandomInitialize();
  20.     }
  21.  
  22.     void InitializeTextures()
  23.     {
  24.         renderTexture1 = new RenderTexture(width, height, 0, RenderTextureFormat.RFloat);
  25.         renderTexture1.enableRandomWrite = true;
  26.         renderTexture1.Create();
  27.  
  28.         renderTexture2 = new RenderTexture(width, height, 0, RenderTextureFormat.RFloat);
  29.         renderTexture2.enableRandomWrite = true;
  30.         renderTexture2.Create();
  31.     }
  32.  
  33.     void RandomInitialize()
  34.     {
  35.         computeShader.SetTexture(1, "currentBuffer", useRenderTexture1 ? renderTexture1 : renderTexture2);
  36.         computeShader.SetTexture(1, "nextBuffer", useRenderTexture1 ? renderTexture2 : renderTexture1);
  37.         computeShader.Dispatch(1, width / 16, height / 16, 1);
  38.     }
  39.  
  40.     private void OnRenderImage(RenderTexture src, RenderTexture dest)
  41.     {
  42.         computeShader.SetTexture(0, "currentBuffer", useRenderTexture1 ? renderTexture1 : renderTexture2);
  43.         computeShader.SetTexture(0, "nextBuffer", useRenderTexture1 ? renderTexture2 : renderTexture1);
  44.         computeShader.Dispatch(0, width / 16, height / 16, 1);
  45.  
  46.         // Swap render textures for the next frame
  47.         useRenderTexture1 = !useRenderTexture1;
  48.  
  49.         // Display the result using Graphics.Blit
  50.         Graphics.Blit(useRenderTexture1 ? renderTexture1 : renderTexture2, dest);
  51.     }
  52.  
  53.     void OnDestroy()
  54.     {
  55.         renderTexture1.Release();
  56.         renderTexture2.Release();
  57.     }
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. #pragma kernel CSMain
  65. #pragma kernel CSRandomInit
  66.  
  67. RWTexture2D<float> currentBuffer;
  68. RWTexture2D<float> nextBuffer;
  69.  
  70. [numthreads(16, 16, 1)]
  71. void CSMain (uint3 id : SV_DispatchThreadID) {
  72.     uint2 texDim;
  73.     currentBuffer.GetDimensions(texDim.x, texDim.y);
  74.  
  75.     uint2 cellPos = id.xy;
  76.     float current = currentBuffer[cellPos];
  77.  
  78.     // Count live neighbors
  79.     int count = 0;
  80.     for (int x = -1; x <= 1; x++) {
  81.         for (int y = -1; y <= 1; y++) {
  82.             if (x == 0 && y == 0) continue;
  83.             float neighbor = currentBuffer[clamp(cellPos + uint2(x, y), uint2(0, 0), texDim - uint2(1, 1))];
  84.             count += (int)neighbor;
  85.         }
  86.     }
  87.  
  88.     // Apply Game of Life rules
  89.     float next = 0.0;
  90.     if (current > 0.5) {  // Cell is alive
  91.         if (count < 2 || count > 3) next = 0.0;  // Loneliness or overcrowding
  92.         else next = 1.0;  // Survival
  93.     } else {  // Cell is dead
  94.         if (count == 3) next = 1.0;  // Reproduction
  95.         else next = 0.0;  // Stay dead
  96.     }
  97.  
  98.     nextBuffer[cellPos] = next;
  99. }
  100.  
  101. [numthreads(16, 16, 1)]
  102. void CSRandomInit (uint3 id : SV_DispatchThreadID) {
  103.     // Seed the pseudo-random number generator with thread ID
  104.     uint seed = ((id.x & 0xFF) << 24) | ((id.y & 0xFF) << 16) | (id.z & 0xFFFF);
  105.    
  106.     // Use a simple pseudo-random algorithm (XORSHIFT) to generate a value between 0 and 1
  107.     uint randState = seed;
  108.     randState ^= randState << 13;
  109.     randState ^= randState >> 17;
  110.     randState ^= randState << 5;
  111.  
  112.     float randomValue = float(randState & 0xFFFFFF) / float(0xFFFFFF);
  113.    
  114.     // Set the cell state based on the random value
  115.     currentBuffer[id.xy] = (randomValue > 0.5) ? 1.0 : 0.0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement