/// <summary>
        /// Converts a materials <see cref="Material.mainTexture"/> property into a <see cref="GenericImage{T}"/> type takes into account the 
        /// materials <see cref="Material.mainTextureOffset"/> and <see cref="Material.mainTextureScale"/> properties. See remarks.
        /// </summary>
        /// <param name="material">The source material to get the <see cref="Material.mainTexture"/> reference from.</param>
        /// <returns>Returns a <see cref="GenericImage{T}"/> type representing the materials texture co-ordinates.</returns>
        /// <remarks>This method does not return the original texture specified by the <see cref="Material.mainTexture"/> property but rather
        /// takes into account the materials <see cref="Material.mainTextureOffset"/> and <see cref="Material.mainTextureScale"/> properties
        /// in order to generate a <see cref="GenericImage{T}"/> type.</remarks>
        /// <exception cref="InvalidCastException">If <see cref="Material.mainTexture"/> cannot be cast to a <see cref="Texture2D"/> type.</exception>
        /// <exception cref="ArgumentNullException">If the <see cref="material"/> parameter is null.</exception>
        public static GenericImage<Color> ToGenericImage(this Material material)
        {
            // ensure material specified
            if (material == null)
            {
                throw new ArgumentNullException("material");
            }

            // if no main texture then also return null
            if (material.mainTexture == null)
            {
                return null;
            }

            var texture = material.mainTexture as Texture2D;

            // ensure material texture is a Texture2D type
            if (texture == null)
            {
                throw new InvalidCastException("Material texture is not a Texture2D!");
            }

            // create a generic image.
            var image = texture.CreateGenericImage();
            var data = image.ToUnityColor32Array();
            var tempImage = new GenericImage<Color>(image.Width, image.Height);
            tempImage.SetPixels(Array.ConvertAll(data, input => new Color(input.r, input.g, input.b, input.a)));

            var offset = material.mainTextureOffset;
            var position = new Point(
                (int)((offset.x * texture.width) % texture.width),
                (int)(texture.height - ((offset.y * texture.height) % texture.height)));

            var scale = material.mainTextureScale;
            var size = new Size((int)(scale.x * texture.width), (int)(scale.y * texture.height));

            var subImage = new GenericImage<Color>(size.Width, size.Height);
            subImage.Draw(tempImage, 0, 0, position.X, position.Y - size.Height, size.Width, size.Height, (source, blendWith) => blendWith);

            // convert and return a generic image
            return subImage;
        }
        /// <summary>
        /// Handles the event for tile selection.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An System.EventArgs that contains no event data.</param>
        private void MainPreviewTileSelection(object sender, TileSelectionEventArgs e)
        {
            // do nothing until selection has completed or if no texture selected
            if (e.Status != TileSelectionStatus.Complete || this.materialControls.TextureAsset == null)
            {
                return;
            }

            var image = new GenericImage <Color>(this.mainPreview.TileWidth, this.mainPreview.TileHeight);

            image.Draw(
                this.materialControls.TextureAsset.CreateGenericImage(),
                0,
                0,
                e.Min.X,
                e.Min.Y,
                this.mainPreview.TileWidth,
                this.mainPreview.TileHeight,
                (source, blendWith) => blendWith);

            this.SelectedTile = image;
        }
        /// <summary>
        /// Draws a <see cref="GenericImage{T}"/> on to a <see cref="Texture2D"/> texture.
        /// </summary>
        /// <param name="texture">A reference to a <see cref="Texture2D"/> type.</param>
        /// <param name="sourceImage">A reference to the <see cref="GenericImage{T}"/> that will be drawn.</param>
        /// <param name="x">The x position where the <see cref="sourceImage"/> will be drawn.</param>
        /// <param name="y">The y position where the <see cref="sourceImage"/> will be drawn.</param>
        /// <param name="sourceX">The source x position within <see cref="sourceImage"/>.</param>
        /// <param name="sourceY">The source y position within <see cref="sourceImage"/>.</param>
        /// <param name="sourceWidth">The source width within the <see cref="sourceImage"/>.</param>
        /// <param name="sourceHeight">The source height within the <see cref="sourceImage"/>.</param>
        /// <param name="flipHorizontally">If true will flip the <see cref="sourceImage"/> horizontally before drawing.</param>
        /// <param name="flipVertically">If true will flip the <see cref="sourceImage"/> vertically before drawing.</param>
        public static void Draw(this Texture2D texture, GenericImage<Color> sourceImage, int x, int y, int sourceX, int sourceY, int sourceWidth, int sourceHeight, bool flipHorizontally, bool flipVertically)
        {
            var textureRectangle = new Rect(0, 0, texture.width, texture.height);
            var sourceRectangle = new Rect(x, y, sourceWidth, sourceHeight);
            var intersect = textureRectangle.Intersect(sourceRectangle);

            if (!intersect.Intersects(new Rect(0, 0, sourceImage.Width, sourceImage.Height)))
            {
                return;
            }

            var tempImage = new GenericImage<Color>((int)intersect.width, (int)intersect.height);
            tempImage.Draw(sourceImage, 0, 0, sourceX, sourceY, tempImage.Width, tempImage.Height, (source, blendWith) => blendWith);

            if (flipHorizontally)
            {
                tempImage.FlipHorizontally();
            }

            if (flipVertically)
            {
                tempImage.FlipVertically();
            }

            var colors = tempImage.ToUnityColorArray();
            texture.SetPixels(x, y, (int)intersect.width, (int)intersect.height, colors);
            texture.Apply();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the event for tile selection.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An System.EventArgs that contains no event data.</param>
        private void MainPreviewTileSelection(object sender, TileSelectionEventArgs e)
        {
            // do nothing until selection has completed or if no texture selected 
            if (e.Status != TileSelectionStatus.Complete || this.materialControls.TextureAsset == null)
            {
                return;
            }

            var image = new GenericImage<Color>(this.mainPreview.TileWidth, this.mainPreview.TileHeight);
            image.Draw(
                this.materialControls.TextureAsset.CreateGenericImage(),
                0,
                0,
                e.Min.X,
                e.Min.Y,
                this.mainPreview.TileWidth,
                this.mainPreview.TileHeight,
                (source, blendWith) => blendWith);

            this.SelectedTile = image;
        }