I created a very simple cube using Blender and exported it using the Three.js Blender Exporter.
I am now attempting to load the exported model and apply a texture using Node. When I run the script, I see no error. What am I doing wrong?
'use strict';
const JSDOM = require('jsdom').JSDOM;
const THREE = require('three');
const dom = new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`);
global.window = dom.window;
global.document = dom.window.document;
global.XMLHttpRequest = require('xhr2');
const loadTexture = (path) => {
return new Promise((resolve, reject) => {
const loader = new THREE.TextureLoader();
loader.load(
path,
// success
(texture) => resolve(texture),
// progress
undefined,
// error
(error) => reject(error)
);
});
};
const loadGeometry = (path) => {
const loader = new THREE.JSONLoader();
return new Promise((resolve, reject) => {
loader.load(
path,
// success
(geometry) => resolve(geometry),
// progress
undefined,
// error
(error) => reject(error)
);
});
};
const createObject = () => {
return Promise.all([
loadTexture('./image.jpg'),
loadGeometry('./cube.json')
]).then(results => {
const texture = results[0];
const material = new THREE.MeshBasicMaterial({map: texture});
const geometry = results[1];
const object = new THREE.Mesh(geometry, material);
return object;
});
};
const scene = new THREE.Scene();
createObject()
.then(object => {
scene.add(object);
console.log('Success!');
})
.catch(error => console.error(error.message));
Here is the model I am using:
{
"normals":[-5.32907e-15,-1,2.98023e-08,1.06581e-14,1,-2.98023e-08,1,4.47034e-08,2.83122e-07,-2.83122e-07,-7.45059e-08,1,-1,-1.3411e-07,-2.23517e-07,2.38419e-07,1.78814e-07,-1],
"faces":[33,0,1,2,3,0,0,0,0,33,4,7,6,5,1,1,1,1,33,0,4,5,1,2,2,2,2,33,1,5,6,2,3,3,3,3,33,2,6,7,3,4,4,4,4,33,4,0,3,7,5,5,5,5],
"metadata":{
"normals":6,
"faces":6,
"type":"Geometry",
"vertices":8,
"generator":"io_three",
"uvs":0,
"version":3
},
"vertices":[1,-1,-1,1,-1,1,-1,-1,1,-1,-1,-1,1,1,-1,0.999999,1,1,-1,1,1,-1,1,-1],
"uvs":[]
}
Success!
? Also note that jsdom probably does not support WebGL, you may need node-webgl or a headless browser here. – Don McCurdyconsole.log
output appear? Consider logging when the texture and model load or fail rather than resolving/rejecting immediately to narrow down the problem. – Don McCurdyconsole.log()
. But I just added a.catch()
withincreateObject()
, and 'lo and behold, I get an error message now:THREE.Object3D.add: object not an instance of THREE.Object3D
. It then appears thatscene.add(object)
is causing this problem. – Chad Johnson.catch()
blockundefined
is being returned. – Chad Johnson