Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added createModel() to enable loading stl/obj files from a plaintext string within editor #6936

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
added fixes
  • Loading branch information
mathewpan2 committed Apr 24, 2024
commit 20e6efe90ee3e55508a2b1fdfac77f892c320500
83 changes: 31 additions & 52 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ import './p5.Geometry';
* rendered without color properties.
*
* Options can include:
* - `path`: Specifies the plain text string of either an stl or obj file to be loaded.
* - `modelString`: Specifies the plain text string of either an stl or obj file to be loaded.
* - `fileType`: Defines the file extension of the model.
* - `normalize`: Enables standardized size scaling during loading if set to true.
* - `successCallback`: Callback for post-loading actions with the 3D model object.
* - `failureCallback`: Handles errors if model loading fails, receiving an event error.
* - `fileType`: Defines the file extension of the model.
* - `flipU`: Flips the U texture coordinates of the model.
* - `flipV`: Flips the V texture coordinates of the model.
*
* @method createModel
* @param {String} object String of the object to be loaded
* @param {String} modelString String of the object to be loaded
* @param {String} [fileType] The file extension of the model
* (<code>.stl</code>, <code>.obj</code>).
* @param {Boolean} normalize If true, scale the model to a
* standardized size when loading
* @param {function(p5.Geometry)} [successCallback] Function to be called
* once the model is loaded. Will be passed
* the 3D model object.
* @param {function(Event)} [failureCallback] called with event error if
* the model fails to load.
* @param {String} [fileType] The file extension of the model
* (<code>.stl</code>, <code>.obj</code>).
* @return {p5.Geometry} the <a href="#/p5.Geometry">p5.Geometry</a> object
*
* @example
Expand Down Expand Up @@ -91,75 +91,53 @@ import './p5.Geometry';
/**
* @method createModel
* @param {String} modelString
* @param {String} [fileType]
* @param {function(p5.Geometry)} [successCallback]
* @param {function(Event)} [failureCallback]
* @param {String} [fileType]
* @return {p5.Geometry} the <a href="#/p5.Geometry">p5.Geometry</a> object
*/
/**
* @method createModel
* @param {String} modelString
* @param {String} [fileType]
* @param {Object} [options]
* @param {function(p5.Geometry)} [options.successCallback]
* @param {function(Event)} [options.failureCallback]
* @param {String} [options.fileType]
* @param {boolean} [options.normalize]
* @param {boolean} [options.flipU]
* @param {boolean} [options.flipV]
* @return {p5.Geometry} the <a href="#/p5.Geometry">p5.Geometry</a> object
*/
p5.prototype.createModel = function(modelString, options) {
p5._validateParameters('loadModel', arguments);
let modelCounter = 0;
p5.prototype.createModel = function(modelString, fileType, options) {
p5._validateParameters('createModel', arguments);
let normalize= false;
let successCallback;
let failureCallback;
let flipU = false;
let flipV = false;
let fileType = 'obj';
if (options && typeof options === 'object') {
normalize = options.normalize || false;
successCallback = options.successCallback;
failureCallback = options.failureCallback;
fileType = options.fileType || fileType;
flipU = options.flipU || false;
flipV = options.flipV || false;
} else if (typeof options === 'boolean') {
normalize = options;
successCallback = arguments[2];
failureCallback = arguments[3];
if (typeof arguments[4] !== 'undefined') {
fileType = arguments[4];
}
successCallback = arguments[3];
failureCallback = arguments[4];
} else {
successCallback = typeof arguments[1] === 'function' ? arguments[1] : undefined;
failureCallback = arguments[2];
if (typeof arguments[3] !== 'undefined') {
fileType = arguments[3];
}
successCallback = typeof arguments[2] === 'function' ? arguments[2] : undefined;
failureCallback = arguments[3];
}
const model = new p5.Geometry();
model.gid = `octa.obj|${normalize}`;
model.gid = `${fileType}|${normalize}|${modelCounter++}`;

if (fileType.match('stl')) {
try {
let uint8array = new TextEncoder().encode(modelString);
let arrayBuffer = uint8array.buffer;
parseSTL(model, arrayBuffer);
if (normalize) {
model.normalize();
}

if (flipU) {
model.flipU();
}

if (flipV) {
model.flipV();
}

if (typeof successCallback === 'function') {
successCallback(model);
}
} catch (error) {
if (failureCallback) {
failureCallback(error);
Expand All @@ -172,21 +150,6 @@ p5.prototype.createModel = function(modelString, options) {
try {
const lines = modelString.split('\n');
parseObj(model, lines);

if (normalize) {
model.normalize();
}

if (flipU) {
model.flipU();
}

if (flipV) {
model.flipV();
}
if (typeof successCallback === 'function') {
successCallback(model);
}
} catch (error) {
if (failureCallback) {
failureCallback(error);
Expand All @@ -205,6 +168,22 @@ p5.prototype.createModel = function(modelString, options) {
);
}
}
if (normalize) {
model.normalize();
}

if (flipU) {
model.flipU();
}

if (flipV) {
model.flipV();
}

if (typeof successCallback === 'function') {
successCallback(model);
}

return model;
};
/**
Expand Down