Skip to content

3D engine that AI agents drive

3.6 KB
Raw Blame
// The recipe of the boat clip for the boat controller and wake (#982): a Kenney fishing boat steered by keyframed controls
// through a baked sea, with a wake behind it. Used by render-buoyancy-film.mjs with --scene boat.
export const BOAT_SIZE = { width: 1.78, length: 3.87, hullHeight: 0.7 };

// The top face of a box, cut into 17 x 17 cells and subdivided 3 times into 136 x 136 (0.44 m apart over 60 m), fine enough for the
// wake to show. The cuts come before the delete: after it the cut finds no edges to split.
const SEA_EDITS = [
  { op: 'subdivide_edges', edges: { all: true }, cuts: 16 },
  { op: 'delete', select: { not: { normal: [0, 1, 0], maxAngle: 10 } } },
  { op: 'subdivide', levels: 3 },
  { op: 'set_position', position: { mul: [{ attr: 'position' }, [60, 1, 60]] } },
];

// The kit's bow points to +z. The boat holds its heading for a second, then the helm goes to the right and it turns through the sea.
const CONTROLS = [
  { time: 0, throttle: 1, rudder: 0 },
  { time: 0.8, throttle: 1, rudder: 0 },
  { time: 1.4, throttle: 1, rudder: 0.8 },
];

// Where the baked hull is every half second (x, z); the camera follows it. It is the bake of the recipe below, so change both together.
const HULL_PATH = [[3, -3], [3, -1.12], [3, 0.62], [3.01, 2.53], [2.87, 4.56], [2.4, 6.36], [1.41, 8.14], [0.06, 9.46], [-1.63, 10.35], [-3.51, 10.7], [-5.36, 10.47]];
const CAMERA_OFFSET = [7, 3, -5];

/** Keyframes that sit `offset` away from the hull at each step of its path, at the height `offset[1]`. */
function followKeyframes(offset) {
  return HULL_PATH.map(([x, z], index) => ({ time: index * 0.5, value: [x + offset[0], offset[1], z + offset[2]] }));
}

export function buildBoatRecipe(boatAssetId, duration) {
  const boatLift = BOAT_SIZE.hullHeight / 2;
  return {
    format: 'crucible.scene', schemaVersion: 1,
    geometries: [
      { id: 'hull', type: 'box' },
      { id: 'sea', type: 'mesh', base: { type: 'box', bevel: 0 }, edits: SEA_EDITS, stack: [{ op: 'ocean', windSpeed: 2.5, choppiness: 0, size: 30, seed: 5, resolution: 5 }] },
    ],
    materials: [
      { id: 'water', color: [0.02, 0.12, 0.2], tag: 'water' },
      { id: 'ghost', color: [1, 1, 1], opacity: 0 },
    ],
    nodes: [
      { id: 'sea', geometry: 'sea', material: 'water' },
      { id: 'hull', geometry: 'hull', material: 'ghost', castShadow: false, translation: [3, 0.8, -3], scale: [BOAT_SIZE.width, BOAT_SIZE.hullHeight, BOAT_SIZE.length] },
      { id: 'boat', parent: 'hull', asset: { id: boatAssetId }, translation: [0, -boatLift / BOAT_SIZE.hullHeight, 0], scale: [1 / BOAT_SIZE.width, 1 / BOAT_SIZE.hullHeight, 1 / BOAT_SIZE.length] },
    ],
    camera: { eye: [10, 3, -8], target: [3, 1.5, -3], fov: 50 },
    environment: { world: true, sky: { type: 'physical', sunElevation: 30, sunRotation: -35, turbidity: 2.5 } },
    motion: { duration, tracks: [{ kind: 'camera', property: 'eye', interpolation: 'LINEAR', keyframes: followKeyframes(CAMERA_OFFSET) }, { kind: 'camera', property: 'target', interpolation: 'LINEAR', keyframes: followKeyframes([0, 1.5, 0]) }, { kind: 'geometry', id: 'sea', property: 'oceanTime', interpolation: 'LINEAR', keyframes: [{ time: 0, value: 0 }, { time: duration, value: duration }] }] },
    physics: {
      duration,
      ocean: { geometry: 'sea' },
      bodies: [{
        node: 'hull', density: 550, velocity: [0, 0, 3.5],
        float: {
          drag: 1.5, angularDrag: 3, points: [[-0.6, 0, -1.4], [0.6, 0, -1.4], [-0.6, 0, 1.4], [0.6, 0, 1.4]],
          boat: { thrust: 3.5, rudder: 1, forward: '+z', controls: CONTROLS, wake: { width: 1.6, strength: 1, height: 0.05, lifetime: 3 } },
        },
      }],
    },
  };
}