Published
Edited
Aug 27, 2021
7 stars
Insert cell
Insert cell
Insert cell
data = [
[3, 2, 1],
[6, 5, 4],
[5, 7, 6]
]
Insert cell
Insert cell
mutable i = 1
Insert cell
Insert cell
loader = new THREE.TextureLoader()
Insert cell
Insert cell
function update() {
/*********** PUT ANIMATION LOGIC HERE **********/
//cubeGroup.rotation.x += 0.001;
cubeGroup.rotation.y += 0.001;
//cubeGroup.rotation.z += 0.001;
/***********************************************/
}
Insert cell
Insert cell
function render(scene) {
renderer.render(scene, camera);
}
Insert cell
renderer = {
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(width, height);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.gammaFactor = 2.2;
renderer.gammaOutput = true;
renderer.physicallyCorrectLights = true;
return renderer;
}
Insert cell
Insert cell
cubeGroup = {
const group = new THREE.Group();
data.forEach((row,i,iarr)=>{
row.forEach((d,j,jarr)=>{
cubeMeshes[i][j].position.set(
i/iarr.length-0.5,
d/max*0.5-0.6,
j/jarr.length-0.5);
//cubeMeshes[i][j].scale.set(1,4,1);
group.add(cubeMeshes[i][j]);
})
})


return group;
}
Insert cell
Insert cell
cubeMeshes = {
const cubeMeshes = cubeGeometries.map(row=>{
return row.map(cubeGeometry => new THREE.Mesh( cubeGeometry, cubeMaterial ))
})


return cubeMeshes;
}
Insert cell
Insert cell
cubeMaterial ={
const cube = new THREE.MeshStandardMaterial({
map:loader.load('https://threejsfundamentals.org/threejs/lessons/resources/images/compressed-but-large-wood-texture.jpg')
});
cube.color.convertSRGBToLinear();
return cube;
}
Insert cell
Insert cell
cubeGeometries = data.map(row=>{
return row.map(c=>{
return new THREE.BoxGeometry( 0.2, c/max, 0.2 );
})
})
Insert cell
Insert cell
mainLight = {
const light = new THREE.DirectionalLight(0xffffff, 3.0);
light.position.set(10, 10, 10);
return light;
}
Insert cell
ambientLight = {
const light = new THREE.HemisphereLight(0xddeeff, 0x202020, 3);
return light;
}
Insert cell
Insert cell
height = 500
Insert cell
Insert cell
fov = 18
Insert cell
aspect = width / height
Insert cell
near = 0.1
Insert cell
far = 1000
Insert cell
camera = {
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(-4, 1, 4);
return camera;
}
Insert cell
Insert cell
max = Math.max.apply(Math, data.map(d=>Math.max.apply(this,d)))
Insert cell
Insert cell
THREE = {
const THREE = window.THREE = await require("three@0.99.0/build/three.min.js");
await require("three@0.99.0/examples/js/controls/OrbitControls.js").catch(() => {});
return window.THREE;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more