private List <Vector3> GetFlexShapeMatchingPositions()
    {
        List <Vector3> vertPos = new List <Vector3>();
        //FlexShapeMatching[] shapesGOs = FindObjectsOfType<FlexShapeMatching>();

        FlexShapeMatching shapes = this.flexShapeMatching;
        int shapeIndex           = 0;
        int shapeIndexOffset     = shapes.m_shapesIndex;
        int shapeStart           = 0;

        for (int s = 0; s < shapes.m_shapesCount; s++)
        {
            shapeIndex++;
            int shapeEnd = shapes.m_shapeOffsets[s];
            for (int i = shapeStart; i < shapeEnd; ++i)
            {
                Vector3 pos = flexShapeMatching.m_shapeRestPositions[i] + shapes.m_shapeCenters[s];
                vertPos.Add(pos);
                shapeIndexOffset++;
            }

            shapeStart = shapeEnd;
        }

        return(vertPos);
    }
 void OnEnable()
 {
     if (flexShapeMatching == null)
     {
         flexShapeMatching = shapeMatchingGO.GetComponent <FlexShapeMatching>();
     }
     //print(particlePositions.Length);
     //print("shared mesh vertices length" + skinnedMeshRenderer.sharedMesh.vertices.Length);
 }
    private void MatchShapes()
    {
        FlexShapeMatching shapes = this.flexShapeMatching;
        //FlexShapeMatching[] shapesGOs = FindObjectsOfType<FlexShapeMatching>();
        //FlexShapeMatching[] shapes_Array = new FlexShapeMatching[m_flexGameObjects.Count];

        //Debug.Log("shape name: " + shapes.name + " shapes count: " + shapes.m_shapesCount + " shape's Index: " + shapes.m_shapesIndex);
        //for (int i = 0; i < shapesGOs.Length; i++) { Debug.Log("shape game object name: " + shapesGOs[i].name); }

        //Debug.Log( "Number of shapes game objects: " + shapesGOs.Length);
        int shapeIndex       = 0;
        int shapeIndexOffset = 0; //no need for offsets, shapes.m_shapesIndex;
        ////Debug.Log("before for loop: " + shapes.m_shapesIndex);
        int shapeStart = 0;
        ////Debug.Log("shape name: " + shapes.name + " shape's index: " + shapes.m_shapesIndex);
        int vertIndex = 0;


        for (int s = 0; s < shapes.m_shapesCount; s++)
        {
            Vector3 shapeCenter = new Vector3();
            shapeIndex++;
            //Debug.Log("count: " + s + "shape index count : " + shapeIndexOffset);
            //Debug.Log("shape name: " + shapes.name + " shape offsets: " + shapes.m_shapeOffsets[s]);
            int shapeEnd = shapes.m_shapeOffsets[s];
            //Debug.Log(s + " shape end: "+ shapeEnd);

            //the number of constraints in each shape
            int shapeCount = shapeEnd - shapeStart;
            //Debug.Log(s + " shape count: " + shapeCount);
            int origShapeIndexOffset = shapeIndexOffset;
            for (int i = shapeStart; i < shapeEnd; ++i)
            {
                //print("vert index:" + vertIndex);
                int mappedIndex = vertMapAsset.vertexParticleMap[vertIndex];
                //print("mapped index:" + mappedIndex);
                Vector3 pos = particlePositions[mappedIndex];
                //Debug.DrawLine(particlePositions[mappedIndex], vertMapAsset.particleRestPositions[mappedIndex], Color.green);

                //Vector3 pos = particlePositions[i];
                //print("shape matching constraint index offset:" + shapeIndexOffset);
                //print(i + " particle position: " + pos);
                shapes.m_shapeRestPositions[shapeIndexOffset] = pos;
                //Debug.Log("vert index:" + vertIndex+ " mapped index: " + mappedIndex + " shape rest position while it is being matched:" + pos);
                //Debug.DrawLine(pos, shapeCenter, Color.green);
                shapeCenter += pos;
                //Debug.DrawLine(pos, shapeCenter, Color.green);
                //Debug.Log("shape Index: " + shapeIndex);
                //Debug.Log("shape Index offset: " + shapeIndexOffset);
                //Debug.Log("shape center postion: " + shapeCenter);
                shapeIndexOffset++;
                vertIndex++;
                //Debug.Log("vertex index: " + vertIndex);
            }

            shapeCenter /= shapeCount;

            for (int i = shapeStart; i < shapeEnd; ++i)
            {
                //print(i);
                Vector3 pos = shapes.m_shapeRestPositions[origShapeIndexOffset];
                pos -= shapeCenter;
                shapes.m_shapeRestPositions[origShapeIndexOffset] = pos;
                origShapeIndexOffset++;
            }
            //print("orig shape index offset:" + origShapeIndexOffset);
            shapeStart = shapeEnd;
        }
    }
 public VertMapAssetBuilder(FlexShapeMatching flexShapeMatching, VertMapAsset vertMapAsset, SkinnedMeshRenderer skin)
 {
     this.flexShapeMatching = flexShapeMatching;
     this.vertMapAsset      = vertMapAsset;
     this.skin = skin;
 }
Esempio n. 5
0
    public static bool CutFlexSoft(Transform target, Vector3 blade1, Vector3 blade2, Vector3 blade3, Vector3 blade4)
    {
        FlexShapeMatching shapes    = target.GetComponent <FlexShapeMatching>();
        FlexParticles     particles = target.GetComponent <FlexParticles>();

        List <int> indicies = new List <int>();
        List <int> offsets  = new List <int>();
        //Debug.Log("Blade: " + blade1.ToString("F4") + blade2.ToString("F4") + blade3.ToString("F4") + blade4.ToString("F4") );
        List <int> otherIndices = new List <int>();
        var        centers      = Enumerable.Range(0, shapes.m_shapesCount).Select(i => shape_to_world(i, shapes)).ToList();
        int        indexBeg     = 0;
        int        indexEnd     = 0;

        //var plane = new Plane(blade1, blade2, blade3);
        for (int i = 0; i < shapes.m_shapesCount; ++i)
        {
            indexEnd = shapes.m_shapeOffsets[i];

            Vector3 shapeCenter = shapes.m_shapeCenters[i];
            for (int j = indexBeg; j < indexEnd; ++j)
            {
                int     id          = shapes.m_shapeIndices[j];
                Vector3 particlePos = particles.m_particles[id].pos;
                if (intersects_quad(particlePos, centers[i], blade1, blade2, blade3, blade4))
                {
                    if (!otherIndices.Contains(id))
                    {
                        otherIndices.Add(id);
                    }
                }
                else
                {
                    indicies.Add(id);
                }
            }
            offsets.Add(indicies.Count);
            indexBeg = indexEnd;
        }
        if (otherIndices.Count == 0)
        {
            return(false);
        }
        for (int i = 0; i < otherIndices.Count; i++)
        {
            if (!indicies.Contains(otherIndices[i]))
            {
                int index   = FindClosestBoneIndexOnSameSide(centers.ToArray(), particles.m_particles[otherIndices[i]].pos, blade1, blade2, blade3, blade4);
                int atIndex = offsets[index];
                indicies.Insert(atIndex, otherIndices[i]);
                for (int j = index; j < offsets.Count; j++)
                {
                    offsets[j] += 1;
                }
            }
        }
        if (indicies.Count - shapes.m_shapeIndicesCount != 0)
        {
            Debug.LogFormat("Shape counts difference: {0}",
                            indicies.Count - shapes.m_shapeIndicesCount);
            Debug.LogFormat("Shape count: {0}", offsets.Count);
        }
        shapes.m_shapeIndicesCount = indicies.Count;
        shapes.m_shapeIndices      = indicies.ToArray();
        shapes.m_shapeOffsets      = offsets.ToArray();

        int shapeStart     = 0;
        int shapeIndex     = 0;
        var rest_positions = new Vector3[indicies.Count];

        for (int s = 0; s < shapes.m_shapesCount; s++)
        {
            shapes.m_shapeTranslations[s] = new Vector3();
            shapes.m_shapeRotations[s]    = Quaternion.identity;

            shapeIndex++;

            int shapeEnd = shapes.m_shapeOffsets[s];
            //-----------------------------------------cccc------------------------------------------------//

            Vector3 cen = Vector3.zero;
            for (int i = shapeStart; i < shapeEnd; ++i)
            {
                int     p   = shapes.m_shapeIndices[i];
                Vector3 pos = particles.m_restParticles[p].pos;
                cen += pos;
            }
            cen /= (shapeEnd - shapeStart);
            shapes.m_shapeCenters[s] = cen;

            //--------------------------------------------cccc---------------------------------------------//
            for (int i = shapeStart; i < shapeEnd; ++i)
            {
                int p = shapes.m_shapeIndices[i];

                // remap indices and create local space positions for each shape
                Vector3 pos = particles.m_restParticles[p].pos;
                rest_positions[i] = pos - shapes.m_shapeCenters[s];
            }

            shapeStart = shapeEnd;
        }
        shapes.m_shapeRestPositions = rest_positions;

        return(true);
    }
Esempio n. 6
0
 public static void world_to_shape(Vector3 pt, int i, FlexShapeMatching fsm)
 {
     fsm.m_shapeCenters[i] = Quaternion.Inverse(fsm.m_shapeRotations[i]) * (pt - fsm.m_shapeTranslations[i]) * 100.0f;
 }
Esempio n. 7
0
    public static Vector3 shape_to_world(int i, FlexShapeMatching fsm)
    {
        var rotated_center = fsm.m_shapeRotations[i] * fsm.m_shapeCenters[i];

        return(rotated_center / 100.0f + fsm.m_shapeTranslations[i]);
    }