kcl-samples → stylized-dump-truck

stylized-dump-truck

stylized-dump-truck

KCL

// Stylized Dump Truck
// A simplified, parametric 3D toy dump truck with front cab, extended cargo bed, and four wheels.

// The model includes:
// - A chassis block forming the base
// - Front and rear fenders with hex wheel cut-outs
// - Four cylindrical wheels with hubcaps
// - A sloped-front cabin
// - A tall, extended dump bed at the rear
// - Flat slab roof
// - Front circular headlights and rear rectangular taillights

// Set units and version
@settings(defaultLengthUnit = m, kclVersion = 1.0)

// Chassis dimensions
chassisLength = 4.2 // total vehicle length
chassisWidth = 1.8 // total vehicle width
chassisHeight = 0.4 // height of main chassis block


// Wheel configuration
wheelBase = chassisLength * 0.7 // distance between front and rear wheels
wheelDiameter = 0.7 // outer diameter of tire
wheelWidth = chassisWidth * 0.15 // width of tire


// Fender geometry
fenderInset = 0.1 // horizontal gap between chassis and fender
fenderLength = chassisLength + fenderInset * 2 // overall fender span along length
fenderWidth = chassisWidth + fenderInset * 2 // overall fender span across width
fenderHeight = 0.35 // height of the fender block


// Elevation levels
groundClearance = 0.4 // vertical gap between ground and bottom of fender
chassisElevation = groundClearance + fenderHeight // bottom of the main chassis


// Cabin
cabinElevation = chassisElevation + chassisHeight // vertical start of cabin
cabinHeight = 1 // total cabin height
windPanelShift = 0.5 // slant of windshield
cabinLength = chassisLength * 0.2 // cabin segment along the length
dumpBedLength = chassisLength - windPanelShift - cabinLength

// Roof
roofElevation = cabinElevation + cabinHeight // roof base height
roofHeight = 0.1 // thickness of the roof block


// Front lights
headlightsElevation = chassisElevation + chassisHeight / 2
headlightsDistance = chassisWidth * 0.75
headlightsSize = chassisHeight * 0.8
lightDepth = 0.05 // extrusion depth for headlights and taillights


// Rear lights
taillightsOffset = chassisHeight * 0.2
taillightsHeight = chassisHeight - (taillightsOffset * 2)
taillightsWidth = taillightsHeight * 1.5
taillightsElevation = chassisElevation + taillightsOffset
taillightsDistance = chassisWidth - (taillightsOffset * 2) - taillightsWidth
taillightsSize = chassisHeight * 0.8

// Reference planes
chassisPlane = offsetPlane(XZ, offset = chassisWidth / 2) // profile plane for chassis and cabin
fenderPlane = offsetPlane(XZ, offset = chassisWidth / 2 + fenderInset) // profile plane for outer fenders


// Fenders
// Protective side covers that house the wheels.
// Positioned slightly wider than the chassis to suggest ruggedness.
fenderProfile = startProfile(fenderPlane, at = [-fenderLength / 2, groundClearance])
  |> yLine(length = fenderHeight)
  |> xLine(length = fenderLength)
  |> yLine(length = -fenderHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

fenderBody = extrude([fenderProfile], length = -chassisWidth - (fenderInset * 2))
  |> appearance(%, color = "#e0e0e0")

  // Wheel Cutouts
// Hexagonal recesses subtracted from the fenders to make space for the wheels.
fn makeWheelCutout(size) {
  wheelSketch = startSketchOn(XZ)
  cutOutProfile = polygon(
    wheelSketch,
    radius = size * 0.75,
    numSides = 6,
    center = [0, size / 2],
  )
  cutOutVoid = extrude(cutOutProfile, length = -wheelWidth * 1.5)
  return cutOutVoid
}
frontLeftCutout = makeWheelCutout(size = wheelDiameter)
  |> translate(x = -wheelBase / 2, y = -fenderWidth / 2, z = 0)
rearLeftCutout = makeWheelCutout(size = wheelDiameter)
  |> translate(x = wheelBase / 2, y = -fenderWidth / 2, z = 0)
frontRightCutout = makeWheelCutout(size = wheelDiameter)
  |> rotate(
       %,
       roll = 0,
       pitch = 0,
       yaw = 180,
     )
  |> translate(x = wheelBase / 2, y = -fenderWidth / 2, z = 0)
rearRightCutout = makeWheelCutout(size = wheelDiameter)
  |> rotate(
       %,
       roll = 0,
       pitch = 0,
       yaw = 180,
     )
  |> translate(x = -wheelBase / 2, y = -fenderWidth / 2, z = 0)
fenderWithCutouts = subtract(
       [fenderBody],
       tools = [
         frontLeftCutout,
         rearLeftCutout,
         frontRightCutout,
         rearRightCutout
       ],
     )
  |> appearance(%, color = "#e3e3e3")

  // Chassis
  // The main structural body of the vehicle, sitting above the fenders.
// Defines the base frame on which the cabin and roof are mounted.
chassisProfile = startProfile(chassisPlane, at = [-chassisLength / 2, chassisElevation])
  |> yLine(length = chassisHeight)
  |> xLine(length = chassisLength)
  |> yLine(length = -chassisHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

chassis = extrude([chassisProfile], length = -chassisWidth)
  |> appearance(%, color = "#ffdd00") // bright yellow body

  // Cabin (front operator area)
// Represents the front driver's cab with a sloped windshield
cabinSideProfile = startProfile(chassisPlane, at = [-chassisLength / 2, cabinElevation])
  |> line(end = [windPanelShift, cabinHeight])
  |> xLine(length = cabinLength)
  |> yLine(length = -cabinHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

cabinBody = extrude([cabinSideProfile], length = -chassisWidth)
  |> appearance(%, color = "#9ce8f2")

// Dump Bed
dumpBedProfile = startProfile(
       chassisPlane,
       at = [
         -chassisLength / 2 + windPanelShift + cabinLength * 1.2,
         cabinElevation * 1.2
       ],
     )
  |> yLine(length = cabinHeight)
  |> xLine(length = -cabinLength)
  |> yLine(length = roofHeight)
  |> xLine(length = cabinLength + dumpBedLength)
  |> yLine(length = -cabinHeight - roofHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

dumpBed = extrude([dumpBedProfile], length = -chassisWidth)
  |> appearance(%, color = "#ffdd00")

  // Roof
  // Flat rectangular panel capping the top of the cabin.
// Simplified to a thin block for a toy-like appearance.
roofProfile = startProfile(
       chassisPlane,
       at = [
         windPanelShift - (chassisLength / 2),
         roofElevation
       ],
     )
  |> yLine(length = roofHeight)
  |> xLine(length = cabinLength)
  |> yLine(length = -roofHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

roof = extrude([roofProfile], length = -chassisWidth)
  |> appearance(%, color = "#ffdd00") // bright yellow body

  // Wheels
  // Cylindrical wheels with a central hubcap detail.
// Created as separate parts and positioned under the fender cutouts.
fn wheelFn(diameter, width) {
  wheelSketch = startSketchOn(XZ)
  wheelProfile = circle(wheelSketch, center = [0, diameter / 2], radius = diameter / 2)
    |> subtract2d(%, tool = circle(wheelSketch, center = [0, diameter / 2], radius = diameter / 4))
  wheelTireBody = extrude(wheelProfile, length = -wheelWidth)
    |> appearance(%, color = "#141414")
  diskProfile = circle(wheelSketch, center = [0, diameter / 2], radius = diameter / 4)
  diskBody = extrude(diskProfile, length = -wheelWidth)
    |> appearance(%, color = "#d1d1d1")
  return [wheelTireBody, diskBody]
}
wheelFrontLeft = wheelFn(diameter = wheelDiameter, width = wheelWidth)
  |> translate(x = -wheelBase / 2, y = -fenderWidth / 2, z = 0)
wheelRearLeft = wheelFn(diameter = wheelDiameter, width = wheelWidth)
  |> translate(x = wheelBase / 2, y = -fenderWidth / 2, z = 0)
wheelFrontRight = wheelFn(diameter = wheelDiameter, width = wheelWidth)
  |> rotate(
       %,
       roll = 0,
       pitch = 0,
       yaw = 180,
     )
  |> translate(x = wheelBase / 2, y = -fenderWidth / 2, z = 0)
wheelRearRight = wheelFn(diameter = wheelDiameter, width = wheelWidth)
  |> rotate(
       %,
       roll = 0,
       pitch = 0,
       yaw = 180,
     )
  |> translate(x = -wheelBase / 2, y = -fenderWidth / 2, z = 0)

  // Headlights
// Two circular front lights mounted on the front face of the chassis.
headlightsPlane = offsetPlane(YZ, offset = -chassisLength / 2)
headlightsSketch = startSketchOn(headlightsPlane)
headlightsProfile = circle(
       headlightsSketch,
       center = [
         headlightsDistance / 2,
         headlightsElevation
       ],
       radius = headlightsSize / 2,
     )
  |> patternLinear2d(
       %,
       instances = 2,
       distance = headlightsDistance,
       axis = [-1, 0],
     )
headlightsBody = extrude(headlightsProfile, length = -lightDepth)
  |> appearance(%, color = "#ededed")

  // Taillights
// Two rectangular rear lights mounted on the back face of the chassis.
taillightsPlane = offsetPlane(YZ, offset = chassisLength / 2)
taillightsSketch = startSketchOn(taillightsPlane)
taillightsProfile = startProfile(
       taillightsSketch,
       at = [
         -chassisWidth / 2 + taillightsOffset,
         taillightsElevation
       ],
     )
  |> yLine(length = taillightsHeight)
  |> xLine(length = taillightsWidth)
  |> yLine(length = -taillightsHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()
  |> patternLinear2d(
       %,
       instances = 2,
       distance = taillightsDistance,
       axis = [1, 0],
     )
taillightsBody = extrude(taillightsProfile, length = lightDepth)
  |> appearance(%, color = "#ff0000")