示例#1
0
    //-------------------------------------------------------------------------
    public bool SetupBinaryImageAndIslands(out int numColliderRegions, out object spriteCollectionDefinition, object spriteCollection, int spriteID)
    {
        numColliderRegions         = 0;
        spriteCollectionDefinition = null;

        mMainTex = GetTextureRef(spriteCollection, spriteID);
        if (mMainTex == null)
        {
            //Debug.LogError("No sprite texture found at sprite '" + tk2dSpriteComponent.name + "' with spriteID " + spriteID + ".");
            Debug.LogError("No sprite texture found at sprite with spriteID " + spriteID + ".");
            return(false);
        }

        spriteCollectionDefinition = GetTK2DSpriteCollectionDefinition(spriteCollection, spriteID);
        if (spriteCollectionDefinition == null)
        {
            // last error is already set in GetTK2DSpriteCollectionDefinition() above.
            return(false);
        }

        if (mOutlineAlgorithm == null)
        {
            mOutlineAlgorithm = new PolygonOutlineFromImageFrontend();
        }

        int  regionXOffset = 0;
        int  regionYOffset = 0;
        int  regionWidth   = UsedTexture.width;
        int  regionHeight  = UsedTexture.height;
        bool isRegionUsed  = false;

        if (mRegionIndependentParameters.CustomTex == null)
        {
            isRegionUsed  = ReadRegionParameters(out regionXOffset, out regionYOffset, out regionWidth, out regionHeight, spriteCollectionDefinition);
            regionYOffset = mMainTex.height - regionYOffset - regionHeight;
        }

        mOutlineAlgorithm.BinaryAlphaThresholdImageFromTexture(ref mBinaryImage, out mBinaryImageWidth, out mBinaryImageHeight, UsedTexture, mRegionIndependentParameters.AlphaOpaqueThreshold,
                                                               isRegionUsed, regionXOffset, regionYOffset, regionWidth, regionHeight);
        int imageSize = mBinaryImageWidth * mBinaryImageHeight;

        if (mClassificationImage == null || mClassificationImage.Length < imageSize)
        {
            mClassificationImage = new int[imageSize];
        }

        bool anyIslandsFound = CalculateIslandStartingPoints(mBinaryImage, mBinaryImageWidth, mBinaryImageHeight, ref mClassificationImage, ref mIslands, ref mSeaRegions);

        if (!anyIslandsFound)
        {
            return(false);
        }

        AlphaMeshCollider.SetupColliderRegions(out mColliderRegions, mIslands, mSeaRegions);
        numColliderRegions = mColliderRegions.Length;
        SetupColliderRegionParameters(ref mColliderRegionParameters, mRegionIndependentParameters.DefaultMaxPointCount, mIslands, mSeaRegions);
        return(true);
    }