xxxxxxxxxx
88
let input;
let m, m0, currentmodel;
let redShader;
let gourd;
function preload() {
m0 = loadModel("teapot.obj", true);
redShader = loadShader("shader.vert", "shader.frag");
}
let setupComplete = false;
function setup() {
if (!setupComplete) {
createCanvas(windowWidth, windowHeight, WEBGL);
input = createFileInput(handleFile);
input.position(10, 10);
noStroke();
console.log('Default model: ' + m0.gid);
setupComplete = true;
} else {
console.log('Why?! Setup is already complete!')
}
}
function draw() {
background(255);
orbitControl(2, 1, 0.05);
if (m) {
currentmodel = m;
} else {
currentmodel = m0;
}
background(155);
// This will depend on your model (I'm using the standard teapot example)
scale(1, -1, 1);
//translate(0, 10, 0);
if (keyIsDown(191)) {
box(100);
} else if (currentmodel) {
shader(redShader);
model(currentmodel);
// resetShader();
}
}
function doubleClicked() {
if (!gourd) {
console.log('load gourd');
gourd = loadModel(
"gourd.obj",
false,
() => {
console.log('switch to gourd');
m = gourd;
}
);
} else if(m === m0) {
m = gourd;
} else {
m = m0;
}
}
function handleFile(file) {
loadStrings(file.data, (lines) => {
// See: https://developer.mozilla.org/en-US/docs/Web/API/Blob
let blob = new Blob([lines.join("\n")], { type: "text/plain" });
loadModel(
// This creates a temporary URL that can be used to fetch
// the data in the Blob.
// It is necessary to add the "#ext=.obj" to the end because
// loadModel() uses the file extension to determine what
// kind of model to load (which is a horrible design, but I digress).
URL.createObjectURL(blob) + "#ext=.obj",
true,
(loaded) => {
console.log('model loaded: ' + loaded.gid);
m = loaded;
},
(err) => {
console.log('loadModel failed: ' + err);
}
);
});
}