2020-08-23 19:16:37 +02:00
|
|
|
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
|
2021-06-25 20:33:44 +02:00
|
|
|
import { FullScreenQuad } from "three/examples/jsm/postprocessing/Pass.js";
|
2020-08-23 19:16:37 +02:00
|
|
|
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
|
|
|
|
|
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
|
|
|
|
|
import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader.js";
|
|
|
|
|
import { SkinViewer, SkinViewerOptions } from "./viewer.js";
|
|
|
|
|
|
|
|
|
|
export class FXAASkinViewer extends SkinViewer {
|
|
|
|
|
|
|
|
|
|
readonly composer: EffectComposer;
|
|
|
|
|
readonly renderPass: RenderPass;
|
|
|
|
|
readonly fxaaPass: ShaderPass;
|
|
|
|
|
|
2020-09-16 16:39:42 +02:00
|
|
|
constructor(options?: SkinViewerOptions) {
|
2021-08-24 20:07:24 +02:00
|
|
|
// Force options.alpha to false, because FXAA is incompatible with transparent backgrounds
|
|
|
|
|
if (options === undefined) {
|
2021-09-19 07:54:27 +02:00
|
|
|
options = { alpha: false, background: "white" };
|
2021-08-24 20:07:24 +02:00
|
|
|
} else {
|
|
|
|
|
options.alpha = false;
|
2021-09-19 07:54:27 +02:00
|
|
|
if (options.background === undefined) {
|
|
|
|
|
options.background = "white";
|
|
|
|
|
}
|
2021-08-24 20:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-06 12:27:18 +02:00
|
|
|
super(options);
|
2020-08-23 19:16:37 +02:00
|
|
|
this.composer = new EffectComposer(this.renderer);
|
|
|
|
|
this.renderPass = new RenderPass(this.scene, this.camera);
|
|
|
|
|
this.fxaaPass = new ShaderPass(FXAAShader);
|
|
|
|
|
this.composer.addPass(this.renderPass);
|
|
|
|
|
this.composer.addPass(this.fxaaPass);
|
|
|
|
|
this.updateComposerSize();
|
2021-08-24 20:07:24 +02:00
|
|
|
|
|
|
|
|
// Default background: white
|
|
|
|
|
this.renderer.setClearColor("white");
|
2020-08-23 19:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSize(width: number, height: number): void {
|
|
|
|
|
super.setSize(width, height);
|
|
|
|
|
if (this.composer !== undefined) {
|
|
|
|
|
this.updateComposerSize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateComposerSize(): void {
|
|
|
|
|
this.composer.setSize(this.width, this.height);
|
|
|
|
|
const pixelRatio = this.renderer.getPixelRatio();
|
|
|
|
|
this.composer.setPixelRatio(pixelRatio);
|
|
|
|
|
this.fxaaPass.material.uniforms["resolution"].value.x = 1 / (this.width * pixelRatio);
|
|
|
|
|
this.fxaaPass.material.uniforms["resolution"].value.y = 1 / (this.height * pixelRatio);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 18:23:32 +02:00
|
|
|
render(): void {
|
2020-08-23 19:16:37 +02:00
|
|
|
this.composer.render();
|
|
|
|
|
}
|
2020-09-16 16:34:22 +02:00
|
|
|
|
|
|
|
|
dispose(): void {
|
|
|
|
|
super.dispose();
|
2021-06-25 20:33:44 +02:00
|
|
|
(this.fxaaPass.fsQuad as FullScreenQuad).dispose();
|
2020-09-16 16:34:22 +02:00
|
|
|
}
|
2020-08-23 19:16:37 +02:00
|
|
|
}
|