@klorad/engine-three
Draws a scene with three.js, follows its events, reports what the pointer does, and never changes world state.
import { createThreeRenderer } from "@klorad/engine-three";
import { ThreeView } from "@klorad/engine-three/react";Peer dependencies: three 0.170 or newer; react and @klorad/react for ThreeView.
| Export | Kind | Description |
|---|---|---|
createThreeRenderer(options) | function | Draws the scene into container. Returns a ThreeRenderer. |
ThreeView (/react) | component | The renderer as a React component; reads the scene from KloradProvider unless given one, and needs no provider when given one. A ref gives a ThreeViewHandle. |
defaultPalette, defaultStyle | value | The default colours and style function. |
Palette, ObjectStyle, StyleContext, StyleFn | type | For objectStyle. |
PointerHit, ObjectDrag | type | What onPick, onPointer and onDrag report. |
CameraView | type | The camera as a target, a bearing, a tilt and a distance. |
ObjectTransform, TransformMode | type | What onTransform reports, and which handles show: "move", "turn" or "scale". |
Land, LandImagery, LandTerrain, LandRelief | type | The land under a scene (ADR-0104): { imagery?, terrain? }, a basemap as { url, credit?, maximumLevel?, tileSize? } (an XYZ template of raster tiles) and terrain as { url, credit? } (a quantized mesh tileset); and where the terrain stands: "off", "loading", "on" or "failed". |
VIEWER_QUALITIES, ViewerQuality | value, type | "auto", "economy", "balanced", "high": what this device may spend on a scene (ADR-0076). |
qualitySettings(quality, inHeadset), QualitySettings | function, type | The settings of a quality: its preset (Auto resolved), pixelRatio, the headset's xrScale and foveation, the shadowMap side (0 draws no runtime shadows), softShadows and detailBias. |
sunPosition(azimuth, elevation) | function | Where the sun of an environment stands in three's frame, for an azimuth and an elevation in degrees. |
localToThree, threeToLocal, geoToThree, orientationToEuler | function | Frame conversions: east is +x, up is +y, north is -z. |
resumeAudio() | function | Lets the page make sound; call it from a click or a key, as browsers ask, before turning sounds on. |
threeCapabilities | value | What this reader draws (ReaderCapabilities): every geometry kind as itself, in three dimensions. |
Options
Prop
Type
ThreeView adds selected, transform (the handles the selected object shows: "move",
"turn", "scale" or null, the default; needs onTransform), autoFrame (default true),
audible and soundMarkers (both default false), selectedFeature and featureFilters (a record of model object id to the feature ids to show), selectedSound (a sound that carries the
move handles instead of the selected object, with soundMarkers and onTransform), className
and style. Its objectStyle, environment, land, quality, transform, selectedSound, sounds, audible,
soundMarkers and its callbacks may
change on every render, and the view follows at once.
It reads its other options when it mounts; change its key to apply new ones.
ThreeRenderer
| Member | Description |
|---|---|
canvas | The canvas element. |
select(objectId | null) | Outlines an object in palette.selected, with a ring on the ground around a compact body; an element of a building (a feature object) has its triangles laid over in palette.feature. Selection is your state, not the world's. |
selectFeature({ of, feature } | null) | Lays palette.feature over an element of a building's model that has no object of its own, by the model's object and the element's id (ADR-0067). |
setFeatureFilter(of, keep | null) | Shows only the features of a model whose ids are in keep, or all of them with null; hidden features are not picked. Kept for tiles that arrive later. |
setEmphasis(emphasis | null) | Makes some objects and elements stand out and draws everything else as lines over a faint body, as an Operational look draws what is not connected (ADR-0128; the emphasis prop of ThreeView): { objects, features }, object ids and, for each model by its object id, the feature ids. A pointer passes through what is faint to what stands out. null draws everything as styled. |
frame(objectIds?, { animate? }) | Points the camera at the given objects, or at all of them, keeping its bearing and tilt. Objects drawn as elements of a building are framed as frameFeature frames them. |
frameFeature({ of, feature }, { animate? }) | Frames an element of a building's model, by its model and its key, on the element's own extent: close, and from at least 60 degrees above, so the walls around it do not hide it. |
view() | Where the camera is, as a CameraView. |
setView(view, { animate? }) | Moves the camera to a view or changes part of it. It eases there unless animate is false. |
setDrag(mode) | What a drag with the main button or one finger does from now on: "pan" or "rotate" (the drag option, default "rotate"). The other button, or Ctrl, ⌘ or Shift, does the other. |
metresPerPixel() | Metres per CSS pixel at the camera target, for a scale bar. |
footprint() | The ground in view, as a box on Earth (PlanBounds). |
toScreen(point) | Where a point of the scene's frame (LocalPoint) falls on the view, as a ScreenPoint: CSS pixels from the view's top left, and shown false behind the camera or off the view. For drawing over the view, such as a measured line. |
enterVR(view?) | Starts an immersive VR session in the visitor's room, standing on the ground under the view's camera (the one on the screen when none is given), facing its heading. Needs xr, and a click or tap. |
moveVR(view) | Moves the visitor in VR to another view; nothing outside a session. |
setXRPlaces(places) | The places a visitor in VR may jump to; the xrPlaces prop sets it on the view. |
setQuality(quality) | Changes what this device spends: the screen's pixels, the shadow map and its filter, and how soon models turn far at once; a headset's resolution and foveation from its next session, since WebXR fixes them for the length of one. |
quality() | The QualitySettings drawn now, Auto resolved for a screen or a headset. A ThreeViewHandle gives it too. |
setEnvironment(environment | undefined) | Draws the scene in another environment, or in the renderer's own light with undefined. |
setLand(land | null) | Draws the scene over this land, or over none. The camera, the selection and every object stay where they are; another basemap over the same terrain keeps the terrain. |
landRelief() | Where the land's terrain stands, as a LandRelief. |
groundHeight(place) | How high the ground stands at { east, north }, in metres over the scene's flat ground: the terrain as drawn there, and 0 where no terrain is drawn. |
setTransform(objectId | null, mode | null, target?) | Shows the handles of a mode ("move", "turn", "scale") on an object, or on a sound with target "sound" (moved only, while sound markers show), or none. Throws unless the renderer was made with onTransform. A renderer made with onTransform also draws the selected object's width, depth and height in metres. |
setSounds(sounds) | Places the scene's sounds (SoundSource[]): each a looped file heard from its point, fading to silence at its reach. |
setAudible(on) | Plays the sounds, or stops them. Nothing is fetched before the first time they are heard. Call resumeAudio() from the visitor's click first. |
setSoundMarkers(shown) | Shows where each sound is, a ball at its point and a ring on the ground at its reach, as an editor's aid. |
load() | What the scene asks of the device that draws it, as a SceneLoad: triangles, texture memory and draw calls in all, and each object's triangles, textures, largest texture and whether it has a far version, the heaviest first. Far versions are left out. For a health check against what a device draws well (ADR-0074). |
dispose() | Stops drawing and removes the canvas and the labels. |
A ThreeViewHandle (the ref of ThreeView) has frame, frameFeature, view, setView, setDrag,
metresPerPixel, footprint, toScreen, enterVR and moveVR, and load, which a view of
another renderer may leave out.
loadModelFile(url, { format?, part? }) reads a model file in any format the renderer draws,
with the same readers a scene uses (a 3MF scaled from its unit to metres), and gives its own copy
of it. For a page that shows or prepares a model outside a scene.
In VR the renderer draws the edges of each eye coarser and the sun's shadows once on entry, not on every frame.
vrSupported() resolves to whether the browser can show immersive VR. standingFor(view) is
where a visitor stands in VR for a view: on the ground under its camera, and the turn that faces
its heading. EYE_HEIGHT (1.6 m) is deprecated since ADR-0053, when the visitor stopped standing
with their eyes at the view's camera.
eyeOf(view) is where a CameraView's camera stands, in the scene's frame, and
viewFrom(eye, target) is the CameraView of a camera standing at eye and looking at
target. Together they turn a package's named views, which keep both points on Earth, into
views the renderer takes, and back.
Land under the scene
view.setLand({
imagery: { url: "https://tiles.example.com/aerial/{z}/{x}/{y}.jpg", credit: "© Example Imagery" },
terrain: { url: "https://tiles.example.com/terrain/", credit: "© Example Terrain" },
});
view.setLand(null);A scene is drawn on a plain ground. Land is the ground of the place itself: a basemap as raster
tiles, terrain as elevation data, or both. Terrain is a quantized mesh tileset, the format the
globe reads: an address that answers layer.json. Its tiles are read whatever their addresses
end in, .terrain or nothing at all, as providers name them differently. The tiles are placed in Earth-centred
coordinates through the scene's Coordinate System, loaded by distance and level of detail, and
given back when out of sight.
The land meets the scene's ground at the scene's origin: over terrain the surface under the
origin is measured and the land is stood there. No object is moved: away from the origin, on a
slope, an object keeps its height above the scene's flat ground and may stand over the land
drawn there, or under it. The camera is kept two metres over the land under it. With land
drawn, the ground grid is an editor's tool only (grid).
Terrain is solid. It is drawn into the depth buffer, so a hill hides what is behind it and the
land hides what is under it, labels included; its triangles are set back a little in depth so
that what lies exactly on the land is drawn over it. A basemap with no terrain is a flat picture
under the scene's ground: it is drawn first, writes no depth and hides nothing. Over terrain the
pointer's ground is the land as drawn: PointerHit.local.up is the land's height under the
pointer, a drag (onDrag) and the handles that do not go up keep an object's height over the
land under it, and land in front of an object takes the pick. groundHeight({ east, north })
gives the height of the drawn land at a place, in metres over the scene's flat ground, and 0
where no terrain is drawn; it is the land at the level of detail loaded at that moment, finest
near the camera.
Terrain shows once elevation data fine enough to draw truly stands under the origin. Until then
landRelief() is "loading" and nothing of the land is drawn; if the source does not answer,
the land is drawn flat with its basemap and the relief is "failed". The credits of what is
drawn are on the view, in an element with the class klorad-three-credits, which a page places
with the CSS custom properties --klorad-credits-right, -left, -bottom, -top and
-width. The reader names no provider: the addresses, their terms and what they ask to be
shown are your app's. See the guide
Put a basemap and terrain under a scene.
ObjectStyle
What a style function may return. Anything it leaves out keeps the default, and a change from one frame's answer to the next is eased.
Prop
Type
What the pointer and the handles report
Prop
Type
CameraView
Prop
Type
The view honours prefers-reduced-motion: with it set, looks change and the camera moves at
once.
Something wrong or unclear? Tell us.