@shopware-ag/dive
Version:
Shopware Spatial Framework
59 lines (58 loc) • 2.79 kB
JavaScript
var g = Object.defineProperty;
var p = (c, t, e) => t in c ? g(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[t] = e;
var a = (c, t, e) => p(c, typeof t != "symbol" ? t + "" : t, e);
import { RenderTarget as y, SRGBColorSpace as w } from "three/webgpu";
import { D as b } from "../../chunks/PerspectiveCamera-35cBnxwG.mjs";
class T {
constructor(t, e, r) {
a(this, "_renderer");
a(this, "_scene");
a(this, "_controller");
this._renderer = t, this._scene = e, this._controller = r;
}
async generateMedia(t) {
const { position: e, target: r, resolution: i } = t, { width: o, height: s } = i, h = this._controller.object.position.clone(), d = this._controller.object.quaternion.clone(), l = this._controller.target.clone(), n = this._renderer.canvas.clientWidth || o, _ = this._renderer.canvas.clientHeight || s;
try {
return "onResize" in this._controller.object && this._controller.object.onResize(o, s), this._controller.object.position.copy(e), this._controller.target.copy(r), this._controller.update(), (await this.drawCanvas(void 0, i)).toDataURL();
} finally {
this._controller.object.position.copy(h), this._controller.object.quaternion.copy(d), this._controller.target.copy(l), this._controller.update(), "onResize" in this._controller.object && this._controller.object.onResize(n, _);
}
}
async drawCanvas(t, e) {
await this._renderer.initAsync();
const r = this._renderer.webgpurenderer, i = Math.max(
1,
(e == null ? void 0 : e.width) ?? (t == null ? void 0 : t.width) ?? (t == null ? void 0 : t.clientWidth) ?? this._renderer.canvas.clientWidth
), o = Math.max(
1,
(e == null ? void 0 : e.height) ?? (t == null ? void 0 : t.height) ?? (t == null ? void 0 : t.clientHeight) ?? this._renderer.canvas.clientHeight
), s = new y(i, o, {
colorSpace: w
}), h = r.getRenderTarget(), d = this._controller.object.layers.mask;
try {
r.setRenderTarget(s), this._controller.object.layers.mask = b.LIVE_VIEW_LAYER_MASK, r.render(this._scene, this._controller.object);
const l = await r.readRenderTargetPixelsAsync(
s,
0,
0,
i,
o
), n = t ?? document.createElement("canvas");
return n.width = i, n.height = o, this._writePixelsToCanvas(n, l, i, o), n;
} finally {
this._controller.object.layers.mask = d, r.setRenderTarget(h), s.dispose();
}
}
_writePixelsToCanvas(t, e, r, i) {
const o = t.getContext("2d");
if (!o)
throw new Error(
"MediaCreator.drawCanvas: 2D canvas context is not available."
);
const s = o.createImageData(r, i);
s.data.set(new Uint8ClampedArray(Array.from(e))), o.putImageData(s, 0, 0);
}
}
export {
T as MediaCreator
};