Truf.js的浅使用(生成网格)

 

随机一组业务数据

随机生成30个随机点,并给每个点设置value属性为随机数

var points = turf.randomPoint(30, { bbox: turf.bbox(boundaries) });

//再生成些随机数做属性
turf.featureEach(points, function (currentFeature, featureIndex) {
  currentFeature.properties = { value: (Math.random() * 100).toFixed(2) };
});

 

差值算法

turf.interpolate() 提供了基于 IDW(反距离权重)算法的将数据插值为格点的方法。插值的精度是由第二个参数与 interpolate_options.units 共同决定的,单位支持 degrees, radians, miles, or kilometers,IDW 要为每个格点计算所有散点的权重,计算规模是 (散点数 * 格点数),所以要在精度与性能间做好平衡。 我们将之前的散点(points)代入

var interpolate_options = {
  gridType: "points",
  property: "value",
  units: "degrees",// 单位
  weight: 10 // 权重
};
// 点数, 格子大小, 配置
var grid = turf.interpolate(points, 0.05, interpolate_options);
// 适当降低插值结果的精度便于显示
grid.features.map((i) => (i.properties.value = i.properties.value.toFixed(2)));

画网格

根据算出的随机点,生成3dPolygon,并把value的值作为高度


/**
 * 添加热力
 * @param borderCoords 范围坐标
 * @param index
 */
const addHeat = async (borderCoords: any, index: number) => {
    let points: any = pointsArr[index]
    // 随机点添加随机value值
    turf.featureEach(points, function (point: any) {
        point.properties.value = getRandNumBetween(10, 100)
    })
    // 配置
    let options: any = {
        gridType: 'points', // FeatureCollection <Point>
        property: 'value', // 差值的属性值
        units: 'kilometers', // 单位 千米
        weight: weight // 权重
    }
    const grid = turf.interpolate(points, cellSize, options)
    console.log(grid, '---grid')
    // 判断范围
    // 将投影坐标转换为地理坐标
    const coords = borderCoords.map((item: any) => {
        const [x, y, z] = item
        const [longitude, latitude] = (window as any).proj4(Projection, 'EPSG:4326', [x, y])
        return [longitude, latitude]
    })
    const poly = turf.polygon([coords])

    // 3dPolygon数组
    const polygon3dArr = []
    for (const item of grid.features) {
        const pt = turf.point(item.geometry.coordinates)
        if (!turf.booleanPointInPolygon(pt, poly)) continue
        const index = grid.features.indexOf(item)
        // 地理坐标
        const [longitude, latitude] = item.geometry.coordinates
        // 地理坐标转换为投影坐标
        let [x, y] = (window as any).proj4('EPSG:4326', Projection, [longitude, latitude])
        // 差值
        // @ts-ignore
        const val = Math.floor(item.properties.value)
        // 盒子z轴高度
        const z = val * 30 + 700
        // 计算四个角
        const topLeft = [x - sideLength, y + sideLength, z]
        const topRight = [x - sideLength, y - sideLength, z]
        const bottomLeft = [x + sideLength, y - sideLength, z]
        const bottomRight = [x + sideLength, y + sideLength, z]

        polygon3dArr.push({
            id: 'polygon3d_' + index, //折线唯一标识id
            coordinates: [topLeft, topRight, bottomLeft, bottomRight], //构成折线的坐标点数组
            color: [ColorList[val][0], ColorList[val][1], ColorList[val][2], val / 200], //颜色值
            height: -z, //3D多边形的高度
            intensity: 1, //亮度
            style: 3, //3DPolygon的样式 请参照API开发文档选取枚举
            generateTop: false, //是否生成顶面
            generateSide: true, //是否生成侧面
            generateBottom: false, //是否生成底面
            z: z,
            value: val
        })
        needDelete3dPolygonArr.push('polygon3d_' + index)
    }
    await __g.polygon3d.add(polygon3dArr)
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要使用 three.js 创建一个网格背景,你可以使用以下步骤: 1. 首先,创建一个场景: ``` const scene = new THREE.Scene(); ``` 2. 然后,使用 `THREE.PlaneBufferGeometry` 创建一个平面几何体,并指定它的宽度和高度: ``` const geometry = new THREE.PlaneBufferGeometry(width, height); ``` 3. 接下来,使用 `THREE.MeshBasicMaterial` 创建一个材质,并指定一张网格背景图片作为贴图: ``` const texture = new THREE.TextureLoader().load('./grid.jpg'); const material = new THREE.MeshBasicMaterial({ map: texture }); ``` 4. 最后,使用上述几何体和材质创建一个网格(mesh)并将它添加到场景中: ``` const mesh = new THREE.Mesh(geometry, material); scene.add(mesh); ``` 这样,你就可以使用 three.js 创建了一个网格背景。 ### 回答2: 使用three.js创建一个网格背景的方法如下: 1. 首先,引入three.js库。可以从官方网站(https://threejs.org/)下载或使用CDN链接。 2. 创建一个HTML元素,作为三维场景的容器。例如,可以使用一个div元素作为容器: ```html <div id="scene-container"></div> ``` 3. 在JavaScript代码中,创建一个场景、相机和渲染器。 ```javascript const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.getElementById('scene-container').appendChild(renderer.domElement); ``` 4. 创建一个网格背景。可以使用THREE.GridHelper类来创建,它可以生成一个网格状的辅助线。 ```javascript const gridHelper = new THREE.GridHelper(10, 10); scene.add(gridHelper); ``` 这里的参数10,10表示网格的大小和细分数目。 5. 添加一些其他物体和光源到场景中,以创建更丰富的场景效果。例如,可以添加一个立方体和一个点光源。 ```javascript const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); const light = new THREE.PointLight(0xffffff, 1, 100); light.position.set(0, 0, 10); scene.add(light); ``` 6. 更新场景并渲染。 ```javascript function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); } animate(); ``` 通过以上步骤,你就可以使用three.js创建一个拥有网格背景的三维场景了。当然,你也可以根据自己的需要调整背景网格的大小、颜色等参数来实现不同的效果。 ### 回答3: three.js是一款用于在Web浏览器中创建3D图形的JavaScript库。要创建一个网格背景,我们可以使用three.js中的GridHelper类。 首先,我们需要在HTML文件中引入three.js库。你可以从官方网站上下载最新版本的库文件,并将其添加到你的HTML文件中。 接下来,我们需要在JavaScript代码中创建一个场景(scene)、一个相机(camera)和渲染器(renderer)。你可以选择适合你需求的场景和相机类型。 初始化场景和相机的代码如下: ``` var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); ``` 然后,我们可以使用GridHelper类来创建网格背景。GridHelper类接受三个参数:网格的大小、每个格子的尺寸和网格线的颜色。你可以根据需要调整这些参数。 创建网格背景的代码如下: ``` var gridHelper = new THREE.GridHelper(10, 1); scene.add(gridHelper); ``` 最后,我们需要设置相机的位置和渲染场景。 设置相机位置的代码如下: ``` camera.position.z = 5; ``` 渲染场景的代码如下: ``` function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); ``` 现在,你可以在浏览器中查看效果了。当你刷新页面时,你将看到一个带有网格背景的场景。你可以通过调整网格的大小和颜色来自定义网格背景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值