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 TestTearClothMesh()
    {
        Vector4[] particles = { new Vector4(1,  1, 0), new Vector4(0,  1, 0), new Vector4(-1,  1, 0),
                                new Vector4(1,  0, 0), new Vector4(0,  0, 0), new Vector4(-1,  0, 0),
                                new Vector4(1, -1, 0), new Vector4(0, -1, 0), new Vector4(-1, -1, 0) };
        int[]     indices = { 0, 1, 4, 0, 4, 3, 1, 2, 5, 1, 5, 4,
                              3,     4, 7, 3, 7, 6, 4, 5, 8, 4, 8, 7 };
        float     stretchStiffness = 1.0f;
        float     bendStiffness    = 1.0f;
        float     pressure         = 0.0f;

        FlexExt.Asset.Handle handle = FlexExt.CreateTearingClothFromMesh(ref particles[0], particles.Length, particles.Length * 2, ref indices[0], indices.Length / 3, stretchStiffness, bendStiffness, pressure);

        float maxStrain = 0.0f;
        int   maxSplits = 10;

        FlexExt.TearingParticleClone[] particlesCopies = new FlexExt.TearingParticleClone[10];
        int numParticlesCopies = 0;
        int maxCopies          = 10;

        FlexExt.TearingMeshEdit[] triangleEdits = new FlexExt.TearingMeshEdit[10];
        int numTriangleEdits = 0;
        int maxEdits         = 10;

        FlexExt.TearClothMesh(handle, maxStrain, maxSplits, ref particlesCopies[0], ref numParticlesCopies, maxCopies, ref triangleEdits[0], ref numTriangleEdits, maxEdits);
        // todo: make the mesh tear

        FlexExt.DestroyTearingCloth(handle);
    }
        void BuildFromMesh()
        {
            if (m_boundaryMesh)
            {
                Vector3[] vertices = m_boundaryMesh.vertices;
                if (vertices != null && vertices.Length > 0)
                {
                    for (int i = 0; i < vertices.Length; ++i)
                    {
                        Vector3 v = vertices[i];
                        vertices[i] = new Vector3(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z);
                    }
                    int[] indices = m_boundaryMesh.triangles;
                    if (indices != null && indices.Length > 0)
                    {
                        FlexExt.Asset.Handle assetHandle = FlexExt.CreateRigidFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, m_particleSpacing, m_meshExpansion);
                        if (assetHandle)
                        {
                            FlexExt.Asset asset         = assetHandle.asset;
                            FlexExt.Asset particlesOnly = new FlexExt.Asset();
                            particlesOnly.numParticles = asset.numParticles;
                            particlesOnly.maxParticles = asset.numParticles;
                            particlesOnly.particles    = asset.particles;
                            StoreAsset(particlesOnly);

                            FlexExt.DestroyAsset(assetHandle);
                        }
                    }
                }
            }
        }
示例#4
0
        public FlexExt.Instance.Handle CreateInstance(FlexExt.Asset.Handle _assetHandle, Matrix4x4 _location, Vector3 _velocity, int _phase, float _massScale)
        {
            FlexExt.ParticleData particleData = FlexExt.MapParticleData(m_containerHandle);

            FlexExt.Instance.Handle instanceHandle = FlexExt.CreateInstance(m_containerHandle, ref particleData, _assetHandle, ref _location, _velocity.x, _velocity.y, _velocity.z, _phase, 1.0f / _massScale);
            UpdateBuffer(particleData);

            FlexExt.UnmapParticleData(m_containerHandle);

            return(instanceHandle);
        }
示例#5
0
        void BuildFromMesh()
        {
            if (m_boundaryMesh)
            {
                Vector3[] vertices = m_boundaryMesh.vertices;
                if (vertices != null && vertices.Length > 0)
                {
                    for (int i = 0; i < vertices.Length; ++i)
                    {
                        Vector3 v = vertices[i];
                        vertices[i] = new Vector3(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z);
                    }
                    int[] indices = m_boundaryMesh.triangles;
                    if (indices != null && indices.Length > 0)
                    {
                        FlexExt.Asset.Handle assetHandle = FlexExt.CreateSoftFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, m_particleSpacing, m_volumeSampling, m_surfaceSampling, m_clusterSpacing, m_clusterRadius, m_clusterStiffness, m_linkRadius, m_linkStiffness);
                        if (assetHandle)
                        {
                            StoreAsset(assetHandle.asset);

                            FlexExt.DestroyAsset(assetHandle);

                            foreach (var i in fixedParticles)
                            {
                                if (i < particles.Length)
                                {
                                    particles[i].w = 0.0f;
                                }
                            }

                            m_referenceShape = -1;
                            if (fixedParticles.Length == 0)
                            {
                                float   minDist2 = float.MaxValue;
                                Vector3 center   = m_boundaryMesh.bounds.center;
                                //Debug.Log(" center of asset bounds: " + center);
                                for (int i = 0; i < shapeCenters.Length; ++i)
                                {
                                    float dist2 = (shapeCenters[i] - center).sqrMagnitude;
                                    if (dist2 < minDist2)
                                    {
                                        minDist2         = dist2;
                                        m_referenceShape = i;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    public void TestCreateRigidFromMesh()
    {
        Vector3[] vertices = { new Vector3(1, 1, 1), new Vector3(-1, -1, 1), new Vector3(-1, 1, -1), new Vector3(1, -1, -1) };
        int[]     indices  = { 0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 3, 2 };
        float     radius   = 0.1f;
        float     expand   = 0.0f;

        FlexExt.Asset.Handle handle = FlexExt.CreateRigidFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, radius, expand);
        FlexExt.Asset        asset  = handle.asset;

        Assert.AreEqual(2680, asset.numParticles);
        Assert.AreEqual(1, asset.numShapes);

        FlexExt.DestroyAsset(handle);
    }
    public void TestCreateTearingClothFromMesh()
    {
        Vector4[] particles = { new Vector4(1,  1, 0, 1), new Vector4(0,  1, 0, 1), new Vector4(-1,  1, 0, 1),
                                new Vector4(1,  0, 0, 1), new Vector4(0,  0, 0, 1), new Vector4(-1,  0, 0, 1),
                                new Vector4(1, -1, 0, 1), new Vector4(0, -1, 0, 1), new Vector4(-1, -1, 0, 1) };
        int[]     indices = { 0, 1, 4, 0, 4, 3, 1, 2, 5, 1, 5, 4,
                              3,     4, 7, 3, 7, 6, 4, 5, 8, 4, 8, 7 };
        float     stretchStiffness = 1.0f;
        float     bendStiffness    = 1.0f;
        float     pressure         = 0.0f;

        FlexExt.Asset.Handle handle = FlexExt.CreateTearingClothFromMesh(ref particles[0], particles.Length, particles.Length * 2, ref indices[0], indices.Length / 3, stretchStiffness, bendStiffness, pressure);
        FlexExt.Asset        asset  = handle.asset;

        Assert.AreEqual(particles.Length, asset.numParticles);
        Assert.AreEqual(indices.Length / 3, asset.numTriangles);

        FlexExt.DestroyTearingCloth(handle);
    }
示例#8
0
 void BuildFromMesh()
 {
     if (m_referenceMesh)
     {
         Vector3[] vertices  = m_referenceMesh.vertices;
         int[]     triangles = m_referenceMesh.triangles;
         if (vertices != null && vertices.Length > 0 && triangles != null && triangles.Length > 0)
         {
             Tesselation(ref vertices, ref triangles, m_meshTesselation);
             int[]     uniqueVerts         = new int[vertices.Length];
             int[]     originalToUniqueMap = new int[vertices.Length];
             int       particleCount       = FlexExt.CreateWeldedMeshIndices(ref vertices[0], vertices.Length, ref uniqueVerts[0], ref originalToUniqueMap[0], m_weldingThreshold);
             Vector4[] particles           = new Vector4[particleCount];
             for (int i = 0; i < particleCount; ++i)
             {
                 Vector3 v = vertices[uniqueVerts[i]];
                 particles[i] = new Vector4(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z, 1.0f);
             }
             foreach (var i in fixedParticles)
             {
                 if (i < particleCount)
                 {
                     particles[i].w = 0.0f;
                 }
             }
             int   indexCount = triangles.Length;
             int[] indices    = new int[indexCount];
             for (int i = 0; i < indexCount; ++i)
             {
                 indices[i] = originalToUniqueMap[triangles[i]];
             }
             FlexExt.Asset.Handle assetHandle = FlexExt.CreateClothFromMesh(ref particles[0], particles.Length, ref indices[0], indices.Length / 3, m_stretchStiffness, m_bendStiffness, m_tetherStiffness, m_tetherGive, m_pressure);
             if (assetHandle)
             {
                 StoreAsset(assetHandle.asset);
                 FlexExt.DestroyAsset(assetHandle);
             }
         }
     }
 }
    public void TestCreateSoftFromMesh()
    {
        Vector3[] vertices         = { new Vector3(1, 1, 1), new Vector3(-1, -1, 1), new Vector3(-1, 1, -1), new Vector3(1, -1, -1) };
        int[]     indices          = { 0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 3, 2 };
        float     particleSpacing  = 0.1f;
        float     volumeSampling   = 0.1f;
        float     surfaceSampling  = 0.1f;
        float     clusterSpacing   = 0.1f;
        float     clusterRadius    = 0.3f;
        float     clusterStiffness = 0.5f;
        float     linkRadius       = 0.2f;
        float     linkStiffness    = 0.5f;

        FlexExt.Asset.Handle handle = FlexExt.CreateSoftFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, particleSpacing, volumeSampling, surfaceSampling, clusterSpacing, clusterRadius, clusterStiffness, linkRadius, linkStiffness);
        FlexExt.Asset        asset  = handle.asset;

        Assert.AreEqual(668, asset.numParticles);
        Assert.AreEqual(502, asset.numShapes);
        Assert.AreEqual(3856, asset.numSprings);

        FlexExt.DestroyAsset(handle);
    }
 void BuildFromMesh()
 {
     if (m_referenceMesh)
     {
         Vector3[] vertices  = m_referenceMesh.vertices;
         int[]     triangles = m_referenceMesh.triangles;
         if (vertices != null && vertices.Length > 0 && triangles != null && triangles.Length > 0)
         {
             for (int i = 0; i < m_meshTesselation; ++i)
             {
                 Vector3[] newVertices  = new Vector3[triangles.Length * 4];
                 int[]     newTriangles = new int[triangles.Length * 4];
                 for (int j = 0; j < triangles.Length / 3; ++j)
                 {
                     Vector3 v0 = vertices[triangles[j * 3 + 0]], v1 = vertices[triangles[j * 3 + 1]], v2 = vertices[triangles[j * 3 + 2]];
                     Vector3 v3 = (v0 + v1) * 0.5f, v4 = (v1 + v2) * 0.5f, v5 = (v2 + v0) * 0.5f;
                     int     i0 = j * 12 + 0, i1 = j * 12 + 1, i2 = j * 12 + 2;
                     int     i3 = j * 12 + 3, i4 = j * 12 + 4, i5 = j * 12 + 5;
                     int     i6 = j * 12 + 6, i7 = j * 12 + 7, i8 = j * 12 + 8;
                     int     i9 = j * 12 + 9, iA = j * 12 + 10, iB = j * 12 + 11;
                     newTriangles[i0] = i0; newTriangles[i1] = i1; newTriangles[i2] = i2;
                     newTriangles[i3] = i3; newTriangles[i4] = i4; newTriangles[i5] = i5;
                     newTriangles[i6] = i6; newTriangles[i7] = i7; newTriangles[i8] = i8;
                     newTriangles[i9] = i9; newTriangles[iA] = iA; newTriangles[iB] = iB;
                     newVertices[i0]  = v0; newVertices[i1] = v3; newVertices[i2] = v5;
                     newVertices[i3]  = v3; newVertices[i4] = v1; newVertices[i5] = v4;
                     newVertices[i6]  = v5; newVertices[i7] = v4; newVertices[i8] = v2;
                     newVertices[i9]  = v3; newVertices[iA] = v4; newVertices[iB] = v5;
                 }
                 vertices  = newVertices;
                 triangles = newTriangles;
             }
             int[]     uniqueVerts         = new int[vertices.Length];
             int[]     originalToUniqueMap = new int[vertices.Length];
             int       particleCount       = FlexExt.CreateWeldedMeshIndices(ref vertices[0], vertices.Length, ref uniqueVerts[0], ref originalToUniqueMap[0], m_weldingThreshold);
             Vector4[] particles           = new Vector4[particleCount];
             for (int i = 0; i < particleCount; ++i)
             {
                 Vector3 v = vertices[uniqueVerts[i]];
                 particles[i] = new Vector4(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z, 1.0f);
             }
             foreach (var i in fixedParticles)
             {
                 if (i < particleCount)
                 {
                     particles[i].w = 0.0f;
                 }
             }
             int   indexCount = triangles.Length;
             int[] indices    = new int[indexCount];
             for (int i = 0; i < indexCount; ++i)
             {
                 indices[i] = originalToUniqueMap[triangles[i]];
             }
             FlexExt.Asset.Handle assetHandle = FlexExt.CreateClothFromMesh(ref particles[0], particles.Length, ref indices[0], indices.Length / 3, m_stretchStiffness, m_bendStiffness, m_tetherStiffness, m_tetherGive, m_pressure);
             if (assetHandle)
             {
                 StoreAsset(assetHandle.asset);
                 FlexExt.DestroyAsset(assetHandle);
             }
         }
     }
 }