//argument buffer for GPU instance drawing //private ComputeBuffer m_argsBuffer; public FluidBoundary(ParticleSource source, float radius, float density, Matrix4x4 RTS) { NumParticles = source.NumParticles; ParticleRadius = radius; DensityCoeff = density; CreateParticles(source, RTS); CreateBoundryPsi(); }
private void CreateParticles(ParticleSource source, Matrix4x4 RTS) { Vector4[] positions = new Vector4[NumParticles]; float inf = float.PositiveInfinity; Vector3 min = new Vector3(inf, inf, inf); Vector3 max = new Vector3(-inf, -inf, -inf); for (int i = 0; i < NumParticles; i++) { Vector4 pos = RTS * source.Positions[i]; positions[i] = pos; if (pos.x < min.x) { min.x = pos.x; } if (pos.y < min.y) { min.y = pos.y; } if (pos.z < min.z) { min.z = pos.z; } if (pos.x > max.x) { max.x = pos.x; } if (pos.y > max.y) { max.y = pos.y; } if (pos.z > max.z) { max.z = pos.z; } } min.x -= ParticleRadius; min.y -= ParticleRadius; min.z -= ParticleRadius; max.x += ParticleRadius; max.y += ParticleRadius; max.z += ParticleRadius; Bounds = new Bounds(); Bounds.SetMinMax(min, max); PositionsBuffer = new ComputeBuffer(NumParticles, 4 * sizeof(float)); PositionsBuffer.SetData(positions); }
public FluidBody(ParticleSource source, float radius, float density, Matrix4x4 RTS, Vector3 initialVel) { NumParticles = source.NumParticles; Density = density; ViscosityCoeff = 0.002f; DampingCoeff = 0.0f; ParticleRadius = radius; ParticleVolume = (4.0f / 3.0f) * Mathf.PI * Mathf.Pow(radius, 3); ParticleMass = ParticleVolume * Density; DensitiesBuffer = new ComputeBuffer(NumParticles, sizeof(float)); PressuresBuffer = new ComputeBuffer(NumParticles, sizeof(float)); CreateParticles(source, RTS, initialVel); }
private void CreateParticles(ParticleSource source, Matrix4x4 RTS, Vector3 initialVel) { Vector4[] positions = new Vector4[NumParticles]; Vector4[] predicted = new Vector4[NumParticles]; Vector4[] velocities = new Vector4[NumParticles]; float inf = float.PositiveInfinity; Vector3 min = new Vector3(inf, inf, inf); Vector3 max = new Vector3(-inf, -inf, -inf); for (int i = 0; i < NumParticles; i++) { Vector4 pos = RTS * source.Positions[i]; positions[i] = pos; predicted[i] = pos; //modified: add initial velocity to the particles //velocities[i] = new Vector3(5.0f, 0.0f, 0.0f); velocities[i] = initialVel; if (pos.x < min.x) { min.x = pos.x; } if (pos.y < min.y) { min.y = pos.y; } if (pos.z < min.z) { min.z = pos.z; } if (pos.x > max.x) { max.x = pos.x; } if (pos.y > max.y) { max.y = pos.y; } if (pos.z > max.z) { max.z = pos.z; } } min.x -= ParticleRadius; min.y -= ParticleRadius; min.z -= ParticleRadius; max.x += ParticleRadius; max.y += ParticleRadius; max.z += ParticleRadius; Bounds = new Bounds(); Bounds.SetMinMax(min, max); PositionsBuffer = new ComputeBuffer(NumParticles, 4 * sizeof(float)); PositionsBuffer.SetData(positions); //Predicted and velocities use a double buffer as solver step //needs to read from many locations of buffer and write the result //in same pass. Could be removed if needed as long as buffer writes //are atomic. Not sure if they are. PredictedBuffer = new ComputeBuffer[2]; PredictedBuffer[0] = new ComputeBuffer(NumParticles, 4 * sizeof(float)); PredictedBuffer[0].SetData(predicted); PredictedBuffer[1] = new ComputeBuffer(NumParticles, 4 * sizeof(float)); PredictedBuffer[1].SetData(predicted); VelocitiesBuffer = new ComputeBuffer[2]; VelocitiesBuffer[0] = new ComputeBuffer(NumParticles, 4 * sizeof(float)); VelocitiesBuffer[0].SetData(velocities); VelocitiesBuffer[1] = new ComputeBuffer(NumParticles, 4 * sizeof(float)); VelocitiesBuffer[1].SetData(velocities); }