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 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,42 @@ import './p5.Geometry';
* @param {String} [fileType]
* @return {p5.Geometry} the <a href="#/p5.Geometry">p5.Geometry</a> object
*/
p5.prototype.loadModel = function(path) {
/**
* @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]
* @param {boolean} [options.flipU]
* @param {boolean} [options.flipV]
* @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;
let normalize= false;
let successCallback;
let failureCallback;
let flipU = false;
let flipV = false;
let fileType = path.slice(-4);
if (typeof arguments[1] === 'boolean') {
normalize = arguments[1];
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;
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];
}
} else {
normalize = false;
successCallback = arguments[1];
successCallback = typeof arguments[1] === 'function' ? arguments[1] : undefined;
failureCallback = arguments[2];
if (typeof arguments[3] !== 'undefined') {
fileType = arguments[3];
Expand All @@ -140,6 +160,15 @@ p5.prototype.loadModel = function(path) {
if (normalize) {
model.normalize();
}

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

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

self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
Expand All @@ -157,6 +186,14 @@ p5.prototype.loadModel = function(path) {
model.normalize();
}

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

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

self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
Expand Down
88 changes: 88 additions & 0 deletions src/webgl/p5.Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,94 @@ p5.Geometry = class Geometry {
return this;
}
/**
* Flips the U texture coordinates of the model.
* @method flipU
* @for p5.Geometry
*
* @returns {p5.Geometry}
*
* @example
* <div>
* <code>
* let modelU;
*
* function preload() {
* modelU = loadModel('path/to/your/model.obj');
deveshidwivedi marked this conversation as resolved.
Show resolved Hide resolved
*
* modelU.flipU();
* }
*
* function setup() {
* createCanvas(400, 400, WEBGL);
* background(200);
*
* // Original model (no texture coordinate flipping)
* fill('orange');
* translate(-100, 0, 0);
* model(modelU);
*
* // Flipped U texture coordinates model
* fill('blue');
* translate(200, 0, 0);
* model(modelU);
* }
* </code>
* </div>
*/
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;
}
});
}
/**
* Flips the V texture coordinates of the model.
* @method flipV
* @for p5.Geometry
*
* @returns {p5.Geometry}
*
* @example
* <div>
* <code>
* let modelV;
*
* function preload() {
* modelV = loadModel('path/to/your/model.obj');
*
* modelV.flipV();
* }
*
* function setup() {
* createCanvas(400, 400, WEBGL);
* background(200);
*
* // Original model (no texture coordinate flipping)
* fill('orange');
* translate(-100, 0, 0);
* model(modelV);
*
* // Flipped V texture coordinates model
* fill('purple');
* translate(200, 0, 0);
* model(modelV);
* }
* </code>
* </div>
*/
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
* @chainable
Expand Down
Loading