import * as THREE from 'https://unpkg.com/three@0.126.1/build/three.module.js' import { OrbitControls } from 'https://unpkg.com/three@0.126.1/examples/jsm/controls/OrbitControls.js' import { CSS2DRenderer, CSS2DObject } from 'https://unpkg.com/three@0.126.1/examples/jsm/renderers/CSS2DRenderer.js' let camera, scene, renderer, renderer2d, wreck const SCALE = 10000 init() animate() function Wreck(vec3) { const point = new THREE.Object3D() point.position.copy(vec3) const div = document.createElement('div') div.className = 'wreck' const w = document.importNode(wreck, true) div.appendChild(w) const label = new CSS2DObject(div) point.add(label) return point; } function loadWreckIcon() { return fetch("wreck.svg") .then(response => response.text()) .then(text => { const parser = new window.DOMParser() const svg = parser.parseFromString(text, "image/svg+xml") wreck = svg.documentElement return wreck }) } function processKillmails(killmails) { let center = new THREE.Vector3() const positions = killmails.map(km => km.victim.position) positions.forEach(pos => center.add(new THREE.Vector3(pos.x, pos.y, pos.z))) center.divideScalar(positions.length) killmails.forEach(km => { const pos = km.victim.position const vec3 = new THREE.Vector3(pos.x, pos.y, pos.z) vec3.sub(center) vec3.divideScalar(SCALE) scene.add(Wreck(vec3)) }) } function init() { camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 0.1, 100) camera.position.set(8, 8, 8) camera.lookAt(0, 0, 0) scene = new THREE.Scene() loadWreckIcon() .then(() => { const url = new URL(window.location.href) return fetch(url.searchParams.get("related") + ".json") }) .then(response => response.json()) .then(killmails => { const url = km => `https://esi.evetech.net/latest/killmails/${km.id}/${km.hash}/?datasource=tranquility` const retrieve = km => fetch(url(km)).then(response => response.json()) return Promise.all(killmails.map(retrieve)) }) .then(processKillmails) const grid = new THREE.GridHelper(30, 30) scene.add(grid) renderer = new THREE.WebGLRenderer({antialias: true}) renderer.setSize(window.innerWidth, window.innerHeight) document.body.appendChild(renderer.domElement) renderer2d = new CSS2DRenderer() renderer2d.setSize(window.innerWidth, window.innerHeight) renderer2d.domElement.style.position = 'absolute' renderer2d.domElement.style.top = '0px' document.body.appendChild(renderer2d.domElement) const controls = new OrbitControls(camera, renderer2d.domElement) controls.minDistance = 2 controls.maxDistance = 24 const loader = new THREE.TextureLoader() loader.loadAsync("https://i.imgur.com/rDGOLFC.jpg") // TODO: Don't use imgur as CDN. .then(skybox => { const rt = new THREE.WebGLCubeRenderTarget(skybox.image.height) rt.fromEquirectangularTexture(renderer, skybox) scene.background = rt }); window.addEventListener('resize', onWindowResize) } function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight camera.updateProjectionMatrix() renderer.setSize(window.innerWidth, window.innerHeight) renderer2d.setSize(window.innerWidth, window.innerHeight) } function animate() { requestAnimationFrame(animate) renderer.render(scene, camera) renderer2d.render(scene, camera) }