Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Runtime 3D Geometry Construction with p5.js createModel

Notes 1

The createModel() function enables instantiation of p5.Geometry objects directly from textual mesh data, bypassing external file dependencies. Unlike loadModel() which fetches binary assets asynchronously from URLs, this method processes OBJ or STL formatted strings already present in memory—ideal for procedurally generated meshes, user-uploaded content, or API-delivered geometry.

Function Signature

createModel(sourceString, format, normalize, [successCallback], [failureCallback], [flipU], [flipV])
Paramter Type Description
sourceString String Raw OBJ or STL content, typically obtained via loadStrings() or generated algorithmically
format String File specification: 'obj' or 'stl' (lowercase, without dot)
normalize Boolean When true, auto-scales geometry to unit dimensions based on bounding box maximum extent
successCallback Function Optional handler receiving the finalized p5.Geometry instance
failureCallback Function Optional error handler receiving expection details
flipU/flipV Boolean Optional texture coordinate inversion flags for mapped materials

Implementation Example

The following generates a tetrahedron from inline vertex data:

let pyramid;

function setup() {
  createCanvas(600, 600, WEBGL);
  
  const tetrahedronData = [
    'v 0 1 0',
    'v -0.816 -0.5 0.577',
    'v 0.816 -0.5 0.577',
    'v 0 -0.5 -1.155',
    'f 1 2 3',
    'f 1 3 4',
    'f 1 4 2',
    'f 2 4 3'
  ].join('\n');
  
  pyramid = createModel(
    tetrahedronData, 
    'obj', 
    true, 
    (geom) => console.log(`Vertices loaded: ${geom.vertices.length}`),
    (err) => console.warn('Parsing error:', err)
  );
}

function draw() {
  background(220);
  orbitControl();
  
  ambientLight(60);
  pointLight(255, 200, 150, 200, -200, 200);
  
  push();
  rotateZ(frameCount * 0.015);
  rotateX(frameCount * 0.012);
  specularMaterial(100, 150, 255);
  shininess(50);
  model(pyramid);
  pop();
}

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.