@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
106 lines (105 loc) • 3.58 kB
JavaScript
"use strict";
import { ThreejsViewer } from "./Threejs";
import { PathTracingRendererContainer } from "../nodes/rop/utils/pathTracing/PathTracingRendererContainer";
export class PathTracingViewer extends ThreejsViewer {
constructor() {
super(...arguments);
this._isRecording = false;
this._recordingSamplesPerFrame = 500;
this._animateWebBound = this._animateWeb.bind(this);
}
// private _rendering: boolean = false;
// protected override _renderFunc: RenderFuncWithDeltaAsync | undefined;
_setupFunctions(options) {
const data = super._setupFunctions(options);
if (data) {
const { renderer, renderScene, camera } = data;
if (renderer instanceof PathTracingRendererContainer) {
this._renderFunc = () => {
if (this._isRecording) {
if (renderer.pbrRenderAllowed()) {
renderer.render(renderScene, camera);
this.updateDebugDisplay();
if (renderer.samplesCount() >= this._recordingSamplesPerFrame && this._onFrameCompleted) {
renderer.markAsNotGenerated();
this._onFrameCompleted();
}
}
} else {
renderer.render(renderScene, camera);
this.updateDebugDisplay();
}
};
renderer.generate(renderScene);
}
}
}
mount(element) {
var _a;
super.mount(element);
if (this._errorMessage) {
return;
}
if (!this._renderer) {
return;
}
if (this._renderer.displayDebug) {
this._debugElement = document.createElement("div");
this._debugElement.style.position = "absolute";
this._debugElement.style.top = "5px";
this._debugElement.style.right = "5px";
this._debugElement.style.padding = "2px 5px";
this._debugElement.style.color = "black";
this._debugElement.style.backgroundColor = "white";
this._debugElement.style.opacity = "60%";
this._debugElement.style.fontSize = "0.6rem";
this._debugElement.style.textAlign = "right";
this._debugElement.style.fontFamily = "monospace";
(_a = this._domElement) == null ? void 0 : _a.append(this._debugElement);
this.updateDebugDisplay();
}
}
setRecordingState(options) {
var _a;
this._isRecording = options.isRecording;
this._onFrameCompleted = options.onFrameCompleted;
this._recordingSamplesPerFrame = options.recordingSamplesPerFrame != null ? options.recordingSamplesPerFrame : this._recordingSamplesPerFrame;
(_a = this._renderer) == null ? void 0 : _a.reset();
}
updateDebugDisplay() {
var _a;
if (!this._debugElement) {
return;
}
const samples = ((_a = this._renderer) == null ? void 0 : _a.samplesCount()) || 0;
this._debugElement.innerText = `${samples}`.padStart(3, "0");
}
dispose() {
var _a, _b;
(_a = this._renderer) == null ? void 0 : _a.dispose();
super.dispose();
(_b = this._debugElement) == null ? void 0 : _b.remove();
}
_animateWeb() {
if (!this._doRender) {
return;
}
this.__animateCommon__();
}
_postRender(delta) {
const renderer = this._renderer;
if (!renderer) {
return;
}
this._runOnBeforeRenderCallbacks(delta, renderer);
if (this._renderFunc) {
this._renderFunc(delta);
}
if (this._renderCSSFunc) {
this._renderCSSFunc();
}
this.controlsController().update(delta);
this._runOnAfterRenderCallbacks(delta, renderer);
this._requestAnimationFrameId = requestAnimationFrame(this._animateWebBound);
}
}