// Update is called once per frame void Update() { // Map buffers for reading / writing. particles = Flex.Map(_particleBuffer); velocities = Flex.Map(_velocityBuffer); phases = Flex.Map(_phaseBuffer); // Spawn particles. //spawnParticles(particles, velocities, phases); // Render particles. RenderParticles(particles, velocities, phases); // Unmap buffers. Flex.Unmap(_particleBuffer); Flex.Unmap(_velocityBuffer); Flex.Unmap(_phaseBuffer); // Write to device (async). Flex.SetParticles(_solver, _particleBuffer); Flex.SetVelocities(_solver, _velocityBuffer); Flex.SetPhases(_solver, _phaseBuffer); // Tick. Flex.UpdateSolver(_solver, Time.deltaTime, 1); // Read back (async). Flex.GetParticles(_solver, _particleBuffer); Flex.GetVelocities(_solver, _velocityBuffer); Flex.GetPhases(_solver, _phaseBuffer); }
public void UpdateMesh(FlexContainer.ParticleData _particleData) { transform.rotation = Quaternion.identity; if (m_scene && m_scene.container && m_fluidMaterial) { int[] indices = m_scene.container.fluidIndices; int indexCount = m_scene.container.fluidIndexCount; if (indices.Length <= 0) { return; } if (m_indexBuffer != null && indexCount != m_indexBuffer.count) // @@@ { m_indexBuffer.Release(); m_indexBuffer = null; } if (m_indexBuffer == null && indexCount > 0) { m_indexBuffer = new ComputeBuffer(indexCount, sizeof(int)); m_mesh.SetIndices(new int[indexCount], MeshTopology.Points, 0); } if (m_indexBuffer != null) { m_indexBuffer.SetData(indices); } Vector3 boundsMin = Vector3.one * 1e10f, boundsMax = Vector3.one * 1e10f; if (indexCount > 0) { int maxParticles = m_scene.container.maxParticles; Flex.GetParticles(m_scene.container.solver, m_positionBuffer0); Flex.GetAnisotropy(m_scene.container.solver, m_anisotropy1Buffer0, m_anisotropy2Buffer0, m_anisotropy3Buffer0); CopyBufferContent(m_positionBuffer0, m_positionBuffer); CopyBufferContent(m_anisotropy1Buffer0, m_anisotropy1Buffer); CopyBufferContent(m_anisotropy2Buffer0, m_anisotropy2Buffer); CopyBufferContent(m_anisotropy3Buffer0, m_anisotropy3Buffer); m_fluidMaterial.SetBuffer("_Points", m_positionBuffer); m_fluidMaterial.SetBuffer("_Anisotropy1", m_anisotropy1Buffer); m_fluidMaterial.SetBuffer("_Anisotropy2", m_anisotropy2Buffer); m_fluidMaterial.SetBuffer("_Anisotropy3", m_anisotropy3Buffer); m_fluidMaterial.SetBuffer("_Indices", m_indexBuffer); FlexUtils.ComputeBounds(_particleData.particleData.particles, ref indices[0], indices.Length, ref boundsMin, ref boundsMax); } Vector3 center = (boundsMin + boundsMax) * 0.5f; Vector3 size = boundsMax - boundsMin; m_bounds.center = Vector3.zero; m_bounds.size = size; m_bounds.Expand(m_scene.container.radius); m_mesh.bounds = m_bounds; transform.position = center; } }
public void TestSetParticlesPhases() { float DELTA_T = 0.016f; float INTERACTION_DISTANCE = 0.5f; float SOLID_REST_DISTANCE = 0.2f; Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc desc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref desc); desc.maxParticles = 1000; desc.maxDiffuseParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref desc); Flex.Params prms = new Flex.Params(); Flex.GetParams(solver, ref prms); prms.radius = INTERACTION_DISTANCE; prms.solidRestDistance = SOLID_REST_DISTANCE; Flex.SetParams(solver, ref prms); Flex.Buffer particles = CreateBuffer(lib, 2, 4, new float[] { -0.001f, 0.0f, 0.0f, 1.0f, 0.001f, 0.0f, 0.0f, 1.0f, }); Flex.SetParticles(solver, particles); Flex.Buffer phases = CreateBuffer(lib, 2, 1, new int[] { Flex.MakePhase(1, Flex.Phase.SelfCollide), Flex.MakePhase(1, Flex.Phase.SelfCollide), }); Flex.SetPhases(solver, phases); Flex.SetActiveCount(solver, 2); Flex.Buffer active = CreateBuffer(lib, 2, 1, new int[] { 0, 1, }); Flex.SetActive(solver, active); Flex.UpdateSolver(solver, DELTA_T, 1); Flex.GetParticles(solver, particles); float[] values; ReadBuffer(lib, particles, 2, 4, out values); Assert.AreEqual(SOLID_REST_DISTANCE, Vector3.Distance(new Vector3(values[0], values[1], values[2]), new Vector3(values[4], values[5], values[6]))); Flex.FreeBuffer(particles); Flex.FreeBuffer(phases); Flex.FreeBuffer(active); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestSetSpringConstraints() { float DELTA_T = 0.016f; float INTERACTION_DISTANCE = 0.5f; float SOLID_REST_DISTANCE = 0.2f; float SPRING_LENGTH = 1.0f; Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc desc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref desc); desc.maxParticles = 1000; desc.maxDiffuseParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref desc); Flex.Params prms = new Flex.Params(); Flex.GetParams(solver, ref prms); prms.radius = INTERACTION_DISTANCE; prms.solidRestDistance = SOLID_REST_DISTANCE; Flex.SetParams(solver, ref prms); Flex.Buffer particles = CreateBuffer(lib, 2, 4, new float[] { -0.001f, 0.0f, 0.0f, 1.0f, 0.001f, 0.0f, 0.0f, 1.0f, }); Flex.SetParticles(solver, particles); Flex.Buffer indices = CreateBuffer(lib, 2, 1, new int[] { 0, 1 }); Flex.Buffer lengths = CreateBuffer(lib, 1, 1, new float[] { SPRING_LENGTH }); Flex.Buffer stiffness = CreateBuffer(lib, 1, 1, new float[] { 1.0f }); Flex.SetSprings(solver, indices, lengths, stiffness, 1); Flex.SetActiveCount(solver, 2); Flex.Buffer active = CreateBuffer(lib, 2, 1, new int[] { 0, 1 }); Flex.SetActive(solver, active); Flex.UpdateSolver(solver, DELTA_T, 1); Flex.GetParticles(solver, particles); float[] values; ReadBuffer(lib, particles, 2, 4, out values); Assert.AreEqual(SPRING_LENGTH, Vector3.Distance(new Vector3(values[0], values[1], values[2]), new Vector3(values[4], values[5], values[6])), 0.001f); Flex.FreeBuffer(particles); Flex.FreeBuffer(indices); Flex.FreeBuffer(lengths); Flex.FreeBuffer(stiffness); Flex.FreeBuffer(active); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestParamsPlaneCollision() { float DELTA_T = 0.016f; float INTERACTION_DISTANCE = 0.5f; float SOLID_REST_DISTANCE = 0.2f; Vector3 GRAVITY = new Vector3(0, -9.81f, 0); Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc desc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref desc); desc.maxParticles = 1000; desc.maxDiffuseParticles = 0; Flex.Solver solver = Flex.CreateSolver(lib, ref desc); Flex.Params prms = new Flex.Params(); Flex.GetParams(solver, ref prms); prms.radius = INTERACTION_DISTANCE; prms.solidRestDistance = SOLID_REST_DISTANCE; prms.gravity = GRAVITY; prms.plane0 = new Vector4(0.0f, 1.0f, 0.0f, 0.0f); prms.numPlanes = 1; Flex.SetParams(solver, ref prms); Flex.Buffer particles = CreateBuffer(lib, 1, 4, new float[] { 0.0f, 2.0f, 0.0f, 1.0f }); Flex.SetParticles(solver, particles); Flex.SetActiveCount(solver, 1); Flex.Buffer active = CreateBuffer(lib, 1, 1, new int[] { 0 }); Flex.CopyDesc cpyDsc; cpyDsc.srcOffset = cpyDsc.dstOffset = 0; cpyDsc.elementCount = 1; Flex.SetActive(solver, active, ref cpyDsc); for (int i = 0; i < 100; ++i) { Flex.UpdateSolver(solver, DELTA_T, 1); } Flex.GetParticles(solver, particles); float[] values; ReadBuffer(lib, particles, 1, 4, out values); Assert.AreEqual(0, values[1], 0.001f); Flex.FreeBuffer(active); Flex.FreeBuffer(particles); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
private void PullParticlesFromGPU(IntPtr solverPtr, FlexContainer cnt, Flex.Memory memory) { Flex.GetParticles(m_solverPtr, m_cntr.m_particlesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, memory); Flex.GetVelocities(m_solverPtr, m_cntr.m_velocitiesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, memory); Flex.GetPhases(m_solverPtr, m_cntr.m_phasesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, memory); Flex.GetSmoothParticles(m_solverPtr, m_cntr.m_smoothedParticlesHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, memory); Flex.GetNormals(m_solverPtr, m_cntr.m_normalsHndl.AddrOfPinnedObject(), m_cntr.m_particlesCount, memory); Flex.GetDensities(m_solverPtr, m_cntr.m_densitiesHndl.AddrOfPinnedObject(), memory); Flex.GetBounds(m_solverPtr, ref m_minBounds, ref m_maxBounds, memory); if (m_cntr.m_shapeCoefficients.Length > 0) { Flex.GetRigidTransforms(m_solverPtr, m_cntr.m_shapeRotationHndl.AddrOfPinnedObject(), m_cntr.m_shapeTranslationsHndl.AddrOfPinnedObject(), memory); } }
public void TestSetGetParticles() { Vector3 GRAVITY = new Vector3(1, 2, 3); float DELTA_T = 0.016f; Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc desc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref desc); desc.maxParticles = 1000; desc.maxDiffuseParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref desc); Flex.Params prms = new Flex.Params(); Flex.GetParams(solver, ref prms); prms.gravity = GRAVITY; Flex.SetParams(solver, ref prms); Flex.Buffer particles = CreateBuffer(lib, 1, 4, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, }); Flex.SetParticles(solver, particles); Flex.SetActiveCount(solver, 1); Flex.Buffer active = CreateBuffer(lib, 1, 1, new int[] { 0, }); Flex.SetActive(solver, active); Flex.UpdateSolver(solver, DELTA_T, 1); Flex.GetParticles(solver, particles); float[] values; ReadBuffer(lib, particles, 1, 4, out values); for (int i = 0; i < 3; ++i) { Assert.AreEqual(GRAVITY * DELTA_T * DELTA_T, new Vector3(values[0], values[1], values[2])); } Flex.FreeBuffer(particles); Flex.FreeBuffer(active); Flex.DestroySolver(solver); Flex.Shutdown(lib); }