1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| const viewer = new Cesium.Viewer("cesiumContainer", { geocoder: false, homeButton: false, sceneModePicker: false, baseLayerPicker: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, animation: false, timeline: false, fullscreenButton: false, selectionIndicator: false, skyBox: false, shouldAnimate: true, }); viewer.cesiumWidget.creditContainer.style.display = "none";
if (!viewer.scene.specularEnvironmentMapsSupported) { window.alert("此浏览器不支持镜面环境贴图。"); }
const modelURL = "../SampleData/models/Pawns/Pawns.glb";
const environmentMapURL = "https://cesium.com/public/SandcastleSampleData/kiara_6_afternoon_2k_ibl.ktx2";
const L00 = new Cesium.Cartesian3( 1.234897375106812, 1.221635103225708, 1.273374080657959 ); const L1_1 = new Cesium.Cartesian3( 1.136140108108521, 1.171419978141785, 1.287894368171692 ); const L10 = new Cesium.Cartesian3( 1.245410919189453, 1.245791077613831, 1.283067107200623 ); const L11 = new Cesium.Cartesian3( 1.107124328613281, 1.112697005271912, 1.153419137001038 ); const L2_2 = new Cesium.Cartesian3( 1.08641505241394, 1.079904079437256, 1.10212504863739 ); const L2_1 = new Cesium.Cartesian3( 1.190043210983276, 1.186099290847778, 1.214627981185913 ); const L20 = new Cesium.Cartesian3( 0.017783647403121, 0.020140396431088, 0.025317270308733 ); const L21 = new Cesium.Cartesian3( 1.087014317512512, 1.084779262542725, 1.111417651176453 ); const L22 = new Cesium.Cartesian3( -0.052426788955927, -0.048315055668354, -0.041973855346441 ); const coefficients = [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22];
const height = 0.0; const hpr = new Cesium.HeadingPitchRoll(0.0, 0.0, 0.0); const origin = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height); const modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(origin, hpr);
try { const model = viewer.scene.primitives.add( await Cesium.Model.fromGltfAsync({ url: modelURL, modelMatrix: modelMatrix, minimumPixelSize: 128, }) );
model.readyEvent.addEventListener(() => { const camera = viewer.camera;
const controller = viewer.scene.screenSpaceCameraController; const r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near); controller.minimumZoomDistance = r * 0.5;
const center = model.boundingSphere.center; const heading = Cesium.Math.toRadians(230.0); const pitch = Cesium.Math.toRadians(-20.0); camera.lookAt( center, new Cesium.HeadingPitchRange(heading, pitch, r * 2.0) ); camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
const ibl = model.imageBasedLighting;
ibl.sphericalHarmonicCoefficients = coefficients;
ibl.specularEnvironmentMaps = environmentMapURL;
model.environmentMapManager.groundColor = Cesium.Color.fromCssColorString("#292817");
Sandcastle.addToggleButton( "使用程序生成的环境光照", false, function (checked) { if (!checked) { ibl.sphericalHarmonicCoefficients = coefficients; ibl.specularEnvironmentMaps = environmentMapURL; } else { ibl.sphericalHarmonicCoefficients = undefined; ibl.specularEnvironmentMaps = undefined; } } ); }); } catch (error) { window.alert(`模型加载出错: ${error}`); }
|