/// <inheritdoc/>
        /// <seealso cref="CustomImmediatePreview"/>
        public override void OnDrawImmediatePreview(IBrushContext context, TileData previewTile, Material previewMaterial, Brush transformer)
        {
            var orientation = this.FindClosestOrientation(previewTile.orientationMask);

            if (orientation == null || orientation.VariationCount == 0)
            {
                return;
            }

            // Wrap variation index if necessary.
            int variationIndex = previewTile.variationIndex;

            if (variationIndex >= orientation.VariationCount)
            {
                variationIndex = 0;
            }
            // Randomization is not supported here!
            if (variationIndex == RANDOM_VARIATION)
            {
                variationIndex = 0;
            }

            var variationGO = orientation.GetVariation(variationIndex) as GameObject;

            if (variationGO != null)
            {
                var       variationTransform = variationGO.transform;
                Matrix4x4 matrix             = ImmediatePreviewUtility.Matrix * transformer.GetTransformMatrix(context.TileSystem, context.Row, context.Column, previewTile.Rotation, variationTransform);

                // Allow variation to provide a custom immediate preview.
                var customImmediatePreview = variationGO.GetComponent <CustomImmediatePreview>();
                if (customImmediatePreview != null)
                {
                    // Bail if custom immediate preview was drawn.
                    if (customImmediatePreview.DrawImmediatePreview(context, previewTile, previewMaterial, matrix))
                    {
                        return;
                    }
                }

                ImmediatePreviewUtility.DrawNow(previewMaterial, variationTransform, matrix, context.Brush as IMaterialMappings);
                return;
            }

            var variationBrush = orientation.GetVariation(variationIndex) as Brush;

            if (variationBrush != null)
            {
                // If transforming brush has not already been overridden then...
                if (transformer == this && !this.overrideTransforms)
                {
                    transformer = variationBrush;
                }
                variationBrush.OnDrawImmediatePreview(context, previewTile, previewMaterial, transformer);
                return;
            }
        }
Пример #2
0
        /// <summary>
        /// Helper method to draw immediate preview of tileset tile.
        /// </summary>
        /// <remarks>
        /// <para>Attached prefab will also be included in preview when applicable.</para>
        /// </remarks>
        /// <param name="context">Describes context of tile that is being previewed.</param>
        /// <param name="previewMaterial">Material to use for preview.</param>
        /// <param name="previewTile">Data for preview tile.</param>
        /// <param name="transformer">Brush used to transform painted tile.
        /// The <see cref="Brush.scaleMode"/> and <see cref="Brush.applyPrefabTransform"/>
        /// fields of transform brush should be used. <c>transformer</c> may refer to this
        /// brush, or it may refer to another (like alias or oriented for example).</param>
        protected void DrawImmediateTilePreview(IBrushContext context, Material previewMaterial, TileData previewTile, Brush transformer)
        {
            var atlasTexture = Tileset.AtlasTexture;

            if (atlasTexture == null)
            {
                return;
            }

            // Prepare mesh for tile background.
            Rect texCoords = Tileset.CalculateTexCoords(previewTile.tilesetIndex);
            var  mesh      = ImmediatePreviewUtility.UpdateQuadPreviewMesh(texCoords);

            // Compute matrix for tile background.
            var tileSystem = context.TileSystem;

            Matrix4x4 matrix;

            tileSystem.CalculateTileMatrix(out matrix, context.Row, context.Column, TileFacing.Sideways, previewTile.Rotation);
            MathUtility.MultiplyMatrixByScale(ref matrix, tileSystem.CalculateCellSizeScale(previewTile.Rotation));

            matrix = ImmediatePreviewUtility.Matrix * matrix;

            // Draw tile background.
            previewMaterial.mainTexture = atlasTexture;
            ImmediatePreviewUtility.DrawNow(previewMaterial, mesh, matrix);

            // Draw preview of tile attachment?
            if (this.attachPrefab != null)
            {
                int rotation = this.applySimpleRotationToAttachment ? previewTile.PaintedRotation : 0;

                var attachmentTransform = this.attachPrefab.transform;
                matrix = tileSystem.transform.localToWorldMatrix * transformer.GetTransformMatrix(tileSystem, context.Row, context.Column, rotation, attachmentTransform);

                ImmediatePreviewUtility.DrawNow(previewMaterial, attachmentTransform, matrix, context.Brush as IMaterialMappings);
            }
        }