private void CreateBoundryPsi() { float cellSize = ParticleRadius * 4.0f; SmoothingKernel K = new SmoothingKernel(cellSize); GridHash grid = new GridHash(Bounds, NumParticles, cellSize); grid.Process(Positions); ComputeShader shader = Resources.Load("FluidBoundary") as ComputeShader; int kernel = shader.FindKernel("ComputePsi"); shader.SetFloat("Density", Density); shader.SetFloat("KernelRadiuse", K.Radius); shader.SetFloat("KernelRadius2", K.Radius2); shader.SetFloat("Poly6", K.POLY6); shader.SetFloat("Poly6Zero", K.Poly6(Vector3.zero)); shader.SetInt("NumParticles", NumParticles); shader.SetFloat("HashScale", grid.InvCellSize); shader.SetVector("HashSize", grid.Bounds.size); shader.SetVector("HashTranslate", grid.Bounds.min); shader.SetBuffer(kernel, "IndexMap", grid.IndexMap); shader.SetBuffer(kernel, "Table", grid.Table); shader.SetBuffer(kernel, "Boundary", Positions); int groups = NumParticles / THREAD_1D; if (NumParticles % THREAD_1D != 0) { groups++; } //Fills the boundarys psi array so the fluid can //collide against it smoothly. The original computes //the phi for each boundary particle based on the //density of the boundary but I find the fluid //leaks out so Im just using a const value. shader.Dispatch(kernel, groups, 1, 1); grid.Dispose(); }
/// <summary> /// Fills the Volume texture with the particles densities. /// That texture can then be used to render the fluid by /// ray tracing in the meshes shader. /// </summary> public void FillVolume(FluidBody body, GridHash grid, SmoothingKernel kernel) { int computeKernel = m_shader.FindKernel("ComputeVolume"); m_shader.SetFloat("VolumeScale", PixelSize); m_shader.SetVector("VolumeSize", Bounds.size); m_shader.SetVector("VolumeTranslate", Bounds.min); m_shader.SetFloat("HashScale", grid.InvCellSize); m_shader.SetVector("HashSize", grid.Bounds.size); m_shader.SetVector("HashTranslate", grid.Bounds.min); m_shader.SetFloat("KernelRadius", kernel.Radius); m_shader.SetFloat("KernelRadius2", kernel.Radius2); m_shader.SetFloat("Poly6", kernel.POLY6); m_shader.SetFloat("Density", body.Density); m_shader.SetInt("NumParticles", body.NumParticles); m_shader.SetFloat("ParticleVolume", body.ParticleVolume); m_shader.SetBuffer(computeKernel, "IndexMap", grid.IndexMap); m_shader.SetBuffer(computeKernel, "Table", grid.Table); m_shader.SetBuffer(computeKernel, "Positions", body.Positions); m_shader.SetBuffer(computeKernel, "Densities", body.Densities); m_shader.SetTexture(computeKernel, "Volume", Volume); m_shader.Dispatch(computeKernel, Groups.x, Groups.y, Groups.z); }
public FluidSolver(FluidBody body, FluidBoundary boundary) { SolverIterations = 2; ConstraintIterations = 2; Body = body; Boundary = boundary; float cellSize = Body.ParticleRadius * 4.0f; int total = Body.NumParticles + Boundary.NumParticles; Hash = new GridHash(Boundary.Bounds, total, cellSize); Kernel = new SmoothingKernel(cellSize); int numParticles = Body.NumParticles; Groups = numParticles / THREAD_1D; if (numParticles % THREAD_1D != 0) { Groups++; } m_shader = Resources.Load("FluidSolver") as ComputeShader; }