Appearance Translucent
Runnable Cesium example: Appearance Translucent. 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
<!--
示例:半透明盒体与不透明盒体的外观对比
要点:PerInstanceColorAppearance 的 translucent 与 closed 决定混合方式和背面剔除
依赖:CesiumJS 1.121 · jsDelivr CDN,几何由 BoxGeometry 生成
-->
<!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}
.hud .row{margin-top:6px}
.hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
.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>外观对比</b></div>
<div class="row">
<button id="btnA" class="on">半透明</button>
<button id="btnB">不透明</button>
</div>
<div class="row" id="info">alpha 0.38 · closed false</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,
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.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
var BOXES = [
{ lng: 116.380, lat: 39.906, size: [180, 180, 260], color: '#4d9bff' },
{ lng: 116.390, lat: 39.903, size: [180, 180, 180], color: '#5fd0a8' },
{ lng: 116.398, lat: 39.909, size: [180, 180, 330], color: '#ffb13d' }
];
/* 一组实例,颜色写在逐实例属性里,外观决定怎么混合 */
function makeInstances(alpha) {
return BOXES.map(function (b) {
return new Cesium.GeometryInstance({
geometry: Cesium.BoxGeometry.fromDimensions({
dimensions: new Cesium.Cartesian3(b.size[0], b.size[1], b.size[2]),
vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
}),
modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(b.lng, b.lat, b.size[2] / 2)
),
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
Cesium.Color.fromCssColorString(b.color).withAlpha(alpha)
)
}
});
});
}
var translucentPrimitive = new Cesium.Primitive({
geometryInstances: makeInstances(0.38),
appearance: new Cesium.PerInstanceColorAppearance({
translucent: true,
closed: false,
flat: false
})
});
var opaquePrimitive = new Cesium.Primitive({
geometryInstances: makeInstances(1.0),
appearance: new Cesium.PerInstanceColorAppearance({
translucent: false,
closed: true,
flat: false
})
});
scene.primitives.add(opaquePrimitive);
scene.primitives.add(translucentPrimitive);
opaquePrimitive.show = false;
/* 立在盒体后方的一块深色底板,透过半透明盒体可以看到它 */
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.389, 39.920, 120),
box: {
dimensions: new Cesium.Cartesian3(3200, 120, 240),
material: Cesium.Color.fromCssColorString('#5b6573')
}
});
var btnA = document.getElementById('btnA');
var btnB = document.getElementById('btnB');
var info = document.getElementById('info');
function show(mode) {
var trans = mode === 'translucent';
translucentPrimitive.show = trans;
opaquePrimitive.show = !trans;
btnA.className = trans ? 'on' : '';
btnB.className = trans ? '' : 'on';
info.textContent = trans
? 'alpha 0.38 · closed false · 背面可见'
: 'alpha 1.0 · closed true · 背面剔除';
}
btnA.addEventListener('click', function () { show('translucent'); });
btnB.addEventListener('click', function () { show('opaque'); });
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.374, 39.897, 1550),
orientation: {
heading: Cesium.Math.toRadians(22),
pitch: Cesium.Math.toRadians(-26),
roll: 0
},
duration: 0
});
})();
</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.