Esempio n. 1
0
    private IEnumerator Split()
    {
        var id = 0;

        var subTopLeft     = Vector3.zero;
        var subBottomRight = Vector3.zero;
        var subTopRight    = Vector3.zero;
        var subBottomLeft  = Vector3.zero;

        var size = quadCorners.bottomRightCorner - quadCorners.topLeftCorner;
        var step = size / 2.0f;

        bool staticX = false, staticY = false, staticZ = false;

        BrainFuckMath.DefineAxis(ref staticX, ref staticY, ref staticZ, size);

        Planetoid.Working = true;
        Splitting         = true;
        Unsplitted        = false;

        for (byte sY = 0; sY < 2; sY++)
        {
            for (byte sX = 0; sX < 2; sX++, id++)
            {
                if (staticX)
                {
                    subTopLeft     = new Vector3(quadCorners.topLeftCorner.x, quadCorners.topLeftCorner.y + step.y * sY, quadCorners.topLeftCorner.z + step.z * sX);
                    subBottomRight = new Vector3(quadCorners.topLeftCorner.x, quadCorners.topLeftCorner.y + step.y * (sY + 1), quadCorners.topLeftCorner.z + step.z * (sX + 1));

                    subTopRight   = new Vector3(quadCorners.topLeftCorner.x, quadCorners.topLeftCorner.y + step.y * sY, quadCorners.topLeftCorner.z + step.z * (sX + 1));
                    subBottomLeft = new Vector3(quadCorners.topLeftCorner.x, quadCorners.topLeftCorner.y + step.y * (sY + 1), quadCorners.topLeftCorner.z + step.z * sX);
                }
                else if (staticY)
                {
                    subTopLeft     = new Vector3(quadCorners.topLeftCorner.x + step.x * sX, quadCorners.topLeftCorner.y, quadCorners.topLeftCorner.z + step.z * sY);
                    subBottomRight = new Vector3(quadCorners.topLeftCorner.x + step.x * (sX + 1), quadCorners.topLeftCorner.y, quadCorners.topLeftCorner.z + step.z * (sY + 1));

                    subTopRight   = new Vector3(quadCorners.topLeftCorner.x + step.x * (sX + 1), quadCorners.topLeftCorner.y, quadCorners.topLeftCorner.z + step.z * sY);
                    subBottomLeft = new Vector3(quadCorners.topLeftCorner.x + step.x * sX, quadCorners.topLeftCorner.y, quadCorners.topLeftCorner.z + step.z * (sY + 1));
                }
                else if (staticZ)
                {
                    subTopLeft     = new Vector3(quadCorners.topLeftCorner.x + step.x * sX, quadCorners.topLeftCorner.y + step.y * sY, quadCorners.topLeftCorner.z);
                    subBottomRight = new Vector3(quadCorners.topLeftCorner.x + step.x * (sX + 1), quadCorners.topLeftCorner.y + step.y * (sY + 1), quadCorners.topLeftCorner.z);

                    subTopRight   = new Vector3(quadCorners.topLeftCorner.x + step.x * (sX + 1), quadCorners.topLeftCorner.y + step.y * sY, quadCorners.topLeftCorner.z);
                    subBottomLeft = new Vector3(quadCorners.topLeftCorner.x + step.x * sX, quadCorners.topLeftCorner.y + step.y * (sY + 1), quadCorners.topLeftCorner.z);
                }

                var quad = Planetoid.SetupSubQuad(Position);
                quad.Splitting  = true;
                quad.ShouldDraw = false;
                quad.InitCorners(subTopLeft, subBottomRight, subTopRight, subBottomLeft);
                quad.Parent   = this;
                quad.LODLevel = quad.Parent.LODLevel + 1;
                quad.ID       = (QuadID)id;
                quad.SetupVectors(quad, id, staticX, staticY, staticZ);

                if (quad.Parent.transform != null)
                {
                    quad.transform.parent = quad.Parent.transform;
                }

                quad.gameObject.name = string.Format("{0}_ID{1}_LOD{2}", quad.gameObject.name, id, quad.LODLevel);

                Subquads.Add(quad);

                if (Planetoid.WaitOnSplit)
                {
                    for (var wait = 0; wait < Planetoid.DispatchSkipFramesCount; wait++)
                    {
                        yield return(Yielders.EndOfFrame);
                    }
                }
            }
        }

        //Dispatch one by one with intervals.
        for (byte i = 0; i < Subquads.Count; i++)
        {
            Subquads[i].ReadyForDispatch = true;

            for (var wait = 0; wait < Planetoid.DispatchSkipFramesCount; wait++)
            {
                yield return(Yielders.EndOfFrame);
            }
        }

        for (byte i = 0; i < Subquads.Count; i++)
        {
            Subquads[i].Splitting  = false;
            Subquads[i].ShouldDraw = true;
        }

        ShouldDraw = false;
        Splitting  = false;

        Planetoid.Working = false;
    }