kcl-samples → stylized-pickup-truck

stylized-pickup-truck

stylized-pickup-truck

KCL

// Stylized Pickup Truck
// A simplified, parametric 3-D model of a pickup truck intended for use as a low-poly prop in real-time engines, visualization scenes, or rapid prototyping.

// The model includes:
// - A chassis block forming the vehicle’s base
// - Front and rear fenders with wheel cut-outs
// - Four detachable wheels (tire + hubcap)
// - A cabin with sloped windshield and rear panel
// - A thin roof slab
// - A recessed cargo bed cut into the chassis
// - Circular headlights and rectangular taillights
//
// All major dimensions are parameterized for quick adjustments.
// Geometry prioritizes clarity and clean topology over realism, making it ideal
// for game assets, placeholder art, or visual-scripting demos.

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

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


// Wheel configuration
wheelBase = 2.6 // 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


// Hood and cabin
hoodLength = chassisLength * 0.2 // front hood segment length
cabinElevation = chassisElevation + chassisHeight // vertical start of cabin
cabinHeight = 1.1 // total cabin height
cabinLength = chassisLength * 0.2 // cabin segment along the length
windPanelShift = 0.43 // slant of windshield
rearPanelShift = 0.15 // slant of rear cabin wall


// 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

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


// 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, roof, and bed are mounted.
chassisProfile = startProfile(chassisPlane, at = [-chassisLength / 2, chassisElevation])
  |> yLine(length = chassisHeight)
  |> xLine(length = chassisLength, tag = $seg01)
  |> yLine(length = -chassisHeight)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

chassis = extrude([chassisProfile], length = -chassisWidth)
  |> appearance(%, color = "#ff291a")

  // Truck Bed Cutout (rear cargo area)
  // The open box section at the back of the chassis used to carry goods.
// This is a void cut into the chassis to simulate a bed.
truckBedOffset = chassisWidth * 0.05
truckBedLength = chassisLength - hoodLength - cabinLength - windPanelShift - rearPanelShift - truckBedOffset
truckBedSketch = startSketchOn(chassis, face = seg01)
truckBedProfile = startProfile(
       truckBedSketch,
       at = [
         truckBedOffset - (chassisLength / 2),
         chassisWidth / 2 - truckBedOffset
       ],
     )
  |> xLine(length = truckBedLength)
  |> yLine(length = truckBedOffset * 2 - chassisWidth)
  |> xLine(length = -truckBedLength)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()
truckBedCutout = extrude(truckBedProfile, length = -chassisHeight * 0.8)

// Cabin (driver/passenger area)
// Enclosed compartment where the driver and passenger sit.
// Has a sloped windshield and back for a stylized look.
cabinSideProfile = startProfile(
       chassisPlane,
       at = [
         -chassisLength / 2 + hoodLength,
         cabinElevation
       ],
     )
  |> line(end = [windPanelShift, cabinHeight])
  |> xLine(length = cabinLength)
  |> line(end = [rearPanelShift, -cabinHeight])
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

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

  // Roof
  // Flat rectangular panel capping the top of the cabin.
// Simplified to a thin block for a toy-like appearance.
roofProfile = startProfile(
       chassisPlane,
       at = [
         hoodLength + 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 = "#ff291a")

  // 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")