public void TestCreateDestroyInstance() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc slvDsc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref slvDsc); slvDsc.maxParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref slvDsc); FlexExt.Container container = FlexExt.CreateContainer(lib, solver, 1000); FlexExt.Asset.Handle asset = CreateTestClothAsset(); FlexExt.ParticleData data = FlexExt.MapParticleData(container); Matrix4x4 transform = Matrix4x4.identity; FlexExt.Instance.Handle instance = FlexExt.CreateInstance(container, ref data, asset, ref transform, 1.0f, 1.0f, 1.0f, Flex.MakePhase(1, Flex.Phase.Default), 1.0f); Assert.AreEqual(asset.asset.numParticles, instance.instance.numParticles); FlexExt.DestroyInstance(container, instance); FlexExt.UnmapParticleData(container); FlexExt.DestroyAsset(asset); FlexExt.DestroyContainer(container); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestCreateDestroyContainerWithDevice() { Flex.InitDesc desc = new Flex.InitDesc { deviceIndex = -1, enableExtensions = false, #if FLEX_CUDA computeType = Flex.ComputeType.CUDA, #else computeType = Flex.ComputeType.D3D11, #endif renderContext = default(IntPtr), renderDevice = default(IntPtr) }; Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback, ref desc); Flex.SolverDesc slvDsc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref slvDsc); slvDsc.maxParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref slvDsc); FlexExt.Container container = FlexExt.CreateContainer(lib, solver, 10000); FlexExt.DestroyContainer(container); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
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); } }
public void TestInitShutdownLibrary() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); int flexVersion = Flex.GetVersion(); Assert.AreEqual(Flex.FLEX_VERSION, flexVersion); Flex.Shutdown(lib); }
public void TestGetLibraryVersion() { Flex.Library lib = Flex.Init(); int flexVersion = Flex.GetVersion(); Assert.AreEqual(Flex.FLEX_VERSION, flexVersion); Flex.Shutdown(lib); }
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); }
// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); Flex.Init(); Distribute.DontCheckForUpdatesInDebug(); LoadApplication(new App()); return(base.FinishedLaunching(app, options)); }
public void TestAllocFreeBuffer() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.Buffer buffer0 = Flex.AllocBuffer(lib, 1000, 4, Flex.BufferType.Host); Flex.Buffer buffer1 = Flex.AllocBuffer(lib, 1000, 4, Flex.BufferType.Device); Flex.FreeBuffer(buffer0); Flex.FreeBuffer(buffer1); 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 TestCreateDestroySolver() { 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.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 TestCreateDestroyContainer() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc slvDsc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref slvDsc); slvDsc.maxParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref slvDsc); FlexExt.Container container = FlexExt.CreateContainer(lib, solver, 10000); FlexExt.DestroyContainer(container); 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); }
static void AcquireLibrary() { if (sm_libraryRefCount++ == 0) { #if FLEX_CUDA Flex.InitDesc desc = new Flex.InitDesc { computeType = Flex.ComputeType.CUDA, enableExtensions = false }; #else Flex.InitDesc desc = new Flex.InitDesc { computeType = Flex.ComputeType.D3D11, enableExtensions = false }; #endif sm_libraryHandle = Flex.Init(Flex.FLEX_VERSION, ErrorCallback, ref desc); } }
public void TestLockUnlockShapeData() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc slvDsc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref slvDsc); slvDsc.maxParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref slvDsc); FlexExt.Container container = FlexExt.CreateContainer(lib, solver, 1000); FlexExt.ShapeData data = FlexExt.MapShapeData(container); Assert.AreNotEqual(IntPtr.Zero, data.positions); FlexExt.UnmapShapeData(container); FlexExt.DestroyContainer(container); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestRegisterSolverCallback() { 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.SolverCallback callback; callback.userData = new IntPtr(1); callback.function = (p) => { Assert.AreEqual(callback.userData, p.userData); }; Flex.SolverCallback oldCallback = Flex.RegisterSolverCallback(solver, callback, Flex.SolverCallbackStage.SubstepBegin); Assert.AreNotEqual(callback.userData, oldCallback.userData); Flex.DestroySolver(solver); Flex.Shutdown(lib); }
public void TestMapUnmapBuffer() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.Buffer buffer = Flex.AllocBuffer(lib, 1000, sizeof(float) * 4, Flex.BufferType.Host); float[] positions = new float[1000 * 4]; for (int i = 0; i < positions.Length; i += 4) { positions[i + 0] = 0.1f * i; positions[i + 1] = positions[i + 3] = 0.0f; positions[i + 3] = 0.1f; } IntPtr ptr = Flex.Map(buffer, 0); Marshal.Copy(positions, 0, ptr, positions.Length); Flex.Unmap(buffer); Flex.FreeBuffer(buffer); Flex.Shutdown(lib); }
public void TestAllocateFreeParticles() { Flex.Library lib = Flex.Init(Flex.FLEX_VERSION, ErrorCallback); Flex.SolverDesc slvDsc = default(Flex.SolverDesc); Flex.SetSolverDescDefaults(ref slvDsc); slvDsc.maxParticles = 1000; Flex.Solver solver = Flex.CreateSolver(lib, ref slvDsc); FlexExt.Container container = FlexExt.CreateContainer(lib, solver, 1000); int n = 1000; int[] indices = new int[n]; int m = FlexExt.AllocParticles(container, n, ref indices[0]); Assert.AreEqual(n, m); FlexExt.FreeParticles(container, n, ref indices[0]); FlexExt.DestroyContainer(container); 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 TestSetGetParams() { 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); Assert.AreNotEqual(GRAVITY, prms.gravity); prms.gravity = GRAVITY; Flex.SetParams(solver, ref prms); Assert.AreEqual(GRAVITY, prms.gravity); 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); }
void Start() { if (m_cntr == null) { m_cntr = GetComponent <FlexContainer>(); } if (m_diffuseParticles == null) { m_diffuseParticles = GetComponent <FlexDiffuseParticles>(); } if (m_parameters == null) { m_parameters = GetComponent <FlexParameters>(); } if (m_colliders == null) { m_colliders = GetComponent <FlexColliders>(); } m_errorCallback = new Flex.ErrorCallback(this.ErrorCallback); m_timers = new Flex.Timers(); Flex.Error flexErr = Flex.Init(100, m_errorCallback, -1); Debug.Log("NVidia FleX v" + Flex.GetVersion()); if (flexErr != Flex.Error.eFlexErrorNone) { Debug.LogError("FlexInit: " + flexErr); } if (m_diffuseParticles) { m_solverPtr = Flex.CreateSolver(m_cntr.m_maxParticlesCount, m_diffuseParticles.m_maxDiffuseParticlesCount, m_maxNeighboursCount); } else { m_solverPtr = Flex.CreateSolver(m_cntr.m_maxParticlesCount, 0, m_maxNeighboursCount); } m_parameters.GetParams(ref m_params); Flex.SetParams(m_solverPtr, ref m_params); m_cntr.UpdateContainer(); m_processors = FindObjectsOfType <FlexProcessor>(); foreach (FlexProcessor fp in m_processors) { fp.FlexStart(this, m_cntr, m_parameters); } PushParticlesToGPU(m_solverPtr, m_cntr, Flex.Memory.eFlexMemoryHost); PushConstraintsToGPU(m_solverPtr, m_cntr, Flex.Memory.eFlexMemoryHost); if (m_colliders) { m_colliders.ProcessColliders(m_solverPtr, Flex.Memory.eFlexMemoryHost); } //Flex.SetShapes(m_solverPtr, m_cnt.shapeGeometry, shapeGeometry.Length, shapeAabbMin, shapeAabbMax, shapeStarts, shapePositions, shapeRotations, // shapePrevPositions, shapePrevRotations, shapeFlags, shapeStarts.Length, Flex.Memory.eFlexMemoryHost); }