phantasmal-world/src/rendering/Renderer.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as THREE from "three";
import {
Color,
HemisphereLight,
MOUSE,
PerspectiveCamera,
Scene,
Vector3,
WebGLRenderer,
} from "three";
import OrbitControlsCreator from "three-orbit-controls";
const OrbitControls = OrbitControlsCreator(THREE);
export class Renderer {
protected renderer = new WebGLRenderer({ antialias: true });
protected camera: PerspectiveCamera;
protected controls: any;
protected scene = new Scene();
constructor() {
this.camera = new PerspectiveCamera(75, 1, 0.1, 5000);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
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));
requestAnimationFrame(this.render_loop);
}
get dom_element(): HTMLElement {
return this.renderer.domElement;
}
2019-07-03 02:56:33 +08:00
set_size(width: number, height: number): void {
this.renderer.setSize(width, height);
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
}
2019-07-03 02:56:33 +08:00
protected reset_camera(position: Vector3, look_at: Vector3): void {
this.controls.reset();
this.camera.position.copy(position);
this.camera.lookAt(look_at);
}
2019-07-03 02:56:33 +08:00
protected render(): void {
this.controls.update();
this.renderer.render(this.scene, this.camera);
}
private render_loop = () => {
this.render();
requestAnimationFrame(this.render_loop);
};
}