/// <summary>Connects a filter to the camera's output.
        /// Fires an imagechange event too as this updates Texture to the filtered version.</summary>
        private void ConnectFilter()
        {
            if (Filter_ != null)
            {
                if (FilterDrawInfo == null)
                {
                    FilterDrawInfo = new Loonim.DrawInfo();
                }

                // Update draw info:
                FilterDrawInfo.SetSize(pixelWidth, pixelHeight);

                // Update source:
                Filter_.Set("source0", SourceCamera.targetTexture);

                // Reallocate the filter:
                Filter_.PreAllocate(FilterDrawInfo);

                // Grab the main output (always a RT):
                Texture = Filter_.Texture as RenderTexture;

                if (Texture == null)
                {
                    // This isn't a valid filter!
                    // It either had no nodes or e.g. e.g. a solid colour.
                    Debug.Log("Invalid filter was set to a FlatWorldUI - removed it.");
                    Filter_ = null;
                }
            }

            if (Filter_ == null)
            {
                // Clear draw info:
                FilterDrawInfo = null;

                // Revert to the camera's output:
                Texture = SourceCamera.targetTexture;
            }

            // Fire an imagechange event into the window:
            Dom.Event e = new Dom.Event("imagechange");
            e.SetTrusted(false);
            document.window.dispatchEvent(e);
        }
        /// <summary>Sets a filter to apply. This is what rasterising elements is all for!</summary>
        public void SetFilter(Loonim.SurfaceTexture tex)
        {
            Filter = tex;

            if (tex == null)
            {
                if (FilterDrawInfo != null)
                {
                    // Tidy it up:
                    tex.Clear();

                    FilterDrawInfo = null;
                }

                if (Renderer != null)
                {
                    // Update it now:
                    Output = Renderer.Texture;

                    if (Material != null)
                    {
                        // Hook up the output:
                        Material.SetTexture("_MainTex", Output);
                    }
                }
            }
            else
            {
                // Create the draw info (for GPU mode):
                FilterDrawInfo = new Loonim.DrawInfo();

                if (Renderer != null)
                {
                    // Update it now:
                    Filter.Set("source0", Renderer.Texture);

                    // Note that the next draw will update Output for us.
                }
            }
        }
        /// <summary>Updates the FlatWorldUI so it builds the mesh for this element.</summary>
        private void UpdateRenderer(LayoutBox box, float width, float height)
        {
            // - Set w/h to width and height:
            int w = (int)width;
            int h = (int)height;

            if (Renderer.SetDimensions(w, h))
            {
                // Output texture changed.

                if (Filter == null)
                {
                    // Update output:
                    Output = Renderer.Texture;

                    if (Material != null)
                    {
                        // Hook up the output:
                        Material.SetTexture("_MainTex", Output);
                    }
                }
                else
                {
                    Filter.Set("source0", Renderer.Texture);
                    // Output will be updated shortly.

                    // Always mark as changed:
                    Filter.Changed = true;
                }
            }

            // Redraw:
            if (Filter != null)
            {
                if (FilterDrawInfo != null)
                {
                    FilterDrawInfo.SetSize(w, h);
                }

                // Draw now:
                Output = Filter.Draw(FilterDrawInfo);

                if (Material != null)
                {
                    // Hook up the output:
                    Material.SetTexture("_MainTex", Output);
                }
            }

            // Temporarily set the positioning of box such that it's at the origin:
            float _x   = box.X;
            float _y   = box.Y;
            float _pX  = box.ParentOffsetLeft;
            float _pY  = box.ParentOffsetTop;
            int   _pos = box.PositionMode;

            // Clear:
            box.X = -box.Border.Left;
            box.Y = -box.Border.Top;
            box.ParentOffsetTop  = box.X;
            box.ParentOffsetLeft = box.Y;
            box.PositionMode     = PositionMode.Fixed;

            // Put the RenderData in the render only queue of *Renderer* and ask it to layout now:
            RenderableData _next = RenderData.Next;
            UpdateMode     _mode = RenderData.NextUpdateMode;

            // Clear:
            RenderData.Next           = null;
            RenderData.NextUpdateMode = UpdateMode.Render;

            // Queue:
            Renderer.Renderer.StylesToUpdate = RenderData;

            // Draw now!
            Renderer.Renderer.Update();

            // Restore (box):
            box.X = _x;
            box.Y = _y;
            box.ParentOffsetTop  = _pX;
            box.ParentOffsetLeft = _pY;
            box.PositionMode     = _pos;

            // Restore (queue):
            RenderData.Next           = _next;
            RenderData.NextUpdateMode = _mode;
        }