AdamsLair / duality

a 2D Game Development Framework
https://adamslair.github.io/duality
MIT License
1.4k stars 289 forks source link

Feature/issues/767 #769

Closed SirePi closed 4 years ago

SirePi commented 4 years ago

This patches issue #767. By just moving the array copy to before altering the vertex data and operating on the copy, you end up with no hits on performances whatsoever.

Tested with the following custom renderer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Duality;
using Duality.Components;
using Duality.Drawing;
using Duality.Resources;

namespace Output
{
    [RequiredComponent(typeof(Transform))]
    public class CustomRenderer : Component, ICmpRenderer, ICmpUpdatable
    {
        [DontSerialize] private VertexC1P3[] vertices;

        public CustomRenderer()
        {
            this.vertices = Enumerable.Range(0, 4).Select(_ => new VertexC1P3()).ToArray();

            this.vertices[0].Pos = new Vector3(Vector2.Zero * 50, 0);
            this.vertices[1].Pos = new Vector3(Vector2.UnitY * 50, 0);
            this.vertices[2].Pos = new Vector3(Vector2.One * 50, 0);
            this.vertices[3].Pos = new Vector3(Vector2.UnitX * 50, 0);

            this.vertices[0].Color = ColorRgba.White;
            this.vertices[1].Color = ColorRgba.Red;
            this.vertices[2].Color = ColorRgba.Green;
            this.vertices[3].Color = ColorRgba.Blue;
        }

        void ICmpRenderer.Draw(IDrawDevice device)
        {
            device.AddVertices<VertexC1P3>(Material.SolidWhite, VertexMode.Quads, this.vertices);
        }

        void ICmpRenderer.GetCullingInfo(out CullingInfo info)
        {
            info.Position = this.GameObj.GetComponent<Transform>().Pos;
            info.Radius = 50;
            info.Visibility = VisibilityFlag.Group0;
        }

        void ICmpUpdatable.OnUpdate()
        {
            if (DualityApp.Mouse.ButtonHit(Duality.Input.MouseButton.Left))
            {
                Camera cam = this.Scene.FindComponent<Camera>();
                cam.RenderPickingMap(DualityApp.WindowSize, DualityApp.WindowSize, false);
                ICmpRenderer renderer = cam.PickRendererAt((int)DualityApp.Mouse.Pos.X, (int)DualityApp.Mouse.Pos.Y);

                if(renderer == this)
                    Logs.Game.Write("THIS!");
            }
        }
    }
}

(Sorry for the extra past commits..)