Skip to content

Instantly share code, notes, and snippets.

@JT5D
Created August 22, 2016 01:49
Show Gist options
  • Save JT5D/4f2caf448b923b88a21c9fcb63788d75 to your computer and use it in GitHub Desktop.
Save JT5D/4f2caf448b923b88a21c9fcb63788d75 to your computer and use it in GitHub Desktop.
3D Multiverse Visualization
<div id="canvas"></div>
var renderer, scene, camera, pointCloud;
var screenW = window.innerWidth;
var screenH = window.innerHeight;
var spdx = 0,
spdy = 0;
var mouseX = 0, mouseY = 0, mouseDown = false;
// mouse
document.addEventListener('mousemove', function(event) {
mouseX = event.clientX;
mouseY = event.clientY;
}, false);
document.body.addEventListener("mousedown", function(event) {
mouseDown = true;
}, false);
document.body.addEventListener("mouseup", function(event) {
mouseDown = false;
}, false);
init();
animate();
function init() {
// dom
var container = document.getElementById('canvas');
// renderer
renderer = new THREE.WebGLRenderer({
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
// scene
scene = new THREE.Scene();
//camera
camera = new THREE.PerspectiveCamera(85, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 50;
// geometry
var geometry = new THREE.TorusKnotGeometry(150, 200, 500, 200);
// vertex colors
var colors = [];
for (var i = 0; i < geometry.vertices.length; i++) {
// blue color
colors[i] = new THREE.Color();
colors[i].setHSL(0.55, Math.random()*2, Math.random()); //to change to white:set third value to 1,random color:set first value to random
}
geometry.colors = colors;
// material
material = new THREE.PointsMaterial({
size: 2,
vertexColors: THREE.VertexColors
});
// point cloud
pointCloud = new THREE.Points(geometry, material);
scene.add(pointCloud);
}
function animate() {
spdy = (screenH / 2 - mouseY) / 40;
spdx = (screenW / 2 - mouseX) / 40;
// rotate on mousedown
if (mouseDown) {
pointCloud.rotation.x = spdy;
pointCloud.rotation.y = spdx;
}
requestAnimationFrame(animate);
render();
}
function render() {
// rotate
pointCloud.rotation.x += 0.0005;
pointCloud.rotation.y += 0.0005;
// render
renderer.render(scene, camera);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js"></script>
canvas {
background: #100a1c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment