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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
| const viewer = new Cesium.Viewer("cesiumContainer", { geocoder: false, homeButton: false, sceneModePicker: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, animation: false, timeline: false, fullscreenButton: false, skyBox: false, shouldAnimate: true, baseLayerPicker: false, shadows: true, projectionPicker: true, baseLayer: Cesium.ImageryLayer.fromProviderAsync( Cesium.TileMapServiceImageryProvider.fromUrl( Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII") ) ), }); viewer.cesiumWidget.creditContainer.style.display = "none";
viewer.extend(Cesium.viewerVoxelInspectorMixin); viewer.scene.debugShowFramesPerSecond = true;
const globalTransform = Cesium.Matrix4.fromScale( Cesium.Cartesian3.fromElements( Cesium.Ellipsoid.WGS84.maximumRadius, Cesium.Ellipsoid.WGS84.maximumRadius, Cesium.Ellipsoid.WGS84.maximumRadius ) );
function ProceduralSingleTileVoxelProvider(shape) { this.shape = shape; this.minBounds = Cesium.VoxelShapeType.getMinBounds(shape).clone(); this.maxBounds = Cesium.VoxelShapeType.getMaxBounds(shape).clone(); this.dimensions = new Cesium.Cartesian3(8, 8, 8); this.names = ["color"]; this.types = [Cesium.MetadataType.VEC4]; this.componentTypes = [Cesium.MetadataComponentType.FLOAT32]; this.globalTransform = globalTransform; }
const scratchColor = new Cesium.Color();
ProceduralSingleTileVoxelProvider.prototype.requestData = function (options) { if (options.tileLevel >= 1) { return Promise.reject(`0 级以上的瓦片不可用`); }
const dimensions = this.dimensions; const voxelCount = dimensions.x * dimensions.y * dimensions.z; const type = this.types[0]; const channelCount = Cesium.MetadataType.getComponentCount(type); const dataColor = new Float32Array(voxelCount * channelCount);
const randomSeed = dimensions.y * dimensions.x + dimensions.x; Cesium.Math.setRandomNumberSeed(randomSeed); const hue = Cesium.Math.nextRandomNumber();
for (let z = 0; z < dimensions.z; z++) { for (let y = 0; y < dimensions.y; y++) { const indexZY = z * dimensions.y * dimensions.x + y * dimensions.x; for (let x = 0; x < dimensions.x; x++) { const lerperX = x / (dimensions.x - 1); const lerperY = y / (dimensions.y - 1); const lerperZ = z / (dimensions.z - 1);
const h = hue + lerperX * 0.5 - lerperY * 0.3 + lerperZ * 0.2; const s = 1.0 - lerperY * 0.2; const v = 0.5 + 2.0 * (lerperZ - 0.5) * 0.2; const color = Cesium.Color.fromHsl(h, s, v, 1.0, scratchColor);
const index = (indexZY + x) * channelCount; dataColor[index + 0] = color.red; dataColor[index + 1] = color.green; dataColor[index + 2] = color.blue; dataColor[index + 3] = 0.75; } } }
const content = Cesium.VoxelContent.fromMetadataArray([dataColor]); return Promise.resolve(content); };
function ProceduralMultiTileVoxelProvider(shape) { this.shape = shape; this.minBounds = Cesium.VoxelShapeType.getMinBounds(shape).clone(); this.maxBounds = Cesium.VoxelShapeType.getMaxBounds(shape).clone(); this.dimensions = new Cesium.Cartesian3(4, 4, 4); this.paddingBefore = new Cesium.Cartesian3(1, 1, 1); this.paddingAfter = new Cesium.Cartesian3(1, 1, 1); this.names = ["color"]; this.types = [Cesium.MetadataType.VEC4]; this.componentTypes = [Cesium.MetadataComponentType.FLOAT32]; this.globalTransform = globalTransform;
this._levelCount = 2; this._allVoxelData = new Array(this._levelCount);
const allVoxelData = this._allVoxelData; const channelCount = Cesium.MetadataType.getComponentCount(this.types[0]); const { dimensions } = this;
for (let level = 0; level < this._levelCount; level++) { const dimAtLevel = Math.pow(2, level); const voxelCountX = dimensions.x * dimAtLevel; const voxelCountY = dimensions.y * dimAtLevel; const voxelCountZ = dimensions.z * dimAtLevel; const voxelsPerLevel = voxelCountX * voxelCountY * voxelCountZ; const levelData = (allVoxelData[level] = new Array( voxelsPerLevel * channelCount ));
for (let z = 0; z < voxelCountX; z++) { for (let y = 0; y < voxelCountY; y++) { const indexZY = z * voxelCountY * voxelCountX + y * voxelCountX; for (let x = 0; x < voxelCountZ; x++) { const index = (indexZY + x) * channelCount; levelData[index + 0] = x / (voxelCountX - 1); levelData[index + 1] = y / (voxelCountY - 1); levelData[index + 2] = z / (voxelCountZ - 1); levelData[index + 3] = 0.5; } } } } }
ProceduralMultiTileVoxelProvider.prototype.requestData = function (options) { const { tileLevel, tileX, tileY, tileZ } = options;
if (tileLevel >= this._levelCount) { return Promise.reject( `${this._levelCount - 1}级以上的瓦片不可用` ); }
const type = this.types[0]; const channelCount = Cesium.MetadataType.getComponentCount(type); const { dimensions, paddingBefore, paddingAfter } = this; const paddedDimensions = Cesium.Cartesian3.fromElements( dimensions.x + paddingBefore.x + paddingAfter.x, dimensions.y + paddingBefore.y + paddingAfter.y, dimensions.z + paddingBefore.z + paddingAfter.z ); const dimAtLevel = Math.pow(2, tileLevel); const dimensionsGlobal = Cesium.Cartesian3.fromElements( dimensions.x * dimAtLevel, dimensions.y * dimAtLevel, dimensions.z * dimAtLevel );
const minimumGlobalCoord = Cesium.Cartesian3.ZERO; const maximumGlobalCoord = new Cesium.Cartesian3( dimensionsGlobal.x - 1, dimensionsGlobal.y - 1, dimensionsGlobal.z - 1 ); let coordGlobal = new Cesium.Cartesian3();
const dataGlobal = this._allVoxelData; const dataTile = new Float32Array( paddedDimensions.x * paddedDimensions.y * paddedDimensions.z * channelCount );
for (let z = 0; z < paddedDimensions.z; z++) { const indexZ = z * paddedDimensions.y * paddedDimensions.x; for (let y = 0; y < paddedDimensions.y; y++) { const indexZY = indexZ + y * paddedDimensions.x; for (let x = 0; x < paddedDimensions.x; x++) { const indexTile = indexZY + x;
coordGlobal = Cesium.Cartesian3.clamp( Cesium.Cartesian3.fromElements( tileX * dimensions.x + (x - paddingBefore.x), tileY * dimensions.y + (y - paddingBefore.y), tileZ * dimensions.z + (z - paddingBefore.z), coordGlobal ), minimumGlobalCoord, maximumGlobalCoord, coordGlobal );
const indexGlobal = coordGlobal.z * dimensionsGlobal.y * dimensionsGlobal.x + coordGlobal.y * dimensionsGlobal.x + coordGlobal.x;
for (let c = 0; c < channelCount; c++) { dataTile[indexTile * channelCount + c] = dataGlobal[tileLevel][indexGlobal * channelCount + c]; } } } } const content = Cesium.VoxelContent.fromMetadataArray([dataTile]); return Promise.resolve(content); };
function createPrimitive(provider, customShader) { viewer.scene.primitives.removeAll();
const voxelPrimitive = viewer.scene.primitives.add( new Cesium.VoxelPrimitive({ provider: provider, customShader: customShader, }) );
viewer.voxelInspector.viewModel.voxelPrimitive = voxelPrimitive; viewer.camera.flyToBoundingSphere(voxelPrimitive.boundingSphere, { duration: 0.0, });
return voxelPrimitive; }
const customShaderColor = new Cesium.CustomShader({ fragmentShaderText: `void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { //使用体素的 RGB 颜色作为漫反射颜色 material.diffuse = fsInput.metadata.color.rgb; //基于体素的 alpha 值计算透明度(1-alpha) float transparency = 1.0 - fsInput.metadata.color.a;
//体素光线穿越距离(travelDistance)放大16倍 float thickness = fsInput.voxel.travelDistance * 16.0; //为模拟光线散射效果,请采用指数衰减法, material.alpha = 1.0 - pow(transparency, thickness); }`, });
Sandcastle.addToolbarMenu([ { text: "椭球-程序化瓦片集(单瓦片层级)", onselect: function () { const provider = new ProceduralSingleTileVoxelProvider( Cesium.VoxelShapeType.ELLIPSOID ); provider.minBounds.z = 0.0; provider.maxBounds.z = 1000000.0; const primitive = createPrimitive(provider, customShaderColor); }, }, { text: "圆柱-程序化瓦片集(单瓦片层级)", onselect: function () { const provider = new ProceduralSingleTileVoxelProvider( Cesium.VoxelShapeType.CYLINDER ); const primitive = createPrimitive(provider, customShaderColor); }, }, { text: "立方体-程序化瓦片集(单瓦片层级)", onselect: function () { const provider = new ProceduralSingleTileVoxelProvider( Cesium.VoxelShapeType.BOX ); const primitive = createPrimitive(provider, customShaderColor); }, }, { text: "立方体-程序化瓦片集(多瓦片层级)", onselect: function () { const provider = new ProceduralMultiTileVoxelProvider( Cesium.VoxelShapeType.BOX ); const primitive = createPrimitive(provider, customShaderColor); }, }, { text: "椭球-程序化瓦片集(多瓦片层级)", onselect: function () { const provider = new ProceduralMultiTileVoxelProvider( Cesium.VoxelShapeType.ELLIPSOID ); provider.minBounds.z = 0.0; provider.maxBounds.z = 1000000.0; const primitive = createPrimitive(provider, customShaderColor); }, }, { text: "圆柱-程序化瓦片集(多瓦片层级)", onselect: function () { const provider = new ProceduralMultiTileVoxelProvider( Cesium.VoxelShapeType.CYLINDER ); const primitive = createPrimitive(provider, customShaderColor); }, }, ]);
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (movement) { const scene = viewer.scene; const camera = scene.camera; const mousePosition = movement.position; const pickedPrimitive = scene.pick(mousePosition); console.log(pickedPrimitive); }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|