Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using Unity.VisualScripting;
- using UnityEngine;
- //Red = 0
- //Blue = 1
- //Yellow = 2
- //Green = 3
- public struct Particle
- {
- public float posX;
- public float posY;
- public float velX;
- public float velY;
- public int type;
- }
- public enum matrixPreset
- {
- random,
- symmetry,
- chains,
- chains2,
- chains3,
- snakes,
- zero
- }
- public enum mode
- {
- _3D,
- _2D,
- }
- public class Main : MonoBehaviour
- {
- public matrixPreset preset;
- public mode mode;
- public RenderTexture renderTexture;
- public ComputeShader computeShader;
- public int resolution;
- public int particleCount;
- public float forceFactor;
- public float maxDist;
- public float friction;
- [Header("3D")]
- public Material particleMaterial;
- public float particleSize;
- float[,] attractionMatrix;
- float[] attractionMatrix1D;
- Particle[] particles;
- bool is3D;
- void Start()
- {
- switch (preset)
- {
- case matrixPreset.random:
- attractionMatrix = new float[,]
- {
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- {Rand(), Rand(), Rand(), Rand(), Rand(), Rand()},
- };
- break;
- case matrixPreset.symmetry:
- float[] rand = new float[22];
- for (int i = 0; i < rand.Length; i++)
- {
- rand[i] = Rand();
- }
- attractionMatrix = new float[,]
- {
- {rand[0], rand[1], rand[2], rand[3], rand[4], rand[5]},
- {rand[1], rand[7], rand[8], rand[9], rand[10], rand[11]},
- {rand[2], rand[8], rand[12], rand[13], rand[14], rand[15]},
- {rand[3], rand[9], rand[13], rand[16], rand[17], rand[18]},
- {rand[4], rand[1], rand[14], rand[17], rand[19], rand[20]},
- {rand[5], rand[1], rand[15], rand[18], rand[20], rand[21]},
- };
- break;
- case matrixPreset.chains:
- attractionMatrix = new float[,]
- {
- {1, 1, -1, -1, -1, 1},
- {1, 1, 1, -1, -1, -1},
- {-1, 1, 1, 1, -1, -1},
- {-1, -1, 1, 1, 1, -1},
- {-1, -1, -1, 1, 1, 1},
- {1, -1, -1, -1, 1, 1},
- };
- break;
- case matrixPreset.chains2:
- attractionMatrix = new float[,]
- {
- {1, .2f, -1, -1, -1, .2f},
- {.2f, 1, .2f, -1, -1, -1},
- {-1, .2f, 1, .2f, -1, -1},
- {-1, -1, .2f, 1, .2f, -1},
- {-1, -1, -1, .2f, 1, .2f},
- {.2f, -1, -1, -1, .2f, 1},
- };
- break;
- case matrixPreset.chains3:
- attractionMatrix = new float[,]
- {
- {1, .2f, 0, 0, 0, 0},
- {.2f, 1, .2f, 0, 0, 0},
- {0, .2f, 1, .2f, 0, 0},
- {0, 0, .2f, 1, .2f, 0},
- {0, 0, 0, .2f, 1, .2f},
- {.2f, 0, 0, 0, .2f, 1},
- };
- break;
- case matrixPreset.snakes:
- attractionMatrix = new float[,]
- {
- {1, .2f, 0, 0, 0, 0},
- {0, 1, .2f, 0, 0, 0},
- {0, 0, 1, .2f, 0, 0},
- {0, 0, 0, 1, .2f, 0},
- {0, 0, 0, 0, 1, .2f},
- {.2f, 0, 0, 0, 0, 1},
- };
- break;
- case matrixPreset.zero:
- attractionMatrix = new float[,]
- {
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0},
- };
- break;
- }
- attractionMatrix1D = new float[36];
- for (int y = 0; y < 6; y++)
- {
- for (int x = 0; x < 6; x++)
- {
- attractionMatrix1D[x + (y * 6)] = attractionMatrix[x, y];
- }
- }
- particles = new Particle[particleCount];
- for (int i = 0; i < particleCount; i++)
- {
- Vector2 position = new Vector2(Random.Range(0f, resolution - 1f), Random.Range(0f, resolution - 1f));
- particles[i] = new Particle
- {
- type = Random.Range(0, 6),
- posX = position.x,
- posY = position.y,
- velX = 0,
- velY = 0
- };
- }
- renderTexture = new RenderTexture(resolution, resolution, 24);
- renderTexture.enableRandomWrite = true;
- //renderTexture.format = RenderTextureFormat.RGB111110Float;
- renderTexture.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R16G16B16A16_SFloat;
- renderTexture.Create();
- }
- private void OnRenderImage(RenderTexture source, RenderTexture destination)
- {
- ClearRenderTexture(renderTexture);
- // Create the ComputeBuffer and set it up
- ComputeBuffer particlesBuffer = new ComputeBuffer(particleCount, sizeof(float) * 4 + sizeof(int));
- particlesBuffer.SetData(particles);
- ComputeBuffer matrixBuffer = new ComputeBuffer(36, sizeof(float));
- matrixBuffer.SetData(attractionMatrix1D);
- computeShader.SetTexture(0, "Result", renderTexture);
- computeShader.SetBuffer(0, "particles", particlesBuffer);
- computeShader.SetBuffer(0, "attractionMatrix", matrixBuffer);
- computeShader.SetFloat("deltaTime", Time.deltaTime);
- computeShader.SetInt("resolution", resolution);
- computeShader.SetInt("particleCount", particleCount);
- computeShader.SetFloat("forceFactor", forceFactor);
- computeShader.SetFloat("frictionFactor", friction);
- computeShader.SetFloat("maxDist", maxDist);
- computeShader.Dispatch(0, Mathf.CeilToInt(particleCount / 64f), 1, 1);
- particlesBuffer.GetData(particles);
- particlesBuffer.Release();
- matrixBuffer.Release();
- Graphics.Blit(renderTexture, destination);
- }
- void ClearRenderTexture(RenderTexture renderTexture)
- {
- // Save the currently active RenderTexture
- RenderTexture currentActiveRT = RenderTexture.active;
- // Set the target RenderTexture as active
- RenderTexture.active = renderTexture;
- // Clear it with black color
- GL.Clear(true, true, Color.black);
- // Restore the original active RenderTexture
- RenderTexture.active = currentActiveRT;
- }
- float Rand()
- {
- return Random.Range(-1f, 1f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement