public override void Init(ComputeBuffer buffer) { if (stateBuffer != null) { Clear(); } GPUParticle[] particles = new GPUParticle[buffer.count]; buffer.GetData(particles); var count = particles.Length; var step = 1f / Mathf.Pow(count, 1f / 3f); stateBuffer = new ComputeBuffer(count, Marshal.SizeOf(typeof(State))); var states = new State[count]; for (int i = 0; i < count; i++) { var p = particles[i]; states[i] = new State(p.pos, p.pos, Random.value, Random.value < ratio ? true : false); } stateBuffer.SetData(states); shader.SetFloat("_Step", step); shader.SetBuffer(0, "_States", stateBuffer); }
protected void Start() { var sideCount = Mathf.FloorToInt(Mathf.Pow(vertexCount, 1f / 3f)); var count = sideCount * sideCount * sideCount; var dsideCount = sideCount * sideCount; particles = new GPUParticle[count]; var scale = (1f / sideCount); var offset = -Vector3.one * 0.5f; for (int x = 0; x < sideCount; x++) { var xoffset = x * dsideCount; for (int y = 0; y < sideCount; y++) { var yoffset = y * sideCount; for (int z = 0; z < sideCount; z++) { var index = xoffset + yoffset + z; var particle = new GPUParticle(Random.Range(0.5f, 1f), new Vector3(x, y, z) * scale + offset, Quaternion.identity, Vector3.one, Vector3.zero, Vector3.zero, Color.white); particles[index] = particle; } } } particleDisplayMat.SetFloat("_Size", scale); // particleDisplayMat.SetColor("_LightColor", Color.white); // particleDisplayMat.SetFloat("_LightIntensity", 1f); mesh = Build(sideCount); }
void Setup(GPUParticleSystem system) { var buffer = system.ParticleBuffer; GPUParticle[] particles = new GPUParticle[buffer.count]; buffer.GetData(particles); var count = particles.Length; bounds = new List <GPUBound>(); Bintree(Vector3.zero, Vector3.one, depth); boundsBuffer = new ComputeBuffer(bounds.Count, Marshal.SizeOf(typeof(GPUBound_t))); boundsBuffer.SetData(bounds.Select(b => b.Structure()).ToArray()); boundsReferencesBuffer = new ComputeBuffer(count, Marshal.SizeOf(typeof(int))); var kernel = shader.FindKernel("Octree"); shader.SetBuffer(kernel, "_Bounds", boundsBuffer); shader.SetInt("_BoundsCount", bounds.Count); shader.SetBuffer(kernel, "_BoundsReferences", boundsReferencesBuffer); Dispatch(kernel, system); shader.SetBuffer(0, "_BoundsReferences", boundsReferencesBuffer); }
void Setup(ComputeBuffer buffer) { GPUParticle[] particles = new GPUParticle[buffer.count]; buffer.GetData(particles); var count = particles.Length; fromBuffer = new ComputeBuffer(count, Marshal.SizeOf(typeof(TRS))); toBuffer = new ComputeBuffer(count, Marshal.SizeOf(typeof(TRS))); var from = new TRS[count]; var to = new TRS[count]; var q = Quaternion.identity; var s = Vector3.one; for (int i = 0; i < count; i++) { var p = particles[i]; from[i] = new TRS(p.pos, p.rot, p.scale); to[i] = new TRS(p.origin, q, s); } fromBuffer.SetData(from); toBuffer.SetData(to); shader.SetBuffer(0, "_From", fromBuffer); shader.SetBuffer(0, "_To", toBuffer); }