CameraGizmo: reuse materials

Currently a CameraGizmo creates 13 materials by default, see console of this playground:

Most of which are colorShader material for lines, so it can be reused, at least inside this CameraGizmo. This playground shows an example reducing material count to 2:

And a patch here:

diff --git a/packages/dev/core/src/Gizmos/cameraGizmo.ts b/packages/dev/core/src/Gizmos/cameraGizmo.ts
index 4d185d17e8..8ca4b89906 100644
--- a/packages/dev/core/src/Gizmos/cameraGizmo.ts
+++ b/packages/dev/core/src/Gizmos/cameraGizmo.ts
@@ -240,18 +240,22 @@ export class CameraGizmo extends Gizmo implements ICameraGizmo {
         const root = new Mesh("rootCameraGizmo", scene);
         const mesh = new Mesh(root.name, scene);
         mesh.parent = root;
+        let material: Exclude<Mesh["material"], null> | undefined = undefined;
 
         for (let y = 0; y < 4; y += 2) {
             for (let x = 0; x < 4; x += 2) {
-                let line = CreateLines("lines", { points: [new Vector3(-1 + x, -1 + y, -1), new Vector3(-1 + x, -1 + y, 1)], colors: [linesColor, linesColor] }, scene);
+                let line = CreateLines("lines", { points: [new Vector3(-1 + x, -1 + y, -1), new Vector3(-1 + x, -1 + y, 1)], colors: [linesColor, linesColor], material }, scene);
+                if (!material) {
+                    material = line.material!;
+                }
                 line.parent = mesh;
                 line.alwaysSelectAsActiveMesh = true;
                 line.isPickable = false;
-                line = CreateLines("lines", { points: [new Vector3(-1, -1 + x, -1 + y), new Vector3(1, -1 + x, -1 + y)], colors: [linesColor, linesColor] }, scene);
+                line = CreateLines("lines", { points: [new Vector3(-1, -1 + x, -1 + y), new Vector3(1, -1 + x, -1 + y)], colors: [linesColor, linesColor], material }, scene);
                 line.parent = mesh;
                 line.alwaysSelectAsActiveMesh = true;
                 line.isPickable = false;
-                line = CreateLines("lines", { points: [new Vector3(-1 + x, -1, -1 + y), new Vector3(-1 + x, 1, -1 + y)], colors: [linesColor, linesColor] }, scene);
+                line = CreateLines("lines", { points: [new Vector3(-1 + x, -1, -1 + y), new Vector3(-1 + x, 1, -1 + y)], colors: [linesColor, linesColor], material }, scene);
                 line.parent = mesh;
                 line.alwaysSelectAsActiveMesh = true;
                 line.isPickable = false;

1 Like

Or to make it better, merge all lines to one mesh:

2 Likes

cc @Cedric

Merging all lines in 1 mesh is the way to go! Can you please open a PR @kzhsw ?

1 Like

Sure:

4 Likes

Great improvement!

1 Like

Also, any thoughts on doing similar thing on BoundingBoxGizmo?

looks to me like it could be a line system for sure.

Anywhere it can be done :slight_smile:

3 Likes