private void Awake() { _library = Flex.Init(); Flex.SolverDesc _solverDesc = new Flex.SolverDesc(); Flex.SetSolverDescDefaults(ref _solverDesc); _solver = Flex.CreateSolver(_library, ref _solverDesc); _particleBuffer = Flex.AllocBuffer(_library, 28, sizeof(float) * 4, Flex.BufferType.Host); _velocityBuffer = Flex.AllocBuffer(_library, 28, sizeof(float) * 4, Flex.BufferType.Host); _phaseBuffer = Flex.AllocBuffer(_library, 28, sizeof(int), Flex.BufferType.Host); _activeBuffer = Flex.AllocBuffer(_library, 28, sizeof(int), Flex.BufferType.Host); unsafe { int *activeIndices = (int *)Flex.Map(_activeBuffer); for (int i = 0; i < 28; ++i) { activeIndices[i] = i; } Flex.Unmap(_activeBuffer); Flex.SetActive(_solver, _activeBuffer); Flex.SetActiveCount(_solver, 28); } }
private void PushParticlesToGPU(IntPtr solverPtr, FlexContainer cnt, Flex.Memory memory) { Flex.SetActive(solverPtr, cnt.m_activeSetHndl.AddrOfPinnedObject(), cnt.m_activeParticlesCount, memory); Flex.SetParticles(solverPtr, cnt.m_particlesHndl.AddrOfPinnedObject(), cnt.m_particlesCount, memory); Flex.SetRestParticles(solverPtr, cnt.m_restParticlesHndl.AddrOfPinnedObject(), cnt.m_particlesCount, memory); Flex.SetVelocities(solverPtr, cnt.m_velocitiesHndl.AddrOfPinnedObject(), cnt.m_particlesCount, memory); Flex.SetNormals(solverPtr, cnt.m_normalsHndl.AddrOfPinnedObject(), cnt.m_particlesCount, memory); Flex.SetPhases(solverPtr, cnt.m_phases, cnt.m_particlesCount, memory); }
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 TestSetGetParticlesVelocities() { 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.Buffer velocities = CreateBuffer(lib, 1, 3, new float[] { 0.0f, 0.0f, 0.0f, }); Flex.SetVelocities(solver, velocities); 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.GetVelocities(solver, velocities); float[] values; ReadBuffer(lib, velocities, 1, 3, out values); Assert.AreEqual(GRAVITY * DELTA_T, new Vector3(values[0], values[1], values[2])); Flex.FreeBuffer(particles); Flex.FreeBuffer(velocities); 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); }
public void TestSetGetActive() { 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.SetActiveCount(solver, 2); Flex.Buffer active = CreateBuffer(lib, 2, 1, new int[] { 2, 1 }); Flex.SetActive(solver, active); Assert.AreEqual(2, Flex.GetActiveCount(solver)); Flex.FreeBuffer(active); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestSetRigidsGetTransforms() { float DELTA_T = 0.016f; float INTERACTION_DISTANCE = 0.5f; float SOLID_REST_DISTANCE = 0.2f; Vector3 GRAVITY = new Vector3(1, 2, 3); 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; prms.gravity = GRAVITY; Flex.SetParams(solver, ref prms); Flex.Buffer particles = CreateBuffer(lib, 8, 4, new float[] { 0.1f, 0.1f, 0.1f, 1.0f, -0.1f, 0.1f, 0.1f, 1.0f, -0.1f, -0.1f, 0.1f, 1.0f, 0.1f, -0.1f, 0.1f, 1.0f, 0.1f, 0.1f, -0.1f, 1.0f, -0.1f, 0.1f, -0.1f, 1.0f, -0.1f, -0.1f, -0.1f, 1.0f, 0.1f, -0.1f, -0.1f, 1.0f, }); Flex.SetParticles(solver, particles); Flex.Buffer active = CreateBuffer(lib, 8, 1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Flex.SetActive(solver, active); Flex.Buffer offsets = CreateBuffer(lib, 2, 1, new int[] { 0, 8 }); Flex.Buffer indices = CreateBuffer(lib, 8, 1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Flex.Buffer positions = CreateBuffer(lib, 8, 3, new float[] { 0.1f, 0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f, }); const float N = 0.57735026918962576450914878050196f; // 1 / sqrt(3) Flex.Buffer normals = CreateBuffer(lib, 8, 4, new float[] { N, N, N, 0, -N, N, N, 0, -N, -N, N, 0, N, -N, N, 0, N, N, -N, 0, -N, N, -N, 0, -N, -N, -N, 0, N, -N, -N, 0, }); Flex.Buffer stiffness = CreateBuffer(lib, 1, 1, new float[] { 1.0f }); Flex.Buffer thresholds = CreateBuffer(lib, 1, 1, new float[] { 1.0f }); Flex.Buffer creeps = CreateBuffer(lib, 1, 1, new float[] { 1.0f }); Flex.Buffer rotations = CreateBuffer(lib, 1, 4, new float[] { 0.0f, 0.0f, 0.0f, 1.0f }); Flex.Buffer translations = CreateBuffer(lib, 1, 3, new float[] { 0.0f, 0.0f, 0.0f }); Flex.SetRigids(solver, offsets, indices, positions, normals, stiffness, thresholds, creeps, rotations, translations, 1, 8); Flex.UpdateSolver(solver, DELTA_T, 1); Flex.Buffer nullBuffer = default(Flex.Buffer); Flex.GetRigids(solver, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, rotations, translations); float[] values; ReadBuffer(lib, translations, 1, 3, out values); Vector3 expectedPosition = GRAVITY * DELTA_T * DELTA_T; Vector3 currentPosition = new Vector3(values[0], values[1], values[2]); Assert.AreEqual(expectedPosition.magnitude, currentPosition.magnitude, 0.001f); Flex.FreeBuffer(translations); Flex.FreeBuffer(rotations); Flex.FreeBuffer(creeps); Flex.FreeBuffer(thresholds); Flex.FreeBuffer(stiffness); Flex.FreeBuffer(normals); Flex.FreeBuffer(positions); Flex.FreeBuffer(indices); Flex.FreeBuffer(offsets); Flex.FreeBuffer(active); Flex.FreeBuffer(particles); Flex.DestroySolver(solver); Flex.Shutdown(lib); }