当前位置: 首页 > news >正文

cesium中,获取光标处颜色

替换官方的helloworld.html即可

<!doctype html>
<html lang="en">
  <head>
    <!-- Use correct character set. -->
    <meta charset="utf-8" />
    <!-- Tell IE to use the latest, best version. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <title>Hello World!</title>
    <script src="../Build/CesiumUnminified/Cesium.js"></script>
    <style>
      @import url(../Build/CesiumUnminified/Widgets/widgets.css);
      html,
      body,
      #cesiumContainer {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div id="cesiumContainer"></div>
    <script>
      const viewer = new Cesium.Viewer("cesiumContainer", {
        contextOptions: { webgl: { preserveDrawingBuffer: true } }, // 关键配置
      });
      const scene = viewer.scene;

      let mousePosition = null;
      const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
      handler.setInputAction((movement) => {
        mousePosition = movement.endPosition;
        test();
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

      function test() {
        // 精确坐标系转换
        const x = Math.floor(mousePosition.x);
        const y = Math.floor(scene.canvas.height - mousePosition.y);

        if (
          x < 0 ||
          x >= scene.canvas.width ||
          y < 0 ||
          y >= scene.canvas.height
        )
          return;

        // 新版核心修正:直接使用WebGL常量
        const gl = scene.context._gl;
        const pixel = new Uint8Array(4);
        gl.readPixels(
          x,
          y,
          1,
          1,
          gl.RGBA, // 对应格式常量
          gl.UNSIGNED_BYTE, // 对应类型常量
          pixel,
        );

        // 空值保护
        if (!pixel || pixel.length < 4) return;

        const color = Cesium.Color.fromBytes(
          pixel[0],
          pixel[1],
          pixel[2],
          pixel[3],
        );
        console.log(
          "%c" + `RGBA: [${pixel.join(", ")}]`,
          `background-color: ${color.toCssHexString()}`,
        );
      }
    </script>
  </body>
</html>

在这里插入图片描述

http://www.dtcms.com/a/55516.html

相关文章:

  • STM32之BKP
  • Redis主从复制
  • 考前冲刺,消防设施操作员考试最后一击
  • 深入剖析分布式事务:原理、方案与实战指南
  • WebGPT: 基于浏览器辅助的问答系统,结合人类反馈优化答案质量
  • 介绍一下Qt中的动态属性
  • 【leetcode hot 100 206】反转链表
  • Manus开源平替-开源通用智能体
  • TCP7680端口是什么服务
  • 【JAVA架构师成长之路】【Redis】第14集:Redis缓存穿透原理、规避、解决方案
  • 【从零开始学习计算机科学】数字逻辑(九)有限状态机
  • Android Studio右上角Gradle 的Task展示不全
  • 【程序自动分析——并查集,离散化】
  • DeepSeek提问术:解锁AI交互新姿势-20 个精准提问框架
  • MES系统如何实现远程访问?
  • 基于SpringBoot实现旅游酒店平台功能一
  • 10 个玉米重组自交系种群植物结构的遗传基础
  • Go 语言 + libbpfgo 实战 eBPF 开发
  • linux c++11 gcc4 环境编译安装googletest/gtest v1.10
  • 为AI聊天工具添加一个知识系统 之138 设计重审 之3 文章学 之 引言 之3 纵观三观( 加入 )
  • 无人机推流/RTMP视频推拉流:EasyDSS无法卸载软件的原因及解决方法
  • 饮食 “巧调理”,缓解手抖有妙方
  • Jenkins在Windows上的使用(二):自动拉取、打包、部署
  • Deepin下创建AppImage应用的快捷方式
  • windows:curl: (60) schannel: SEC_E_UNTRUSTED_ROOT (0x80090325)
  • 【UCB CS 61B SP24】 Lecture 25 26 - Minimum Spanning Trees 学习笔记
  • Mysql-主从搭建如何指定库表同步以及新增库表同步
  • 从毕达哥拉斯定理到向量距离和夹角的计算
  • SCSS预处理器(详细讲解、入门教程)
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(一)