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);
    }
    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);
    }
Beispiel #3
0
    private void OnDestroy()
    {
        Flex.FreeBuffer(_particleBuffer);
        Flex.FreeBuffer(_velocityBuffer);
        Flex.FreeBuffer(_phaseBuffer);

        Flex.DestroySolver(_solver);
        Flex.Shutdown(_library);
    }
Beispiel #4
0
        void OnApplicationQuit()
        {
            foreach (FlexProcessor fp in m_processors)
            {
                fp.FlexClose(this, m_cntr, m_parameters);
            }

            Flex.DestroySolver(m_solverPtr);
            Flex.Shutdown();
        }
Beispiel #5
0
    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);
    }
Beispiel #6
0
    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);
    }
Beispiel #7
0
    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);
    }
Beispiel #8
0
    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);
    }
Beispiel #10
0
        void DestroyContainer()
        {
            if (m_containerHandle)
            {
                FlexExt.DestroyContainer(m_containerHandle);
                m_containerHandle.Clear();
            }

            if (m_solverHandle)
            {
                Flex.DestroySolver(m_solverHandle);
                m_solverHandle.Clear();
            }

            if (m_drawFluidHelper)
            {
                //DestroyImmediate(m_drawFluidHelper.gameObject);
                m_drawFluidHelper.enabled = false;
                m_drawFluidHelper.gameObject.SetActive(false);
                m_destroyObjects.Add(m_drawFluidHelper.gameObject);
                m_drawFluidHelper = null;
            }

            if (m_detectShapesHelper)
            {
                //DestroyImmediate(m_detectShapesHelper.gameObject);
                m_detectShapesHelper.gameObject.SetActive(false);
                m_destroyObjects.Add(m_detectShapesHelper.gameObject);
                m_detectShapesHelper = null;
            }

            if (m_flexScene)
            {
                m_flexScene.fixedUpdate -= FixedUpdate;
                m_flexScene.update      -= Update;
                m_flexScene.onGUI       -= OnGUI;
                m_flexScene.gameObject.SetActive(false);
                m_destroyObjects.Add(m_flexScene.gameObject);
                m_flexScene = null;
            }

            if (m_particleBuffer != null)
            {
                m_particleBuffer.Release();
            }
            m_particleBuffer = null;

            ReleaseLibrary();
        }
Beispiel #11
0
    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 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);
    }
Beispiel #13
0
    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 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);
    }
Beispiel #15
0
    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);
    }
Beispiel #16
0
    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);
    }
Beispiel #17
0
    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);
    }