# GSplatProcessor

Class · category: Graphics

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/framework/gsplat/gsplat-processor.js#L104

GSplatProcessor enables GPU-based processing of Gaussian Splat data using custom shader code.
Gaussian Splats store per-splat attributes (position, rotation, scale, color, spherical harmonics)
in texture streams. This processor reads from source streams and writes results to destination
streams, enabling operations like painting, selection marking, or custom data transforms.

Custom streams can be added to loaded gsplat resources via [GSplatFormat#addExtraStreams](https://api.playcanvas.com/engine/classes/GSplatFormat.md#addextrastreams),
or you can create fully procedural splat data using [GSplatContainer](https://api.playcanvas.com/engine/classes/GSplatContainer.md).

The source and destination can reference the same resource or component, as long as the read and
write streams don't overlap (you cannot read and write the same stream in one pass).

By default (when source streams are not specified), the processor provides access to the format's
built-in getCenter(), getRotation(), getScale(), and getColor() functions for reading splat data.
Note: getCenter() must be called first as it loads shared data used by the other functions.

Custom uniforms can be passed to the shader via [setParameter](https://api.playcanvas.com/engine/classes/GSplatProcessor.md#setparameter), including scalar values,
vectors, and additional textures for effects like brush patterns or lookup tables.

The following built-in uniforms are available in processing shaders:
- `srcNumSplats` (uint) - Number of splats in source resource
- `dstNumSplats` (uint) - Number of splats in destination resource

**Example**

```ts
// Create a processor that reads splat positions and writes to a customColor texture
const processor = new GSplatProcessor(
    app.graphicsDevice,
    { component: entity.gsplat },  // source: all streams auto-bound
    { component: entity.gsplat, streams: ['customColor'] }, // destination: customColor stream only
    {
        processGLSL: `
            uniform vec4 uPaintSphere;
            uniform vec4 uPaintColor;

            void process() {
                vec3 center = getCenter();
                float dist = distance(center, uPaintSphere.xyz);
                if (dist < uPaintSphere.w) {
                    writeCustomColor(uPaintColor);
                } else {
                    writeCustomColor(vec4(0.0));
                }
            }
        `,
        processWGSL: `
            uniform uPaintSphere: vec4f;
            uniform uPaintColor: vec4f;

            fn process() {
                let center = getCenter();
                let dist = distance(center, uniform.uPaintSphere.xyz);
                if (dist < uniform.uPaintSphere.w) {
                    writeCustomColor(uniform.uPaintColor);
                } else {
                    writeCustomColor(vec4f(0.0));
                }
            }
        `
    }
);

// Set uniforms and execute
processor.setParameter('uPaintSphere', [0, 1, 0, 0.5]);
processor.setParameter('uPaintColor', [1, 0, 0, 1]);
processor.process();
```

## Constructors

### constructor

```ts
new GSplatProcessor(device: GraphicsDevice, source: GSplatProcessorBinding, destination: GSplatProcessorBinding, options: object)
```

Creates a new GSplatProcessor instance.

**Parameters**

- `device` ([`GraphicsDevice`](https://api.playcanvas.com/engine/classes/GraphicsDevice.md)): The graphics device.
- `source` ([`GSplatProcessorBinding`](https://api.playcanvas.com/engine/interfaces/GSplatProcessorBinding.md)): Source configuration specifying where to read from.
  Can specify resource directly or component (for instance textures).
- `destination` ([`GSplatProcessorBinding`](https://api.playcanvas.com/engine/interfaces/GSplatProcessorBinding.md)): Destination configuration specifying where to write.
  Can specify resource directly or component (for instance textures).
- `options` (`object`): Shader options for the processing logic.
    - `options.processGLSL` (`string`, optional): GLSL code at module scope. Must define a `void process()`
      function that implements the processing logic. Can include uniform declarations and helper functions.
    - `options.processWGSL` (`string`, optional): WGSL code at module scope. Must define a `fn process()`
      function that implements the processing logic. Can include uniform declarations and helper functions.

## Properties

### blendState

```ts
blendState: BlendState = BlendState.NOBLEND
```

The blend state to use when processing. Allows accumulation of results
(e.g., additive blending for painting). Defaults to no blending.

## Methods

### deleteParameter

```ts
deleteParameter(name: string): void
```

Removes a shader parameter.

**Parameters**

- `name` (`string`): The name of the parameter to remove.

### destroy

```ts
destroy(): void
```

Destroys this processor and releases all resources.

### getParameter

```ts
getParameter(name: string): number | number[] | ArrayBufferView<ArrayBufferLike> | Texture | StorageBuffer | undefined
```

Gets a shader parameter value previously set with [setParameter](https://api.playcanvas.com/engine/classes/GSplatProcessor.md#setparameter).

**Parameters**

- `name` (`string`): The name of the parameter.

**Returns** `number | number[] | ArrayBufferView<ArrayBufferLike> |` [`Texture`](https://api.playcanvas.com/engine/classes/Texture.md) `|` [`StorageBuffer`](https://api.playcanvas.com/engine/classes/StorageBuffer.md) `| undefined`: The parameter value, or undefined if not set.

### process

```ts
process(): void
```

Executes the processing, reading from source streams and writing to destination streams.

### setParameter

```ts
setParameter(name: string, data: number | number[] | ArrayBufferView<ArrayBufferLike> | Texture | StorageBuffer): void
```

Sets a shader parameter for this processor. Parameters are applied during processing.

**Parameters**

- `name` (`string`): The name of the parameter (uniform name in shader).
- `data` (`number | number[] | ArrayBufferView<ArrayBufferLike> |` [`Texture`](https://api.playcanvas.com/engine/classes/Texture.md) `|` [`StorageBuffer`](https://api.playcanvas.com/engine/classes/StorageBuffer.md)): The value for the parameter.
