Hello everyone!
First time on this forum, this morning I made a dynamic grid system, which can be reused easily ( I don’t like to have to randomly change the values in the code and RS my server, I prefer to see in the browser the right values and test them )
-
Adjustable width and height
-
Customizable grid density (gridRatio)
-
Major grid lines frequency control
-
Opacity control
-
Customizable colors for main grid and lines
-
Real-time updates via GUI control panel
I’d like to share the code
gridSystem.js
const gridConfig = {
width: 10,
height: 10,
gridRatio: 1,
majorUnit: 5,
gridOpacity: 0.5,
mainColor: "#FFFFFF",
lineColor: "#808080"
};
class GridSystem {
constructor(scene) {
this.scene = scene;
this.ground = null;
this.createGrid();
this.setupGUI();
}
createGrid() {
if (this.ground) {
this.ground.dispose();
}
this.ground = BABYLON.MeshBuilder.CreateGround("ground", {
width: gridConfig.width,
height: gridConfig.height,
subdivisions: 1
}, this.scene);
if (BABYLON.GridMaterial) {
const gridMaterial = new BABYLON.GridMaterial("gridMaterial", this.scene);
gridMaterial.opacity = gridConfig.gridOpacity;
gridMaterial.mainColor = BABYLON.Color3.FromHexString(gridConfig.mainColor);
gridMaterial.lineColor = BABYLON.Color3.FromHexString(gridConfig.lineColor);
gridMaterial.gridRatio = gridConfig.gridRatio;
gridMaterial.majorUnitFrequency = gridConfig.majorUnit;
gridMaterial.backFaceCulling = false;
this.ground.material = gridMaterial;
} else {
console.error("GridMaterial isn't loaded !");
const backupMaterial = new BABYLON.StandardMaterial("backupMaterial", this.scene);
backupMaterial.wireframe = true;
this.ground.material = backupMaterial;
}
}
updateGridMaterial() {
if (this.ground && this.ground.material && this.ground.material.getClassName() === "GridMaterial") {
this.ground.material.opacity = gridConfig.gridOpacity;
this.ground.material.mainColor = BABYLON.Color3.FromHexString(gridConfig.mainColor);
this.ground.material.lineColor = BABYLON.Color3.FromHexString(gridConfig.lineColor);
this.ground.material.gridRatio = gridConfig.gridRatio;
this.ground.material.majorUnitFrequency = gridConfig.majorUnit;
}
}
setupGUI() {
const gui = new dat.GUI();
const gridFolder = gui.addFolder('Grid Settings');
gridFolder.add(gridConfig, 'width', 1, 50).onChange(() => this.createGrid());
gridFolder.add(gridConfig, 'height', 1, 50).onChange(() => this.createGrid());
gridFolder.add(gridConfig, 'gridRatio', 0.1, 5).step(0.1).onChange(() => this.updateGridMaterial());
gridFolder.add(gridConfig, 'majorUnit', 1, 10).step(1).onChange(() => this.updateGridMaterial());
gridFolder.add(gridConfig, 'gridOpacity', 0, 1).onChange(() => this.updateGridMaterial());
gridFolder.addColor(gridConfig, 'mainColor').onChange(() => this.updateGridMaterial());
gridFolder.addColor(gridConfig, 'lineColor').onChange(() => this.updateGridMaterial());
gridFolder.open();
}
}
then simply call it up in your app.js
const gridSystem = new GridSystem(scene);
You must also add 2 scripts to your html
babylon.js.materials.min.js and dat-gui
<script src="https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js"></script>
Hope it helps people!