playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
133 lines (132 loc) • 3.38 kB
JavaScript
import { GSplatStreams } from "../gsplat/gsplat-streams.js";
import { WORKBUFFER_UPDATE_AUTO, 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;
}
_lodRangeMin = 0;
_lodRangeMax = 99;
set lodRangeMin(value) {
if (this._lodRangeMin !== value) {
this._lodRangeMin = value;
this.lodDirty = true;
}
}
get lodRangeMin() {
return this._lodRangeMin;
}
set lodRangeMax(value) {
if (this._lodRangeMax !== value) {
this._lodRangeMax = value;
this.lodDirty = true;
}
}
get lodRangeMax() {
return this._lodRangeMax;
}
_aabb = null;
parameters = null;
_streams = null;
lodDirty = false;
dirtyVersion = 0;
_workBufferUpdate = WORKBUFFER_UPDATE_AUTO;
_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.dirtyVersion++;
}
get workBufferModifier() {
return this.parentPlacement?.workBufferModifier ?? this._workBufferModifier;
}
set workBufferUpdate(value) {
if (value === WORKBUFFER_UPDATE_ONCE) {
this.dirtyVersion++;
} else {
this._workBufferUpdate = value;
}
}
get workBufferUpdate() {
return this._workBufferUpdate;
}
markDirty() {
this.dirtyVersion++;
}
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
};