Skip to content

Commit 162f73f

Browse files
authored
chore: share a single sphere mesh; dedup primitive UV logic (#9614)
1 parent 3ec2cbf commit 162f73f

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

Explorer/Assets/DCL/Infrastructure/ECS/Unity/PrimitiveRenderer/MeshPrimitive/SpherePrimitive.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ namespace ECS.Unity.PrimitiveRenderer.MeshPrimitive
55
{
66
public class SpherePrimitive : IPrimitiveMesh
77
{
8+
// Sphere geometry is constant (fixed radius and tessellation) and is never mutated per instance
9+
// (MeshSetupSphere.Execute is a no-op), so every sphere shares this single immutable mesh.
10+
private static Mesh? sharedMesh;
11+
812
public Mesh Mesh { get; }
913

1014
public SpherePrimitive()
15+
{
16+
Mesh = sharedMesh ??= CreateSharedMesh();
17+
}
18+
19+
private static Mesh CreateSharedMesh()
1120
{
1221
var newMesh = new Mesh();
1322
SphereFactory.Create(ref newMesh);
14-
Mesh = newMesh;
23+
return newMesh;
1524
}
1625
}
1726
}

Explorer/Assets/DCL/Infrastructure/ECS/Unity/PrimitiveRenderer/Tests/InstantiatePrimitiveRenderingSystemShould.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public void UpdateInvalidatedRenderer(PBMeshRenderer input, PBMeshRenderer.MeshO
9191
system.Update(0);
9292

9393
//Act
94+
// Sphere primitives share a single immutable mesh, so the re-instantiated mesh is the same
95+
// object as the initial one. Clear the recorded calls so the assertion below counts only the
96+
// Execute triggered by re-instantiation and not the initial setup (which the mesh argument can
97+
// no longer disambiguate).
98+
setupMeshes[input.MeshCase].ClearReceivedCalls();
99+
94100
input.IsDirty = true;
95101
world.Get<PrimitiveMeshRendererComponent>(entity).PrimitiveMesh = null;
96102
system.Update(0);

Explorer/Assets/DCL/Infrastructure/Utility/Primitives/BoxFactory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ public static void Create(ref Mesh mesh)
232232

233233
public static void UpdateMesh(ref Mesh mesh, RepeatedField<float> boxUVs = null)
234234
{
235-
if (boxUVs is { Count: > 0 })
236-
mesh.SetUVs(0, PrimitivesUtility.FloatArrayToV2List(boxUVs, mesh.uv), 0, VERTICES_NUM);
237-
else
238-
mesh.SetUVs(0, defaultUVs, 0, VERTICES_NUM);
235+
PrimitivesUtility.ApplyUVs(mesh, boxUVs, defaultUVs, VERTICES_NUM);
239236
}
240237
}
241238
}

Explorer/Assets/DCL/Infrastructure/Utility/Primitives/PlaneFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public static void Create(ref Mesh mesh)
2828
vertices[6] = new Vector3(-halfSize.x, halfSize.y, 0);
2929
vertices[7] = new Vector3(-halfSize.x, -halfSize.y, 0);
3030

31-
Vector2[] uvs = PrimitivesBuffersPool.UVS.Rent(VERTICES_NUM);
3231
defaultUVs = new Vector2[VERTICES_NUM];
3332

3433
defaultUVs[0] = new Vector2(0f, 0f);
@@ -85,10 +84,7 @@ public static void Create(ref Mesh mesh)
8584

8685
public static void UpdateMesh(ref Mesh mesh, RepeatedField<float> planeUvs)
8786
{
88-
if (planeUvs is { Count: > 0 })
89-
mesh.SetUVs(0, PrimitivesUtility.FloatArrayToV2List(planeUvs, mesh.uv), 0, VERTICES_NUM);
90-
else
91-
mesh.SetUVs(0, defaultUVs, 0, VERTICES_NUM);
87+
PrimitivesUtility.ApplyUVs(mesh, planeUvs, defaultUVs, VERTICES_NUM);
9288
}
9389
}
9490
}

Explorer/Assets/DCL/Infrastructure/Utility/Primitives/PrimitivesUtility.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ public static Vector2[] FloatArrayToV2List(IList<float> uvs, Vector2[] uvsResult
1414

1515
return uvsResult;
1616
}
17+
18+
// Writes UV channel 0: the scene-provided UVs when present, otherwise the primitive's default set
19+
public static void ApplyUVs(Mesh mesh, IList<float>? customUVs, Vector2[] defaultUVs, int verticesNum)
20+
{
21+
if (customUVs is { Count: > 0 })
22+
mesh.SetUVs(0, FloatArrayToV2List(customUVs, mesh.uv), 0, verticesNum);
23+
else
24+
mesh.SetUVs(0, defaultUVs, 0, verticesNum);
25+
}
1726
}
1827
}

0 commit comments

Comments
 (0)