Multimodal Code Generation in Practice: A Guide for Complex Scenarios

    This article examines why runnable code is not the same as usable code in multimodal generation, using Three.js examples such as a cyclist's kinematics and long-running particle animations. It explains how to express non-negotiable constraints explicitly, externalize design decisions into a contract file, and automate visual and geometric validation. It also offers model-selection guidance by task type and a practical engineering workflow for stable results in complex graphics and long-cycle iteration.

    1. Why “It Runs” Is Not the Same as “It Works”

    In graphical programming tasks, getting a model to output code that is syntactically correct and executable is not difficult. The hard part is producing code that is visually correct and logically consistent. A recurring pattern from hands-on testing is that when the same Three.js prompt is given to models at different capability tiers, the gap rarely shows up in whether the scene renders. It shows up in detail fidelity and in stability across multiple rounds of revision.

    These tasks are essentially constrained code generation problems, with constraints at three levels:

    1. Syntax: correct API calls and resolvable dependencies.
    2. Structure: sensible scene graph, coordinate system, camera, and lighting organization.
    3. Semantics: human posture, mechanical structure, materials, and lighting that match real-world intuition.

    Most failures happen at the third level. Models tend to produce approximations that “look right” while lacking explicit modeling of physical and kinematic constraints.

    2. Two Abstracted High-Difficulty Scenarios

    2.1 The Constraint Problem of a Human Cycling Pose

    Drawing “a person riding a road bike” in pure code is a classic inverse kinematics problem: the hip, knee, and ankle joints must maintain a closed-form solution along the pedal’s circular path, while the torso pitch and the relative position of the hands on the handlebars must not drift. Common errors include feet detaching from the pedals, leg length changing from frame to frame, and distorted frame-to-wheel proportions.

    A reusable way to express constraints is to declare the skeleton and constraint relationships explicitly in code rather than leaving the model to improvise:

    1. // Example: explicit kinematic constraints reduce model improvisation
    2. const rider = {
    3. hip: new THREE.Vector3(0, 1.05, -0.15),
    4. torsoPitch: THREE.MathUtils.degToRad(42),
    5. limb: { thigh: 0.42, shin: 0.40, foot: 0.22 }
    6. };
    7. function solveLeg(pedalAngle) {
    8. const pedal = new THREE.Vector3(
    9. crankRadius * Math.cos(pedalAngle),
    10. crankCenter.y + crankRadius * Math.sin(pedalAngle),
    11. 0
    12. );
    13. // Two-bone IK keeps the foot locked to the pedal
    14. return twoBoneIK(rider.hip, pedal, limb.thigh, limb.shin);
    15. }

    The engineering lesson is that writing non-negotiable constraints into the code skeleton and leaving the freely creative parts to the model significantly improves the first-pass success rate.

    2.2 Long-Horizon Consistency in Particle Systems

    Another case is a continuous animation with tens of thousands of particles that must maintain consistent transitions and lighting across chapters over dozens of minutes and dozens of revision rounds. The difficulty is not the quality of any single frame but long-context consistency: the model must remember early force-field parameters, the color palette, and the decay rhythm, avoiding a fresh “re-creation” on every revision.

    A workable approach is to maintain an externalized “design contract” and attach a summary of it to every revision request:

    1. [Design Contract v7]
    2. Force field: curlNoise(scale=0.35, speed=0.6)
    3. Particles: 42000, lifetime: 3.2s
    4. Palette: #0B1026 -> #6C4AB6 -> #F2C6A0
    5. Transitions: 1.5s cross-fade between chapters, fixed light direction
    6. Do not change: palette and camera focal length

    3. Understanding Capability Tiers Correctly

    The observation that the gap between flagship and lightweight models is narrowing does not mean their capabilities are equivalent. It shows that task difficulty is clearly stratified: for prompts with clear structure and explicitly expressible constraints, lightweight models are already sufficient; ultra-long context, cross-file refactoring, and complex physics simulation still require higher capability tiers. Selection guidance:

    Task Characteristics Recommended Strategy
    Single file, well-defined constraints Lightweight model + strong constraint prompting
    Multi-round, long-horizon iteration High-context capability + external contract file
    Cross-module refactoring High-capability model + static checks and test regression

    4. A Practical Engineering Workflow

    1. Constraints first: write the skeleton and immutable parameters before letting the model fill in details.
    2. Externalize the contract: store design decisions in a separate file and include it as part of every round’s input.
    3. Automated validation: use rendered screenshot comparison, geometric assertions, and inter-frame differencing for regression.
    4. Tiered dispatch: route simple subtasks to lightweight models and escalate complex ones to control cost.
    5. Observability: use logging services and monitoring alerts to record each round’s input summary and output differences for traceability.

    In the Baidu AI Cloud environment, this workflow can combine a container platform to host rendering and validation tasks, object storage to keep versioned artifacts, and a message queue to decouple long-running tasks, turning a one-off model demo into sustainable engineering capability.

    5. Conclusion

    The competitive focus of multimodal code generation is shifting from “whether it can generate” to “whether it can generate stably under constraints.” What developers need to build is the engineering habit of making non-negotiable constraints explicit, externalizing design decisions, and automating output validation. With this approach, results remain usable in complex graphics and long-cycle iteration scenarios regardless of which capability tier of model is used.