playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
115 lines (112 loc) • 3.6 kB
JavaScript
import { GSplatStreams } from '../gsplat/gsplat-streams.js';
import { WORKBUFFER_UPDATE_ALWAYS, WORKBUFFER_UPDATE_ONCE, WORKBUFFER_UPDATE_AUTO } from '../constants.js';
import { GsplatAllocId } from './gsplat-alloc-id.js';
class GSplatPlacement {
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;
}
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 undefined;
}
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);
}
}
constructor(resource, node, lodIndex = 0, parameters = null, parentPlacement = null, id = null){
this.intervals = new Map();
this.id = 0;
this.allocId = GsplatAllocId.get();
this.lodIndex = 0;
this._lodBaseDistance = 5;
this._lodMultiplier = 3;
this._aabb = null;
this.parameters = null;
this._streams = null;
this.lodDirty = false;
this.renderDirty = false;
this.workBufferUpdate = WORKBUFFER_UPDATE_AUTO;
this._lastFormatVersion = -1;
this._workBufferModifier = null;
this.parentPlacement = 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;
}
}
export { GSplatPlacement };