首页 > 其他分享 >Cesium 绘制更新网格数据

Cesium 绘制更新网格数据

时间:2024-11-18 09:56:33浏览次数:1  
标签:const 网格 baseInfo 128 let rgba Cesium 绘制

一、源码

import type { Viewer, Primitive, Material } from "cesium";

const cesiumUtils = {
    viewer: <Viewer | null>null,
    rainPrimitive: <Primitive | null>null,
    interValIndex: 0,
    playIndex: 1,//播放材质索引
    isPlayIng: false,
    isPauseIng: false,
    setStatus: (options: any): void => { },
    imgArr: <Array<string>>[],
    m4DataArr: <Array<Array<string>>>[],
    gridmaterial: new Cesium.Material({
        fabric: {
            type: Cesium.Material.GridType,
            uniforms: {
                color: new Cesium.Color(0.0, 1.0, 1.0, 1.0),
                cellAlpha: 0.1,
                lineCount: new Cesium.Cartesian2(8.0, 8.0),
                lineThickness: new Cesium.Cartesian2(1.0, 1.0),
                lineOffset: new Cesium.Cartesian2(0.0, 0.0),
            },
        },
    }),
    gradientColors: [
        "#FF0000", // 红色
        "#FF3333",
        "#FF6666",
        "#FF9999",
        "#FFCCCC",
        "#CCFFFF", // 淡蓝色
        "#99CCFF",
        "#6699FF",
        "#3366FF",
        "#0000FF", // 蓝色
    ],
    gradientColors2: [
        "rgba(0,0,255,0.1)",
        "rgba(0,0,255,0.21)",
        "rgba(0,0,255,0.3)",
        "rgba(0,0,255,0.3)",
        "rgba(0,255,255,0.9)",
        "rgba(0,255,255,1)",
        "rgba(0,255,255,1)",
        "rgba(0,255,255,1)",
        "rgba(0,255,255,0.1)",
        "rgba(0,0,255,0.1)",
    ],
    drawMesh(dataArr: Array<string>) {
        const baseInfo = dataArr[0].split(" ");
        const width = Number(baseInfo[16]) - 1;
        const height = Number(baseInfo[17]) - 1;
        let startLon = Number(baseInfo[12]);
        let endLon = Number(baseInfo[13]);
        let startLat = Number(baseInfo[14]);
        let endLat = Number(baseInfo[15]);
        let isNumber = 2;
        const canvas = document.createElement("canvas");
        canvas.width = width * isNumber;
        canvas.height = height * isNumber;

        let ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
        this.imgArr = []
        for (let i = 0; i < width; i++) {
            for (let j = 0; j < height; j++) {
                const startX = i * isNumber;
                const startY = j * isNumber;

                let color = "rgba(128,128,128,0.3)"
                if (Math.random() < 0.95) {
                    color = "rgba(128,128,128,0.3)"
                } else {
                    color = "rgba(128,128,128,1)"
                }
                // this.gradientColors[Number((Math.sin(index)).toFixed(0))];
                ctx.fillStyle = color;
                ctx.fillRect(startX, startY, isNumber, isNumber);
            }
        }
        let img = canvas.toDataURL("image/png");
        this.imgArr.push(img)
        let instance = new Cesium.GeometryInstance({
            geometry: new Cesium.RectangleGeometry({
                rectangle: Cesium.Rectangle.fromDegrees(
                    startLon,
                    startLat,
                    endLon,
                    endLat
                ),
                vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
            }),
        });

        let rectangleAppearance = new Cesium.EllipsoidSurfaceAppearance({
            material: Cesium.Material.fromType("Image", {
                image: img,
            }),
        });
        let viewer = this.viewer as Viewer
        this.rainPrimitive = viewer.scene.primitives.add(
            new Cesium.GroundPrimitive({
                geometryInstances: instance,
                appearance: rectangleAppearance,
            })
        );


    },
    setMateiral(dataArr: Array<string>) {
        const baseInfo = dataArr[0].split(" ");
        const width = Number(baseInfo[16]) - 1;
        const height = Number(baseInfo[17]) - 1;
        let isNumber = 2;
        const canvas = document.createElement("canvas");
        canvas.width = width * isNumber;
        canvas.height = height * isNumber;
        let ctx = canvas.getContext("2d") as CanvasRenderingContext2D;

        for (let i = 0; i < width; i++) {
            for (let j = 0; j < height; j++) {
                const startX = i * isNumber;
                const startY = j * isNumber;

                let color = "rgba(128,128,128,0.3)"
                if (Math.random() < 0.95) {
                    color = "rgba(128,128,128,0.3)"
                } else {
                    color = "rgba(128,128,128,1)"
                }
                // this.gradientColors[Number((Math.sin(index)).toFixed(0))];
                ctx.fillStyle = color;
                ctx.fillRect(startX, startY, isNumber, isNumber);
            }
        }
        let img = canvas.toDataURL("image/png");
        this.imgArr.push(img);
    },
    setPrimitive(dataArr: Array<string>, firstFrame: string) {
        const baseInfo = dataArr[0].split(" ");
        let startLon = Number(baseInfo[12]);
        let endLon = Number(baseInfo[13]);
        let startLat = Number(baseInfo[14]);
        let endLat = Number(baseInfo[15]);

        let instance = new Cesium.GeometryInstance({
            geometry: new Cesium.RectangleGeometry({
                rectangle: Cesium.Rectangle.fromDegrees(
                    startLon,
                    startLat,
                    endLon,
                    endLat
                ),
                vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
            }),
        });

        let rectangleAppearance = new Cesium.MaterialAppearance({
            material: new Cesium.Material({
                fabric: {
                    type: 'Image',
                    uniforms: {
                        image: firstFrame
                    }
                }
            }),
        }

        )
        let viewer = this.viewer as Viewer
        this.rainPrimitive = viewer.scene.primitives.add(
            new Cesium.GroundPrimitive({
                geometryInstances: instance,
                appearance: rectangleAppearance,
            })
        );
    },
    updateMaterial(material: any) {
        let primitive = this.rainPrimitive as Primitive
        primitive.appearance.material.uniforms['image'] = material
    },
    startPlay(setStatus: (options: any) => void) {
        let that = this;
        this.isPlayIng = true
        console.log(that.imgArr.length);
        if (this.imgArr.length > 0 && this.rainPrimitive != null) {
            this.interValIndex = setInterval(() => {
                const material = that.imgArr[that.playIndex];
                that.updateMaterial(material)
                that.playIndex = that.playIndex + 1
                console.log(that.playIndex);
                let options = {
                    step: that.playIndex / that.imgArr.length,
                    isPlayIng: this.isPlayIng,
                    isPauseIng: this.isPauseIng
                }
                setStatus(options)
                this.setStatus = setStatus
                if (that.playIndex == that.imgArr.length) {
                    clearInterval(this.interValIndex)
                }
            }, 3000)
        }
    },
    pauseOrPlay() {
        if (this.isPlayIng &&this.playIndex!=this.imgArr.length) {
            
            if (!this.isPauseIng) {
                clearInterval(this.interValIndex)
                this.isPauseIng = true;
                let options = {
                    step: this.playIndex / this.imgArr.length,
                    isPlayIng: this.isPlayIng,
                    isPauseIng: this.isPauseIng
                }
                this.setStatus(options)
            } else {
                this.isPauseIng = false
                this.startPlay(this.setStatus)
            }

        }

    },
    async setM4Data(urlArr: Array<string>) {
        this.m4DataArr = [];
        for (let i = 0; i < urlArr.length; i++) {
            const element = urlArr[i];
            let m4Data = await getM4Data(element);
            this.m4DataArr.push(m4Data)
        }
    },
    async initBaseData(urlArrr: Array<string>, viewer: Viewer) {
        this.viewer = viewer
        await this.setM4Data(urlArrr)
        this.m4DataArr.forEach(x => {
            this.setMateiral(x)
        })

        this.setPrimitive(this.m4DataArr[0], this.imgArr[0])


        return true;
    },

    restorePlay() {
        clearInterval(this.interValIndex);
        this.playIndex = 1
        this.isPlayIng = false;
        this.isPauseIng = false

        if (this.rainPrimitive != null) {
            this.viewer?.scene.primitives.remove(this.rainPrimitive)
            this.rainPrimitive = null
        }
        this.setStatus({
            isPauseIng: false,
            isPlayIng: false,
            step: 0
        })
    }
};




//获取最大值
function geRenderData(dataArr: any) {
    let maxValue = 0;

    let renderData = []
    for (let i = 1; i < dataArr.length; i++) {
        const element = dataArr[i];
        const tmpArr = element.trim().split("      ")
        let rowArr = []
        for (let j = 0; j < tmpArr.length; j++) {
            const res = Number(tmpArr[j]);
            if (res > maxValue) {
                maxValue = res
            }
            rowArr.push(res)
        }
        renderData.push(rowArr)
    }
    return {
        maxValue,
        renderData
    }
}
//获取数据
async function getM4Data(url: string) {
    const fetchData = await fetch(url);
    const data = await fetchData.text();
    const dataArr = data.split("\n");

    return dataArr;
}

export { getM4Data, cesiumUtils };

 

标签:const,网格,baseInfo,128,let,rgba,Cesium,绘制
From: https://www.cnblogs.com/xiaoqiyaozou/p/18551807

相关文章

  • scratch绘制八个正方形 2024年9月scratch四级真题 中国电子学会 图形化编程 scratch四
    目录scratch绘制八个正方形一、题目要求1、准备工作2、功能实现二、案例分析1、角色分析2、背景分析3、前期准备三、解题思路1、思路分析2、详细过程四、程序编写五、考点分析六、推荐资料1、入门基础2、蓝桥杯比赛3、考级资料4、视频课程5、python资料s......
  • Cesium初级开发教程之十五:抗锯齿和分辨率优化
    一、效果图 二、代码//抗锯齿viewer.scene.fxaa=true;viewer.scene.postProcessStages.fxaa.enabled=trueviewer._cesiumWidget._supportsImageRenderingPixelated=Cesium.FeatureDetection.supportsImageRenderingPixelated()vi......
  • canvas绘制文本
    练习一下canvas对文本的基本应用<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><div><labelfor="name">......
  • python岭迹图绘制函数
    一.岭迹图是什么?    岭迹图(RidgeTracePlot)是一种可视化工具,用于展示岭回归中正则化参数($\alpha$)对回归系数的影响。它能帮助我们理解特征的稳定性和正则化在控制模型复杂度中的作用。二.什么时候需要看岭迹图?存在共线性特征时当特征间高度相关,普通最小二乘法(OLS)......
  • 磁场的绘制
    详见如下python代码 importmatplotlib.pyplotaspltimportnumpyasnp#定义常量mu_0=4*np.pi*1e-7#真空的磁导率(Tm/A)#定义磁偶极子类classMagneticDipole:def__init__(self,magnetic_moment,position):self.magnetic_moment=np......
  • 电场的绘制
    详见python小程序 importnumpyasnpimportmatplotlib.pyplotasplt#定义常量k=8.99e9#库仑常数(Nm^2/C^2)#定义电荷类classPointCharge:def__init__(self,charge,position):self.charge=chargeself.position=np.array(po......
  • 小程序21-绘制轮播图
    在小程序中,提供了swiper和swiper-item组件实现轮播图swiper:滑块视图容器,其中只能放置swiper-item组件swiper-item:只可放置在swiper组件中,宽高自动设置为100%,代表swiper中的每一项文档所在位置:wswiper文档链接swiper常用属性autoplay:是否自动切换circular:是......
  • 绘制层次结构图
    绘制层次结构图WPS的智能图形要收费,先做个免费的不美观的版本。基于matplotlib,networkx,graphviz,pydot按需修改输入内容input_data为输入的文本。外观rankdir为指定方向。mpatches.Rectangle为节点外形。比例缩放matplotlib窗口,调整节点长宽。调整字体大小,当前为pl......
  • ICEMCFD网格划分基础教程
    (1)(2)ICEMCFD基础教程(一级水平)大小:5.4G声明:带案例文件,为英文视频教程。————————————————————————目录——————————————————————————————————————————————本课程涵盖的研讨会/案例包括课程1:NACA0012具......
  • [题解]P5687 [CSP-S2019 江西] 网格图
    P5687[CSP-S2019江西]网格图简单来说题目就是给定一个\(n\timesm\)的网格图,同行边权相同,同列边权相同,求该网格图的最小生成树。根据Kruskal算法的贪心思想,我们要优先选择权值尽可能小的行,并将这条边应用于尽可能多的列。列方向同理。为了保证最终生成树的连通性,我们显然要......