An interior is lit by the pieces below. <Daylight> drives the sun, the beam, the glass and the lamps, so one time value sets the light for the whole room.
| Piece | Does |
|---|---|
<Daylight time> |
Sun (moon at night) and sky light for an hour of the day, easing between hours |
<LightBeam sun> |
A beam of sunlight through the window in dusty air, following the sun |
<WindowPane> |
The glass: bright by day, deep blue at night |
<Lamp on="dark"> |
A light on an object, such as a table lamp or a candle, that comes on at dusk |
shadowOnly |
A roof or wall that is never drawn but keeps the sun out |
<Scene occlusion> |
Soft darkening in corners and under furniture |
<Scene toneMapping="neutral"> |
Colours stay closer to the values you set, so pastels keep their colour |
A room through the day#
This example is a cut-away room with a window in its right wall. Buttons change the hour. The beam moves with the sun, the glass darkens at night and the lamp comes on at dusk.
The default keys, DAY, describe an outdoor day with a blue sky, which turns cream walls grey indoors. ROOM_DAY has a stronger sun and a warm, bright fill:
import type { DaylightKey } from "@apricotjs/react-native";
const key = (
hour: number,
sun: string,
sunIntensity: number,
sky: string,
ground: string,
fillIntensity: number,
background: string,
): DaylightKey => ({ hour, sun, sunIntensity, sky, ground, fillIntensity, background, reflections: 1 });
export const ROOM_DAY: DaylightKey[] = [
key(0, "#8796cc", 0.9, "#4a4f74", "#24222f", 1.3, "#33324a"),
key(5.5, "#93a0d0", 0.9, "#5a5d80", "#2e2b38", 1.4, "#3e3c54"),
key(7, "#ffcfa4", 4, "#f6e9e2", "#dcbca4", 2.3, "#e8d8d2"),
key(12.5, "#fff3e4", 7, "#fff6ee", "#e8caae", 2.7, "#e3d5cf"),
key(16.5, "#ffb06a", 8, "#fff3e6", "#e8bf98", 2.73, "#dccbc4"),
key(18.6, "#ff8448", 6.5, "#f3c6ad", "#c9967c", 2.1, "#d8aea4"),
key(19.6, "#8d98cc", 1, "#5d5c82", "#2e2a3a", 1.5, "#46425c"),
];import {
Box,
CameraRig,
Cylinder,
Daylight,
DirectionalLight,
Lamp,
LightBeam,
Merged,
Scene,
WindowPane,
} from "@apricotjs/react-native";
import { useState } from "react";
import { Button, StyleSheet, View } from "react-native";
import { ROOM_DAY } from "./roomDay";
const W = 2; // half the room's width (x)
const D = 1.5; // half its depth (z)
const H = 2.6; // its height
const WINDOW = { y: 1.4, z: 0, width: 1, height: 1.2 };
const T = 0.1; // wall thickness
function Shell() {
const top = WINDOW.y + WINDOW.height / 2;
const bottom = WINDOW.y - WINDOW.height / 2;
return (
<Merged>
{/* Floor and back wall */}
<Box size={[2 * W, 0.1, 2 * D]} position={[0, -0.05, 0]} color="#ebc79c" />
<Box size={[2 * W, H, T]} position={[0, H / 2, -D - T / 2]} color="#faf1e8" />
{/* Right wall, round the window */}
<Box size={[T, bottom, 2 * D]} position={[W + T / 2, bottom / 2, 0]} color="#faf1e8" />
<Box size={[T, H - top, 2 * D]} position={[W + T / 2, (H + top) / 2, 0]} color="#faf1e8" />
<Box size={[T, WINDOW.height, D - WINDOW.width / 2]} position={[W + T / 2, WINDOW.y, -(D + WINDOW.width / 2) / 2]} color="#faf1e8" />
<Box size={[T, WINDOW.height, D - WINDOW.width / 2]} position={[W + T / 2, WINDOW.y, (D + WINDOW.width / 2) / 2]} color="#faf1e8" />
</Merged>
);
}
export function DayRoom() {
const [hour, setHour] = useState(16.5);
return (
<View style={{ flex: 1 }}>
<Scene
style={StyleSheet.absoluteFill}
look="none"
toneMapping="neutral"
shadows={{ softness: 3, darkness: 0.94 }}
occlusion={{ radius: 0.5, strength: 1 }}
lamps={1}
>
{/* north={Math.PI}: the window in the +x wall faces west. */}
<Daylight time={hour} north={Math.PI} keys={ROOM_DAY} />
{/* Fill light on the walls facing the camera */}
<DirectionalLight position={[-5, 6, 6]} intensity={0.9} color="#fff4ea" castShadow={false} />
<Shell />
{/* Shadow-only roof and walls */}
<Box size={[2 * W + 0.4, T, 2 * D + 0.4]} position={[0, H + T / 2, 0]} shadowOnly />
<Box size={[2 * W + 0.4, H, T]} position={[0, H / 2, D + T / 2]} shadowOnly />
<Box size={[T, H, 2 * D + 0.4]} position={[-W - T / 2, H / 2, 0]} shadowOnly />
<WindowPane size={[WINDOW.width, WINDOW.height]} position={[W + T / 2, WINDOW.y, WINDOW.z]} rotation={[0, Math.PI / 2, 0]} />
<LightBeam
sun
from={[W, WINDOW.y, WINDOW.z]}
size={[WINDOW.width, WINDOW.height]}
facing={[-1, 0, 0]} // into the room
bounds={[-W, -D, W, D]} // the walls it may land on
intensity={2}
/>
{/* Table lamp */}
<Merged position={[-W + 0.4, 0, -D + 0.4]}>
<Cylinder radius={0.25} height={0.5} position={[0, 0.25, 0]} color="#e5c595" />
<Cylinder radius={0.04} height={0.35} position={[0, 0.67, 0]} color="#8d5c3d" />
</Merged>
<Lamp position={[-W + 0.4, 0.9, -D + 0.4]} on="dark" glow={0.12} />
<CameraRig subject={null} view={{ azimuth: -36, elevation: 32, distance: 9, target: [0, 1, 0] }} orbit={{ turn: 40, elevation: [12, 70] }} />
</Scene>
<View style={{ position: "absolute", bottom: 40, left: 0, right: 0, flexDirection: "row", justifyContent: "center" }}>
<Button title="Morning" onPress={() => setHour(8.5)} />
<Button title="Afternoon" onPress={() => setHour(16.5)} />
<Button title="Sunset" onPress={() => setHour(18.6)} />
<Button title="Night" onPress={() => setHour(22)} />
</View>
</View>
);
}Daylight indoors#
<Daylight> sets the sun's direction, colour and brightness and the sky's light for time, in hours from 0 to 24. A plain value eases to the new hour over transition seconds (default 1.5). A shared value, such as one driven by a slider, is followed exactly.
| Prop | Meaning |
|---|---|
north |
Turns the compass, in radians. By default the sun rises at +x (east) and sets at −x. Turn it so the window faces the light you want |
keys |
Replaces the day's colours and brightness: an array of DaylightKey (see Lights reference). An interior needs its own keys, such as ROOM_DAY above |
strength={{ sun, sky }} |
Multiplies the sun's and sky's brightness without new keys |
background={false} |
Keeps your own background instead of the sky colour |
Use look="none" with <Daylight>. Otherwise the look's lights add to the daylight.
The beam through the window: <LightBeam>#
<LightBeam> draws a soft, see-through volume of light. It is brightest where it comes in and fades along its length and towards its edges.
from is the middle of the opening and size its width and height. facing is the direction into the room: a window in a wall facing −x is [-1, 0, 0]. The beam keeps the opening's shape along its whole length.
With sun, the beam follows the sun's (or moon's) rays and takes the sun's colour. It lands on the floor or on a wall within bounds, and shows only while the sun is on the window's side of the wall. intensity is its brightness when the sun is at its brightest.
Without sun, set to, the middle of where the beam lands. Use this for a fixed beam or a spotlight's cone on a stage.
The beam lights the air only. The bright patch on the floor comes from the sun itself: <Daylight>, or a <DirectionalLight> through the window. Turn shadows on so that walls and roofs block the sun everywhere else.
The glass: <WindowPane>#
<WindowPane> is a pane of glass one centimetre thick along its own z axis. Rotate it to sit in the wall. It glows warm by day (day colour) and turns deep blue at night (night colour), following <Daylight>. It casts no shadow, so light passes through.
Lamps#
<Lamp> is a point light attached to an object. Put it inside a lamp's model or a <Placeable> and it moves with it.
<Placeable id="lamp" on="surface" position={[0.3, 0.45, 0.2]} turn={0}>
<Model src={require("./assets/table-lamp.glb")} ground />
<Lamp position={[0, 0.36, 0]} on="dark" glow={0.2} />
</Placeable>| Prop | Default | Meaning |
|---|---|---|
position |
[0, 0, 0] |
Where the bulb is, in its parent's space |
on |
true |
true, false or "dark". "dark" is on from dusk with <Daylight>, and always on without one. Switching fades |
transition |
0.6 |
Seconds to fade on or off |
color |
a warm bulb (#ffc98a) |
|
intensity |
1.5 |
Brightness when on, in candela |
distance |
3 |
How far it reaches, metres |
glow |
0 |
Radius of a soft glow round the bulb while on, metres |
How many lamps give light#
<Scene lamps={n}> sets how many lamps may light the scene at once. The default is 0, so lamps only glow. Past n, the brightest lamps give light and the others only glow. Lamps can be switched, added and carried at any time without a pause. Each lamp adds a little GPU time to everything it lights, so keep n small. Lamps cast no shadows.
For many lamps, such as a street or a restaurant, use <Scene lighting="dynamic" lamps={16}>. Each lamp then costs GPU time only while it is on, but every light in the scene costs a little more. Use it when you have many lamps.
Follow the time of day yourself: useDaylight#
useDaylight() returns a function that reads the current light, or null without a <Daylight>. Call it in useFrame. This sign shows only after dark:
import { Box, type SceneObject, useDaylight, useFrame } from "@apricotjs/react-native";
import { useRef } from "react";
export function NightSign() {
const daylight = useDaylight();
const sign = useRef<SceneObject>(null);
useFrame(() => {
const dark = daylight()?.dark ?? 0;
if (sign.current) sign.current.visible = dark > 0.5;
return false;
});
return <Box ref={sign} size={[1, 0.3, 0.05]} position={[0, 2.2, -1.4]} material="light" color="#ff6b9a" />;
}The value is a DaylightNow:
| Field | Meaning |
|---|---|
hour |
The hour shown, easing towards time after a plain change |
dark |
0 by day to 1 at night. It rises over the hour before sunset |
level |
The sun's brightness, 0–1 |
direct |
How much light comes straight from the sun or moon, 0–1 |
sunDirection |
Unit vector towards the sun (or the moon at night) |
It also has the current colours and intensities.
Making it look solid#
| Option | Effect | Cost |
|---|---|---|
occlusion |
Soft darkening where surfaces meet or come close: corners, under a sofa, between cushions. true or { radius, strength, resolution, samples } |
One extra pass |
toneMapping="neutral" |
Colours closer to the values you set. "agx" (default) is softer, "filmic" has more contrast, "none" applies no tone mapping |
None |
exposure |
Brightens (above 1) or darkens (below 1) the whole picture | None |
shadows={{ softness }} |
Softer edges (texels, default 2) | Small |
shadows={{ blur: true, softness: 8 }} |
Wide, blurred edges, as under the soft light of a room or an overcast sky | More than softness alone |
shadowOnly on a shape |
Not drawn, only casting: a roof over a cut-away room, a tree off screen whose shadow falls in | Its shadow only |
Performance#
A furnished room has many shapes. To keep it fast on mid-range Android phones:
- Build each piece with
<Merged>. Each piece is then one draw call. - Wrap the room in
<Bundle>to cut the CPU cost of drawing it. The<LightBeam>and glass can go inside it or outside. - Turn on
<Scene debug>. The HUD shows GPU and CPU time per frame, so you know which to reduce.
See Performance.
Related#
- Lighting and shadows.
- Lights reference:
Lamp,LightBeam,WindowPane,Daylight. - Placement.
- Furnish a room recipe.