playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
81 lines (78 loc) • 2.57 kB
JavaScript
import { Vec2 } from '../../core/math/vec2.js';
import { Texture } from '../../platform/graphics/texture.js';
import { TextureUtils } from '../../platform/graphics/texture-utils.js';
class GSplatStreams {
get textureDimensions() {
return this._textureDimensions;
}
destroy() {
for (const texture of this.textures.values()){
texture.destroy();
}
this.textures.clear();
}
init(format, numElements) {
this.format = format;
this._textureDimensions = TextureUtils.calcTextureSize(numElements, new Vec2());
const streams = this._isInstance ? format.instanceStreams : format.resourceStreams;
for (const stream of streams){
const texture = this.createTexture(stream.name, stream.format, this._textureDimensions);
this.textures.set(stream.name, texture);
}
this._formatVersion = format.extraStreamsVersion;
}
getTexture(name) {
this.syncWithFormat(this.format);
return this.textures.get(name);
}
getTexturesInOrder() {
const result = [];
if (this.format) {
const allStreams = this._isInstance ? this.format.instanceStreams : this.format.resourceStreams;
for (const stream of allStreams){
const texture = this.textures.get(stream.name);
if (texture) {
result.push(texture);
}
}
}
return result;
}
syncWithFormat(format) {
if (format) {
if (this.format === format && this._formatVersion === format.extraStreamsVersion) {
return;
}
this.format = format;
const streams = this._isInstance ? format.instanceStreams : format.resourceStreams;
for (const stream of streams){
if (!this.textures.has(stream.name)) {
const texture = this.createTexture(stream.name, stream.format, this._textureDimensions);
this.textures.set(stream.name, texture);
}
}
this._formatVersion = format.extraStreamsVersion;
}
}
resize(width, height) {
this._textureDimensions.set(width, height);
for (const texture of this.textures.values()){
texture.resize(width, height);
}
}
createTexture(name, format, size, data) {
return Texture.createDataTexture2D(this.device, name, size.x, size.y, format, data ? [
data
] : undefined);
}
constructor(device, isInstance = false){
this.format = null;
this.textures = new Map();
this._textureDimensions = new Vec2();
this._isInstance = false;
this._formatVersion = -1;
this.device = device;
this._isInstance = isInstance;
}
}
export { GSplatStreams };