p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

loadModel recalls setup (one time) trying to load dynamically a model

Open pirelaurent opened this issue 4 years ago • 2 comments
trafficstars

Most appropriate sub-area of p5.js?

  • [x] Events
  • [x] Utilities
  • [x] WebGL

Details about the bug:

  • p5.js version: v1.4.0 June 29, 2021
  • Web browser and version: chrome 94.0.4606.71
  • Operating System: win 10 pro Steps to reproduce this:
var original;

function preload() {
// ***even if you load -- or not -- a first model here 
    original = loadModel("./model/cube.obj")
}

// *** setup will be called again 
function setup() {
    console.log("entering setup at" + frameCount)
    can = createCanvas(300, 300, WEBGL);
    can.position(260, 200);
}

function draw() {
    background(0);
// *** if you try to load a model outside preload 
    if (frameCount % 100 == 99) {
        console.log('load a model at' + frameCount)
        original = loadModel("./model/cube.obj")
    }
    if (original != null) model(original)
}

Console output :

**bugModel.js:10 entering setup at0**
bugModel.js:20 load a model at99
**bugModel.js:10 entering setup at99**
bugModel.js:20 load a model at199
bugModel.js:20 load a model at299
bugModel.js:20 load a model at399
bugModel.js:20 load a model at499
bugModel.js:20 load a model at599
bugModel.js:20 load a model at699

As one can see, any loadModel outside preload will call again setup once.
If your setup includes some createElement, this will display html twice as was indicated by bug #3981, but its a more general pb for any code in setup.

Notice that i had tried to use full function with callback loadModel(path, normalize, [successCallback], [failureCallback], [fileType]) but same behavior.

As a workaround, i protect setup from reentrancy :

var protectSetup = false; 
function setup() {
    if (protectSetup) return;
    console.log("entering setup at" + frameCount)
    can = createCanvas(300, 300, WEBGL);
    can.position(260, 200);
    protectSetup = true;
}

And everything is ok, but it seems abnormal that setup is reactivated by the first loadModel call outside preload.
Main usage : User interface allows to enter the name of a file (model) to work with.

pirelaurent avatar Oct 08 '21 10:10 pirelaurent

After investigating in a more complex sketch, it seems that loadmodel destroy something about the canvas , so it needs to call setup to reconstruct it. My previous reentrancy workaround must be after the createCanvas in setup :

function setup() {
    can = createCanvas(800, 800, WEBGL);
    can.position(260, 80);
    //------- some things to do only once 
    if (protectSetup) return;
    fillHtmlWithDefault();
    protectSetup = true;
}

pirelaurent avatar Oct 09 '21 07:10 pirelaurent

Related to #5455

awelles avatar Dec 08 '21 03:12 awelles

Hi! I think this should be fixed now in 1.5.0 after https://github.com/processing/p5.js/pull/5816. I'm going to close this issue now for organizational purposes but feel free to reopen this if you notice it happening again or if there are any details that still aren't resolved!

davepagurek avatar Jan 14 '23 20:01 davepagurek