UNPKG

playcanvas

Version:

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

136 lines (135 loc) 4.84 kB
export type GSplatVaryingDescriptor = { /** * - The varying name. Must be a valid shader identifier. */ name: string; /** * - The component data type: {@link TYPE_FLOAT32}, {@link TYPE_INT32} or * {@link TYPE_UINT32}. */ type: number; /** * - The number of components, 1 to 4. */ components: number; }; /** * Manages custom varying streams for the gsplat render customization. Streams added here generate * set functions available to the `gsplatModifyVS` shader chunk, where they run once per splat, * and matching get functions available to the `gsplatModifyPS` shader chunk, where the per-splat * value can be read for each rendered fragment. * * Access the instance via {@link GSplatParams#varyings}. * * @category Graphics */ export class GSplatVaryings { /** * Creates a new GSplatVaryings instance. * * @param {GraphicsDevice} device - The graphics device. * @ignore */ constructor(device: GraphicsDevice); /** * @type {GraphicsDevice} * @private */ private _device; /** * @type {GSplatVaryingDescriptor[]} * @private */ private _streams; /** * @type {number} * @private */ private _words; /** * @type {number} * @private */ private _version; /** * Gets the varying stream descriptors. Do not modify the returned array. * * @type {GSplatVaryingDescriptor[]} */ get streams(): GSplatVaryingDescriptor[]; /** * The number of u32 words the varying streams add to the per-splat projection cache of the * {@link GSPLAT_RENDERER_RASTER_GPU_SORT} renderer. * * @type {number} * @ignore */ get words(): number; /** * The version of the varying streams, incremented on every change. * * @type {number} * @ignore */ get version(): number; /** * Adds varying streams. For each stream, a set function (`set<Name>`) is generated and made * available to the `gsplatModifyVS` shader chunk, where it runs once per splat, and a * matching get function (`get<Name>`) is made available to the `gsplatModifyPS` shader * chunk, where the per-splat value can be read for each rendered fragment. * * Supported types are {@link TYPE_FLOAT32}, {@link TYPE_INT32} and {@link TYPE_UINT32}, with * 1 to 4 components. Adding or removing streams rebuilds the gsplat shaders. * * Note: on some platforms each component is stored in per-splat video memory, so its size * scales with the number of rendered splats. Keep the data as compact as possible - prefer * fewer components, and consider bit-packing multiple small values into a single uint * component instead of using separate streams. * * @param {GSplatVaryingDescriptor[]} streams - The streams to add. * @example * // Add a per-splat flag, written once per splat in gsplatModifyVS using setFlag(value), * // and read per fragment in gsplatModifyPS using getFlag() * app.scene.gsplat.varyings.add([{ * name: 'flag', * type: TYPE_UINT32, * components: 1 * }]); */ add(streams: GSplatVaryingDescriptor[]): void; /** * Removes varying streams previously added by {@link GSplatVaryings#add}. * * @param {string[]} names - The names of the streams to remove. */ remove(names: string[]): void; /** * Marks the streams as changed: recomputes the cache word count eagerly (so consumers never * see a stale value) and bumps the version. The shader chunks are regenerated and applied to * the material once per frame by the engine via {@link GSplatVaryings#apply}. * * @private */ private _changed; /** * Generates the shader chunks implementing the varying streams: declarations and set * functions for the vertex stage (and its compute projector equivalent), declarations and * get functions for the fragment stage, and the projection cache read / write code used by * the hybrid renderer. * * @returns {object} The generated chunk sources. * @private */ private _generateChunks; /** * Regenerates the shader chunks and applies them to the material, from where the renderers * pick them up (and rebuild shaders) via the existing chunk synchronization. Consumers track * {@link GSplatVaryings#version} to call this only when the streams changed. * * @param {ShaderMaterial} material - The gsplat material to apply the chunks to. * @ignore */ apply(material: ShaderMaterial): void; } import type { ShaderMaterial } from '../materials/shader-material.js'; import type { GraphicsDevice } from '../../platform/graphics/graphics-device.js';