ImmediateModePanel draws at incorrect location if not an immediate child of ImmediateModeCanvas

Avatar

Hi Freya! I understand that immediate mode panel and canvas are an experimental feature, but it was immediately very useful for me to use Shapes for drawing HUD elements, thank you for that! It's been working well but there was a small issue: when the panel is not an immediate child of the canvas, but is nested more deeply inside some more transforms (e.g. for more complex layouts), it then doesn't correctly calculates position / rotation / scale relatively to the canvas.

I even managed to fix the issue myself by calculating local position, etc. recursively, changing this code in ImmediateModePanel

if( drawRelativeToPanel ) {
Draw.PushMatrix();
Draw.Matrix *= Matrix4x4.TRS( tf.localPosition, tf.localRotation, tf.localScale );
}

to this

Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
Vector3 scale = Vector3.one;

RectTransform t = tf;
while (t.gameObject != ImCanvas.gameObject) {
position += t.localPosition;
rotation *= t.localRotation; scale = new Vector3(scale.x * t.localScale.x, scale.y * t.localScale.y, scale.z * t.localScale.z);
t = t.parent as RectTransform;
}

if (drawRelativeToPanel) {
Draw.PushMatrix();
Draw.Matrix *= Matrix4x4.TRS(position, rotation, scale);
}

Not the most elegant fix I guess, but it seems to do the trick, at least for the position (I didn't test if rotation and scale math is right). Would be nice to have this fixed properly in your original sources! Meanwhile I can use the custom versions of the classes. Thanks again for this!

Reporting a bug? please specify Unity version:
2022.3.9f1
Reporting a bug? please specify Shapes version:
4.3.1
Reporting a bug? please specify Render Pipeline:
URP