Esempio n. 1
0
        private void CalculateTexCoordsAndWeightings(TextureBrush brush, Vector2[] texCoords, Vector2[] vertexPositions)
        {
            var min = new Vector2(float.MaxValue, float.MaxValue);
            var max = new Vector2(float.MinValue, float.MinValue);

            for (var v = 0; v < _numVertices; v++)
            {
                var vx = vertexPositions[v];

                if (vx.X < min.X)
                {
                    min.X = vx.X;
                }

                if (vx.Y < min.Y)
                {
                    min.Y = vx.Y;
                }

                if (vx.X > max.X)
                {
                    max.X = vx.X;
                }

                if (vx.Y > max.Y)
                {
                    max.Y = vx.Y;
                }
            }

            var areaSize = max - min;

            var textureSize = Vector2.Zero;

            if (brush.TextureScaling == TextureScaling.Tiled)
            {
                var size = _drawing.GetSurfaceDimensions(brush.Texture);
                textureSize  = new Vector2(size.Width, size.Height);
                textureSize *= brush.TilingScale;
            }

            for (var v = 0; v < _numVertices; v++)
            {
                var vx = vertexPositions[v];
                var position_fraction = (vx - min) / areaSize;

                if (_fill == FillType.DualTextured)
                {
                    _tex0Weightings[v] = CalculateTexture0WeightFromAreaPositionFraction(position_fraction);
                }

                switch (brush.TextureScaling)
                {
                case TextureScaling.Stretch:
                    position_fraction.Y = 1.0f - position_fraction.Y;     //Flip Y to match origin for texcoords at topleft not bottomleft
                    texCoords[v]        = position_fraction;
                    break;

                case TextureScaling.Tiled:
                    var pixelDistanceFromCentre = position_fraction - new Vector2(0.5f, 0.5f);
                    pixelDistanceFromCentre *= areaSize;
                    var texSizeMultiplesFromCentre = pixelDistanceFromCentre / textureSize;
                    texSizeMultiplesFromCentre.Y = -texSizeMultiplesFromCentre.Y;      //Switch from calcuation origin bottom left to top left
                    var texSizeMultiplesFromTopLeft = texSizeMultiplesFromCentre + new Vector2(0.5f, 0.5f);
                    texCoords[v] = texSizeMultiplesFromTopLeft;
                    break;
                }
            }
        }