Hi my friends,
I am a beginner in react and babylonJS,
I am following the documents to learn about react and babylonJS , and tried to create a editor, I have put the babylonJS component in a parent component, that allows some interactions to pass the props to children, For example, to change the grid size and grid plane width, with user input, but I do not know how to change the babylonJS scene with the updated state, for example, I am trying to update the planesize and gridsize that I used to create the groundmesh. How can I achieve that?
Many thanks!
import React from 'react';
import { ArcRotateCamera, Color3,Vector3, HemisphericLight, MeshBuilder} from '@babylonjs/core';
import {GridMaterial} from '@babylonjs/materials';
import SceneComponent from './SceneComponent';
import './ViewPortComponent.css';
const ViewPortComponent = props => {
const [settingData, setSettings] = React.useState( {
planesize: 6000,
gridsize: 50,
} );
if (props.children.planesize !== settingData.planesize){
setSettings({
planesize: props.children.planesize,
gridsize: props.children.gridsize,
})
}
React.useEffect( () => {
})
const onSceneReady = (scene) => {
var camera = new ArcRotateCamera("mainCamera", 0, 0, 10, new Vector3(0, 0, 0), scene);
camera.setPosition(new Vector3(0, 500, -600));
const canvas = scene.getEngine().getRenderingCanvas();
camera.attachControl(canvas, true);
var light = new HemisphericLight("light", new Vector3(0, 500, 0), scene);
light.intensity = 0.7;
let ground = MeshBuilder.CreateGround("ground", {width: settingData.planesize, height: settingData.planesize}, scene);
let groundMaterial = new GridMaterial("groundMaterial", scene);
groundMaterial.majorUnitFrequency = 10;
groundMaterial.minorUnitVisibility = 0.5;
groundMaterial.gridRatio = settingData.gridsize;
groundMaterial.backFaceCulling = false;
groundMaterial.mainColor = new Color3(1, 1, 1);
groundMaterial.lineColor = new Color3(1.0, 1.0, 1.0);
groundMaterial.opacity = 0.98;
ground.material = groundMaterial;
}
const onRender = scene => {
}
return(
<SceneComponent antialias onSceneReady={onSceneReady} onRender={onRender} id='viewport'>
</SceneComponent>
)
}
export default ViewPortComponent;