Code viewer for World: Load Cubes (clone by Steph...

// Cloned by Stephen Walsh on 22 Feb 2023 from World "Load Cubes (clone by Stephen Walsh)" by Stephen Walsh 
// Please leave this clone trail here.
 


// Cloned by Stephen Walsh on 6 Feb 2023 from World "Load Cubes" by Vanya Cadogan 
// Please leave this clone trail here.
 


// Cloned by Vanya Cadogan on 5 Feb 2023 from World "One Cube World (Three.js)" by Starter user 
// Please leave this clone trail here.
 

const skycolor = 'lightblue';
const startRadius = 100;               // distance from centre we start the camera at
const maxRadius = startRadius * 10;     // maximum distance from camera we render things 

// const jsonPath = "uploads/cadogav2/test_data.json";
// const jsonPath = "uploads/cadogav2/torus_37x11x37.json";
// const jsonPath = "uploads/stewalsh/sphinx-v2.json";
// const jsonPath = "uploads/stewalsh/output5.json";
const jsonPath = "uploads/stewalsh/mc-wh.json";

const colors = {
  0: "#B8255F",
  1: "#FF9933",
  2: "#7ECC49",
  3: "#6ACCBC",
  4: "#4073FF",
  5: "#884DFF",
  6: "#EB96EB",
  7: "#FF8D85",
  8: "#FF5733",
  9: "#DAF7A6",
  10: "#FFEC59",
  11: "#C05780",
  12: "#6C88C4",
  13: "#74737A",
  14: "#CCCCFF",
  15: "#9FE2BF",
  16: "#8A3186",
  17: "#4057A7",
  18: "#042838",
  19: "#2DCED9",
  20: "#CBD6E2",
  21: "#4FB06D",
  22: "#BE398D",
  23: "#FF3B30",
  24: "#FFFFFF"
};

const colorsLength = Object.keys(colors).length;

let cubes = [];

function createAllCubes(data, scaleFactor) {
    const group = new THREE.Group();
    
    data.elements.forEach(element => {
        const cube = createCube(element);
        
        //cubes.push(cube)

        group.add(cube);
        
    });
    
    //group.add(...cubes)
    
    // Scale group
    group.scale.set(scaleFactor, scaleFactor, scaleFactor);
    
    // Transfer new scale directly to the children
    let position = new THREE.Vector3()
    let scale = new THREE.Vector3()

    group.children.forEach(child => {
        child.getWorldPosition(position)
        child.position.copy(position)
    
        child.getWorldScale(scale)
        child.scale.copy(scale);
    });

    // Add children to the scene
    ABWorld.scene.add(...group.children)
    
    // Clear the group
    group.children = [];
}

function createCube(blockData) {
    const from = blockData.from.map(x => Math.round(x));
    const to = blockData.to.map(x => Math.round(x));
    const width = to[0] - from[0];
    const height = to[1] - from[1];
    const depth = to[2] - from[2];
    console.log(from, to, width, height, depth);
    const geometry = new THREE.BoxGeometry(width, height, depth);
    const colorIndex = blockData.color !== undefined? blockData.color % (colorsLength - 1) : Math.floor(Math.random() * (colorsLength - 1));
    const material = new THREE.MeshBasicMaterial({ color: colors[colorIndex] });
    const cube = new THREE.Mesh(geometry, material);
    // x, y, z
    cube.position.set(from[0] + width / 2, from[1] + height / 2, from[2] + depth / 2);
    return cube;
}


// Define what the World does at the start of a run: 

AB.world.newRun = function() 
{
   // ABWorld.renderer = new THREE.WebGLRenderer({ antialias: true });
    
    // start a 3D scene: 
    ABWorld.init3d(startRadius, maxRadius, skycolor);
    
    // Grid
    
    const gridHelper = new THREE.GridHelper( 10, 10 );
    ABWorld.scene.add( gridHelper );

    const loader = new THREE.FileLoader();
    
    loader.load(jsonPath, function ( data ) {
	    console.log("blockData");
	    jsonData = JSON.parse(data)
        console.log(jsonData);
        
        createAllCubes(jsonData, 1.0);
	});
};