Skip to content

Commit f68bfc1

Browse files
committed
interactive tiling is now complete
1 parent 1bd86f0 commit f68bfc1

File tree

1 file changed

+60
-63
lines changed
  • src/components/crystal-toolkit/scene

1 file changed

+60
-63
lines changed

src/components/crystal-toolkit/scene/Scene.ts

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default class Scene {
7171
private animationHelper: AnimationHelper;
7272
private tiling: any;
7373
private maxTiling: any;
74+
private arrayOfTileRoots: any;
7475

7576
private cacheMountBBox(mountNode: Element) {
7677
this.cachedMountNodeSize = { width: mountNode.clientWidth, height: mountNode.clientHeight };
@@ -110,6 +111,36 @@ export default class Scene {
110111
mountNode.appendChild(this.renderer.domElement);
111112
}
112113

114+
/*
115+
this function returns a 3-dimensional empty array based on the tiling array
116+
e.g. _getTiles([1, 1, 1] === [ [ [[], []], [[], []] ], [ [[], []], [[], []] ] ]
117+
all of the arrays are unique instances, not copies
118+
this allows us to create an array that can store the contents of each tiles and
119+
be accessed with the tile indices. For example:
120+
arr = _getTiles([2, 2, 2])
121+
arr[0][1][2].push(scene)
122+
scene = arr[0][1][2][0]
123+
*/
124+
private static getEmptyTilesArray(tiling: number[]) {
125+
let grid = [];
126+
for (let x = 0; x <= tiling[2]; x++) {
127+
let arrX = [];
128+
for (let y = 0; y <= tiling[1]; y++) {
129+
let arrY = [];
130+
for (let z = 0; z <= tiling[0]; z++) {
131+
let arrZ = [];
132+
// @ts-ignore
133+
arrY.push(arrZ);
134+
}
135+
// @ts-ignore
136+
arrX.push(arrY);
137+
}
138+
// @ts-ignore
139+
grid.push(arrX);
140+
}
141+
return grid;
142+
}
143+
113144
private configureLabelRenderer(mountNode: Element) {
114145
const labelRenderer = new CSS2DRenderer();
115146
this.labelRenderer = labelRenderer;
@@ -374,7 +405,11 @@ export default class Scene {
374405
) {
375406
this.tiling = tiling;
376407
this.maxTiling = maxTiling;
377-
408+
this.arrayOfTileRoots = Scene.getEmptyTilesArray([
409+
this.maxTiling,
410+
this.maxTiling,
411+
this.maxTiling
412+
]);
378413
this.settings = Object.assign(defaults, settings);
379414
this.objectBuilder = new ThreeBuilder(this.settings);
380415
this.cameraState = cameraState;
@@ -412,6 +447,23 @@ export default class Scene {
412447
this.renderInlet();
413448
}
414449

450+
/*
451+
loop through the arrayOfTileRoots and set the visibility of each object.
452+
In particular, set threeObject.visible = true if the x, y, and z indices
453+
are all less than the x, y, and z indices in tiling.
454+
*/
455+
updateTiles(tiling) {
456+
const [xCut, yCut, zCut] = tiling;
457+
this.arrayOfTileRoots.forEach((arrX, x) => {
458+
arrX.forEach((arrY, y) => {
459+
arrY.forEach((arrZ, z) => {
460+
arrZ[0].visible = x <= xCut && y <= yCut && z <= zCut;
461+
});
462+
});
463+
});
464+
this.renderScene();
465+
}
466+
415467
public resizeRendererToDisplaySize() {
416468
const canvas = this.renderer.domElement as HTMLCanvasElement;
417469
this.cacheMountBBox(canvas.parentElement as Element);
@@ -512,36 +564,6 @@ export default class Scene {
512564
return tiles;
513565
};
514566

515-
/*
516-
this function returns a 3-dimensional empty array based on the tiling array
517-
e.g. _getTiles([1, 1, 1] === [ [ [[], []], [[], []] ], [ [[], []], [[], []] ] ]
518-
all of the arrays are unique instances, not copies
519-
this allows us to create an array that can store the contents of each tiles and
520-
be accessed with the tile indices. For example:
521-
arr = _getTiles([2, 2, 2])
522-
arr[0][1][2].push(scene)
523-
scene = arr[0][1][2][0]
524-
*/
525-
const _getEmptyTilesArray = (tiling: number[]) => {
526-
let grid = [];
527-
for (let x = 0; x <= tiling[2]; x++) {
528-
let arrX = [];
529-
for (let y = 0; y <= tiling[1]; y++) {
530-
let arrY = [];
531-
for (let z = 0; z <= tiling[0]; z++) {
532-
let arrZ = [];
533-
// @ts-ignore
534-
arrY.push(arrZ);
535-
}
536-
// @ts-ignore
537-
arrX.push(arrY);
538-
}
539-
// @ts-ignore
540-
grid.push(arrX);
541-
}
542-
return grid;
543-
};
544-
545567
const emptyLattice = [
546568
[0, 0, 0],
547569
[0, 0, 0],
@@ -557,8 +579,8 @@ export default class Scene {
557579
const traverseTiles = (
558580
o: SceneJsonObject,
559581
root: THREE.Object3D,
560-
tiles: number[][],
561-
arrayOfTileRoots: number[][][][]
582+
tiles: number[][]
583+
// arrayOfTileRoots: number[][][][]
562584
) => {
563585
// @ts-ignore
564586
let lattice = o.lattice ? o.lattice : emptyLattice;
@@ -567,10 +589,10 @@ export default class Scene {
567589
const tileRootObject = new THREE.Object3D();
568590
tileRootObject.name = sceneJson.name!;
569591
sceneJson.visible && (tileRootObject.visible = sceneJson.visible);
570-
592+
console.log(this.arrayOfTileRoots);
571593
root.add(tileRootObject);
572594
const [x, y, z] = tile;
573-
arrayOfTileRoots[x][y][z].push(tileRootObject);
595+
this.arrayOfTileRoots[x][y][z].push(tileRootObject);
574596

575597
let tileOffsets: number[][] = lattice.map((vector: number[], index: number) => {
576598
return vector.map((x: number) => x * tile[index]);
@@ -642,50 +664,25 @@ export default class Scene {
642664
});
643665
};
644666

645-
/*
646-
loop through the arrayOfTileRoots and set the visibility of each object.
647-
In particular, set threeObject.visible = true if the x, y, and z indices
648-
are all less than the x, y, and z indices in tiling.
649-
*/
650-
const updateTiles = (arrayOfTileRoots, tiling) => {
651-
const [xCut, yCut, zCut] = tiling;
652-
arrayOfTileRoots.forEach((arrX, x) => {
653-
arrX.forEach((arrY, y) => {
654-
arrY.forEach((arrZ, z) => {
655-
arrZ[0].visible = x <= xCut && y <= yCut && z <= zCut;
656-
});
657-
});
658-
});
659-
};
660-
661-
const updateMaxTiling = () => {};
662-
663667
// TODO: does it make sense to split this code into two cases?
664668
// set up the threeObjects and containers
665-
let arrayOfTileRoots: number[][][][];
666669
const rootObject = new THREE.Object3D();
667670
if (this.maxTiling > 0) {
668671
// if needed, create a parent for all Scene objects
669672
rootObject.name = 'root';
670673
rootObject.visible = true;
671674
const maxTilingArray = [this.maxTiling, this.maxTiling, this.maxTiling];
672675
let tiles = _getTiles(maxTilingArray);
673-
arrayOfTileRoots = _getEmptyTilesArray(maxTilingArray);
674-
traverseTiles(sceneJson, rootObject, tiles, arrayOfTileRoots);
675-
updateTiles(arrayOfTileRoots, this.tiling);
676-
console.log(arrayOfTileRoots);
676+
traverseTiles(sceneJson, rootObject, tiles);
677+
this.updateTiles(this.tiling);
678+
console.log(this.arrayOfTileRoots);
677679
} else {
678680
rootObject.name = sceneJson.name!;
679681
sceneJson.visible && (rootObject.visible = sceneJson.visible);
680682
traverseScene(sceneJson, rootObject, emptyLattice, '');
681683
} // TODO: take this out, hope things dont break, fix if they do
682684

683-
// for demonstration purposes TODO: remove next two lines
684-
// arrayOfTileRoots[1][1][1][0].visible = false;
685-
// arrayOfTileRoots[0][0][0][0].visible = false;
686-
687685
// can cause memory leak
688-
//console.log('rootObject', rootObject, rootObject);
689686
this.scene.add(rootObject);
690687
this.setupCamera(rootObject); // TODO: this could introduce issues if the new root is not compatible
691688

0 commit comments

Comments
 (0)