Factory Digital Twin
A 600×400 m plant with 12 buildings extruded to metric heights, 80 assets and 5 pipe racks overlaid; heights and pipe racks can be toggled.
Business context
Plant twins are a standard module of industrial IoT platforms. The users are line engineers, maintenance crews and park managers checking whether building volumes, asset placement and pipe-rack routing match the drawings. Geometry comes from BIM or CAD outlines and elevations, and asset points from the asset register and IoT gateways. A single plant often has a dozen buildings and hundreds of assets; the 12 and 80 here are a representative slice.
BIM exports local metric coordinates, so an origin is chosen, the model translated onto it and the height datum unified so roof elevations and asset mounting heights agree; components are merged by discipline or floor when there are too many. Cesium suits city-scale twins with terrain and imagery, and three.js requires custom camera and picking work; METER_OFFSETS attaches metric coordinates directly to a geographic origin, and extrusion and labels share the same data.
Extrusion height is scaled globally by elevationScale, so relative heights on screen are not real storey heights. Asset elevations are computed against roofs, so turning heights off drops them to the ground; the position appears to change while the plan coordinates do not. Pipe-rack width is in meters and becomes invisible when zoomed out, so it says nothing about pipe diameter.
How it works
All geometry is written directly in meters: the site runs from (-300,-200) to (300,200) with a corner removed where x>120 and y>60; the 12 buildings are rectangles of 105×80–90 m and 14–44 m tall, and getElevation returns the height with elevationScale 1, so the top of each extrusion is the true elevation. Assets are placed randomly within their building after a 12 m inset, at roof height plus 2.5 m, falling back to 1.5 m at ground level when heights are off; pipe racks run at z=6 m along two transverse and three longitudinal corridors with a 3 m width.
Coordinates use METER_OFFSETS with coordinateOrigin at the site center [116.40, 39.92]; the local origin coincides with the view target, values stay within ±300 m and Mercator precision jitter never appears. CARTESIAN carries no geographic anchor, which suits molecules and particles, but overlaying the site on a real basemap would require converting to longitude and latitude yourself. Clearing 'building heights' turns extruded off so plan outlines and asset positions align more clearly; in 'assets' mode buildings drop to flat slabs at alpha 120 and asset radii grow to 9 m, making the 80 points the subject.
| Setting | Effect |
|---|---|
| coordinateOrigin: [116.40, 39.92] | Geographic origin the local metric coordinates are attached to |
| METER_OFFSETS | Coordinates in meters, converted through Mercator by latitude |
| extruded + getElevation | Extrudes volumes to building heights of 14–44 m |
| widthUnits: 'meters' | Pipe-rack width of 3 m, scaling with zoom |
Note. Put the origin at the geometric center of the site so asset coordinates stay within ±300 m; float precision and camera distance are both easier to control.
Source
<!--
示例:厂区数字孪生
要点:METER_OFFSETS 挂到 [116.40, 39.92],厂区、建筑、设备与管廊全部用米制局部坐标描述
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>厂区数字孪生</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
.bar button:hover{background:#26303d}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>厂区 600 × 400 m · 设备 80 台</span><span class="sep"></span>
<button data-m="buildings" class="on">建筑</button>
<button data-m="devices">设备</button>
<label><input id="hgt" type="checkbox" checked>显示建筑高度</label>
<label><input id="pipe" type="checkbox" checked>显示管廊</label>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var ORIGIN = [116.40, 39.92];
var rand = rng(2401);
var i, j;
var boundary = [
[-300, -200, 0], [300, -200, 0], [300, 200, 0],
[120, 200, 0], [120, 60, 0], [-300, 60, 0]
];
var B = [
[-290, -170, 105, 80, 26],
[-175, -170, 105, 80, 34],
[-60, -170, 105, 80, 22],
[55, -170, 105, 80, 38],
[170, -170, 105, 80, 18],
[-290, -60, 105, 80, 30],
[-175, -60, 105, 80, 24],
[-60, -60, 105, 80, 44],
[55, -60, 105, 80, 16],
[-290, 80, 105, 90, 28],
[-175, 80, 105, 90, 20],
[-60, 80, 105, 90, 14]
];
var buildings = [];
for (i = 0; i < B.length; i++) {
var x = B[i][0], y = B[i][1], w = B[i][2], d = B[i][3];
buildings.push({
poly: [[x, y, 0], [x + w, y, 0], [x + w, y + d, 0], [x, y + d, 0]],
h: B[i][4],
c: i % 3 === 0 ? [96, 124, 158, 225] : (i % 3 === 1 ? [108, 138, 172, 225] : [84, 110, 144, 225])
});
}
var devices = [];
for (i = 0; i < 80; i++) {
var bb = B[i % B.length];
var dx = bb[0] + 12 + rand() * (bb[2] - 24);
var dy = bb[1] + 12 + rand() * (bb[3] - 24);
devices.push({ ground: [dx, dy, 1.5], roof: [dx, dy, bb[4] + 2.5] });
}
var pipes = [
[[-295, -75, 6], [295, -75, 6]],
[[-295, 50, 6], [295, 50, 6]],
[[-180, -190, 6], [-180, 55, 6]],
[[-65, -190, 6], [-65, 55, 6]],
[[50, -190, 6], [50, 55, 6]]
];
var mode = 'buildings', showHeight = true, showPipes = true;
function buildLayers() {
var layers = [];
layers.push(new deck.PolygonLayer({
id: 'site-boundary', data: [boundary],
coordinateSystem: deck.COORDINATE_SYSTEM.METER_OFFSETS,
coordinateOrigin: ORIGIN,
getPolygon: function (d) { return d; },
filled: false, stroked: true,
getLineColor: [92, 168, 245, 235],
getLineWidth: 2, lineWidthUnits: 'pixels',
pickable: false
}));
layers.push(new deck.PolygonLayer({
id: 'site-buildings', data: buildings,
coordinateSystem: deck.COORDINATE_SYSTEM.METER_OFFSETS,
coordinateOrigin: ORIGIN,
getPolygon: function (d) { return d.poly; },
extruded: showHeight && mode !== 'devices',
getElevation: function (d) { return d.h; },
elevationScale: 1, filled: true, stroked: true, wireframe: true,
getFillColor: function (d) {
return mode === 'devices' ? [64, 88, 118, 120] : d.c;
},
getLineColor: [24, 32, 44, 210],
opacity: 0.92, pickable: false
}));
layers.push(new deck.ScatterplotLayer({
id: 'site-devices', data: devices,
coordinateSystem: deck.COORDINATE_SYSTEM.METER_OFFSETS,
coordinateOrigin: ORIGIN,
getPosition: function (d) {
return showHeight && mode !== 'devices' ? d.roof : d.ground;
},
getRadius: mode === 'devices' ? 9 : 5, radiusUnits: 'meters',
getFillColor: mode === 'devices' ? [255, 196, 86, 245] : [206, 178, 118, 210],
stroked: true, getLineColor: [40, 28, 12, 200],
getLineWidth: 1, lineWidthUnits: 'pixels',
pickable: false
}));
if (showPipes) {
layers.push(new deck.PathLayer({
id: 'site-pipes', data: pipes,
coordinateSystem: deck.COORDINATE_SYSTEM.METER_OFFSETS,
coordinateOrigin: ORIGIN,
getPath: function (d) { return d; },
getWidth: 3, widthUnits: 'meters', widthMinPixels: 1,
getColor: [120, 200, 214, 220],
capRounded: true, jointRounded: true,
pickable: false
}));
}
return layers;
}
var deckgl = new deck.Deck({
canvas: 'cv', width: '100%', height: '100%',
initialViewState: {
longitude: ORIGIN[0],
latitude: ORIGIN[1],
zoom: 16.4,
pitch: 55,
bearing: -22
},
controller: true,
layers: buildLayers()
});
var btns = document.querySelectorAll('.bar button[data-m]');
for (i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function () {
mode = this.getAttribute('data-m');
for (j = 0; j < btns.length; j++) {
btns[j].className = btns[j] === this ? 'on' : '';
}
deckgl.setProps({ layers: buildLayers() });
});
}
document.getElementById('hgt').addEventListener('change', function () {
showHeight = this.checked;
deckgl.setProps({ layers: buildLayers() });
});
document.getElementById('pipe').addEventListener('change', function () {
showPipes = this.checked;
deckgl.setProps({ layers: buildLayers() });
});
window.__deck = deckgl;
})();
</script>
</body>
</html>