예제 #1
0
        public static void Blur(D2dDestructible destructible)
        {
            var alphaData   = destructible.AlphaData;
            var alphaWidth  = destructible.AlphaWidth;
            var alphaHeight = destructible.AlphaHeight;

            D2dHelper.ReserveTempAlphaData(alphaWidth, alphaHeight);

            BlurHorizontally(alphaData, D2dHelper.tempAlphaData, alphaWidth, alphaHeight);

            BlurVertically(D2dHelper.tempAlphaData, alphaData, alphaWidth, alphaHeight);
        }
예제 #2
0
        protected virtual void LateUpdate()
        {
            if (ready == true && AlphaModified.IsSet == true && alphaTex != null)
            {
                var w = AlphaModified.SizeX;
                var h = AlphaModified.SizeY;

                // Replace all pixels?
                if (w * h > 1000)
                {
                    alphaTex.SetPixels32(alphaData);
                    alphaTex.Apply();
                }
                // Replace updated pixels?
                else
                {
                    var i = 0;

                    D2dHelper.ReserveTempAlphaData(w, h);

                    for (var y = AlphaModified.MinY; y < AlphaModified.MaxY; y++)
                    {
                        var o = y * alphaWidth;

                        for (var x = AlphaModified.MinX; x < AlphaModified.MaxX; x++)
                        {
                            D2dHelper.tempAlphaData[i++] = alphaData[o + x];
                        }
                    }

                    alphaTex.SetPixels32(AlphaModified.MinX, AlphaModified.MinY, w, h, D2dHelper.tempAlphaData);
                    alphaTex.Apply();
                }

                NotifyModified(AlphaModified);
            }
        }
예제 #3
0
        /// <summary>This method allows you to manually try and fracture the specified D2dDestructible.</summary>
        public static bool TryFracture(D2dDestructible destructible, int pointCount, bool splitAfterFracture, int splitFeather, int splitHealThreshold)
        {
            if (pointCount > 1 && destructible != null && destructible.Ready == true)
            {
                if (pointCount > 20)
                {
                    pointCount = 20;
                }

                var w = destructible.AlphaWidth;
                var h = destructible.AlphaHeight;
                var t = w * h;

                GenerateVoronoiPoints(pointCount, w, h);
                GenerateVoronoiData(pointCount, w, h, t);

                if (voronoiCount > 1)
                {
                    var alphaData = destructible.AlphaData;
                    var alphaRect = new D2dRect(0, w, 0, h);

                    destructible.SplitBegin();

                    chunks.Clear();

                    for (var i = pointCount - 1; i >= 0; i--)
                    {
                        var rect = voronoiRects[i];

                        if (rect.IsSet == true)
                        {
                            voronoiCount -= 1;

                            var chunk = destructible.SplitNext(voronoiCount == 0);

                            chunks.Add(chunk);

                            rect.Expand(1);
                            rect.ClampTo(alphaRect);

                            D2dHelper.ReserveTempAlphaData(rect.SizeX, rect.SizeY);

                            if (tempAlphaData == null || tempAlphaData.Length < rect.SizeX * rect.SizeY)
                            {
                                tempAlphaData = new Color32[rect.SizeX * rect.SizeY];
                            }

                            // Write black and white mask
                            for (var y = rect.MinY; y < rect.MaxY; y++)
                            {
                                var o = y * w;
                                var z = (y - rect.MinY) * rect.SizeX - rect.MinX;

                                for (var x = rect.MinX; x < rect.MaxX; x++)
                                {
                                    var alpha = voronoi[o + x] == i ? 255 : 0;

                                    D2dHelper.tempAlphaData[z + x] = new Color32(255, 255, 255, (byte)alpha);
                                }
                            }

                            // Blur
                            D2dBlur.BlurHorizontally(D2dHelper.tempAlphaData, tempAlphaData, rect.SizeX, rect.SizeY);
                            D2dBlur.BlurVertically(tempAlphaData, D2dHelper.tempAlphaData, rect.SizeX, rect.SizeY);

                            // Combine alpha
                            for (var y = rect.MinY; y < rect.MaxY; y++)
                            {
                                var o = y * w;
                                var z = (y - rect.MinY) * rect.SizeX - rect.MinX;

                                for (var x = rect.MinX; x < rect.MaxX; x++)
                                {
                                    var index      = z + x;
                                    var maskPixel  = D2dHelper.tempAlphaData[index];
                                    var alphaPixel = alphaData[o + x];

                                    alphaPixel.a = (byte)(alphaPixel.a * (maskPixel.a * recip255));

                                    D2dHelper.tempAlphaData[index] = alphaPixel;
                                }
                            }

                            chunk.SubsetAlphaWith(D2dHelper.tempAlphaData, rect);
                        }
                    }

                    destructible.SplitEnd(D2dDestructible.SplitMode.Fracture);

                    if (splitAfterFracture == true)
                    {
                        for (var i = chunks.Count - 1; i >= 0; i--)
                        {
                            var chunk = chunks[i];

                            if (chunk != null)
                            {
                                D2dSplitter.TrySplit(chunk, splitFeather, splitHealThreshold);
                            }
                        }
                    }

                    return(true);
                }
            }

            return(false);
        }