# GSplatComponent

Class · extends [`Component`](https://api.playcanvas.com/engine/classes/Component.md) · category: Graphics

Source: https://github.com/playcanvas/engine/blob/dfcc50fbbfba2388843041a875ec0ef5d1a586c5/src/framework/components/gsplat/component.js#L71

The GSplatComponent enables an [Entity](https://api.playcanvas.com/engine/classes/Entity.md) to render 3D Gaussian Splats. Splats are always
loaded from [Asset](https://api.playcanvas.com/engine/classes/Asset.md)s rather than being created programmatically. The asset type is
`gsplat` which supports multiple file formats including `.ply`, `.sog`, `.meta.json` (SOG
format), and `.lod-meta.json` (streaming LOD format).

You should never need to use the GSplatComponent constructor directly. To add a
GSplatComponent to an [Entity](https://api.playcanvas.com/engine/classes/Entity.md), use [Entity#addComponent](https://api.playcanvas.com/engine/classes/Entity.md#addcomponent):

```javascript
const entity = new Entity();
entity.addComponent('gsplat', {
    asset: asset
});
```

Once the GSplatComponent is added to the entity, you can access it via the [Entity#gsplat](https://api.playcanvas.com/engine/classes/Entity.md#gsplat)
property:

```javascript
entity.gsplat.customAabb = new BoundingBox(new Vec3(), new Vec3(10, 10, 10));

console.log(entity.gsplat.customAabb);
```

Relevant Engine API examples:

- [Simple Splat Loading](https://playcanvas.github.io/#/gaussian-splatting/simple)
- [Billions of Splats](https://playcanvas.github.io/#/gaussian-splatting/billions)
- [Downtown Streaming](https://playcanvas.github.io/#/gaussian-splatting/downtown)
- [Global Sorting](https://playcanvas.github.io/#/gaussian-splatting/global-sorting)
- [LOD Instances](https://playcanvas.github.io/#/gaussian-splatting/lod-instances)
- [LOD Streaming](https://playcanvas.github.io/#/gaussian-splatting/lod-streaming)
- [LOD Streaming with Spherical Harmonics](https://playcanvas.github.io/#/gaussian-splatting/lod-streaming-sh)
- [Multi-Splat](https://playcanvas.github.io/#/gaussian-splatting/multi-splat)
- [Multi-View](https://playcanvas.github.io/#/gaussian-splatting/multi-view)
- [Picking](https://playcanvas.github.io/#/gaussian-splatting/picking)
- [Reveal Effect](https://playcanvas.github.io/#/gaussian-splatting/reveal)
- [Shader Effects](https://playcanvas.github.io/#/gaussian-splatting/shader-effects)
- [Spherical Harmonics](https://playcanvas.github.io/#/gaussian-splatting/spherical-harmonics)

## Accessors

### asset

```ts
get asset(): number | Asset
set asset(value: number | Asset)
```

Gets the gsplat asset id for this gsplat component.

### castShadows

```ts
get castShadows(): boolean
set castShadows(value: boolean)
```

Gets whether gsplat will cast shadows for lights that have shadow casting enabled.

### customAabb

```ts
get customAabb(): BoundingBox | null
set customAabb(value: BoundingBox | null)
```

Gets the custom object space bounding box for visibility culling of the attached gsplat.
Returns the custom AABB if set, otherwise falls back to the resource's AABB.

### id

```ts
get id(): number
```

Gets the unique identifier for this component. This ID is used by the picking system
and is also written to the work buffer when `app.scene.gsplat.enableIds` is enabled, making
it available to custom shaders for effects like highlighting or animation.

### layers

```ts
get layers(): number[]
set layers(value: number[])
```

Gets the array of layer IDs ([Layer#id](https://api.playcanvas.com/engine/classes/Layer.md#id)) to which this gsplat belongs.

### lodFalloff

```ts
get lodFalloff(): number
set lodFalloff(value: number)
```

Gets how quickly this splat's level of detail drops with distance from the camera.

### lodRangeMax

```ts
get lodRangeMax(): number
set lodRangeMax(value: number)
```

Gets the maximum allowed LOD index.

### lodRangeMin

```ts
get lodRangeMin(): number
set lodRangeMin(value: number)
```

Gets the minimum allowed LOD index.

### resource

```ts
get resource(): GSplatResourceBase | null
set resource(value: GSplatResourceBase | null)
```

Gets the GSplat resource. Returns the directly set resource if available,
otherwise returns the resource from the assigned asset.

### workBufferUpdate

```ts
get workBufferUpdate(): number
set workBufferUpdate(value: number)
```

Gets the work buffer update mode.

## Methods

### deleteParameter

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

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

**Parameters**

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

### getInstanceTexture

```ts
getInstanceTexture(name: string): Texture | null
```

Gets an instance texture by name. Instance textures are per-component textures defined
in the resource's format with `storage: GSPLAT_STREAM_INSTANCE`.

**Parameters**

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

**Returns** [`Texture`](https://api.playcanvas.com/engine/classes/Texture.md) `| null`: The texture, or null if not found.

**Example**

```ts
// Add an instance stream to the resource format
resource.format.addExtraStreams([
    { name: 'instanceTint', format: PIXELFORMAT_RGBA8, storage: GSPLAT_STREAM_INSTANCE }
]);

// Get the instance texture and fill it with data
const texture = entity.gsplat.getInstanceTexture('instanceTint');
if (texture) {
    const data = texture.lock();
    // Fill texture data...
    texture.unlock();
}
```

### getParameter

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

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

**Parameters**

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

**Returns** `number | number[] | ArrayBufferView<ArrayBufferLike> | undefined`: The parameter value, or undefined if not set.

### hide

```ts
hide(): void
```

Stop rendering this component without removing its mesh instance from the scene hierarchy.

### setParameter

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

Sets a shader parameter for this gsplat instance. Parameters set here are applied
during rendering.

**Parameters**

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

### setWorkBufferModifier

```ts
setWorkBufferModifier(value: { glsl?: string; wgsl?: string } | null): void
```

Sets custom shader code for modifying splats when written to the work buffer.

Must provide all three functions:
- `modifySplatCenter`: Modify the splat center position
- `modifySplatRotationScale`: Modify the splat rotation and scale
- `modifySplatColor`: Modify the splat color

Calling this method automatically triggers a work buffer re-render.

**Parameters**

- `value` (`{ glsl?: string; wgsl?: string } | null`): The modifier code for GLSL and/or WGSL.

**Example**

```ts
entity.gsplat.setWorkBufferModifier({
    glsl: `
        void modifySplatCenter(inout vec3 center) {}
        void modifySplatRotationScale(vec3 originalCenter, vec3 modifiedCenter, inout vec4 rotation, inout vec3 scale) {}
        void modifySplatColor(vec3 center, inout vec4 color) { color.rgb *= vec3(1.0, 0.0, 0.0); }
    `,
    wgsl: `
        fn modifySplatCenter(center: ptr<function, vec3f>) {}
        fn modifySplatRotationScale(originalCenter: vec3f, modifiedCenter: vec3f, rotation: ptr<function, vec4f>, scale: ptr<function, vec3f>) {}
        fn modifySplatColor(center: vec3f, color: ptr<function, vec4f>) { (*color).r = 1.0; (*color).g = 0.0; (*color).b = 0.0; }
    `
});
```

### show

```ts
show(): void
```

Enable rendering of the component if hidden using [hide](https://api.playcanvas.com/engine/classes/GSplatComponent.md#hide).

## Inherited from [Component](https://api.playcanvas.com/engine/classes/Component.md)

- `entity: Entity`
- `system: ComponentSystem`
- `get enabled(): boolean` · `set enabled(value: boolean)`
- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
