69 lines
2.6 KiB
TypeScript
69 lines
2.6 KiB
TypeScript
import * as THREE from 'three';
|
|
import Renderer from './renderer/renderer.class';
|
|
import Asset from './assets/asset.class';
|
|
import Quadtree from './physics/quadtree.class';
|
|
import { FRUSTUMSIZE } from './consts';
|
|
import Input from './renderer/input.class';
|
|
import { Random, clamp } from './common';
|
|
import Selector from './renderer/selector.class';
|
|
|
|
if(!ArrayBuffer.prototype.hasOwnProperty("transferToFixedLength"))
|
|
{
|
|
throw new Error("Your web browser doesn't includes the latest features needed to make this website work properly.\nPlease upgrade your browser and try again.");
|
|
}
|
|
|
|
Renderer.init();
|
|
Input.init(Renderer.canvas);
|
|
Selector.init();
|
|
|
|
const r = new Random(0);
|
|
|
|
const quad = new Quadtree({x1: -FRUSTUMSIZE * Renderer.aspect / 2, x2: FRUSTUMSIZE * Renderer.aspect / 2, y1: -FRUSTUMSIZE / 2, y2: FRUSTUMSIZE / 2});
|
|
|
|
const assets: Asset[] = [];
|
|
for(let i = 0; i < 10000; i++)
|
|
{
|
|
assets[i] = new Asset(new THREE.Matrix4(), 1);
|
|
|
|
assets[i]
|
|
.move(r.nextFloat(-0.5 * FRUSTUMSIZE * Renderer.aspect, 0.5 * FRUSTUMSIZE * Renderer.aspect), r.nextFloat(-0.5 * FRUSTUMSIZE, 0.5 * FRUSTUMSIZE))
|
|
.rotate(r.nextFloat(Math.PI * 2))
|
|
.scale(r.nextFloat(0.01, 0.15), r.nextFloat(0.01, 0.15))
|
|
|
|
Asset.instance.setMatrixAt(i, assets[i].mat);
|
|
quad.insert(i, assets[i]);
|
|
}
|
|
|
|
Asset.instance.count = assets.length;
|
|
|
|
Asset.instance.computeBoundingBox();
|
|
Asset.instance.computeBoundingSphere();
|
|
|
|
Renderer.scene.add(Asset.instance);
|
|
|
|
Renderer.startRendering();
|
|
|
|
Input.onDragStart((_, button) => { if(button & 1) Selector.hide(); });
|
|
Input.onDragEnd((start, end, button) => {
|
|
if(button & 1)
|
|
{
|
|
const s = performance.now();
|
|
const selection = quad.query({x1: Math.min(start.x, end.x), x2: Math.max(start.x, end.x), y1: Math.min(start.y, end.y), y2: Math.max(start.y, end.y)}).map(e => assets[e]);
|
|
console.log("Fetching %s out of %s elements in %sms", selection.length, assets.length, performance.now() - s);
|
|
|
|
if(Input.keys['Shift']) Selector.toggle(selection);
|
|
else Selector.select(selection);
|
|
}
|
|
});
|
|
Input.onDrag((delta, start, end, button) => { if(button & 1) Selector.preview(start, end); else Renderer.move(-delta.x, -delta.y); });
|
|
Input.onClick((point, button) => {
|
|
if(button & 1)
|
|
{
|
|
const selection = quad.fetch(point.x, point.y).map(e => assets[e]);
|
|
|
|
if(Input.keys['Shift']) Selector.toggle(selection);
|
|
else Selector.select(selection);
|
|
}
|
|
});
|
|
Input.onWheel(delta => Renderer.zoom = clamp(Renderer.zoom * 1 + (delta * -0.001), 1, 5));
|
|
Input.onMove(p => { if(!Input.dragging) Selector.ghost(assets[quad.fetch(p.x, p.y)[0]]); }); |