Skip to content

Instantly share code, notes, and snippets.

@bnolan
Last active October 3, 2019 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnolan/9601bc3d8372b3fb2d1381d5a7478122 to your computer and use it in GitHub Desktop.
Save bnolan/9601bc3d8372b3fb2d1381d5a7478122 to your computer and use it in GitHub Desktop.
Babylon 3.3 running in Ejecta
const BABYLON = require('babylonjs')
var createScene = function () {
// Create the scene space
var scene = new BABYLON.Scene(engine);
// Add a camera to the scene and attach it to the canvas
var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 4, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
// Add lights to the scene
var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
// This is where you create and manipulate meshes
var box = BABYLON.MeshBuilder.CreateBox("sphere", {}, scene);
// If we don't do this we get EXC_BAD_ACCESS in glDrawElements in Ejecta
box.convertToUnIndexedMesh()
scene.registerBeforeRender(() => {
box.rotation.y += 0.013
box.rotation.x += 0.007
})
return scene;
};
var w = window.innerWidth;
var h = window.innerHeight;
var scale = window.devicePixelRatio;
var w2 = w/2;
var h2 = h/2;
var canvas = document.getElementById('canvas');
if (!canvas) {
canvas = document.createElement('canvas')
document.body.appendChild(canvas)
}
canvas.width = w * scale;
canvas.height = h * scale;
canvas.style.width = w;
canvas.style.height = h;
canvas.clientWidth = w
canvas.clientHeight = h
let options = { disableWebGL2Support: true }
var engine = new BABYLON.Engine(canvas, false, options); // Generate the BABYLON 3D engine
var scene = createScene()
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment