ArcGIS JSAPI 高级教程 - 创建渐变色材质的自定义几何体

ArcGIS JSAPI 高级教程 - 创建渐变色材质的自定义几何体

工作中遇到一个比较复杂的功能,其中用到渐变色,于是研究了一下,发现虽然 JS API 不直接支持渐变色,但是也可以自定义创建渐变色,通过 Mesh 自定义顶点、索引来实现。

这里简单介绍一下原理,并且提供在线渐变色示例。

在这里插入图片描述

本文包括 核心代码、完整代码以及在线示例


核心代码

介绍一下原理:首先通过 canvas 创建渐变色,可以选择两个颜色或者多个颜色。

根据想要创建的几何体,构建顶点和索引数据,简单图形的话,根据经验即可,如果是复杂几何体,

可以通过一些工具来获取,然后通过 Mesh 创建几何体对象即可,最终添加到地图中。



// 创建渐变色
function createLinearGradient() {

    const canvas = document.createElement("canvas");
    const width = 32 * 32;
    const height = 32 * 32;
    canvas.width = width;
    canvas.height = height;

    const ctx = canvas.getContext("2d");
    ctx.globalAlpha = globalAlpha;

    // Create the linear gradient with which to fill the canvas
    const gradient = ctx.createLinearGradient(0, 0, width, 0);

    // 这里创建三个渐变色,可随意调整
    gradient.addColorStop(0, "#0000ff");
    gradient.addColorStop(0.5, "#ff0000");
    gradient.addColorStop(1, "#ffffff");
    // Fill the canvas with the gradient pattern
    ctx.fillStyle = gradient;
    ctx.fillRect(0, 0, width, height);

    return canvas;
}

const uv = 1;

// 材质
const material = {
    colorTexture: {
        data: createLinearGradient(),
        wrap: 'clamp'
    },
};

// 创建 box
const mesh = new Mesh({
    // 顶点属性
    vertexAttributes: {
        position: cameraPositionGeographic,
        uv: [
            0, 0,
            uv, 0,
            uv, 0,
            uv, 0,
            uv, 0,
        ]
    },
    // 三角面材质纹理
    components: [
        {
            faces: [0, 2, 1],
            material
        },
        {
            faces: [0, 2, 3],
            material
        },
        {
            faces: [0, 3, 4],
            material
        },
        {
            faces: [0, 4, 1],
            material
        },
        {
            faces: [1, 2, 4],
            material: {
                color: "transparent"
            }
        },
        {
            faces: [2, 3, 4],
            material: {
                color: "transparent"
            }
        }],
    spatialReference,
})


完整代码


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>ArcGIS JS API mesh 自定义几何渐变色示例</title>
    <style>
        html, body, #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>

    <!-- 引入ArcGIS JS API样式和脚本 -->
    <link rel="stylesheet" href="https://js.arcgis.com/4.28/esri/themes/light/main.css">
    <script src="https://js.arcgis.com/4.28/"></script>

    <script>
        require([
            "esri/Map",
            "esri/views/SceneView",
            "esri/WebScene",
            "esri/geometry/Mesh",
            "esri/geometry/Point",
            "esri/geometry/SpatialReference",
            "esri/Graphic",
        ], function (Map, SceneView, WebScene, Mesh, Point, SpatialReference, Graphic,
        ) {

            const view = new SceneView({
                container: "viewDiv",
                map: new Map({
                    basemap: "hybrid",
                }),
            });

            const spatialReference = SpatialReference.WebMercator;
            // 视椎体顶点
            const cameraPositionGeographic = [12121597.211481025, 4060392.3041098495, 673.0166350845248, 12121688.817691227, 4060067.135944337, 825.3678379664198, 12121265.251479909, 4060329.6045611626, 825.3678379654884, 12121245.121950569, 4060297.137362419, 571.5452030794695, 12121668.70426917, 4060034.659804911, 571.5452030794695]

            // 透明度
            const globalAlpha = 0.7;

            // 创建渐变色
            function createLinearGradient() {

                const canvas = document.createElement("canvas");
                const width = 32 * 32;
                const height = 32 * 32;
                canvas.width = width;
                canvas.height = height;

                const ctx = canvas.getContext("2d");
                ctx.globalAlpha = globalAlpha;

                // Create the linear gradient with which to fill the canvas
                const gradient = ctx.createLinearGradient(0, 0, width, 0);

                // 这里创建三个渐变色,可随意调整
                gradient.addColorStop(0, "#0000ff");
                gradient.addColorStop(0.5, "#ff0000");
                gradient.addColorStop(1, "#ffffff");
                // Fill the canvas with the gradient pattern
                ctx.fillStyle = gradient;
                ctx.fillRect(0, 0, width, height);

                return canvas;
            }

            const uv = 1;

            // 材质
            const material = {
                colorTexture: {
                    data: createLinearGradient(),
                    wrap: 'clamp'
                },
            };

            // 创建 box
            const mesh = new Mesh({
                // 顶点属性
                vertexAttributes: {
                    position: cameraPositionGeographic,
                    uv: [
                        0, 0,
                        uv, 0,
                        uv, 0,
                        uv, 0,
                        uv, 0,
                    ]
                },
                // 三角面材质纹理
                components: [
                    {
                        faces: [0, 2, 1],
                        material
                    },
                    {
                        faces: [0, 2, 3],
                        material
                    },
                    {
                        faces: [0, 3, 4],
                        material
                    },
                    {
                        faces: [0, 4, 1],
                        material
                    },
                    {
                        faces: [1, 2, 4],
                        material: {
                            color: "transparent"
                        }
                    },
                    {
                        faces: [2, 3, 4],
                        material: {
                            color: "transparent"
                        }
                    }],
                spatialReference,
            })

            const conusGraphic = new Graphic({
                // 缩放至矩阵范围
                geometry: mesh,
                symbol: {
                    type: "mesh-3d",
                    symbolLayers: [
                        {
                            type: "fill",
                        },
                    ],
                },
            });

            // 将圆柱体添加到视图
            view.graphics.add(conusGraphic);

            // 当视图加载完成后执行
            view.when(() => {
                // 将视图缩放到圆柱体范围
                view.goTo({
                    target: mesh,
                    tilt: 90,
                    heading: 150
                }, {duration: 1500});
            });
        });
    </script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>



在这里插入图片描述


在线示例

ArcGIS Maps SDK for JavaScript 在线示例:创建渐变色材质的自定义几何体

内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value”)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个不同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了不同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非科班Java出身GISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值