public static bool Downsample(RenderTexture renderTexture, int steps, ref RenderTexture temporary)
        {
            if (steps > 0 && renderTexture != null)
            {
                // Perform initial downsample to get buffer
                var oldActive         = RenderTexture.active;
                var desc              = new RenderTextureDescriptor(renderTexture.width / 2, renderTexture.height / 2, renderTexture.format, 0);
                var halfRenderTexture = GetRenderTexture(desc);

                P3dPaintReplace.BlitFast(halfRenderTexture, renderTexture, Color.white);

                // Ping pong downsamples
                for (var i = 1; i < steps; i++)
                {
                    desc.width       /= 2;
                    desc.height      /= 2;
                    renderTexture     = halfRenderTexture;
                    halfRenderTexture = GetRenderTexture(desc);

                    Graphics.Blit(renderTexture, halfRenderTexture);

                    ReleaseRenderTexture(renderTexture);
                }

                temporary = halfRenderTexture;

                RenderTexture.active = oldActive;

                return(true);
            }

            return(false);
        }
        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
        }
Ejemplo n.º 3
0
        /// <summary>This allows you to manually execute all commands in the paint stack.
        /// This is useful if you need to modify the state of your object before the end of the frame.</summary>
        public void ExecuteCommands(bool sendNotifications)
        {
            if (activated == true)
            {
                var hidePreview = true;

                if (CommandsPending == true)
                {
                    var oldActive      = RenderTexture.active;
                    var swap           = P3dHelper.GetRenderTexture(current.descriptor);
                    var preparedMesh   = default(Mesh);
                    var preparedMatrix = default(Matrix4x4);

                    // Blit so we don't lose non-UVd areas
                    P3dPaintReplace.BlitFast(swap, current, Color.white);

                    // Paint
                    if (paintCommands.Count > 0)
                    {
                        ExecuteCommands(paintCommands, sendNotifications, ref current, ref swap, ref preparedMesh, ref preparedMatrix);
                    }

                    // Preview
                    if (previewCommands.Count > 0)
                    {
                        if (previewSet == false)
                        {
                            preview    = P3dHelper.GetRenderTexture(current.descriptor);
                            previewSet = true;
                        }

                        hidePreview = false;

                        preview.DiscardContents();

                        Graphics.Blit(current, preview);

                        previewCommands.Sort(P3dCommand.Compare);

                        ExecuteCommands(previewCommands, sendNotifications, ref preview, ref swap, ref preparedMesh, ref preparedMatrix);
                    }

                    P3dHelper.ReleaseRenderTexture(swap);

                    RenderTexture.active = oldActive;
                }

                if (hidePreview == true && previewSet == true)
                {
                    P3dHelper.ReleaseRenderTexture(preview);

                    preview    = null;
                    previewSet = false;
                }

                if (materialSet == false)
                {
                    UpdateMaterial();
                }

                material.SetTexture(slot.Name, previewSet == true ? preview : current);
            }
        }