# Web (Spark.js)

Render Gaussian splats and RGBD point clouds in React with Spark.

Use [Spark](https://sparkjs.dev/docs/) to render Gaussian splats and point
clouds in a React web app. Serve your files from your app's public directory.

The [World Labs JavaScript client](https://www.npmjs.com/package/@worldlabsai/client)
exports `TaskAsset` for file references and `PosedRGBDAsset` for calibrated RGBD
frames. Each frame contains `imageAsset`, `camera`, and `depth`.

## Gaussian splats

An `.spz` file is ready to render. For a `TaskAsset`, use its `url` to load the
file. Install `three`, `@react-three/fiber`, `@react-three/drei`, and
`@sparkjsdev/spark` in your React app:

```jsx
import { Canvas, extend, useThree } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
import { SparkRenderer, SplatMesh } from "@sparkjsdev/spark";

const Spark = extend(SparkRenderer);
const Splats = extend(SplatMesh);

function Scene() {
  const gl = useThree((state) => state.gl);
  return (
    <>
      <Spark args={[{ renderer: gl }]} />
      <Splats args={[{ url: "/scene.spz" }]} />
      <OrbitControls />
    </>
  );
}

export default function Viewer() {
  return (
    <Canvas style={{ height: "100vh" }} camera={{ position: [0, 0, 5] }}>
      <Scene />
    </Canvas>
  );
}
```

Replace the example path with your SPZ file's URL and move the camera to
frame your scene. Add a `Splats` element for each additional file.

## Point clouds from RGBD images

A `PosedRGBDAsset` contains the ingredients for a colored point cloud:

- **RGB** (`imageAsset`) gives each point its color.
- **Depth** (`depth.depthAsset` for linear EXR, or `depth.logDepthAsset` for log-depth PNG) gives each pixel its distance along the camera's forward axis.
- **Confidence** (`depth.confidenceAsset`), when present, lets you filter unreliable points.

Use `camera.intrinsics` to turn depth pixels into 3D points, then
apply each camera's pose to place them in the same world. See
[Cameras and posed images](/cameras-and-posed-images) for depth decoding and
coordinate conventions.

Once decoded, render the points as small spherical splats. This React component
accepts world-space `points` with `{ x, y, z, r, g, b }` and RGB values from
0 to 255, after any confidence filtering:

```jsx
import { useMemo } from "react";
import { extend } from "@react-three/fiber";
import { SplatMesh } from "@sparkjsdev/spark";
import * as THREE from "three";

const Splats = extend(SplatMesh);

export function PointCloud({ points, radius = 0.01 }) {
  const options = useMemo(
    () => ({
      constructSplats(splats) {
        for (const { x, y, z, r, g, b } of points) {
          splats.pushSplat(
            new THREE.Vector3(x, y, z),
            new THREE.Vector3(radius, radius, radius),
            new THREE.Quaternion(),
            1,
            new THREE.Color().setRGB(r / 255, g / 255, b / 255, THREE.SRGBColorSpace),
          );
        }
      },
    }),
    [points, radius],
  );
  return <Splats args={[options]} />;
}
```

In `Scene`, replace the SPZ `Splats` element with `<PointCloud points={points} />`;
keep the Spark renderer and controls. This previews the observed surfaces in
the RGBD images. For more, see Spark's
[React example](https://github.com/sparkjsdev/spark-react-r3f) and
[procedural splats](https://sparkjs.dev/docs/procedural-splats/).