Docs
Guides

Style objects by readings

Colour and scale objects by their current state, and show when a sensor has gone quiet.

The default look encodes correspondence and freshness. To show your own state, give the renderer an objectStyle function. It runs for every object whenever the object, its readings or its staleness change.

style-by-readings.ts
import type { StyleFn } from "@klorad/engine-three";

/** Colours a room by how full it is, and greys it out when its counter goes quiet. */
export const occupancyStyle: StyleFn = ({ object, readings, stale, palette }) => {
  if (stale) return { color: palette.stale, opacity: 0.6 };
  const people = readings.occupancy?.value;
  if (typeof people !== "number") return { color: object.color ?? palette.object };
  const load = people / Number(object.properties.capacity ?? 50);
  const color = load > 0.9 ? "#dc2626" : load > 0.6 ? "#f59e0b" : palette.shadow;
  return { color, heightScale: Math.max(0.2, Math.min(load, 1.5)) };
};

The function receives:

Prop

Type

and returns any of color, opacity, heightScale, glow, pose and label. Anything it leaves out keeps the default, and the view eases from one answer to the next.

States that are not a colour

Some states are better shown than coloured. pose adds degrees to an object's orientation: a door that is open is drawn open, a barrier that is up is drawn up. glow makes an object emit its colour, and on a glTF model it scales the parts the model was authored to emit light with, so a lamp lights and dims. label writes text beside the object, which is how a reading gets onto the picture.

Do not let colour carry a state alone: some people cannot tell the colours apart, and a stale reading in grey looks like any other grey. Pair the colour with a label (the value, or "No data"), and with a list or a table beside the view for the states that matter.

site-style.ts
import type { StyleFn } from "@klorad/engine-three";

/** A door that swings open, a lamp that lights the ground, and every reading written beside its object. */
export const siteStyle: StyleFn = ({ object, readings, stale, palette }) => {
  if (stale) return { color: palette.stale, opacity: 0.7, label: `${object.name}: no fresh reading` };
  // A lamp: glow from 0 to 1. On a glTF model it scales the parts authored to emit light.
  if (object.properties.kind === "lamp") return { glow: Number(readings.level?.value ?? 0) / 100, label: `${readings.level?.value ?? 0} %` };
  // A door hinged at its position: an open one is turned 80 degrees. The door's orientation in the world does not change.
  if (readings.door?.value === "open") return { color: "#e8a33d", pose: { heading: 80 }, label: "open" };
  return { color: palette.shadow, label: object.name };
};

A pose turns the object about its own position. To swing a door on its hinge or lift an arm at its post, give the object an extrusion whose footprint starts at the hinge. The Digital Shadow and Actions pages of the sandbox do both.

In React

rooms-view.tsx
"use client";

import { useState } from "react";
import { defaultStyle } from "@klorad/engine-three";
import { ThreeView } from "@klorad/engine-three/react";
import { occupancyStyle } from "./style-by-readings";

// The style may change between renders and the view follows. Its other options are read when it mounts.
export function RoomsView() {
  const [byOccupancy, setByOccupancy] = useState(true);
  return (
    <>
      <ThreeView objectStyle={byOccupancy ? occupancyStyle : defaultStyle} palette={{ background: "#0b1220" }} />
      <button onClick={() => setByOccupancy((on) => !on)}>{byOccupancy ? "Show correspondence" : "Show occupancy"}</button>
    </>
  );
}

ThreeView follows its objectStyle prop: pass another function and the view eases to the new looks. A style may therefore read the state of the component that passes it, such as which quantity a person chose to colour by. The other options are read when the view mounts.

Keep it pure

A style function is called often and must not change anything: no scene writes, no fetches. If a colour depends on a fact about the world that is not in the scene yet, put it there first (as a property or a reading), so every view and every hook agrees on it. What belongs to one view only, such as the quantity it colours by, can stay in the component.

Something wrong or unclear? Tell us.