# Post-processing

Add bloom or another TSL effect to a view or shared scene.



Pass a Three.js `RenderPipeline` when registering a view or scene. PortalGL
handles sizing and drawing it as part of the normal `dom.update(time)` call.

## Add bloom to a card [#add-bloom-to-a-card]

Save this factory as `bloom-pipeline.ts`. It creates the scene pass and effect
once; the returned cleanup releases everything it creates.

```ts title="bloom-pipeline.ts"
import { pass, vec4 } from 'three/tsl'
import { bloom } from 'three/addons/tsl/display/BloomNode.js'
import {
  RenderPipeline,
  type PerspectiveCamera,
  type Scene,
  type WebGPURenderer,
} from 'three/webgpu'

export function createBloomPipeline(
  renderer: WebGPURenderer,
  scene: Scene,
  camera: PerspectiveCamera
) {
  const scenePass = pass(scene, camera)
  const color = scenePass.getTextureNode('output')
  const glow = bloom(color, 0.8, 0.3, 0.2)
  const pipeline = new RenderPipeline(renderer)
  pipeline.outputNode = vec4(color.rgb.add(glow.rgb), color.a)

  return {
    pipeline,
    glow,
    destroy() {
      pipeline.dispose()
      glow.dispose()
      scenePass.dispose()
    },
  }
}
```

In the [quickstart](./getting-started.md), add these imports:

```ts
import { Color } from 'three/webgpu'
import { createBloomPipeline } from './bloom-pipeline'
```

Then replace its `dom.addView()` call with:

```ts
scene.background = new Color('#111119')
const effects = createBloomPipeline(renderer, scene, camera)

dom.addView(element, {
  scene,
  camera,
  pipeline: effects.pipeline,
  onFrame: ({ delta }) => {
    cube.rotation.y += delta * 0.5
  },
})
```

Add `effects.destroy()` immediately after `dom.destroy()` in the quickstart's
cleanup function. Keep its `prepare()` and frame loop as they are.

To tune bloom later, change `effects.glow.strength.value`. Reuse the graph instead
of rebuilding it each frame.

## Apply effects to shared objects [#apply-effects-to-shared-objects]

Use `dom.addScene({ scene, camera, pipeline: effects.pipeline })` instead of
`addView()`. Every object tracked by that shared scene goes through the same
effects. The pipeline does not process other views or scenes.

Create a separate pipeline and scene-pass graph for each registration, using the
same renderer as `ThreeDOM`. Each graph must include its scene and camera, as the
factory above does. Do not call `pipeline.render()` separately.

## Transparency and clipping [#transparency-and-clipping]

The example uses an opaque background so bloom is visible around the cube.
For a transparent overlay, use an alpha-enabled renderer, keep `scene.background`
null, and decide what alpha the effect should output. Preserving the original
alpha also preserves its silhouette; adding bloom RGB alone will not reveal a
halo outside it.

A view's `clip` crops the final image, including effects, without changing camera
framing. Object clipping in a shared scene happens before post-processing, so
blur or bloom can spread beyond the object's window.

Keep the pipeline's default output color transform unless your graph explicitly
applies its own. PortalGL does not add a second conversion.

## Preparation and cleanup [#preparation-and-cleanup]

`await dom.prepare()` prepares sized views using their real effect graphs.
Pause the application loop while it runs and wait for it to settle before
disposing pipelines or assets. Preparation draws can advance temporal effects;
reset their history if your effect requires it.

PortalGL owns its internal targets. Your application owns the pipeline and its
passes, so keep the factory's cleanup alongside the view's cleanup.
