Duplicate immediate mode rendering when creating objects at runtime
While creating objects at runtime that contains an immediate mode rendering script, I get duplicate rendering for as many objects as I have (i.e. if I add 5 objects at runtime, I'll end up with each object being rendered 5 times, so it will be 25 renders total).
Steps:
1. Create a new project using Unity 2021.1.15f1 and URP
2. To simplify things, create an empty scene, add a Camera at [0, 0, -2]
3. Create an empty GameObject and add the following script:
using System.Collections;
using System.Collections.Generic;
using Shapes;
using UnityEngine;
using ScaleMode = Shapes.ScaleMode;
public class ShapesTest : ImmediateModeShapeDrawer {
[SerializeField]
private float dotRadius = 4.0f;
[SerializeField]
private float innerRadius = 8.0f;
[SerializeField]
private float innerThickness = 2.0f;
[SerializeField]
private float outerRadius = 12.0f;
[SerializeField]
private float outerThickness = 2.0f;
public Color color = Color.magenta;
public override void DrawShapes(Camera cam) {
using (Draw.Command(cam)) {
Draw.ResetAllDrawStates();
Draw.BlendMode = ShapesBlendMode.Additive;
Draw.Thickness = innerThickness;
Draw.LineGeometry = LineGeometry.Flat2D;
Draw.SizeSpace = ThicknessSpace.Pixels;
Draw.ThicknessSpace = ThicknessSpace.Pixels;
Draw.RadiusSpace = ThicknessSpace.Pixels;
Draw.Position = transform.position;
Draw.ScaleMode = ScaleMode.Uniform;
Draw.Color = color;
Draw.Disc(Vector3.zero, Quaternion.identity, dotRadius);
Draw.Ring(Vector3.zero, Quaternion.identity, innerRadius);
Draw.Thickness = outerThickness;
Draw.Ring(Vector3.zero, Quaternion.identity, outerRadius);
}
}
}
4. Run the scene
5. While running, clone the object with the script 4 times (or just create a new one and attach the script, cloning the object is not the issue as far as I can see)
6. Run the Frame debugger, and check Shapes command, you should see the object being rendered 25 times instead of the expected 5
managed to reproduce it when using the latest release, but it doesn't happen anymore in my internal dev branch, so it looks like this has been accidentally fixed due to another issue :)
there was a major issue I found that this likely was caused by, even though the fix was intended for something else