From Reference Image to Runnable 3D Scene: A Practical Guide to Procedural Modeling
This guide explains how to turn a reference image into an interactive 3D scene that runs in a browser as a single HTML file. It focuses on controlling the reconstruction process through prompt constraints, camera and layout setup, lighting direction, procedural materials, code-drawn text, standard interactions, and a delivery checklist covering offline execution, a clean console, and visual comparison against the reference.
1. The Problem: Why Image-Based Modeling Is Hard to Control
Turning a photo into an interactive 3D scene that runs in a browser involves three layers of work: semantic understanding, visual fidelity, and engineering delivery. Semantic understanding requires the model to identify the main subject, secondary objects, and spatial relationships. Visual fidelity requires composition, perspective, lighting direction, and material characteristics to remain consistent with each other. Engineering delivery requires a single-file implementation that runs on double-click with no console errors.
Many attempts fail not because the result “looks wrong,” but because a reconstruction task is treated as free creation: arbitrary proportions, drifting light directions, and materials built by piling on textures. The result has similar elements but incorrect spatial relationships. So the first goal of the prompt is not to describe a style, but to constrain the degrees of freedom.
2. Prompt Engineering: Constraints Instead of Adjectives
A reusable prompt should contain six parts: input description, tech stack constraints, prohibited items, scene constraints, interaction constraints, and output format. An example structure (illustrative, not tied to a specific product API):
You will receive a reference image. Recreate the 3D scene in it using a single HTML file.Tech constraints: use a mature modular 3D library, pin the version with an import map, no external models or textures.Prohibited: no external resources, no large inline image data.Scene constraints: all geometry, materials, and lighting must be generated procedurally; composition must match the reference; main light direction must match.Interaction constraints: support drag-to-rotate and scroll-to-zoom; hands or dynamic elements run on local time.Output format: first summarize the scene and key visual elements in one sentence, then provide the complete code.
Three techniques matter most. First, break “does it look alike” into verifiable clauses, such as “subject on the right, sun in the center, moon on the left.” Second, state prohibited items explicitly so the model does not take shortcuts by referencing external resources. Third, pin dependency versions to reduce runtime failures caused by interface changes.
3. Composition and Perspective: Set the Camera First, Then Place Objects
Reconstruction accuracy is determined first by the camera. Infer the viewpoint height, pitch angle, and focal length tendency from the reference image: a low-angle upward shot emphasizes the monumentality of the subject, while a telephoto tendency compresses the distance between foreground and background. Once the camera is set, use a “framing grid” to divide the image into subject, secondary, and background zones, and place objects by zone instead of adjusting each one by feel.
// Illustrative: establish the camera and framing baseline firstconst camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 1000);camera.position.set(6, 2.2, 12);camera.lookAt(0, 3.2, 0);
In practice, keep composition parameters in a single configuration object so you can tune them repeatedly without polluting business code:
const layout = {subject: { x: 1.2, y: 0, z: 0, scale: 1.0 },sun: { x: 0, y: 6.5, z: -2 },moon: { x: -4, y: 5.0, z: -3 },camera: { fov: 45, pos: [6, 2.2, 12], target: [0, 3.2, 0] }};
4. Lighting: Key Light, Fill Light, and Ground Reflection
For night scene reconstruction, the key is that the main light direction must match the reference image. A common approach: use a warm key light from below or the lower side aimed at the subject, use cool light only as fill to outline contours, and keep ambient light low to avoid a washed-out image. If the reference has a wet ground surface, give the ground high reflectivity and low roughness so light strips and neon form elongated reflections.
const warm = new THREE.DirectionalLight(0xffb066, 2.2);warm.position.set(0, 1.5, 6); // from bottom to topconst cool = new THREE.DirectionalLight(0x4466ff, 0.6);cool.position.set(-6, 8, -4); // cool fill lightscene.add(warm, cool, new THREE.AmbientLight(0xffffff, 0.15));
For more realistic reflections, use planar reflection or a low-resolution environment capture when performance allows, but treat it as an optional enhancement rather than a default dependency, to avoid compatibility issues in single-file delivery.
5. Procedural Materials: Describing Metal, Stone, and Wet Surfaces with Parameters
Material reconstruction does not require textures. Color + metalness + roughness + procedural textures cover most scenes. Metal gears use high metalness and low roughness; a stone base uses low metalness, high roughness, and a slight noise overlay; bronze patina can mix two colors by noise; wet asphalt emphasizes low roughness and reflections.
const gold = new THREE.MeshStandardMaterial({ color: 0xd9a441, metalness: 0.95, roughness: 0.25 });const stone = new THREE.MeshStandardMaterial({ color: 0x8a8a86, metalness: 0.0, roughness: 0.9 });const wet = new THREE.MeshStandardMaterial({ color: 0x101014, metalness: 0.1, roughness: 0.12 });
Procedural textures can be generated with Canvas as noise or stripes and then applied to materials, satisfying the “no external textures” constraint while providing detail.
6. Text and UI: Draw Everything in Code
Roman numerals, place names, years, neon signs, and stone inscriptions in the reference image should be generated as Canvas textures applied to planes, not introduced as images. This meets the constraints and makes text changes and localization easier later.
function makeTextTexture(text, opts = {}) {const c = document.createElement('canvas');c.width = opts.w || 512; c.height = opts.h || 256;const g = c.getContext('2d');g.fillStyle = opts.bg || 'rgba(0,0,0,0)'; g.fillRect(0, 0, c.width, c.height);g.font = opts.font || 'bold 64px serif';g.fillStyle = opts.color || '#fff';g.textAlign = 'center'; g.textBaseline = 'middle';g.fillText(text, c.width / 2, c.height / 2);const tex = new THREE.CanvasTexture(c);tex.anisotropy = 4;return tex;}
7. Interaction and Motion: Making the Scene Feel Alive
Interaction should include at least drag-to-rotate and scroll-to-zoom, implemented with a mature orbit controller. For dynamic elements such as clock hands, compute angles from local time to add a time dimension:
const now = new Date();const sec = now.getSeconds() * Math.PI / 30;const min = now.getMinutes() * Math.PI / 30 + sec / 60;const hour = (now.getHours() % 12) * Math.PI / 6 + min / 12;// Apply hour/min/sec to the rotation of the hands
8. Delivery and Verification: Single File, Runnable, No Errors
Before delivery, run four checks: run offline to confirm no external requests; confirm zero console errors; confirm interactions work; and compare composition and lighting against the reference item by item. Write acceptance criteria as a checklist and tick them off, avoiding subjective “looks about right” judgments.
9. Common Failure Modes and Fixes
- Composition drift: fix the camera and layout configuration first, then fine-tune individual objects.
- Plastic-looking materials: increase metalness, lower roughness, and add environment reflection.
- Blurry text: increase Canvas resolution and set anisotropic filtering.
- Performance stutter: control geometry count and shadow map size, and lower the pixel ratio if needed.
10. Conclusion
The engineering core of image-based modeling is translating aesthetic judgment into verifiable constraints: camera and layout first, locked lighting direction, parameterized materials, code-drawn text, and standardized interaction. Following this process improves reconstruction accuracy and keeps the deliverable stable in single-file form, making team collaboration and future iteration easier.
