cerebellum-alcohol-infographic/javascript/brain.js

84 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2021-10-18 09:49:37 +00:00
const scene = new THREE.Scene();
const width = 400;
const height = 300;
const camera = new THREE.PerspectiveCamera( 75, width / height, 0.01, 1000 );
camera.position.set(-0.8, 0.11, -0.9);
scene.background = new THREE.Color('white');
const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
const canvas = document.getElementById('brain-container');
canvas.appendChild( renderer.domElement );
// Controls
const controls = new THREE.OrbitControls(camera, canvas);
controls.enablePan = false;
controls.enableZoom = false;
controls.autoRotate = true;
controls.target.set(0, 0.5, 0);
controls.update();
2021-10-18 09:49:37 +00:00
// Lights
{
const color = 0xFFFFFF;
2021-10-18 19:28:00 +00:00
const intensity = 0.6;
2021-10-18 09:49:37 +00:00
const light = new THREE.AmbientLight(color, intensity);
2021-10-18 19:28:00 +00:00
scene.add(light);
2021-10-18 09:49:37 +00:00
}
// Upper light
{
const color = 0xFFFFFF;
2021-10-18 19:28:00 +00:00
const intensity = 1;
2021-10-18 09:49:37 +00:00
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(0, 5, 0);
light.target.position.set(0, 0.5, 0);
scene.add(light);
scene.add(light.target);
}
// Lower light
{
const color = 0xFFFFFF;
const intensity = 2;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(0, -5, 0);
light.target.position.set(0, 0.5, 0);
scene.add(light);
scene.add(light.target);
}
2021-10-18 19:25:35 +00:00
var brain = null;
2021-10-18 09:49:37 +00:00
const objLoader = new THREE.GLTFLoader();
objLoader.load('assets/brain.gltf', (root) => {
2021-10-18 19:25:35 +00:00
console.log(root.scene.children[2]);
brain = root.scene.children[2];
brain.children[5].material.emissive = new THREE.Color('black');
scene.add(brain);
2021-10-18 09:49:37 +00:00
});
2021-10-18 19:25:35 +00:00
const cerebellumHighlightColor = new THREE.Color(0x9D6720);
const originalColor = new THREE.Color('black');
var highlightColors = [cerebellumHighlightColor, originalColor];
var highlightProgress = 0;
var highlightIndex = 0;
2021-10-18 09:49:37 +00:00
function render() {
renderer.render(scene, camera);
controls.update();
2021-10-18 09:49:37 +00:00
2021-10-18 19:25:35 +00:00
if (brain) {
highlightProgress += 0.025;
brain.children[5].material.emissive.lerp(highlightColors[highlightIndex], highlightProgress);
if (highlightProgress >= 1) {
highlightProgress = 0;
highlightIndex = (highlightIndex + 1) % 2;
}
}
2021-10-18 09:49:37 +00:00
requestAnimationFrame(render);
}
requestAnimationFrame(render);