Thats how I tried to make it work btw:
import * as BABYLON from ‘babylonjs’;
export {
engine,
scene,
canvas
};
export {
engineOptions,
adaptToDeviceRatio,
onSceneMount,
width,
height
};
export default {
scene: BABYLON.Scene,
engine: BABYLON.Engine,
canvas: HTMLCanvasElement,
onResizeWindow() {
if (this.engine) {
this.engine.resize();
}
},
componentDidMount() {
this.engine = new BABYLON.Engine(
this.canvas,
true,
this.props.engineOptions,
this.props.adaptToDeviceRatio
);
let scene = new BABYLON.Scene(this.engine);
this.scene = scene;
if (typeof this.props.onSceneMount === 'function') {
this.props.onSceneMount({
scene,
engine: this.engine,
canvas: this.canvas
});
} else {
console.error('onSceneMount function not available');
}
// Resize the babylon engine when the window is resized
window.addEventListener('resize', this.onResizeWindow);
},
componentWillUnmount() {
window.removeEventListener('resize', this.onResizeWindow);
},
onCanvasLoaded(c) {
if (c !== null) {
this.canvas = c;
}
},
render() {
// 'rest' can contain additional properties that you can flow through to canvas:
// (id, className, etc.)
let { width, height, rest } = this.props;
let opts;
if (width !== undefined && height !== undefined) {
opts.width = width;
opts.height = height;
}
return (
<canvas
{...opts}
ref={this.onCanvasLoaded}
/>
);
}
}
And:
import * as React from ‘react’;
import * as BABYLON from ‘babylonjs’;
import BabylonScene from ‘./babylon.js’; // import the component above linking to file we just created.
class PageWithScene extends React.Component{
onSceneMount(e) {
const { canvas, scene, engine } = e;
// This creates and positions a free camera (non-mesh)
let camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
// This targets the camera to scene origin
camera.setTarget(BABYLON.Vector3.Zero());
// This attaches the camera to the canvas
camera.attachControl(canvas, true);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
let light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
// Our built-in 'sphere' shape. Params: name, subdivs, size, scene
let sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
// Move the sphere upward 1/2 its height
sphere.position.y = 1;
// Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
let ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
engine.runRenderLoop(() => {
if (scene) {
scene.render();
}
});
}
render() {
return (
<div>
<BabylonScene onSceneMount={this.onSceneMount} />
</div>
);
}
}