Public
Edited
Nov 11, 2023
1 star
Also listed in…
Big data visualization
Insert cell
Insert cell
Insert cell
renderer.domElement
Insert cell
{
while (true) {
renderer.render(scene, camera);

const elapsedTime = clock.getElapsedTime();

// Update material
material.uniforms.uTime.value = elapsedTime;

yield null;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
generateGalaxy()
Insert cell
generateGalaxy = () => {
disposeScene(scene, renderer);

/**
* Geometry
*/
const geometry = new THREE.BufferGeometry();

const positions = new Float32Array(parameters.count * 3);
const colors = new Float32Array(parameters.count * 3);
const scales = new Float32Array(parameters.count * 1);
const randomness = new Float32Array(parameters.count * 3);

const insideColor = new THREE.Color(parameters.insideColor);
const outsideColor = new THREE.Color(parameters.outsideColor);

for (let i = 0; i < parameters.count; i++) {
const i3 = i * 3;

// Position
const radius = Math.random() * parameters.radius;

const branchAngle =
((i % parameters.branches) / parameters.branches) * Math.PI * 2;

const randomX =
Math.pow(Math.random(), parameters.randomnessPower) *
(Math.random() < 0.5 ? 1 : -1) *
parameters.randomness *
radius;
const randomY =
Math.pow(Math.random(), parameters.randomnessPower) *
(Math.random() < 0.5 ? 1 : -1) *
parameters.randomness *
radius;
const randomZ =
Math.pow(Math.random(), parameters.randomnessPower) *
(Math.random() < 0.5 ? 1 : -1) *
parameters.randomness *
radius;

positions[i3] = Math.cos(branchAngle) * radius;
positions[i3 + 1] = 0;
positions[i3 + 2] = Math.sin(branchAngle) * radius;

// Randomness
randomness[i3] = randomX;
randomness[i3 + 1] = randomY;
randomness[i3 + 2] = randomZ;

// Color
const mixedColor = insideColor.clone();
mixedColor.lerp(outsideColor, radius / parameters.radius);

colors[i3] = mixedColor.r;
colors[i3 + 1] = mixedColor.g;
colors[i3 + 2] = mixedColor.b;

// Scale
scales[i] = Math.random();
}

geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
geometry.setAttribute("aScale", new THREE.BufferAttribute(scales, 1));
geometry.setAttribute(
"aRandomness",
new THREE.BufferAttribute(randomness, 3)
);

/**
* Points
*/
const points = new THREE.Points(geometry, material);
scene.add(points);
}
Insert cell
material = new THREE.ShaderMaterial({
depthWrite: false,
blending: THREE.AdditiveBlending,
vertexColors: true,
vertexShader: galaxyVertexShader,
fragmentShader: galaxyFragmentShader,
uniforms: {
uTime: { value: 0 },
uSize: { value: 30 * renderer.getPixelRatio() }
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
scene = {
const scene = new THREE.Scene();
return scene;
}
Insert cell
parameters = ({
count: 200000,
size: 0.005,
radius: 5,
branches: 3,
spin: 1,
randomness: 0.5,
randomnessPower: 3,
insideColor: "#ff6030",
outsideColor: "#1b3984"
})
Insert cell
Insert cell
Insert cell
Insert cell
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