Image planes

Make a textured plane match an HTML rectangle.

Use a projected quad for images, video textures, or plane-based shaders. Unlike a 3D object binding, it matches the full width and height of the element.

See the GLSync quad images demo for image planes that follow HTML images as you scroll and resize, with custom fluid, edge-bending, and particle effects.

Attach an image

This helper uses the dom from getting started and an already-loaded color texture. Add it before starting the loop, or while the loop is running; the plane stays hidden until its bounds are ready.

import type { ThreeDOM } from 'portalgl/three'
import {
  Mesh,
  MeshBasicNodeMaterial,
  PerspectiveCamera,
  PlaneGeometry,
  Scene,
  type Texture,
} from 'three/webgpu'

export function addImagePlane(dom: ThreeDOM, element: HTMLElement, texture: Texture) {
  const scene = new Scene()
  const camera = new PerspectiveCamera(45, 1, 0.1, 100)
  camera.position.z = 4

  const geometry = new PlaneGeometry(1, 1)
  const material = new MeshBasicNodeMaterial({ map: texture })
  const mesh = new Mesh(geometry, material)
  scene.add(mesh)

  const world = dom.addScene({ scene, camera })
  const binding = world.track(element, { depth: 4 })
  binding.bindQuad(mesh)

  return {
    binding,
    destroy() {
      world.destroy()
      scene.remove(mesh)
      geometry.dispose()
      material.dispose()
      // The supplied texture can be shared; its owner disposes it.
    },
  }
}

Load an image and use the helper:

import { SRGBColorSpace, TextureLoader } from 'three'
import { addImagePlane } from './add-image-plane'

const texture = await new TextureLoader().loadAsync('/photo.jpg')
texture.colorSpace = SRGBColorSpace
const image = addImagePlane(dom, element, texture)

// On removal, when this is the texture's only consumer:
// image.destroy()
// texture.dispose()

Give the element the image's aspect ratio to avoid stretching. For object-fit: cover behavior, adjust UVs or crop in your shader; PortalGL only controls the plane's transform.

Use your own mesh or shader

bindQuad() accepts a centered unit XY plane, including a subdivided PlaneGeometry(1, 1, 32, 32). Put it directly in the registered scene and keep the scene root untransformed. PortalGL updates its position, rotation, size, and visibility each frame.

Quad bindings do not add clipping. A shader can deform the plane beyond its original rectangle. Use an appropriate shader mask when you need a strict crop.

Read the rectangle in world space

if (binding.valid) {
  const { position, quaternion, scale } = binding.quad
  // position is the center; quaternion faces the camera.
  // scale.x and scale.y are the full world-space width and height.
}

valid means bounds are available, not that the element is on screen. Treat binding.quad as read-only. Use .clone(false) if you need a snapshot that stops following layout changes.

Destroying the scene registration releases its bindings. To release just one mesh, save the handle returned by bindQuad() and call its destroy() method; the mesh stays in the scene and regains its original transform.