Skip to content
shuff57Public

About

A beginner-friendly 3D game engine for the web: real physics (Jolt) and three.js in a handful of globals

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

spaSHial

spaSHial is a small 3D game engine for the web. It draws with three.js and moves things with real rigid-body physics (Jolt, a physics engine used in commercial games). The whole API is a handful of globals: Solid, Group, world, camera, kb and mouse. No imports, no install, no build step.

It is the 3D sibling of moSHion, a 2D engine, and it grew out of the seSHion high-school JavaScript course, where students write games in a sandboxed iframe. If you know moSHion, the hooks, Group, the four collider words and the touch verbs (collides, overlaps and friends) are the same.

function setup() {
  world.gravity.y = -10;          // gravity starts off. Turn it on.
  new Floor();
  const box = new Cube(0, 4, 0);
  box.color = 'tomato';
  box.bounciness = 0.6;
}

function update() {
  // Space blows everything away from the middle
  if (kb.presses('space')) world.explodeAt(0, 0, 0, 6, 8);
}

Try it

  • The live site has a demo gallery.
  • The sandbox is an editor with 17 examples. Copy link puts the whole sketch in the address, so anyone can open it.
  • The docs have 33 pages and about 115 demos you can run and change, one for every kind of interaction: shapes, textures, models, levels from text, physics, forces, touching, joints, keyboard, mouse, ray casts, camera, lights, sound, saving and the HUD.

Run it on your computer

git clone https://github.com/shuff57/spashial
cd spashial
node scripts/serve.mjs          # then open http://127.0.0.1:8180/

It must be served over HTTP: browsers will not run modules or WebAssembly from file://. Any static server works if it sends Access-Control-Allow-Origin: * (the sketch runs in a sandboxed frame with an opaque origin, so everything it loads is a cross-origin request). scripts/serve.mjs does that.

Use it in your own page

The sketch runs in an iframe sandboxed without allow-same-origin, so it cannot read your page, its cookies or its storage. The host hands it the code with one message:

<iframe id="frame" sandbox="allow-scripts allow-downloads allow-modals"
        src="https://spashial.lefthanddev.com/runner.html"></iframe>
<script>
  addEventListener('message', (e) => {
    if (e.data?.source === 'preview-storage-request')
      frame.contentWindow.postMessage({ source: 'preview-storage-init', code: mySketch }, '*');
  });
</script>

embed.js wraps this (runSketch(iframe, code, { onReady, onLog, onError })). The messages and the hosting notes are in the embedding guide (page embedding). To host your own copy, copy runner.html, spashial.js, physics.js and vendor/ into one folder.

Publishing

  • Cloudflare Pages serves https://spashial.lefthanddev.com (project spashial, direct upload, like moSHion). It deploys by itself on every push to main (.github/workflows/cloudflare.yml, using the repository secrets CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID). To deploy by hand, run CLOUDFLARE_API_TOKEN=... CLOUDFLARE_ACCOUNT_ID=... npm run deploy:cloudflare. The token needs Cloudflare Pages: Edit. _headers gives every file access-control-allow-origin: *, which the sandboxed runner needs. Cloudflare redirects /sandbox.html to /sandbox; that is fine.

What is built, and what is not

Built and tested: six shapes plus Hull and TriangleMesh; colliders (dynamic, static, kinematic, none); gravity, bounce, friction, drag, gravity scale, speed limits and continuous collision; forces, impulses and torque; touch verbs; groups; hinge, ball, distance, slider and glue joints; collision groups; ray casts; keyboard and mouse; follow, orbit and free cameras; ambient, sun, point and spot lights with shadows; a 2D layer for scores and bars; textures (eight patterns drawn in code, or your own pictures); glTF models; text labels that float above a solid; Tiles, a level built from rows of text; sound (eight built-in sounds made in code, your own files, and playAt for sound from a place); saving (storeItem, getItem); solid.debug, which draws what really collides; and online play (multiplayer, online: rooms, moves and shared bodies) between two tabs of one browser, with no server.

Not built yet: online play between two computers (a room needs a server, and this site has none: the online play above works between two tabs of the same browser, and the tab that hosts is the room), height-map terrain, vehicles, ragdolls, motorised joints and model animations. Setting a property the engine does not know (solid.glow = 5) is quietly ignored, so check the reference before suspecting your code. Graphics speed on integrated graphics has not been measured; physics has (about 3 ms a step at 400 awake bodies on a fast desktop CPU).

Known limits

  • Dense piles of 'none' solids are the expensive case: they never sleep, and every overlapping pair costs a JavaScript contact callback each step (200 solids all inside each other: about 55 ms a step; one zone holding 200 coins: 1.4 ms). Keep sensors apart, or few.
  • Two TriangleMesh solids, one 'static' and one 'kinematic', report no contact with each other (Jolt does not collide a mesh against a mesh when neither is dynamic). A kinematic cube against a static mesh does.
  • The mouse picks by the collider's shape, not by scale (which is only how big it is drawn): a button widened with scale.x = 3 is still clicked where its collider is. Hide it, or resize it (w), to change what can be clicked. A hidden, fully transparent or scaled-to-nothing solid is looked through; a visible 'none' solid can be pointed at.
  • Pointer lock (mouse.lock()) needs allow-pointer-lock on the iframe; the docs frames do not have it.
  • The keyboard needs the preview to have focus: click the picture first.

What is in this repository

Path What it is
spashial.js The engine's sketch API on top of three.js: Solid, Group, world, camera, lights, input, the loop.
textures.js, sound.js The built-in texture patterns (drawn in code) and the sound system (Web Audio, with the eight built-in sounds synthesised in code).
online.js, online-local-host.js Online play: the sketch's half, and the host page's half with no server (two tabs of one browser).
physics.js The only file that talks to Jolt. It knows nothing about three.js.
runner.html The sandboxed page a sketch runs in.
vendor/ Jolt (WebAssembly, single-threaded) and three.js with its glTF loader, with their licences. Refresh with npm run vendor.
index.html, sandbox.html, site.css, embed.js The landing page, the editor, the shared styles and the helper that runs a sketch in a frame.
docs/ The docs site. docs/content/ is the pages; docs/spashial.d.ts is the typings. See docs/README.md.
demos/ Standalone sketches. demos/index.json lists them for the sandbox and the landing page.
tests/ See below.

Tests

npm install
npx playwright install chromium     # once, for the browser tests
npm test
Test What it checks
tests/physics.test.mjs The Jolt layer, in Node: resting heights, joints, ray casts, collision groups, meshes (250 tests).
tests/types.test.mjs docs/spashial.d.ts accepts the sketches students really write: tests/types-probe.ts is type-checked with tsc --strict.
tests/sound.test.mjs The sound system, in Node, against a fake Web Audio (57 tests).
tests/online.test.mjs Online play, in Node: the sketch half wired to the host half over a real BroadcastChannel (92 tests; 2 more compare with seSHion files and are skipped here).
tests/docs.test.mjs The docs agree with themselves, with docs/spashial.d.ts and with the running engine: every documented name exists and every documented default is true.
tests/browser.test.mjs The engine in a real browser, from inside the sandboxed frame: the sketch API, errors with line numbers, input, quality, shadows, every example.
tests/docs-demos.test.mjs Every docs demo, every "Try it" row and every standalone demo: runs it, performs its key presses and clicks, and checks the claim its page makes.
tests/docs-site.test.mjs The landing page, the docs site and the sandbox: autorun, one demo at a time, search, deep links, share links, no sideways scrolling on a phone.

Headless Chromium draws with software GL, so frame rate means nothing and no test asserts on it. If Playwright cannot find a browser, point CHROMIUM_PATH at one.

Adding to the engine

The engine's API is described in three places that must agree: spashial.js (what it does), docs/spashial.d.ts (the typings) and docs/content/reference-data.mjs (one row per name). Add the property to the first, then the other two, and tests/docs.test.mjs tells you which one is out of step. The steps are in docs/README.md. Where the engine's files came from, and how to bring in a newer copy, is in VENDORED.md.

Licence

MIT, see LICENSE. three.js and Jolt Physics (via JoltPhysics.js) are MIT too: see THIRD-PARTY.md.

About

A beginner-friendly 3D game engine for the web: real physics (Jolt) and three.js in a handful of globals

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages