int CalculateMaxArrayCount(NativeArray <ShapeControlPoint> shapePoints)
        {
            bool  hasSprites    = false;
            float smallestWidth = 99999.0f;

            if (null != spriteArray)
            {
                foreach (var sprite in m_SpriteArray)
                {
                    if (sprite != null)
                    {
                        hasSprites = true;
                        float pixelWidth = BezierUtility.GetSpritePixelWidth(sprite);
                        smallestWidth = (smallestWidth > pixelWidth) ? pixelWidth : smallestWidth;
                    }
                }
            }

            // Approximate vertex Array Count.
            float shapeLength = BezierUtility.BezierLength(shapePoints, splineDetail * splineDetail) * 2.0f;
            int   adjustWidth = hasSprites ? ((int)(shapeLength / smallestWidth) * 6) + (shapePoints.Length * 6 * splineDetail) : 0;
            int   adjustShape = shapePoints.Length * 4 * splineDetail;

            adjustShape = optimizeGeometry ? (adjustShape) : (adjustShape * 2);
            var validFT = ValidateSpriteShapeTexture();

#if !UNITY_EDITOR
            adjustShape = validFT ? adjustShape : 0;
#endif
            int maxArrayCount = adjustShape + adjustWidth;
            return(maxArrayCount);
        }
示例#2
0
        int CalculateMaxArrayCount(NativeArray <ShapeControlPoint> shapePoints)
        {
            int   maxVertexCount = 1024 * 64;
            bool  hasSprites     = false;
            float smallestWidth  = 99999.0f;

            if (null != spriteArray)
            {
                foreach (var sprite in m_SpriteArray)
                {
                    if (sprite != null)
                    {
                        hasSprites = true;
                        float pixelWidth = BezierUtility.GetSpritePixelWidth(sprite);
                        smallestWidth = (smallestWidth > pixelWidth) ? pixelWidth : smallestWidth;
                    }
                }
            }

            // Approximate vertex Array Count. Include Corners and Wide Sprites into account.
            float smallestSegment = smallestWidth;
            float shapeLength     = BezierUtility.BezierLength(shapePoints, splineDetail, ref smallestSegment) * 4.0f;
            int   adjustShape     = shapePoints.Length * 4 * splineDetail;
            int   adjustWidth     = hasSprites ? ((int)(shapeLength / smallestSegment) * splineDetail) + adjustShape : 0;

            adjustShape   = optimizeGeometry ? (adjustShape) : (adjustShape * 2);
            adjustShape   = ValidateSpriteShapeTexture() ? adjustShape : 0;
            maxArrayCount = adjustShape + adjustWidth;
            maxArrayCount = math.min(maxArrayCount, maxVertexCount);
            return(maxArrayCount);
        }
        void SampleCurve(Vector3 startPoint, Vector3 startTangent, Vector3 endPoint, Vector3 endTangent, ref List <IntPoint> path)
        {
            if (startTangent.sqrMagnitude > 0f || endTangent.sqrMagnitude > 0f)
            {
                for (int j = 0; j <= m_ColliderDetail; ++j)
                {
                    float   t        = j / (float)m_ColliderDetail;
                    Vector3 newPoint = BezierUtility.BezierPoint(startPoint, startTangent + startPoint, endTangent + endPoint, endPoint, t) * s_ClipperScale;

                    path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));
                }
            }
            else
            {
                Vector3 newPoint = startPoint * s_ClipperScale;
                path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));

                newPoint = endPoint * s_ClipperScale;
                path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));
            }
        }
        unsafe JobHandle BakeMesh(bool needUpdateSpriteArrays)
        {
            JobHandle jobHandle = default;

            if (needUpdateSpriteArrays)
            {
                UpdateSprites();
            }

            int pointCount = m_Spline.GetPointCount();
            NativeArray <ShapeControlPoint>   shapePoints   = new NativeArray <ShapeControlPoint>(pointCount, Allocator.Temp);
            NativeArray <SpriteShapeMetaData> shapeMetaData = new NativeArray <SpriteShapeMetaData>(pointCount, Allocator.Temp);

            for (int i = 0; i < pointCount; ++i)
            {
                ShapeControlPoint shapeControlPoint;
                shapeControlPoint.position     = m_Spline.GetPosition(i);
                shapeControlPoint.leftTangent  = m_Spline.GetLeftTangent(i);
                shapeControlPoint.rightTangent = m_Spline.GetRightTangent(i);
                shapeControlPoint.mode         = (int)m_Spline.GetTangentMode(i);
                shapePoints[i] = shapeControlPoint;

                SpriteShapeMetaData metaData;
                metaData.corner      = m_Spline.GetCorner(i);
                metaData.height      = m_Spline.GetHeight(i);
                metaData.spriteIndex = (uint)m_Spline.GetSpriteIndex(i);
                metaData.bevelCutoff = 0;
                metaData.bevelSize   = 0;
                shapeMetaData[i]     = metaData;
            }

            if (spriteShapeRenderer != null && ValidatePoints(shapePoints))
            {
                if (m_LegacyGenerator)
                {
                    SpriteShapeUtility.GenerateSpriteShape(spriteShapeRenderer, m_ActiveShapeParameters,
                                                           shapePoints.ToArray(), shapeMetaData.ToArray(), m_AngleRangeInfoArray, m_EdgeSpriteArray,
                                                           m_CornerSpriteArray);
                }
                else
                {
                    bool  hasSprites    = false;
                    float smallestWidth = 99999.0f;
                    foreach (var sprite in m_SpriteArray)
                    {
                        if (sprite != null)
                        {
                            hasSprites = true;
                            float pixelWidth = BezierUtility.GetSpritePixelWidth(sprite);
                            smallestWidth = (smallestWidth > pixelWidth) ? pixelWidth : smallestWidth;
                        }
                    }

                    // Approximate vertex Array Count.
                    float shapeLength = BezierUtility.BezierLength(shapePoints, splineDetail * splineDetail);
                    int   adjustWidth = hasSprites ? ((int)(shapeLength / smallestWidth) * 6) + (pointCount * 6 * splineDetail) : 0;
                    int   adjustShape = pointCount * 4 * splineDetail;
#if !UNITY_EDITOR
                    adjustShape = (spriteShape != null && spriteShape.fillTexture != null) ? adjustShape : 0;
#endif
                    int maxArrayCount = adjustShape + adjustWidth;

                    // Collider Data
                    if (m_ColliderData.IsCreated)
                    {
                        m_ColliderData.Dispose();
                    }
                    m_ColliderData = new NativeArray <float2>(maxArrayCount, Allocator.Persistent);

                    // Tangent Data
                    if (!m_TangentData.IsCreated)
                    {
                        m_TangentData = new NativeArray <Vector4>(1, Allocator.Persistent);
                    }

                    NativeArray <ushort>             indexArray;
                    NativeSlice <Vector3>            posArray;
                    NativeSlice <Vector2>            uv0Array;
                    NativeArray <Bounds>             bounds    = spriteShapeRenderer.GetBounds();
                    NativeArray <SpriteShapeSegment> geomArray = spriteShapeRenderer.GetSegments(shapePoints.Length * 8);
                    NativeSlice <Vector4>            tanArray  = new NativeSlice <Vector4>(m_TangentData);

                    if (m_EnableTangents)
                    {
                        spriteShapeRenderer.GetChannels(maxArrayCount, out indexArray, out posArray, out uv0Array, out tanArray);
                    }
                    else
                    {
                        spriteShapeRenderer.GetChannels(maxArrayCount, out indexArray, out posArray, out uv0Array);
                    }

                    var spriteShapeJob = new SpriteShapeGenerator()
                    {
                        m_Bounds         = bounds,
                        m_PosArray       = posArray,
                        m_Uv0Array       = uv0Array,
                        m_TanArray       = tanArray,
                        m_GeomArray      = geomArray,
                        m_IndexArray     = indexArray,
                        m_ColliderPoints = m_ColliderData
                    };
                    spriteShapeJob.Prepare(this, m_ActiveShapeParameters, maxArrayCount, shapePoints, shapeMetaData, m_AngleRangeInfoArray, m_EdgeSpriteArray, m_CornerSpriteArray);
                    jobHandle = spriteShapeJob.Schedule();
                    spriteShapeRenderer.Prepare(jobHandle, m_ActiveShapeParameters, m_SpriteArray);
                    JobHandle.ScheduleBatchedJobs();
                }
            }

            if (m_DynamicOcclusionOverriden)
            {
                spriteShapeRenderer.allowOcclusionWhenDynamic = m_DynamicOcclusionLocal;
                m_DynamicOcclusionOverriden = false;
            }

            shapePoints.Dispose();
            shapeMetaData.Dispose();
            return(jobHandle);
        }