Building Interactive 3D Scenes Entirely in Code
A practical guide to turning visual briefs into executable specs and shipping single-file, asset-free 3D pages: procedural geometry, layered aesthetics, interaction, performance, and a fixed evaluation checklist.
Code is expanding from “business logic” into a general interface that connects imagery, interactive entertainment, three-dimensional space, and perceptible physical effects. A standalone page you can open in the browser—with no linked models, textures, or audio, only computation—can still show a sunset highway, water highlights, particle bursts, and heat haze. That is a sign that generative programming can meet both aesthetic and engineering bars at once. This article is not tied to any specific model or tool; it focuses on methods you can actually ship.
1. Write visual requirements as executable specs
A request such as “draw a bird riding a bicycle and output an interactive 3D page” looks like one sentence. In practice it encodes five kinds of constraint: geometry, animation, camera, materials, environment, and interaction. Write an acceptance spec before stacking effects:
- Subject: body proportions; ride cycle (cadence, body rise and fall, a stable head); small changes in expression or pose.
- Scene: road curvature; density of signs and vegetation; far layers (coast, isolated island, sky).
- Lighting: key-light direction and color temperature; whether fill exists; whether approximate shadows are acceptable.
- Interaction: orbit, zoom, reset; animation must not stop while dragging.
- Delivery: a single file, no external binary assets, openable in mainstream desktop browsers.
The more specific the spec, the less later iteration depends on “feel.” “Cinematic water” should become: directional highlights nearby, cooler color in the distance, thin haze near the horizon, and occasional objects leaping from the water without clipping.
2. A recommended structure for a single-file procedural scene
Standalone pages work well for teaching, design reviews, and fast critique. Organize modules inside one file instead of dumping every statement together.
- Scene graph: camera, lights, hero, environment, and post-processing as separate objects.
- Procedural geometry: parameterized curves for the road; noise or layered sines for terrain and ocean displacement; vegetation as a few prototypes instanced with random scale and rotation.
- Materials: prefer procedural shading (base color, roughness, metalness, emission) over external images.
- Time: drive wheels, water, particles, and audio envelopes from a shared frame clock so pause and speed changes stay consistent.
- Budget: vertex count, particle count, shadow resolution, and post-process toggles as constants so you can degrade on weaker machines.
Illustrative layout (pseudocode for boundaries, not a particular API):
init()camera, controls, lightsroad = curveRoad(controlPoints)hero = buildRider(params)ocean = buildOcean(waveLayers)fx = particles + heatHazeanimate(t)hero.update(t)ocean.update(t)fx.update(t)render()
Keep geometry and animation parameters tunable and visual rules in a few functions so you can polish without rewriting the page.
3. Where aesthetics come from: layers, not more effects
Shots that hold up at close range usually have clear hierarchy, not the longest effect list.
- Composition: the road as a leading line toward sunset or a distant island; the subject near a third, not always centered.
- Color: warm key light against cooler sea, with saturation ranked so signs and plants do not steal the hero.
- Scale: road width, wheel radius, bird height, and tree height must share one scale, or the scene “spins but looks fake.”
- Motion: looping ride cycle; gulls on slow circles or splines; dolphin leaps as sparse events, not every frame.
- Water: stacked waves (large swell plus small ripples); highlights that follow view and sun; less detail plus fog far away, crests kept nearby.
- Volcano-style demos: control terrain, lava emission, eruption particles, heat-haze distortion, and low-frequency sound separately. Particles use velocity, gravity, lifetime, and turbulence; heat haze is a mild screen-space warp with a hard intensity cap to avoid dizziness. Sound can be oscillators and noise envelopes—no linked audio files.
Computing all of this in code is feasible if you accept approximation: real fluids, global illumination, and volumetric clouds are expensive. The demo goal is a believable look, not offline film simulation.
4. Interaction, performance, and maintainability
360° drag is basic, but handle edges: polar limits so the camera cannot flip underground; distance limits so it cannot enter the subject; damping so motion eases after release. Animation and the controller should share one timestep so water does not freeze and particles do not explode while dragging.
Performance priorities:
- Low-poly or billboards in the distance; higher tessellation only nearby.
- Particle caps; recycle oldest particles when over budget.
- Shadows on the hero and road only; fake highlights on the sea.
- Quality tiers: disable heat haze, lower ocean tessellation, fewer birds.
- Even a single file needs section comments and a constants block, or a second edit costs more than a rewrite.
For cloud demos, host the static page on ordinary object storage, accelerate with a CDN, and watch load-failure rates. Do not hide the first frame behind login; review speed drops. If the team uses a general cloud platform such as Baidu AI Cloud, treat it only as hosting and logging; scene correctness still comes from in-page parameters and tests.
5. How to judge a generation
Do not stop at “stunning.” Reuse one task set: riding hero, coastal sunset, procedural water, volcano particles and haze, single file with no externals, smooth interaction. Score each on four points: first-run success, reasonable scale and occlusion, a unified key light, and whether a degraded mode still reads the intent.
Common failures: intersecting meshes, animation cycles that do not close, water that looks like a sliding texture, particles spawning in mid-air, sound out of sync with eruptions, and references to missing URLs. Fix in order: correctness (closed loops, no clipping, interactive), then beauty (color, fog, event sparsity), then post-processing.
6. Shipping advice
Treat a 3D demo as a software feature, not an art lottery: write the spec and budget first, generate geometry and shading procedurally, drive motion and sound from one timeline, then accept against a checklist. Code can then be a stable interface among storyboards, interactive demos, and spatial expression. Whether the image “feels like a short film” depends on hierarchy, proportion, and motion design—not on more effects you cannot maintain.
