/*
I create a method to build a mesh then returned to be used, it gives an error, "Cannot read property ‘visible’ of null’ in three.js file
am I doing something wrong???
*/
var scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var texture = new THREE.TextureLoader().load('https://cdn.pixabay.com/photo/2017/08/31/14/51/emotions-2700972_960_720.jpg');
var material = new THREE.MeshBasicMaterial({
map: texture,
wireframe: true
});
var geometry = new THREE.BoxGeometry(2.65, 2.65, 2.65);
var cube = new THREE.Mesh(geometry, material);
cylind = cylinder({
r1: .4,
r2: 1,
h: 2,
sh: 20,
sv: 10,
cup: false,
// textura: 'https://cdn.pixabay.com/photo/2017/08/31/14/51/emotions-2700972_960_720.jpg'
});
ob = new THREE.Group();
ob.add(cube);
scene.add(ob);
setTimeout(()=>{
// if I add it here it shows "Cannot read property ‘visible’ of null’ "
// ob.add(cylind);
},5000);
controls = new THREE.DeviceOrientationControls(ob);
camera.position.z = 5;
var animate = function() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
function cylinder(parameters) {
this.ret="mesh";
this.geo;
this.mesh;
this.r1 = 1;
this.r2 = 1;
this.h = 1;
this.sh = 10;
this.sv = 10;
this.cup = false;
this.buffer = true;
this.mat = null;
this.textura = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTtrzfNmlas9LW_v3P7uUomsyklfGTuJCKpvcnXGfGy3axhOQSC';
if (parameters.r1 != undefined) this.r1 = parameters.r1;
if (parameters.r2 != undefined) this.r2 = parameters.r2;
if (parameters.h != undefined) this.h = parameters.h;
if (parameters.sh != undefined) this.sh = parameters.sh;
if (parameters.sv != undefined) this.sv = parameters.sv;
if (parameters.cup != undefined) this.cup = parameters.cup;
if (parameters.buffer != undefined) this.buffer = parameters.buffer;
if (this.buffer) this.geo = new THREE.CylinderBufferGeometry(this.r1, this.r2, this.h, this.sh, this.sv);
else this.geo = new THREE.CylinderGeometry(this.r1, this.r2, this.h, this.sh, this.sv)
if(parameters.ret!=undefined) this.ret=parameters.ret;
if(this.ret==="geometry") return geo;
if (parameters.textura == undefined && parameters.mat == undefined){
loader = new THREE.TextureLoader();
loader.load(this.textura,
function(texture) {
this.mat = new THREE.MeshBasicMaterial({
map: texture
});
this.mesh = new THREE.Mesh(this.geo, this.mat);
// if I add it from right here not shows errors,
ob.add(this.mesh);//
return this.mesh;
})
}
if (parameters.textura != undefined && mat == undefined) {
this.textura = parameters.textura;
loader = new THREE.TextureLoader();
loader.load(this.textura,
function(texture) {
this.mat = new THREE.MeshBasicMaterial({
// color: 0x0faaf0
map: texture
});
//doesn't take the material
this.mesh = new THREE.Mesh(this.geo, this.mat);
alert(this.textura+"\n\n"+texture);
return this.mesh;
});
}
if (parameters.mat != undefined) this.mat = parameters.mat;
this.mesh = new THREE.Mesh(this.geo, this.mat);
return this.mesh;
}
</script>