Skip to content

ThreeView Class

ThreeView is the main class for creating and managing 3D map visualizations using Three.js and WebGL. It provides a comprehensive API for layer management, camera control, rendering, and event handling.

import ThreeView, { JAPAN_GSI_ELEVATION_DECODER } from "@navaramap/three";
import { DefaultPlugin } from "@navaramap/three-default-plugin";
import { Vector3 } from "three";
// Create ThreeView instance
const view = new ThreeView({
shadow: true,
animation: true,
backgroundColor: 0x0a0a0f,
});
const plugin = new DefaultPlugin();
view.addPlugin(plugin);
// Initialize the view
await view.init();
// Add default photorealistic objects (sky, stars, sun, light probe)
const defaultLayers = plugin.addDefaultPhotorealScene();
// Add terrain layer
const terrainSource = view.addSource({
type: "raster-dem",
url: "https://cyberjapandata.gsi.go.jp/xyz/dem_png/{z}/{x}/{y}.png",
elevationDecoder: JAPAN_GSI_ELEVATION_DECODER(),
maxZoom: 15,
minZoom: 5,
});
view.addLayer({
type: "terrain",
source: terrainSource,
terrain: {
castShadow: true,
receiveShadow: true,
},
});
// Add hillshade layer
const hillshadeSource = view.addSource({
type: "raster-dem",
// Credit:
// - Geospatial Information Authority of Japan Tiles - Digital Elevation Map
// https://maps.gsi.go.jp/development/ichiran.html
url: "https://cyberjapandata.gsi.go.jp/xyz/dem_png/{z}/{x}/{y}.png",
elevationDecoder: JAPAN_GSI_ELEVATION_DECODER(),
minZoom: 6,
maxZoom: 15,
});
view.addLayer({
type: "raster",
source: hillshadeSource,
hillshade: {},
});
// Add raster tile layer
const rasterSource = view.addSource({
type: "raster-tile",
url: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
maxZoom: 23,
});
view.addLayer({
type: "raster",
source: rasterSource,
raster: {
color: new Color().setHex(0xffffff),
opacity: 1,
},
});
// Set camera position
view.setCamera({
lng: 139.7,
lat: 35.7,
height: 1000,
pitch: -45,
heading: 0,
roll: 0,
});

A page can run several ThreeView instances at once, for example to compare two maps side by side. Give each view its own canvas. The views can be initialized concurrently, and disposing one does not interrupt the others.

const left = new ThreeView({ canvas: leftCanvas });
const right = new ThreeView({ canvas: rightCanvas });
await Promise.all([left.init(), right.init()]);

All views on a page share one pool of background workers, so their tile processing competes for the same CPU cores instead of multiplying them. The first view to call init() creates the pool, and its workerPoolSize, mobileOptimization and memoryBudget.maxWorkerHeapBytes decide the pool size and worker heap budget. The same options passed to a later view do not change the pool while it exists, and a console warning reports when they differ. The pool is shut down when the last view is disposed, and the next view to initialize creates a new one.

Everything else belongs to each view: the WebGL context, the tile cache (cacheBytes), the remaining memoryBudget limits, and the font worker. Memory use therefore grows with the number of views, and data shown in two views is downloaded and decoded once per view. When showing several views on a memory-constrained device, lower cacheBytes on each of them.

Type: HTMLElement | undefined

Description: The HTML container element for rendering the view. When specified, ThreeView adds a canvas within this container and sizes the view to it. The canvas is absolutely positioned, so give the container its own size. A container with position: static is set to position: relative until the view is disposed. Without container and canvas, ThreeView appends a viewport-sized element to document.body.

Example:

const view = new ThreeView({
container: document.getElementById("map") ?? undefined,
});

Type: HTMLCanvasElement | OffscreenCanvas | undefined

Description: The canvas element used for rendering. When specified, this canvas is used. When not specified, a new canvas is created. The view is sized to the parent element of the canvas, and the canvas is given that size in pixels. In a flex or grid layout the parent therefore needs min-width: 0 and min-height: 0 (or overflow: hidden) to be able to shrink.

Example:

const view = new ThreeView({
canvas: document.getElementById("canvas") as HTMLCanvasElement,
});

Type: number | undefined

Description: Override for the device pixel ratio. Affects rendering quality on high-DPI displays. When not specified, the device’s default value is used.

Example:

const view = new ThreeView({
pixelRatio: 2,
});

Type: boolean | undefined

Description: Whether to disable automatic resizing. By default the view follows the size of its container (container, or the parent element of canvas), including size changes that happen without a window resize. When true, call resize() yourself.

Default: false

Example:

const view = new ThreeView({
disableAutoResize: true,
});

Type: boolean | undefined

Description: Whether to enable debug mode. When true, additional debug information such as performance statistics overlays is displayed.

Default: false

Example:

const view = new ThreeView({
debug: true,
});

Type: AtmosphereOptions | undefined

Description: Configuration options for atmospheric rendering. Configures the sky, sun, and atmospheric scattering effects. Sun and moon positions are automatically calculated based on the date specified in the date property and reflected in related Descriptors such as SunLightDesc.

export type AtmosphereOptions = {
atmosphereAssetsUrl?: string; // URL for atmosphere asset files
stbnUrl?: string; // URL for STBN textures
date?: Date; // Date and time used for sun/moon position calculations
};

Example:

const view = new ThreeView({
atmosphere: {
atmosphereAssetsUrl: "/assets/atmosphere",
date: new Date("2024-06-21T12:00:00"),
},
});
// The date can be changed after initialization
await view.init();
view.atmosphere.date = new Date("2024-12-21T18:00:00");

Type: Color | undefined

Description: The background color of the scene. Specify an instance of the Color class.

Default: 0x0a0a0f (dark blue-gray)

Example:

import ThreeView, { Color } from "@navaramap/three";
const view = new ThreeView({
backgroundColor: new Color().setHex(0x1a1a2e),
});

Type: boolean | undefined

Description: Configuration option for feature picking. When enabled, the featureClick event fires when a feature is clicked or tapped, and the featureHover / featureEnter / featureLeave events fire as the pointer hovers features.

Default: true

Example:

const view = new ThreeView({
picking: true,
});
// Listen for pick events
view.on("featureClick", (info) => {
if (info) {
console.log("Selected feature:", info.properties);
}
});

Type: boolean | undefined

Description: Whether to run the main loop every frame. When true, rendering occurs continuously. When false, rendering occurs only when changes are detected or forceUpdate() is called.

Default: false

Example:

const view = new ThreeView({
animation: true,
});

Type: number | undefined

Description: Number of MSAA (Multi-Sample Anti-Aliasing) samples. When 0, MSAA is disabled. Use with caution as it impacts performance.

Default: 0

Example:

const view = new ThreeView({
multisampling: 4,
});

Type: boolean | undefined

Description: Whether to use half-precision floating-point numbers (half-float) for post-processing. When true, rendering quality is improved.

Default: true

Example:

const view = new ThreeView({
halfFloat: true,
});

Type: boolean | undefined

Description: Whether to render depth reversed (1 at the near plane, 0 at the far plane) into 32-bit floating-point depth buffers. This keeps depth precise from street level to space, and the GPU can skip hidden fragments before shading them, which a logarithmic depth buffer prevents. It needs the WebGL extensions EXT_clip_control and EXT_color_buffer_float; without them, the view uses logarithmicDepthBuffer instead. Must be specified at initialization.

Default: true

Example:

const view = new ThreeView({
reversedDepthBuffer: true,
});

Type: boolean | undefined

Description: Whether to use a logarithmic depth buffer where the reversed depth buffer is turned off or unsupported. It keeps depth precise at large scales, but every shader then writes its own depth, so the GPU shades hidden fragments too. With both options false, depth uses three.js’s standard depth buffer, which loses precision far from the camera. While the reversed depth buffer is in use, this option has no effect (the view logs a warning when it is set to true); set reversedDepthBuffer: false to use a logarithmic depth buffer.

Default: true

Example:

const view = new ThreeView({
logarithmicDepthBuffer: true,
});

Type: boolean | undefined

Description: Whether to enable shadow mapping. Must be specified at initialization and cannot be changed later.

Default: false

Example:

const view = new ThreeView({
shadow: true,
});

Type: number | undefined

Description: Milliseconds without data or tile processing activity that must elapse before the idle event fires. Continuous animations and effects do not count as activity. Lowering this value makes the engine report idle sooner. Raising it delays the notification until a longer quiet period has passed.

Default: 100

Example:

const view = new ThreeView({
idleThreshold: 200,
});
view.on("idle", () => {
console.log("Engine has been idle for 200 ms");
});

Type: boolean | undefined

Description: Whether to enable optimizations for mobile devices. When true, settings suitable for mobile devices are applied, such as lower pixel ratios and lighter effects.

Default: Auto-detected from the device (mobile devices are optimized automatically). Set explicitly to override the detection.

Example:

const view = new ThreeView({
mobileOptimization: true,
});

Type: number | undefined

Description: Memory budget in bytes for tile caches (WASM buffers + estimated GPU cost). Tiles leaving the view are retained until the budget is exceeded, then evicted least-recently-visited first, so panning back is refetch-free while total usage stays capped.

Default: Device-dependent. Desktop: a quarter of the reported device memory capped at 2 GB. Mobile: 512 MB (256 MB when the device reports less than 4 GB). See getDefaultCacheBytes().

Example:

const view = new ThreeView({
cacheBytes: 512 * 1024 * 1024, // 512 MB
});

Type: Partial<LodFogSettings> | undefined

Description: LOD fog: a distance-based screen-space-error relaxation used by tile LOD selection: far tiles tolerate a larger error and stay coarser while near tiles keep full resolution. Purely an LOD control. It never affects visual fog rendering. Partial values are merged over the device default.

type LodFogSettings = {
enabled: boolean;
// Distance scale of the relaxation ramp (2.0e-4 ≈ 63% strength at 5km)
density: number;
// Maximum SSE relaxation (in pixels) at far distance
sseFactor: number;
};

Default: Device-memory-dependent. Desktop: { density: 2.0e-4, sseFactor: 2.0 }. Low-memory devices ship a stronger curve so the tile working set stays small. See getDefaultLodFog().

Example:

const view = new ThreeView({
lodFog: { density: 2.5e-4, sseFactor: 3.0 },
});

Type: Partial<DynamicSseSettings> | undefined

Description: Dynamic screen-space error (CesiumJS dynamicScreenSpaceError equivalent): tilted, street-level horizon views tolerate a larger error for far tiles, cutting the tile working set in exactly the views that over-refine. Zero effect looking straight down. Fades out as the camera climbs past maxHeight meters. Partial values are merged over the default.

type DynamicSseSettings = {
enabled: boolean;
// Distance scale of the relaxation ramp before tilt/height scaling
density: number;
// Maximum SSE relaxation (in pixels) at full tilt and saturation
sseFactor: number;
// Fraction of the height band below which the effect is at full strength
heightFalloff: number;
// Camera height band (meters above the ellipsoid) the effect fades across
minHeight: number;
maxHeight: number;
};

Default: { enabled: true, density: 2.0e-4, sseFactor: 24.0, heightFalloff: 0.25, minHeight: 0, maxHeight: 8000 }. See getDefaultDynamicSse().

Example:

const view = new ThreeView({
dynamicSse: { sseFactor: 16.0, maxHeight: 4000 },
});

Type: number | undefined

Description: Number of background workers that decode tiles and build meshes. Each worker has its own JavaScript engine instance and WebAssembly heap, so a smaller pool uses less memory but loads tiles more slowly after fast camera moves. The value is rounded down and clamped to the range from 1 to navigator.hardwareConcurrency, with a console warning when it changes, and also applies with mobileOptimization. On mobile, and when the browser does not report navigator.deviceMemory, the default memoryBudget.maxWorkerHeapBytes is a fixed amount per worker, so a larger pool raises peak memory. Only init() reads this option. All views on a page share one pool, so only the first view to call init() sets its size (see Multiple Views).

Default: navigator.hardwareConcurrency (at most 2 with mobileOptimization)

Example:

const view = new ThreeView({
workerPoolSize: 4,
});

Type: object | undefined

Description: Overrides for the worker-side memory budgets and the memory-pressure LOD degrade. Defaults derive from the device memory together with cacheBytes. See getDefaultMemoryBudgets().

type MemoryBudgetOptions = {
// WASM heap budget per tile worker; the pool recycles a worker above it
maxWorkerHeapBytes?: number;
// Font-worker cache budget (font data + atlas pixels; caps further growth)
fontBudgetBytes?: number;
// In-flight data fetch cap per tile pipeline
maxPendingRequests?: number;
// Resting (base) memory-pressure SSE multiplier; > 1 keeps far tiles coarser even without pressure
sseMultiplierMin?: number;
// Ceiling the dynamic memory-pressure SSE degrade can climb to
sseMultiplierMax?: number;
};

Example:

const view = new ThreeView({
memoryBudget: {
maxWorkerHeapBytes: 128 * 1024 * 1024,
sseMultiplierMin: 1.0,
sseMultiplierMax: 8.0,
},
});

Type: GlobeOptions

Description: Additional options related to globe display. ThreeView constructor options inherit from GlobeOptions.

type GlobeOptions = {
maxSse?: number; // Screen space error threshold for LOD calculations (initialization only)
segments?: number; // Number of segments for mesh tessellation (initialization only)
color?: Color; // Base color of the globe surface
hideUnderground?: boolean; // Whether to hide underground geometry
shouldComputeNormalFromVertex?: boolean; // Whether to compute normals from vertex positions (initialization only)
transparent?: boolean; // Whether to make the material transparent
opacity?: number; // Global material opacity (0.0-1.0)
wireframe?: boolean; // Whether to render in wireframe mode
};

Disabling hideUnderground may cause unexpected behavior with some effect Descriptors.

Example:

import ThreeView, { Color } from "@navaramap/three";
const view = new ThreeView({
maxSse: 1,
segments: 10,
color: new Color().setHex(0x1a1a2e),
hideUnderground: true,
wireframe: false,
});