2019-05-29 00:40:29 +08:00
|
|
|
import * as THREE from 'three';
|
2019-07-01 01:55:30 +08:00
|
|
|
import { Color, HemisphereLight, MOUSE, PerspectiveCamera, Scene, Vector3, WebGLRenderer } from 'three';
|
2019-05-29 00:40:29 +08:00
|
|
|
import OrbitControlsCreator from 'three-orbit-controls';
|
|
|
|
|
|
|
|
const OrbitControls = OrbitControlsCreator(THREE);
|
|
|
|
|
|
|
|
export class Renderer {
|
2019-07-01 01:55:30 +08:00
|
|
|
protected renderer = new WebGLRenderer({ antialias: true });
|
|
|
|
protected camera: PerspectiveCamera;
|
|
|
|
protected controls: any;
|
|
|
|
protected scene = new Scene();
|
2019-05-29 20:46:29 +08:00
|
|
|
|
2019-05-29 23:04:06 +08:00
|
|
|
constructor() {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.camera = new PerspectiveCamera(75, 1, 0.1, 5000);
|
|
|
|
this.controls = new OrbitControls(
|
2019-07-01 01:55:30 +08:00
|
|
|
this.camera,
|
|
|
|
this.renderer.domElement
|
|
|
|
);
|
2019-05-29 20:46:29 +08:00
|
|
|
this.controls.mouseButtons.ORBIT = MOUSE.RIGHT;
|
|
|
|
this.controls.mouseButtons.PAN = MOUSE.LEFT;
|
|
|
|
|
|
|
|
this.scene.background = new Color(0x151C21);
|
|
|
|
this.scene.add(new HemisphereLight(0xffffff, 0x505050, 1));
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
requestAnimationFrame(this.render_loop);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
get dom_element(): HTMLElement {
|
2019-05-29 20:46:29 +08:00
|
|
|
return this.renderer.domElement;
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
set_size(width: number, height: number) {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.renderer.setSize(width, height);
|
|
|
|
this.camera.aspect = width / height;
|
|
|
|
this.camera.updateProjectionMatrix();
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
protected reset_camera(position: Vector3, look_at: Vector3) {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.controls.reset();
|
2019-07-01 01:55:30 +08:00
|
|
|
this.camera.position.copy(position);
|
|
|
|
this.camera.lookAt(look_at);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
protected render() {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.controls.update();
|
|
|
|
this.renderer.render(this.scene, this.camera);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 01:55:30 +08:00
|
|
|
private render_loop = () => {
|
|
|
|
this.render();
|
|
|
|
requestAnimationFrame(this.render_loop);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
}
|