UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

117 lines (116 loc) 3.34 kB
import { GSplatStreams } from "../gsplat/gsplat-streams.js"; import { WORKBUFFER_UPDATE_AUTO, WORKBUFFER_UPDATE_ALWAYS, WORKBUFFER_UPDATE_ONCE } from "../constants.js"; import { GsplatAllocId } from "./gsplat-alloc-id.js"; class GSplatPlacement { resource; node; intervals = /* @__PURE__ */ new Map(); id = 0; allocId = GsplatAllocId.get(); lodIndex = 0; _lodBaseDistance = 5; _lodMultiplier = 3; set lodBaseDistance(value) { if (this._lodBaseDistance !== value) { this._lodBaseDistance = value; this.lodDirty = true; } } get lodBaseDistance() { return this._lodBaseDistance; } set lodMultiplier(value) { if (this._lodMultiplier !== value) { this._lodMultiplier = value; this.lodDirty = true; } } get lodMultiplier() { return this._lodMultiplier; } _aabb = null; parameters = null; _streams = null; lodDirty = false; renderDirty = false; workBufferUpdate = WORKBUFFER_UPDATE_AUTO; _lastFormatVersion = -1; _workBufferModifier = null; parentPlacement = null; constructor(resource, node, lodIndex = 0, parameters = null, parentPlacement = null, id = null) { this.id = id ?? parentPlacement?.id ?? 0; this.resource = resource; this.node = node; this.lodIndex = lodIndex; this.parameters = parameters ?? parentPlacement?.parameters ?? null; this.parentPlacement = parentPlacement; } destroy() { this._streams?.destroy(); this._streams = null; this.intervals.clear(); this.resource = null; } set workBufferModifier(value) { this._workBufferModifier = value; this.renderDirty = true; } get workBufferModifier() { return this.parentPlacement?.workBufferModifier ?? this._workBufferModifier; } consumeRenderDirty() { const format = this.resource?.format; if (format && this._lastFormatVersion !== format.extraStreamsVersion) { this._lastFormatVersion = format.extraStreamsVersion; this.renderDirty = true; } if (this.workBufferUpdate === WORKBUFFER_UPDATE_ALWAYS) { this.renderDirty = true; } else if (this.workBufferUpdate === WORKBUFFER_UPDATE_ONCE) { this.renderDirty = true; this.workBufferUpdate = WORKBUFFER_UPDATE_AUTO; } const dirty = this.renderDirty; this.renderDirty = false; return dirty; } set aabb(aabb) { this._aabb = aabb?.clone() ?? null; } get aabb() { const aabb = this._aabb ?? this.resource?.aabb; return aabb; } getLodDistance(level) { return this.lodBaseDistance * Math.pow(this.lodMultiplier, level); } getInstanceTexture(name, device) { const resource = this.resource; if (!resource?.format) { return void 0; } if (!this._streams && resource.format.instanceStreams.length > 0) { this._streams = new GSplatStreams(device, true); this._streams.textureDimensions.copy(resource.streams.textureDimensions); this._streams.syncWithFormat(resource.format); } return this._streams?.getTexture(name); } get streams() { return this.parentPlacement?.streams ?? this._streams; } ensureInstanceStreams(device) { const resource = this.resource; if (!resource?.format) { return; } if (!this._streams && resource.format.instanceStreams.length > 0) { this._streams = new GSplatStreams(device, true); this._streams.textureDimensions.copy(resource.streams.textureDimensions); this._streams.syncWithFormat(resource.format); } } } export { GSplatPlacement };