コード例 #1
0
 public void Awake()
 {
     this.fParticles = GetComponent <FlexParticles>();
 }
コード例 #2
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);
    }