Cesium 自定義底圖顏色

function colorRgb(inColor) {
  // 16進(jìn)制顏色值的正則
  const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  // 把顏色值變成小寫
  let color = inColor.toLowerCase()
  if (reg.test(color)) {
    // 如果只有三位的值,需變成六位,如:#fff => #ffffff
    if (color.length === 4) {
      let colorNew = '#'
      for (let i = 1; i < 4; i += 1) {
        colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1))
      }
      color = colorNew
    }
    // 處理六位的顏色值,轉(zhuǎn)為RGB
    const colorChange = []
    for (let i = 1; i < 7; i += 2) {
      colorChange.push(parseInt('0x' + color.slice(i, i + 2)))
    }
    return colorChange
  }
  return []
}

const changeImageryProviderColors = (viewer, baseLayer) => {
  // 更改底圖的著色器 代碼
  const baseFragmentShaderSource =
    viewer.scene.globe._surfaceShaderSet.baseFragmentShaderSource.sources
  for (let i = 0; i < baseFragmentShaderSource.length; i++) {
    const oneSource = baseFragmentShaderSource[i]
    // 格式必須一致 不能多有空格 且保持版本一致性
    const strS = 'color = czm_saturation(color, textureSaturation);\n#endif\n'
    let strT = 'color = czm_saturation(color, textureSaturation);\n#endif\n'
    if (baseLayer.invertColor) {
      strT += `
        color.r = 1.0 - color.r;
        color.g = 1.0 - color.g;
        color.b = 1.0 - color.b;
      `
      strT += `
      color.r = color.r * ${baseLayer.filterRGB[0]}.0/255.0;
      color.g = color.g * ${baseLayer.filterRGB[1]}.0/255.0;
      color.b = color.b * ${baseLayer.filterRGB[2]}.0/255.0;
      `
    }

    if (oneSource.indexOf(strS) !== -1) {
      baseFragmentShaderSource[i] = baseFragmentShaderSource[i].replace(
        strS,
        strT
      )
    }
  }
}

const aMapProvider = new Cesium.UrlTemplateImageryProvider({  
    url: `https://webst0{s}.is.autonavi.com/appmaptile?style=${style}&x={x}&y={y}&z={z}&lang=zh_cn&size=1`,  
    subdomains: ['1', '2', '3', '4'], 
    tilingScheme: new Cesium.WebMercatorTilingScheme(),  
    minximuLevel: 3,
    maximumLevel: 15,
  });

const aMapImagery = viewer.imageryLayers.addImageryProvider(aMapProvider);

// 設(shè)置 濾鏡效果
  aMapImagery.invertColor = 1.0;
  // aMapImagery.filterRGB = [255.0, 255.0, 255.0]
  aMapImagery.filterRGB = colorRgb("#4e70a6");
  // aMapImagery.filterRGB = colorRgb("#ffff00");

  // 更改cesium的著色器代碼 關(guān)于濾鏡和反色的 [在不更改cesium源文件的情況下]
  changeImageryProviderColors(viewer, aMapImagery);

  // aMapImagery.alpha = 0.5; // 透明度
  // aMapImagery.dayAlpha = 0.5; // 日間透明度
  // aMapImagery.nightAlpha = 0.5; // 夜間透明度
  aMapImagery.brightness = 0.6; // 亮度
  aMapImagery.contrast = 1.8; // 對(duì)比度
  aMapImagery.hue = 1; // 色相
  aMapImagery.saturation = 0.0; // 飽和度
  aMapImagery.gamma = 0.3; // 伽馬值
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容