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

Enhanced loadModel() method signature with independent U and V flipping options. #6669

Merged
merged 18 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
88 changes: 88 additions & 0 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,94 @@ p5.prototype.loadModel = function(path) {
}
return model;
};
/**
* @method loadModel
* @param {String} path
* @param {Object} [options]
* @param {function(p5.Geometry)} [options.successCallback]
* @param {function(Event)} [options.failureCallback]
* @param {String} [options.fileType]
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} [options.normalize]
* @return {p5.Geometry} the <a href="#/p5.Geometry">p5.Geometry</a> object
*/
p5.prototype.loadModel = function(path,options) {
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
p5._validateParameters('loadModel', arguments);
let normalize= false;
let successCallback;
let failureCallback;
let fileType = path.slice(-4);
if (options && typeof options === 'object') {
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
normalize = options.normalize || false;
successCallback = options.successCallback;
failureCallback = options.failureCallback;
fileType = options.fileType || fileType;
} else if (typeof options === 'boolean') {
normalize = options;
successCallback = arguments[2];
failureCallback = arguments[3];
if (typeof arguments[4] !== 'undefined') {
fileType = arguments[4];
}
} else {
successCallback = typeof arguments[1] === 'function' ? arguments[1] : undefined;
failureCallback = arguments[2];
if (typeof arguments[3] !== 'undefined') {
fileType = arguments[3];
}
}

const model = new p5.Geometry();
model.gid = `${path}|${normalize}`;
const self = this;

if (fileType.match(/\.stl$/i)) {
this.httpDo(
path,
'GET',
'arrayBuffer',
arrayBuffer => {
parseSTL(model, arrayBuffer);

if (normalize) {
model.normalize();
}
self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
}
},
failureCallback
);
} else if (fileType.match(/\.obj$/i)) {
this.loadStrings(
path,
strings => {
parseObj(model, strings);

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

self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
}
},
failureCallback
);
} else {
p5._friendlyFileLoadError(3, path);

if (failureCallback) {
failureCallback();
} else {
p5._friendlyError(
'Sorry, the file type is invalid. Only OBJ and STL files are supported.'
);
}
}
return model;
};

/**
* Parse OBJ lines into model. For reference, this is what a simple model of a
Expand Down
18 changes: 18 additions & 0 deletions src/webgl/p5.Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ p5.Geometry = class Geometry {
this.vertexColors = [];
return this;
}
flipU() {
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
this.uvs = this.uvs.flat().map((val, index) => {
if (index % 2 === 0) {
return 1 - val;
} else {
return val;
}
});
}
flipV() {
this.uvs = this.uvs.flat().map((val, index) => {
if (index % 2 === 0) {
return val;
} else {
return 1 - val;
}
});
}
/**
* computes faces for geometry objects based on the vertices.
* @method computeFaces
Expand Down