processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.53k stars 3.3k forks source link

Allow loading of models from strings #6891

Closed Kabaril closed 1 month ago

Kabaril commented 6 months ago

Increasing access

Most appropriate sub-area of p5.js?

Feature enhancement details

The current loadModel function takes a path to load a model from a .stl or .obj file, however this requires that a server exists that serves these assets.

A function that loads models directly from a string can increase portability of examples (as in copy + paste) and allows loading models without a server.

The following example:

//draw a spinning octahedron
let octahedron;

function preload() {
  octahedron = loadModel('assets/octahedron.obj');
}

function setup() {
  createCanvas(100, 100, WEBGL);
  describe('Vertically rotating 3-d octahedron.');
}

function draw() {
  background(200);
  rotateX(frameCount * 0.01);
  rotateY(frameCount * 0.01);
  model(octahedron);
}

could also be written as:

const octahedron_model = `
v 0.000000E+00 0.000000E+00 40.0000
v 22.5000 22.5000 0.000000E+00
v 22.5000 -22.5000 0.000000E+00
v -22.5000 -22.5000 0.000000E+00
v -22.5000 22.5000 0.000000E+00
v 0.000000E+00 0.000000E+00 -40.0000

f     1 2 3
f     1 3 4
f     1 4 5
f     1 5 2
f     6 5 4
f     6 4 3
f     6 3 2
f     6 2 1
f     6 1 5
`

//draw a spinning octahedron
let octahedron;

function preload() {
  octahedron = loadModelString(octahedron_model, {type: 'obj'});
}

function setup() {
  createCanvas(100, 100, WEBGL);
  describe('Vertically rotating 3-d octahedron.');
}

function draw() {
  background(200);
  rotateX(frameCount * 0.01);
  rotateY(frameCount * 0.01);
  model(octahedron);
}

The changes in code should be trivial, as this can reuse already existing helper functions.

welcome[bot] commented 6 months ago

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

davepagurek commented 6 months ago

I agree this feature makes sense! Although I would follow the precedent set by shaders, where loadShader is called in preload and loads from a file, and createShader is called in setup and loads from a string. That would imply that we'd have loadModel from a file and createModel from a string.

mathewpan2 commented 6 months ago

I'd like to try and work on this.

mathewpan2 commented 6 months ago

@davepagurek I implemented the method to load obj/stl from a string, but wonder if this is still up for debate and I was a little premature. Should I make the pr?

davepagurek commented 6 months ago

Feel free to make a PR, thanks!