skinview3d/src/viewer.ts

195 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-06-17 05:10:30 +02:00
import { applyMixins, CapeContainer, ModelType, SkinContainer, RemoteImage, TextureSource } from "skinview-utils";
import { NearestFilter, PerspectiveCamera, Scene, Texture, Vector2, WebGLRenderer } from "three";
2020-01-24 12:34:50 +01:00
import { RootAnimation } from "./animation.js";
import { PlayerObject } from "./model.js";
2018-07-17 20:49:00 +02:00
2020-01-26 20:39:29 +01:00
export type LoadOptions = {
2020-05-29 10:34:58 +02:00
/**
2020-09-09 07:50:09 +02:00
* Whether to make the object visible after the texture is loaded. Default is true.
2020-05-29 10:34:58 +02:00
*/
2020-01-26 20:39:29 +01:00
makeVisible?: boolean;
2020-05-29 10:34:58 +02:00
}
2018-07-17 20:49:00 +02:00
2020-06-14 09:45:36 +02:00
export type SkinViewerOptions = {
width?: number;
height?: number;
skin?: RemoteImage | TextureSource;
cape?: RemoteImage | TextureSource;
2020-09-09 07:50:09 +02:00
/**
* Whether the canvas contains an alpha buffer. Default is true.
* This option can be turned off if you use an opaque background.
*/
alpha?: boolean;
2020-09-09 07:50:09 +02:00
/**
* Render target.
* A new canvas is created if this parameter is unspecified.
*/
canvas?: HTMLCanvasElement;
/**
* Whether to preserve the buffers until manually cleared or overwritten. Default is false.
*/
preserveDrawingBuffer?: boolean;
2020-06-14 09:45:36 +02:00
}
2020-01-26 20:39:29 +01:00
function toMakeVisible(options?: LoadOptions): boolean {
if (options && options.makeVisible === false) {
return false;
}
return true;
}
2018-07-17 20:49:00 +02:00
2020-01-26 20:39:29 +01:00
class SkinViewer {
readonly canvas: HTMLCanvasElement;
2020-01-26 20:39:29 +01:00
readonly scene: Scene;
readonly camera: PerspectiveCamera;
readonly renderer: WebGLRenderer;
readonly playerObject: PlayerObject;
readonly animations: RootAnimation = new RootAnimation();
2018-07-17 20:49:00 +02:00
2020-08-23 17:35:36 +02:00
readonly skinCanvas: HTMLCanvasElement;
readonly capeCanvas: HTMLCanvasElement;
2020-01-26 20:39:29 +01:00
private readonly skinTexture: Texture;
private readonly capeTexture: Texture;
2018-07-17 20:49:00 +02:00
2020-02-01 13:44:10 +01:00
private _disposed: boolean = false;
2019-12-27 12:04:32 +01:00
private _renderPaused: boolean = false;
constructor(options: SkinViewerOptions = {}) {
this.canvas = options.canvas === undefined ? document.createElement("canvas") : options.canvas;
2020-06-14 09:45:36 +02:00
2017-10-01 14:00:45 +02:00
// texture
this.skinCanvas = document.createElement("canvas");
this.skinTexture = new Texture(this.skinCanvas);
this.skinTexture.magFilter = NearestFilter;
this.skinTexture.minFilter = NearestFilter;
2017-10-01 14:00:45 +02:00
this.capeCanvas = document.createElement("canvas");
this.capeTexture = new Texture(this.capeCanvas);
this.capeTexture.magFilter = NearestFilter;
this.capeTexture.minFilter = NearestFilter;
2017-10-01 14:00:45 +02:00
this.scene = new Scene();
2017-10-01 14:00:45 +02:00
// Use smaller fov to avoid distortion
this.camera = new PerspectiveCamera(40);
2017-10-01 14:00:45 +02:00
this.camera.position.y = -12;
this.camera.position.z = 60;
2017-10-01 14:00:45 +02:00
this.renderer = new WebGLRenderer({
canvas: this.canvas,
alpha: options.alpha !== false, // default: true
preserveDrawingBuffer: options.preserveDrawingBuffer === true // default: false
});
2020-08-23 13:58:48 +02:00
this.renderer.setPixelRatio(window.devicePixelRatio);
2017-10-01 14:00:45 +02:00
this.playerObject = new PlayerObject(this.skinTexture, this.capeTexture);
2019-04-20 16:08:49 +02:00
this.playerObject.name = "player";
this.playerObject.skin.visible = false;
this.playerObject.cape.visible = false;
2017-10-01 14:00:45 +02:00
this.scene.add(this.playerObject);
2020-01-26 20:39:29 +01:00
window.requestAnimationFrame(() => this.draw());
2020-06-14 09:45:36 +02:00
2020-06-17 03:08:51 +02:00
if (options.skin !== undefined) {
2020-06-17 05:10:30 +02:00
this.loadSkin(options.skin);
2020-06-17 03:08:51 +02:00
}
if (options.cape !== undefined) {
2020-06-17 05:10:30 +02:00
this.loadCape(options.cape);
2020-06-14 09:45:36 +02:00
}
2020-06-17 03:08:51 +02:00
if (options.width !== undefined) {
this.width = options.width;
}
if (options.height !== undefined) {
this.height = options.height;
}
2020-01-26 20:39:29 +01:00
}
2018-07-02 13:52:36 +02:00
2020-01-26 20:39:29 +01:00
protected skinLoaded(model: ModelType, options?: LoadOptions): void {
this.skinTexture.needsUpdate = true;
this.playerObject.skin.modelType = model;
if (toMakeVisible(options)) {
2017-10-01 14:00:45 +02:00
this.playerObject.skin.visible = true;
2020-01-26 20:39:29 +01:00
}
}
2017-10-01 14:00:45 +02:00
2020-01-26 20:39:29 +01:00
protected capeLoaded(options?: LoadOptions): void {
this.capeTexture.needsUpdate = true;
if (toMakeVisible(options)) {
2017-10-01 14:00:45 +02:00
this.playerObject.cape.visible = true;
2020-01-30 18:35:02 +01:00
}
2019-12-27 12:04:32 +01:00
}
2020-06-14 09:45:36 +02:00
protected resetSkin(): void {
this.playerObject.skin.visible = false;
}
protected resetCape(): void {
this.playerObject.cape.visible = false;
}
2020-02-01 12:13:46 +01:00
private draw(): void {
2019-12-27 12:04:32 +01:00
if (this.disposed || this._renderPaused) {
return;
}
2019-12-31 17:34:00 +01:00
this.animations.runAnimationLoop(this.playerObject);
this.doRender();
2019-12-27 12:04:32 +01:00
window.requestAnimationFrame(() => this.draw());
2017-10-01 14:00:45 +02:00
}
2020-02-01 12:13:46 +01:00
protected doRender(): void {
this.renderer.render(this.scene, this.camera);
}
2020-02-01 12:13:46 +01:00
setSize(width: number, height: number): void {
2017-10-01 14:00:45 +02:00
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(width, height);
}
2020-02-01 12:13:46 +01:00
dispose(): void {
2020-02-01 13:44:10 +01:00
this._disposed = true;
2017-10-01 14:00:45 +02:00
this.renderer.dispose();
this.skinTexture.dispose();
this.capeTexture.dispose();
}
2020-02-01 13:44:10 +01:00
get disposed(): boolean {
return this._disposed;
}
2020-02-01 12:13:46 +01:00
get renderPaused(): boolean {
2019-12-27 12:04:32 +01:00
return this._renderPaused;
}
set renderPaused(value: boolean) {
const toResume = !this.disposed && !value && this._renderPaused;
this._renderPaused = value;
if (toResume) {
window.requestAnimationFrame(() => this.draw());
}
}
2020-02-01 12:13:46 +01:00
get width(): number {
2020-02-01 13:43:02 +01:00
return this.renderer.getSize(new Vector2()).width;
}
2017-10-01 14:00:45 +02:00
2020-02-01 12:13:46 +01:00
set width(newWidth: number) {
this.setSize(newWidth, this.height);
}
2017-10-01 14:00:45 +02:00
2020-02-01 12:13:46 +01:00
get height(): number {
2020-02-01 13:43:02 +01:00
return this.renderer.getSize(new Vector2()).height;
}
2017-10-01 14:00:45 +02:00
2020-02-01 12:13:46 +01:00
set height(newHeight: number) {
this.setSize(this.width, newHeight);
}
2017-10-02 15:29:41 +02:00
}
2020-01-26 20:39:29 +01:00
interface SkinViewer extends SkinContainer<LoadOptions>, CapeContainer<LoadOptions> { }
applyMixins(SkinViewer, [SkinContainer, CapeContainer]);
export { SkinViewer };