Cardboard
Runnable Cesium example: Cardboard. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:打开 VR 按钮,把场景切到可分屏的立体模式
说明:Viewer 的 vrButton 打开后右下角出现 VR 按钮,场景由一圈立柱和一个地面体块组成
核心逻辑:vrButton: true + 关闭球体 + BoxGeometry 立柱,相机站在场景里
依赖:CesiumJS 1.121 · 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>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
html,body{height:100%;margin:0}
body{background:#0e1116;color:#e6e8ec;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
#c{height:100%}
.hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;max-width:250px}
.hud b{color:#e6e8ec;font-weight:600}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
<div><b>VR 按钮</b></div>
<div>右下角有 VR 按钮,点击进入全屏立体渲染</div>
<div>没有头显时浏览器会拒绝进入,属于设备限制</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
'use strict';
var viewer = new Cesium.Viewer('c', {
baseLayer: false,
vrButton: true,
baseLayerPicker: false, geocoder: false, homeButton: false,
sceneModePicker: false, navigationHelpButton: false,
animation: false, timeline: false, fullscreenButton: false,
infoBox: false, selectionIndicator: false
});
var scene = viewer.scene;
scene.backgroundColor = Cesium.Color.fromCssColorString('#070b10');
scene.globe.show = false;
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
var CENTER_LON = 116.3913;
var CENTER_LAT = 39.9074;
var CENTER = Cesium.Cartesian3.fromDegrees(CENTER_LON, CENTER_LAT, 0);
function boxInstance(lon, lat, w, h, d, color, baseHeight) {
var geometry = new Cesium.BoxGeometry({
vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
minimumCorner: new Cesium.Cartesian3(-w / 2, -d / 2, 0),
maximumCorner: new Cesium.Cartesian3(w / 2, d / 2, h)
});
return new Cesium.GeometryInstance({
geometry: geometry,
modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(lon, lat, baseHeight || 0)
),
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(color)
}
});
}
var instances = [];
/* 地面:一块很薄的体块 */
instances.push(boxInstance(CENTER_LON, CENTER_LAT, 120, 0.3, 120,
Cesium.Color.fromCssColorString('#2a3440'), 0));
/* 一圈立柱,近处三根、远处四根,形成明显视差 */
var colors = ['#4d9bff', '#39c0a0', '#f2a03d', '#e45c7a', '#8e7cf0', '#3fb6e8', '#d8d3c8'];
for (var i = 0; i < 7; i++) {
var ang = (i / 7) * Math.PI * 2;
var r = 22 + (i % 3) * 12;
instances.push(boxInstance(
CENTER_LON + r * Math.cos(ang) * 1e-5 / 0.85,
CENTER_LAT + r * Math.sin(ang) * 1e-5,
3 + (i % 3), 14 + i * 4, 3 + (i % 3),
Cesium.Color.fromCssColorString(colors[i]), 1
));
}
scene.primitives.add(new Cesium.Primitive({
geometryInstances: instances,
appearance: new Cesium.PerInstanceColorAppearance({ flat: false, translucent: false }),
asynchronous: false
}));
/* 相机站在地面上,略高于人眼 */
var eye = Cesium.Cartesian3.fromDegrees(CENTER_LON - 0.00018, CENTER_LAT - 0.00028, 1.7);
viewer.camera.lookAt(
eye,
new Cesium.HeadingPitchRange(0, Cesium.Math.toRadians(-4), 0)
);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.