Reputation: 179
I have just started using THREE.js and i am trying to make a "cube" made out of cubes, similar to this :
I am trying to accomplish this with a 3D array that follows the structure: [[grid][line,line][[cube,cube],[cube,cube]]]
. As in, the cube is made up of grids, which are made up of lines, which are made of the cubes themselves. Here is the code:
function lineOfMeshes(w, g) {
var cubes = [];
for (var i = 0; i < w; i++) {
cubes.push(new THREE.Mesh(geometry, material));
cubes[i].position.x += i * g;
}
//console.log("LINE:" + cubes);
return cubes;
}
function gridOfMeshes(w, g) {
cubes = [];
for (var line = 0; line < w; line++) {
cubes.push(lineOfMeshes(w, g));
for (var cube = 0; cube < w; cube++) {
cubes[line][cube].position.z += line * g;
}
}
//console.log("GRID: " + cubes);
return cubes;
}
function cubeOfMeshes(w, g) {
cubes = [];
for (var grid = 0; grid < w; grid++) {
cubes.push(gridOfMeshes(w, g));
for (var line=0;line<w;line++) {
for (var cube = 0; cube < w; cube++) {
cubes[grid][line][cube].position.z += line * g;
}
}
}
//console.log("CUBE"+ cubes);
return cubes;
}
//var container = document.getElementById("3dcontainer");
var scene = new THREE.Scene();
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 controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.autoRotate = true;
var geometry = new THREE.BoxGeometry(); //object that contains all the points and faces of the cube
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); //material that colors the box
//var cube = new THREE.Mesh(geometry, material); //a mesh is an object that takes a geometry and applies a material to it
///////////////////////////////////////////////////////
let gridWidth = 5;
let gridGap = 3;
cubes = cubeOfMeshes(gridWidth, gridGap);
cubes.forEach(grid => {
grid.forEach(line => {
line.forEach(cube=>{
scene.add(cube);
})
});
});
camera.position.z = 5;
///////////////////////////////////////////////////////
//render loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
However, when I run it it gives the error:
index.js:31 Uncaught TypeError: Cannot read property 'position' of undefined
at cubeOfMeshes (index.js:31)
at index.js:65
I'm not sure why this is happening. I believe I am indexing correctly. Any help is appreciated, thank you in advance.
Upvotes: 1
Views: 1785
Reputation:
It's because in gridOfMeshes
and cubeOfMeshes
you didn't put const
, let
, or var
in front of cubes
so they are effectively the same variable and getting overwritten
note: If you add
'use strict';
To the top of your JavaScript then it would point out this error in the JavaScript console with
Uncaught ReferenceError: cubes is not defined
at cubeOfMeshes (js:40)
at js:80
Further, if you use an editor like Visual Studio Code and you install the eslint plugin. You might need to configure it but if you spend a few minutes setting it up it will highlight these kinds of errors as you type.
Upvotes: 1