예제 #1
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);
            }
        }