Draw.Line renders incorrectly on WebGL

Avatar
  • updated

When drawing lines using immediate mode (Draw.Line) with GPU instancing active on a WebGL/WebGPU build, lines are rendered at wrong positions or not visible at all. The issue does not reproduce in the Unity Editor or on Desktop builds.

Our AI found this fix for the bug that works for us. We want to share so it gets fixed upstream.

Bug:
_PointStart and _PointEnd declared as float3 inside UNITY_INSTANCING_BUFFER produce incorrect vertex positions when GPU instancing is active on WebGL. Lines either render at wrong positions or not at all.

Root cause:
WebGL 2.0 uses std140 uniform buffer layout, which pads float3 to 16 bytes (float4-sized). Unity's PROP_DEF(float3, ...) / PROP(...) accessor doesn't account for this, causing misaligned reads in the instancing buffer. Not reproduced on Direct3D, which uses different constant buffer packing rules.

Fix:
In both Line 2D Core.cginc and Line 3D Core.cginc, change the PROP_DEF declarations and add a .xyz swizzle on read:

  // Before
  PROP_DEF(float3, _PointStart)
  PROP_DEF(float3, _PointEnd)
  ...
  half3 aLocal = PROP(_PointStart);
  // After
  PROP_DEF(float4, _PointStart)
  PROP_DEF(float4, _PointEnd)
  ...
  half3 aLocal = PROP(_PointStart).xyz;

This matches the actual float4-padded layout WebGL 2.0 expects and restores correct line rendering. Any other float3 properties in instancing buffers may be affected by the same issue.

Best regards,

Hendrik

Reporting a bug? please specify Unity version:
6000.3.3f1
Reporting a bug? please specify Shapes version:
4.5.1
Reporting a bug? please specify Render Pipeline:
URP