Jump to content

Hot reloading engine


WhoAteDaCake
 Share

Recommended Posts

// @flow
// $FlowIgnore
import * as BABYLON from 'babylonjs';
import shortid from 'shortid';

type Color = {
  r: number,
  b: number,
  g: number,
};

export default class Scene {
  _scene: BABYLON.Scene;
  _camera: BABYLON.ArcRotateCamera;
  _light: BABYLON.HemisphericLight;

  constructor(engine: BABYLON.Engine, canvas: HTMLElement) {
    const scene = new BABYLON.Scene(engine);
    scene.clearColor = new BABYLON.Color4(0.5, 0.8, 0.6, 0.8);

    const camera = new BABYLON.ArcRotateCamera('Camera', 1.5 * Math.PI, Math.PI / 8, 50, BABYLON.Vector3.Zero(), scene);
    camera.setTarget(BABYLON.Vector3.Zero());
    camera.attachControl(canvas, false);

    const light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(1, 1, 1), scene);
    light.intensity = 0.5;

    const sphereMaterial = new BABYLON.StandardMaterial('texture1', scene);
    sphereMaterial.alpha = 1;
    // sphereMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
    sphereMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0.5);

    const boxCfg = {
      size: 5,
      updatable: true,
    };

    const sphere = BABYLON.MeshBuilder.CreateBox('sphere1', boxCfg, scene);
    sphere.material = sphereMaterial;
    sphere.position.y = 5;

    this._scene = scene;
    this._camera = camera;
    this._light = light;
  }
  render = () => {
    this._scene.render();
  }
  dispose = () => {
    this._scene.dispose();
  }
}


import { Engine } from 'babylonjs';
import Scene from './Scene';

export default class RenderEngine {
  constructor(container) {
    this._container = container;
  }
  animate() {
    this._engine.runRenderLoop(this._scene.render);
    window.addEventListener('resize', this._engine.resize);
  }
  createScene() {
    this._scene = new Scene(this._engine, this._canvas);
  }
  render = () => {
    this._canvas = document.createElement('canvas');
    this._container.appendChild(this._canvas);
    this._engine = new Engine(this._canvas, false);
    this.createScene();
    this.animate();
  }
  reRender = () => {
    this._engine.dispose();
    this._container.removeChild(this._canvas);

    this.render();
  }
}


const root = document.getElementById('root');
const engine = new RenderEngine(root);
engine.render();

if (module.hot) {
  module.hot.accept('./RenderEngine', () => {
    engine.reRender();
  });
}

I am currently learning babylon.js and with my set up I have hot reloading, so when any files are changed, engine.reRender() will be called.

To test it out I change up color of sphereMaterial. It creates new canvas, but for some reason it still shows the old colors...

Is there any way to clear out the scene and engine to load the webgl with new configuration?

Thank you.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...