public void Paint(P3dGroup group, P3dCommand command)
        {
            var commandMaterial = command.Material;

            //if (bounds.Intersects(lastBounds) == true)
            {
                for (var i = PaintableTextures.Count - 1; i >= 0; i--)
                {
                    var paintableTexture = PaintableTextures[i];
                    var renderTexture    = paintableTexture.PreparePaint();                     // Prepare the paint regardless, to sync undo states

                    if (paintableTexture.Group == group)
                    {
                        var oldActive = RenderTexture.active;
                        var swap      = default(RenderTexture);

                        RenderTexture.active = renderTexture;

                        if (command.RequireSwap == true)
                        {
                            swap = P3dHelper.GetRenderTexture(renderTexture.width, renderTexture.height, renderTexture.depth, renderTexture.format);

                            P3dHelper.Blit(swap, renderTexture);

                            commandMaterial.SetTexture(P3dShader._Buffer, swap);
                        }

                        command.Apply();

                        if (command.RequireMesh == true)
                        {
                            switch (paintableTexture.Channel)
                            {
                            case P3dChannel.UV: commandMaterial.SetVector(P3dShader._Channel, new Vector4(1.0f, 0.0f, 0.0f, 0.0f)); break;

                            case P3dChannel.UV2: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 1.0f, 0.0f, 0.0f)); break;

                            case P3dChannel.UV3: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); break;

                            case P3dChannel.UV4: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); break;
                            }

                            commandMaterial.SetPass(0);

                            Graphics.DrawMeshNow(lastMesh, lastMatrix, paintableTexture.MaterialIndex);
                        }
                        else
                        {
                            Graphics.Blit(default(Texture), renderTexture, commandMaterial);
                        }

                        RenderTexture.active = oldActive;

                        if (swap != null)
                        {
                            P3dHelper.ReleaseRenderTexture(swap);
                        }
                    }
                }
            }
        }
        public void Paint(P3dCommand command, List <P3dWindowPaintable> paintables)
        {
            var commandMaterial = command.Material;

            //if (bounds.Intersects(lastBounds) == true)
            {
                for (var i = PaintableTextures.Count - 1; i >= 0; i--)
                {
                    var paintableTexture = PaintableTextures[i];
                    var renderTexture    = paintableTexture.PreparePaint();                     // Prepare the paint regardless, to sync undo states

                    if (P3dHelper.IndexInMask(paintableTexture.Group, command.Groups) == true)
                    {
                        var oldActive = RenderTexture.active;
                        var swap      = default(RenderTexture);

                        RenderTexture.active = renderTexture;

                        if (true)                         // TODO
                        {
                            swap = P3dHelper.GetRenderTexture(renderTexture.width, renderTexture.height, renderTexture.depth, renderTexture.format);

                            P3dHelper.Blit(swap, renderTexture);

                            commandMaterial.SetTexture(P3dShader._Buffer, swap);
                        }

                        command.Apply();

                        if (command.RequireMesh == true)
                        {
                            for (var j = paintables.Count - 1; j >= 0; j--)
                            {
                                var otherP = paintables[j];

                                for (var k = otherP.PaintableTextures.Count - 1; k >= 0; k--)
                                {
                                    var otherT = otherP.PaintableTextures[k];

                                    if (otherT.OldTexture == paintableTexture.OldTexture)
                                    {
                                        switch (otherT.Channel)
                                        {
                                        case P3dChannel.UV: commandMaterial.SetVector(P3dShader._Channel, new Vector4(1.0f, 0.0f, 0.0f, 0.0f)); break;

                                        case P3dChannel.UV2: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 1.0f, 0.0f, 0.0f)); break;

                                        case P3dChannel.UV3: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); break;

                                        case P3dChannel.UV4: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); break;
                                        }

                                        commandMaterial.SetPass(0);

                                        Graphics.DrawMeshNow(otherP.lastMesh, otherP.lastMatrix, otherT.MaterialIndex);
                                    }
                                }
                            }
                        }
                        else
                        {
                            Graphics.Blit(default(Texture), renderTexture, commandMaterial);
                        }

                        RenderTexture.active = oldActive;

                        if (swap != null)
                        {
                            P3dHelper.ReleaseRenderTexture(swap);
                        }
                    }
                }
            }
        }
        private void Render(P3dCommand command, P3dScene.Mat mat, P3dScene.Image image, P3dScene.Obj obj, int subMesh)
        {
            var oldActive = RenderTexture.active;

            if (image.Current == null)
            {
                if (image.Width > 0 && image.Height > 0 && image.Pixels != null && image.Pixels.Length > 0)
                {
                    var texture = new Texture2D(1, 1);

                    if (texture.LoadImage(image.Pixels) == true)
                    {
                        var desc = mat.Desc;

                        desc.width  = image.Width;
                        desc.height = image.Height;

                        image.Current = P3dHelper.GetRenderTexture(mat.Desc);

                        P3dPaintReplace.BlitFast(image.Current, texture, Color.white);
                    }
                    else
                    {
                        image.Current = P3dHelper.GetRenderTexture(mat.Desc);

                        P3dPaintReplace.BlitFast(image.Current, default(Texture), default(Color));
                    }

                    DestroyImmediate(texture);
                }
                else
                {
                    image.Current = P3dHelper.GetRenderTexture(mat.Desc);

                    P3dPaintReplace.BlitFast(image.Current, default(Texture), default(Color));
                }
            }

            var swap = P3dHelper.GetRenderTexture(image.Current.descriptor);

            if (command.Preview == true)
            {
                if (image.Preview == null)
                {
                    image.Preview = P3dHelper.GetRenderTexture(image.Current.descriptor);

                    P3dPaintReplace.BlitFast(image.Preview, image.Current, Color.white);
                }

                P3dPaintReplace.BlitFast(swap, image.Preview, Color.white);

                command.Apply(image.Preview);
            }
            else
            {
                P3dPaintReplace.BlitFast(swap, image.Current, Color.white);

                command.Apply(image.Current);
            }

            RenderTexture.active = swap;

            if (command.RequireMesh == true)
            {
                P3dHelper.Draw(command.Material, obj.Mesh, obj.Matrix, subMesh, obj.Coord);
            }
            else
            {
                P3dHelper.Draw(command.Material);
            }

            RenderTexture.active = oldActive;

            if (command.Preview == true)
            {
                P3dHelper.ReleaseRenderTexture(image.Preview);

                image.Preview = swap;

                previewDrawn = true;
            }
            else
            {
                P3dHelper.ReleaseRenderTexture(image.Current);

                image.Current = swap;
            }

            paintedGroups.Add(command.Priority);             // Group is stored in priority
        }