Complete 3D rendering system using Three.js for meshes, materials, lighting, and cameras
The 3D Graphics API provides a complete Three.js-powered 3D rendering system with mesh management, primitive shapes, material system, lighting, and GLB model loading.
Creates a cube mesh with the specified properties.
const cubeId = createCube(2, 0xff0000, [0, 1, 0], { metalness: 0.5 });
Creates a sphere mesh.
const ballId = createSphere(1.5, 0x00ff00, [5, 2, 0], 16);
Creates a flat plane mesh (ground, walls, etc.).
const groundId = createPlane(100, 100, 0x808080, [0, -1, 0]);
Creates a cylinder (or truncated cone when radii differ).
radiusTop for a true cylinder)const pillar = createCylinder(0.5, 0.5, 4, 0x888888, [3, 2, 0]);
const barrel = createCylinder(0.6, 0.8, 1.2, 0x8B4513, [0, 0.6, 5]);
Creates a cone โ useful for projectiles, trees, directional indicators, and hat shapes.
const tree = createCone(1.5, 4, 0x228b22, [0, 2, -5]);
const bullet = createCone(0.1, 0.4, 0xffff00, [px, py, pz]);
Creates a capsule (cylinder capped with hemispheres) โ ideal for humanoid character bodies and rounded obstacles.
const player = createCapsule(0.4, 1.8, 0x4488ff, [0, 0.9, 0]);
Creates a torus (donut shape) โ good for rings, portals, halos, and pickup items.
const ring = createTorus(2, 0.3, 0xffd700, [0, 5, 0]); // golden ring portal
Retrieves a mesh object by its ID for direct manipulation.
const mesh = getMesh(cubeId);
mesh.rotation.y += 0.01;
Removes a mesh from the scene and frees its GPU resources. Both names are identical โ either works.
destroyMesh(cubeId); // or removeMesh(cubeId) โ both are identical
Shows or hides a mesh without removing it from the scene.
true to show, false to hidesetMeshVisible(ghostId, false); // hide without destroying
setMeshVisible(ghostId, true); // show again
Moves a mesh to an absolute world position.
setPosition(enemyMesh, enemy.x, enemy.y, enemy.z);
Sets the absolute rotation of a mesh (Euler angles in radians).
setRotation(wheelId, 0, 0, Math.PI / 2);
Adds to the current rotation of a mesh each frame โ ideal for spin animations.
dt * speed)// In update(dt):
rotateMesh(orbId, 0, dt * 1.5, 0); // spin around Y axis
Scales a mesh along each axis.
setScale(hitboxId, 2, 0.5, 2); // flatten and widen
Returns the current world position of a mesh.
const [x, y, z] = getPosition(bulletMesh);
Loads a GLB/GLTF 3D model asynchronously.
const modelId = await loadModel("/models/character.glb", [0, 0, 0], 2);
Sets the direction of the scene's main directional light (like the sun). The vector points toward the light source.
setLightDirection(-0.5, -1, -0.3); // angled overhead sun
Sets the colour of the main directional light.
setLightColor(0xffffdd); // warm daylight
setLightColor(0x4444aa); // cool moonlight
Sets the overall scene ambient lighting โ fills in shadows and ensures dark areas are still visible.
setAmbientLight(0x404060, 0.8); // cool, slightly dim fill light
Creates a positional point light that casts light in all directions from a specific world position โ flames, lamps, explosions.
setPointLightPosition, removeLight)const torchLight = createPointLight(0xff8800, 2, 15, torchX, torchY, torchZ);
// Later, move it:
setPointLightPosition(torchLight, newX, newY, newZ);
// Remove:
removeLight(torchLight);
Enables distance fog, fading objects to color between near and far world units.
setFog(0x202040, 30, 80); // atmospheric night fog
clearFog(); // remove fog entirely
Physically Based Rendering properties can be applied to any mesh after creation to achieve metallic, rough, or reflective looks.
Applies PBR material properties to an existing mesh. Works best when an image skybox is set (provides IBL reflections).
// Mirror-like chrome ball
const chrome = createSphere(1, 0xffffff, [0, 1, 0]);
setPBRProperties(chrome, { metalness: 1.0, roughness: 0.0 });
// Brushed metal
const brushed = createSphere(1, 0xaaaaaa, [3, 1, 0]);
setPBRProperties(brushed, { metalness: 1.0, roughness: 0.5 });
// Matte rubber
const rubber = createSphere(1, 0x222222, [6, 1, 0]);
setPBRProperties(rubber, { metalness: 0.0, roughness: 1.0 });
Call createImageSkybox() in your init() before adding PBR meshes. The skybox is used as an environment map โ metallic and smooth surfaces will reflect it realistically.