Saturday, October 18, 2025
HomeLanguagesJavascriptp5.js model() Function

p5.js model() Function

The model() function is used to render a 3D model to the screen. The model to be rendered has to be loaded first using the loadModel() function.

Syntax:

model( model )

Parameters: This function accepts one parameter as mentioned above and described below.

  • model: It is the p5.Geometry object that specifies the model that has to be rendered to the screen.

The program below illustrate the model() function in p5.js:

Example:




let ballObj, cubeObj, coneObj;
let currentObj;
let newFont;
  
// Load all the models in preload()
function preload() {
  newFont = loadFont("fonts/Montserrat.otf");
  ballObj = loadModel("models/ball.obj", true);
  cubeObj = loadModel("models/cube.obj", true);
  coneObj = loadModel("models/cone.obj", true);
  currentObj = ballObj;
}
  
function setup() {
  createCanvas(400, 300, WEBGL);
  
  textFont(newFont, 14);
  
  modelSelector = createSelect();
  modelSelector.position(30, 40);
  modelSelector.option("ball");
  modelSelector.option("cube");
  modelSelector.option("cone");
  modelSelector.changed(modelChanged);
}
  
// Function to change the model depending
// on the selected dropdown 
function modelChanged() {
  let selected = modelSelector.value();
  console.log(selected);
  switch (selected) {
    case "ball":
      currentObj = ballObj;
      break;
    case "cube":
      currentObj = cubeObj;
      break;
    case "cone":
      currentObj = coneObj;
      break;
    default:
      break;
  }
}
  
function draw() {
  background("green");
  text("Use the dropdown to select the model to display", -185, -125);
  scale(0.75);
  lights();
  rotateX(frameCount * 0.05);
  rotateY(frameCount * 0.05);
  noStroke();
  
  // Load the given model
  model(currentObj);
}


Output:

model-selector

Reference: https://p5js.org/reference/#/p5/model

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS