void SetComputeData() { int kernelId = _ComputeFlock.FindKernel("CSMain"); int numThreadGroup = BoidsCount / NUM_THREAD_X; _ComputeFlock.SetFloat("_GridH", sorter.GetGridH()); _ComputeFlock.SetVector("_GridDim", gridDim); _ComputeFlock.SetFloat("DeltaTime", Time.deltaTime); _ComputeFlock.SetFloat("RotationSpeed", RotationSpeed); _ComputeFlock.SetFloat("BoidSpeed", BoidSpeed); _ComputeFlock.SetFloat("BoidSpeedVariation", BoidSpeedVariation); _ComputeFlock.SetVector("FlockPosition", Target.transform.position); _ComputeFlock.SetVector("PlaneNormal", _planeNormal); _ComputeFlock.SetVector("PlanePoint", _planePoint); _ComputeFlock.SetVector("SpherePos", Target.transform.position); _ComputeFlock.SetFloat("NeighbourDistance", NeighbourDistance); _ComputeFlock.SetInt("BoidsCount", BoidsCount); _ComputeFlock.SetInt("AffectorCount", AffectorCounts); _ComputeFlock.SetFloat("AffectorForce", AffectorForce); _ComputeFlock.SetFloat("AffectorDistance", AffectorDistance); _ComputeFlock.SetInt("useAffector", useAffector); _ComputeFlock.SetBuffer(kernelId, "_GridIndicesBufferRead", sorter.GetGridIndicesBuffer()); _ComputeFlock.SetBuffer(kernelId, "boidBuffer_read", ParticleBoidBufferRead); _ComputeFlock.SetBuffer(kernelId, "boidBuffer_write", ParticleBoidBufferWrite); _ComputeFlock.SetBuffer(kernelId, "position_debug", ParticleGridPositionCheck); _ComputeFlock.SetBuffer(kernelId, "affector_write", BoidAffectorBuffer); _ComputeFlock.Dispatch(kernelId, numThreadGroup, 1, 1); //ComputeShaderUtil.Swap(ref ParticleBoidBufferWrite, ref ParticleBoidBufferRead); ComputeShaderUtil.Swap(ref ParticleBoidBufferRead, ref ParticleBoidBufferWrite); }
void Simulate() { // set params sphSimulator.SetInt("_NumParticles", numParticles); sphSimulator.SetFloat("_GridH", sorter.GetGridH()); sphSimulator.SetVector("_GridDim", gridDim); sphSimulator.SetFloat("_TimeStep", Mathf.Min(MAX_TIMESTEP, Time.deltaTime)); sphSimulator.SetFloat("_Smoothlen", smoothLen); sphSimulator.SetFloat("_PressureStiffness", pressureStiffness); sphSimulator.SetFloat("_RestDensity", restDensity); sphSimulator.SetFloat("_DensityCoef", particleMass * 315.0f / (64.0f * Mathf.PI * Mathf.Pow(smoothLen, 9.0f))); sphSimulator.SetFloat("_GradPressureCoef", particleMass * -45.0f / (Mathf.PI * Mathf.Pow(smoothLen, 6.0f))); sphSimulator.SetFloat("_LapViscosityCoef", particleMass * viscosity * 45.0f / (Mathf.PI * Mathf.Pow(smoothLen, 6.0f))); sphSimulator.SetFloat("_Restitution", restitution); sphSimulator.SetFloat("_MaxVelocity", maxVelocity); sphSimulator.SetFloat("_Time", Time.time); sphSimulator.SetFloat("_SphereRad", sphere.transform.localScale.x / 2); sphSimulator.SetVector("_SpherePos", sphere.transform.position); int knl = 0; //calc density knl = sphSimulator.FindKernel("SimDensity"); sphSimulator.SetBuffer(knl, "_ParticlesBufferRO", particleRO); sphSimulator.SetBuffer(knl, "_DensityBufferRW", densityRW); sphSimulator.SetBuffer(knl, "_GridIndicesBufferRead", sorter.GetGridIndicesBuffer()); sphSimulator.Dispatch(knl, numParticles / SIM_BLOCK_SIZE, 1, 1); ComputeShaderUtil.Swap(ref densityRO, ref densityRW); //calc force knl = sphSimulator.FindKernel("SimForce"); sphSimulator.SetBuffer(knl, "_ParticlesBufferRO", particleRO); sphSimulator.SetBuffer(knl, "_DensityBufferRO", densityRO); sphSimulator.SetBuffer(knl, "_ForceBufferRW", forceRW); sphSimulator.SetBuffer(knl, "_GridIndicesBufferRead", sorter.GetGridIndicesBuffer()); sphSimulator.Dispatch(knl, numParticles / SIM_BLOCK_SIZE, 1, 1); ComputeShaderUtil.Swap(ref forceRO, ref forceRW); //calc integration knl = sphSimulator.FindKernel("SimIntegrate"); sphSimulator.SetBuffer(knl, "_ParticlesBufferRO", particleRO); sphSimulator.SetBuffer(knl, "_ParticlesBufferRW", particleRW); sphSimulator.SetBuffer(knl, "_ForceBufferRO", forceRO); sphSimulator.SetBuffer(knl, "_GridIndicesBufferRead", sorter.GetGridIndicesBuffer()); sphSimulator.Dispatch(knl, numParticles / SIM_BLOCK_SIZE, 1, 1); ComputeShaderUtil.Swap(ref particleRO, ref particleRW); }
void Update() { // ---- Grid Optimization ------------------------------------------------------------------- gridOptimizer.GridSort(ref particlesBufferRead); // Pass the buffer you want to optimize // ---- Grid Optimization ------------------------------------------------------------------- // ---- Your Particle Process ------------------------------------------------------------------- ParticleCS.SetInt("_NumParticles", numParticles); ParticleCS.SetVector("_GridDim", gridDim); ParticleCS.SetInt("_DispIdx", (int)(dispIdx * numParticles * 0.001f)); ParticleCS.SetFloat("_GridH", gridOptimizer.GetGridH()); int kernel = ParticleCS.FindKernel("Update"); ParticleCS.SetBuffer(kernel, "_ParticlesBufferRead", particlesBufferRead); ParticleCS.SetBuffer(kernel, "_ParticlesBufferWrite", particlesBufferWrite); ParticleCS.SetBuffer(kernel, "_GridIndicesBufferRead", gridOptimizer.GetGridIndicesBuffer()); // Get and use a GridIndicesBuffer to find neighbor ParticleCS.Dispatch(kernel, threadGroupSize, 1, 1); // ---- Your Particle Process ------------------------------------------------------------------- SwapBuffer(ref particlesBufferRead, ref particlesBufferWrite); }