2019-07-03 00:08:06 +08:00
|
|
|
import * as THREE from "three";
|
|
|
|
import {
|
|
|
|
Color,
|
|
|
|
HemisphereLight,
|
|
|
|
MOUSE,
|
|
|
|
PerspectiveCamera,
|
|
|
|
Scene,
|
|
|
|
Vector3,
|
|
|
|
WebGLRenderer,
|
|
|
|
} from "three";
|
|
|
|
import OrbitControlsCreator from "three-orbit-controls";
|
2019-05-29 00:40:29 +08:00
|
|
|
|
|
|
|
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);
|
2019-07-03 00:08:06 +08:00
|
|
|
this.controls = new OrbitControls(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;
|
|
|
|
|
2019-07-03 00:08:06 +08:00
|
|
|
this.scene.background = new Color(0x151c21);
|
2019-05-29 20:46:29 +08:00
|
|
|
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-03 02:56:33 +08:00
|
|
|
set_size(width: number, height: number): void {
|
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-03 02:56:33 +08:00
|
|
|
protected reset_camera(position: Vector3, look_at: Vector3): void {
|
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-03 02:56:33 +08:00
|
|
|
protected render(): void {
|
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-07-03 00:08:06 +08:00
|
|
|
};
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|