예제 #1
0
        /// <summary>
        /// 绘制Texture2D
        /// </summary>
        /// <param name="texture">目标Texture2D</param>
        /// <param name="transform">变换矩阵</param>
        /// <param name="overlayColor">要叠加的颜色</param>
        /// <param name="pivot">变换轴点</param>
        /// <param name="mode">图像混合模式</param>
        /// <returns></returns>
        public Texture2DCombiner DrawTexture(Texture2D texture, Matrix4x4 transform, Color overlayColor, Vector2 pivot, MixMode mode = MixMode.Overlay)
        {
            var width  = texture.width;
            var height = texture.height;

            PrepareTransform(ref transform, in pivot, in width, in height, out var area);
            if (_renderCanvas == null)
            {
                var pixels       = texture.GetPixels();
                var canvasPixels = _canvas.GetPixels(area.x, area.y, area.width, area.height);
                var areaX        = area.xMax;
                var areaY        = area.yMax;
                for (var i = area.y - 1; ++i < areaY;)
                {
                    for (var j = area.x - 1; ++j < areaX;)
                    {
                        var position = (Vector2)transform.MultiplyPoint(new Vector2(j, i));
                        if (!position.x.InRange(0, width) || !position.y.InRange(0, height))
                        {
                            continue;
                        }
                        var canvasPixelIndex = j - area.x + (i - area.y) * area.width;
                        var origin           = canvasPixels[canvasPixelIndex];
                        var target           = BilinearInterpolation(position, pixels, width) * overlayColor;
                        canvasPixels[canvasPixelIndex] = MixColor(in origin, in target, mode);
                    }
                }
                _canvas.SetPixels(area.x, area.y, area.width, area.height, canvasPixels);
            }
            else
            {
                var kernel = GetKernel(mode);
                _shader.SetVector(ShaderSizeName, new Vector4(area.x, area.y, width, height));
                _shader.SetTexture(kernel, ShaderSourceName, texture);
                _shader.SetVector(ShaderColorName, overlayColor);
                _shader.SetMatrix(ShaderTransformName, transform);
                var currentTexture = _renderCanvas.Active();
                _shader.Dispatch(kernel, Mathf.CeilToInt(area.width / 24.0F), Mathf.CeilToInt(area.height / 24.0F), 1);
                RenderTexture.active = currentTexture;
            }
            return(this);
        }