Skip to content
vrhermit

vrhermit

Code & Writing by Joseph Simpson

  • Canvatorium
  • Technical
  • Professional
  • Personal
vrhermit
vrhermit
Code & Writing by Joseph Simpson

Babylon JS with WebXR support

BabylonJS

This is just a simple Hello World scene that I sometimes use a template for new projects.

  • Scene
  • Code

Open 3D content in new tab

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Babylon JS: Basic Scene</title>

    <script src="https://preview.babylonjs.com/babylon.js"></script>

    <style>
    html,
    body {
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }

    #renderCanvas {
        width: 100%;
        height: 100%;
        touch-action: none;
    }
    </style>
</head>

<body>
    <div>
        <canvas id="renderCanvas"></canvas>
        <script>
        window.addEventListener("DOMContentLoaded", async function() {
            var canvas = document.getElementById("renderCanvas");
            var engine = new BABYLON.Engine(canvas, true);
            var createScene = async function() {
                var scene = new BABYLON.Scene(engine);
                var camera = new BABYLON.FreeCamera(
                    "camera1",
                    new BABYLON.Vector3(0, 5, -10),
                    scene
                );
                camera.attachControl(canvas, true);
                camera.setTarget(BABYLON.Vector3.Zero());
                camera.attachControl(canvas, false);

                var light = new BABYLON.HemisphericLight(
                    "light1",
                    new BABYLON.Vector3(0, 1, 0),
                    scene
                );

                var ground = BABYLON.Mesh.CreateGround("ground1", 20, 20, 2, scene);
                var material = new BABYLON.StandardMaterial(scene);
                material.alpha = 1;
                material.diffuseColor = new BABYLON.Color3.FromHexString("#9ba8b8");
                ground.material = material;

                var cube = BABYLON.MeshBuilder.CreateBox("box", {
                    height: 1,
                    width: 1,
                    depth: 1
                });
                cube.position = new BABYLON.Vector3(0, 1, 0);

                // Add XR support
                var experience = await scene.createDefaultXRExperienceAsync({
                    floorMeshes: [ground]
                });

                return scene;
            };

            // Start the render loop
            var scene = await createScene();
            engine.runRenderLoop(function() {
                scene.render();
            });
            window.addEventListener("resize", function() {
                engine.resize();
            });
        });
        </script>
    </div>
</body>

</html>

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • More
  • Click to print (Opens in new window) Print
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Like this:

Like Loading...

Work with Me

Ready to streamline your workflows and enhance your digital presence?

Do you want to take your first step into Spatial Computing?

Discover how Radical Application Development can help transform your business.

Get in touch or learn more at radicalappdev.com

Mastodon Mastodon
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy

Follow my work

Github Linkedin YouTube

© 2025 Joseph Simpson | Radical Application Development

  • Canvatorium
  • Technical
  • Professional
  • Personal
Search
%d