Exemplo n.º 1
0
        public void SetViewportParams(int2 scroll, float zoom)
        {
            // In HiDPI windows we follow Apple's convention of defining window coordinates as for standard resolution windows
            // but to have a higher resolution backing surface with more than 1 texture pixel per viewport pixel.
            // We must convert the surface buffer size to a viewport size - in general this is NOT just the window size
            // rounded to the next power of two, as the NextPowerOf2 calculation is done in the surface pixel coordinates
            var scale             = Window.WindowScale;
            var surfaceBufferSize = Window.SurfaceSize.NextPowerOf2();
            var bufferSize        = new Size((int)(surfaceBufferSize.Width / scale), (int)(surfaceBufferSize.Height / scale));

            // PERF: Calling SetViewportParams on each renderer is slow. Only call it when things change.
            // If zoom evaluates as different due to floating point weirdness that's OK, it will be going away soon
            if (lastBufferSize != bufferSize || lastScroll != scroll || lastZoom != zoom)
            {
                if (lastBufferSize != bufferSize)
                {
                    SpriteRenderer.SetViewportParams(bufferSize, 0f, 0f, 1f, int2.Zero);
                }

                WorldSpriteRenderer.SetViewportParams(bufferSize, depthScale, depthOffset, zoom, scroll);
                WorldModelRenderer.SetViewportParams(bufferSize, zoom, scroll);

                lastBufferSize = bufferSize;
                lastScroll     = scroll;
                lastZoom       = zoom;
            }
        }
Exemplo n.º 2
0
        public void BeginWorld(Rectangle worldViewport)
        {
            if (renderType != RenderType.None)
            {
                throw new InvalidOperationException("BeginWorld called with renderType = {0}, expected RenderType.None.".F(renderType));
            }

            BeginFrame();

            var worldBufferSize = worldViewport.Size.NextPowerOf2();

            if (worldSprite == null || worldSprite.Sheet.Size != worldBufferSize)
            {
                if (worldBuffer != null)
                {
                    worldBuffer.Dispose();
                }

                // Render the world into a framebuffer at 1:1 scaling to allow the depth buffer to match the artwork at all zoom levels
                worldBuffer = Context.CreateFrameBuffer(worldBufferSize);

                // Pixel art scaling mode is a customized bilinear sampling
                worldBuffer.Texture.ScaleFilter = TextureScaleFilter.Linear;
            }

            if (worldSprite == null || worldViewport.Size != worldSprite.Bounds.Size)
            {
                var worldSheet = new Sheet(SheetType.BGRA, worldBuffer.Texture);
                worldSprite = new Sprite(worldSheet, new Rectangle(int2.Zero, worldViewport.Size), TextureChannel.RGBA);
            }

            worldBuffer.Bind();

            if (worldBufferSize != lastWorldBufferSize || lastWorldViewport != worldViewport)
            {
                var depthScale = worldBufferSize.Height / (worldBufferSize.Height + depthMargin);
                WorldSpriteRenderer.SetViewportParams(worldBufferSize, depthScale, depthScale / 2, worldViewport.Location);
                WorldModelRenderer.SetViewportParams(worldBufferSize, worldViewport.Location);

                lastWorldViewport   = worldViewport;
                lastWorldBufferSize = worldBufferSize;
            }

            renderType = RenderType.World;
        }
Exemplo n.º 3
0
        public void BeginWorld(Rectangle worldViewport)
        {
            if (renderType != RenderType.None)
            {
                throw new InvalidOperationException($"BeginWorld called with renderType = {renderType}, expected RenderType.None.");
            }

            BeginFrame();

            if (worldSheet == null)
            {
                throw new InvalidOperationException($"BeginWorld called before SetMaximumViewportSize has been set.");
            }

            if (worldSprite == null || worldViewport.Size != lastWorldViewportSize)
            {
                // Downscale world rendering if needed to fit within the framebuffer
                var vw = worldViewport.Size.Width;
                var vh = worldViewport.Size.Height;
                var bw = worldSheet.Size.Width;
                var bh = worldSheet.Size.Height;
                worldDownscaleFactor = 1;
                while (vw / worldDownscaleFactor > bw || vh / worldDownscaleFactor > bh)
                {
                    worldDownscaleFactor++;
                }

                var s = new Size(vw / worldDownscaleFactor, vh / worldDownscaleFactor);
                worldSprite           = new Sprite(worldSheet, new Rectangle(int2.Zero, s), TextureChannel.RGBA);
                lastWorldViewportSize = worldViewport.Size;
            }

            worldBuffer.Bind();

            if (lastWorldViewport != worldViewport)
            {
                WorldSpriteRenderer.SetViewportParams(worldSheet.Size, worldDownscaleFactor, depthMargin, worldViewport.Location);
                WorldModelRenderer.SetViewportParams(worldSheet.Size, worldViewport.Location);

                lastWorldViewport = worldViewport;
            }

            renderType = RenderType.World;
        }
        public void SetViewportParams(int2 scroll, float zoom)
        {
            // PERF: Calling SetViewportParams on each renderer is slow. Only call it when things change.
            var resolutionChanged = lastResolution != Resolution;

            if (resolutionChanged)
            {
                lastResolution = Resolution;
                SpriteRenderer.SetViewportParams(lastResolution, 0f, 0f, 1f, int2.Zero);
            }

            // If zoom evaluates as different due to floating point weirdness that's OK, setting the parameters again is harmless.
            if (resolutionChanged || lastScroll != scroll || lastZoom != zoom)
            {
                lastScroll = scroll;
                lastZoom   = zoom;
                WorldSpriteRenderer.SetViewportParams(lastResolution, depthScale, depthOffset, zoom, scroll);
                WorldModelRenderer.SetViewportParams(lastResolution, zoom, scroll);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="scroll"></param>
        /// <param name="zoom"></param>
        public void SetViewportParams(Int2 scroll, float zoom)
        {
            var resolutionChanged = lastResolution != Resolution;

            if (resolutionChanged)
            {
                lastResolution = Resolution;
                RgbaSpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, Int2.Zero);
                SpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, Int2.Zero);
                RgbaColorRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, Int2.Zero);
            }

            //If zoom evaluates as different due to floating point weirdness that's ok,setting the parameters again is harmless.
            if (resolutionChanged || lastScroll != scroll || lastZoom != zoom)
            {
                lastScroll = scroll;
                lastZoom   = zoom;

                WorldRgbaSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
                WorldSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
                WorldModelRenderer.SetViewportParams(Resolution, zoom, scroll);
                WorldRgbaColorRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
            }
        }