logo
SpaceRock

rendering · physics-puzzle

SpaceRock

1
Unlock

Install with the CLI:

bjs download SpaceRock

SpaceRock

Turns a plain MeshPrimitive sphere into a lumpy, faceted rock with mineral veins.

What it does

SpaceRock keeps the sphere and reshapes it, instead of swapping in a rock model. Once, on the first frame the mesh exists, each vertex is read as a direction from the centre and moved to wherever the rock's surface lies that way. The sphere stays the entity's position, its collision radius and its pooled mesh, so physics and pooling carry on as before. The UV seam's duplicate vertices share a position, so they get the same direction and the same push, and the rock has no cracks.

Colour comes from the direction too: a mottled base threaded with veins of mineralColor. The surface is the seeded asteroid shape the space shooter's rocks use, so rocks in both games are one family. craggy blends from the bare sphere at 0 to the full surface at 1. A lumpy rock pokes out past a sphere of the same size, so reach sets where its farthest lobe lands, as a multiple of the radius. That keeps the silhouette close to the radius the physics uses.

By default each pool slot takes its seed from its entity id. A stream of pooled rocks is a stream of different rocks, and each slot keeps its shape for life. Set seed to give every rock the same shape.

Setup

The engine work ships with the component, in adapter/register.ts. A project made with bjs create installs it for you: src/main.ts calls every adapter/register.ts that bjs download puts in the project, before the scene loads. If you boot the game from your own code, call it once, right after building the adapter:

TypeScript
import { registerSpaceRockExtension } from './Components/SpaceRock/adapter/register';

const adapter = new BabylonAdapter();
registerSpaceRockExtension(adapter);
const game = new ArcadeGame(adapter, { … });

Without it the rock stays a sphere and the System warns once. scale still applies.

Use it in a scene

An asteroid from SpaceGolf:

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add SpaceRock
    bjs download SpaceRock

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "Asteroid": {
    "components": {
      "MeshPrimitive": {
        "primitive": "sphere",
        "diameter": 2,
        "segments": 12,
        "material": { "diffuseColor": [1, 1, 1], "specularColor": [0.1, 0.1, 0.1] }
      },
      "SpaceRock": {
        "reach": 1.1,
        "baseColor": [0.43, 0.37, 0.33],
        "mineralColor": [0.95, 0.66, 0.3],
        "mineralAmount": 0.07
      }
    }
  }
}

The material stays white. The rock's colours are vertex colours, and Babylon multiplies them by the material's colour.

Props

  • seed (number, default 0) — shape seed. 0 derives one from the entity id, so each pool slot differs.
  • craggy (number, default 0.85) — 0 is the bare sphere, 1 the full craggy surface.
  • reach (number, default 1.2) — where the farthest lobe lands, as a multiple of the sphere's radius.
  • scale (number, default 1) — uniform visual scale on the mesh. The physics radius does not follow it.
  • baseColor (RGB, default [0.46, 0.42, 0.4]) — the mottled base.
  • mineralColor (RGB, default [0.3, 0.95, 0.85]) — vein colour.
  • mineralAmount (number, default 0.18) — how much of the surface is vein, 0 to 1.
  • flat (boolean, default true) — faceted shading. Each facet catches the light on its own, which is what makes a small rock read as rock and not as a dented ball.

Events

None either way.

Dependencies

  • MeshPrimitive — holds the vertices it works on. Without one beside it, SpaceRock does nothing.

Notes

  • Shape and colour are set once per mesh. A parked rock keeps its shape, and a slot handed out again is not reshaped. Only scale is read live, and it is applied again each time a slot is handed out.
  • scale only changes the look. The SpaceGolf director sets an asteroid's GravityMass radius and its scale together, from the level's radius.
  • Made for spheres. Any other primitive is reshaped as if its radius were 0.5.
  • The plugin is Babylon only. Under Three.js or Babylon Lite the rocks stay spheres.

More like this

MeshPrimitive
AxisViewCamera
BlackHole
CaptureZone
DirectionalLight
EnvironmentTexture
Flash
GravityBody
GravityLens
GravityMass
Hand
HemisphericLight
LensFlare
MassDriver
Mesh
MotionTrail
NebulaSky
PointerDrag
Renderable
RoadTreadmill
Shadow
ShipDamageFX
Skybox
SmokeScreen
SpaceDust
SpaceGolfDirector
SpaceGolfHUD
SpaceShooterFX
Spin
ToneMapping
TrajectoryPreview
WhiteHole
Wormhole

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search