因为项目需要,调研了智慧城市,智慧园区,数字孪生的相关概念,发现数字孪生下有一个小分支,叫做3d物联网,简单说就是使用三维技术模拟出客户的场景(楼宇,工厂,设备间,设备运行等),要实现这个web端的几种三维技术就必须熟悉其中一二,而我是零基础起步。threejs看了几天,发现确实很强大,但是场景里的所有东西都需要从头开始搭建,估计一个人按规定时间很难完成,后来发现了maptalks,基于地图,可以先使用地图里的元素(建筑,道路,水系等)构建一个基础场景(有些需求可能基础场景已经够用),如下:

这个基本都是地图数据的渲染,效果已经很炫酷了。
我这边是需要虚拟出一个建筑,可以放大,旋转,看到内部情况,报警位置等,所以在阅读了maptalks大量的例子后,利用threelayer层来构建建筑,threelayer其实就是threejs的东西,只是可以直接使用地理坐标系,而我们的基础场景又是基于地图的,所以关于坐标系啥的就可以不用想太多。
这遍文章就贴下楼宇电梯的核心代码,关于建筑的构建,发光,楼层等构建,后面的文章再继续总结。
其中low是起步高度,height是运动的最大高度,altitude是物体的海拔高度,就是所处的高度位置;用随机数控制电梯在某层停止的效果;
var OPTIONS1 = {
altitude: 0,
low: 0,
height: 10,
trip: false,
rotation: false,
play: true,
chunkLength: 0.1
}
class TripBox extends maptalks.BaseObject {
constructor(coordinate, options, material, layer) {
options = maptalks.Util.extend({}, OPTIONS1, options, { layer, coordinate });
super();
//Initialize internal configuration
this._initOptions(options);
const { altitude, chunkLength, height, low } = options;
const h = layer.distanceToVector3(height, height).x;
const positions = [0, 0, 0, 0, 0, h];
//generate geometry
const geometry = new THREE.BoxBufferGeometry(0.08, 0.08, 0.1);
//Initialize internal object3d
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L140
this._createMesh(geometry, material);
//set object3d position
const z = layer.distanceToVector3(low, low).x;
const position = layer.coordinateToVector3(coordinate, z);
this.getObject3d().position.copy(position);
this._calTripData(layer);
}
_calTripData(layer) {
const { coordinate, chunkLength, height, low } = this.getOptions();
const z = layer.distanceToVector3(low, low).x;
const v = layer.coordinateToVector3(coordinate, z);
const positions = [v];
for (let i = low; i <= height; i += 0.5) {
const z = layer.distanceToVector3(i, i).x;
const v = layer.coordinateToVector3(coordinate, z);
positions.push(v);
}
this._positions = positions;
// this._bearings = bearings;
this._idx = -1;
this._reidx = (height - low) * 2;
this._rotating = false;
this.speed = 0;
}
_animation() {
const { play, trip, rotation, height, low } = this.getOptions();
const { _idx, _positions } = this;
if (this.speed == 0) {
this.speed = Math.round(Math.random() * 800);
}
if (this.speed > 500) {
this.speed--;
if (this.speed <= 0) this.speed = 0;
} else {
this._idx++;
if (this._idx < (height - low) * 2) {
this.getObject3d().position.copy(_positions[this._idx]);
}
if (this._idx >= (height - low) * 2) {
this._idx = (height - low) * 2;
this._reidx--;
this.getObject3d().position.copy(_positions[this._reidx]);
if (this._reidx <= 0) {
this._reidx = (height - low) * 2;
this._idx = -1;
}
}
this.speed--;
if (this.speed <= 0) this.speed = 0;
}
}
}
调用代码如下:
var dt = [
{ coordinate: [13.416, 52.53793], low: 0, height: 500 },
{ coordinate: [13.41629, 52.53798], low: 50, height: 350 },
{ coordinate: [13.4167, 52.538], low: 100, height: 200 },
{ coordinate: [13.41703, 52.53806], low: 0, height: 300 },
{ coordinate: [13.41744, 52.53694], low: 40, height: 240 },
{ coordinate: [13.41753, 52.53681], low: 100, height: 500 },
{ coordinate: [13.41775, 52.53679], low: 80, height: 260 },
{ coordinate: [13.41801, 52.53705], low: 200, height: 300 },
{ coordinate: [13.41776, 52.5372], low: 150, height: 480 },
{ coordinate: [13.41744, 52.53716], low: 30, height: 220 }
];
var dt_linds = [];
var dts = [];
dt.forEach(element => {
var line_dt = new Line(element.coordinate, { low: 0, height: 500, interactive: false },
new THREE.LineBasicMaterial({ color: '#FF7F24', transparent: true, opacity: 0.7, blending: THREE.AdditiveBlending }),
threeLayer
);
dt_linds.push(line_dt);
var dt = new TripBox(
element.coordinate,
{ low: element.low, height: element.height },
new THREE.MeshPhongMaterial({
color: "#FFFF00",
blending: THREE.AdditiveBlending,
}),
threeLayer
);
dt.getObject3d().layers.enable(1);
dts.push(dt);
});
赶鸭子上架的项目,最终效果实现了,但是理论不足,只能把代码贴出来,以后再优化时能想起来大概思路。
效果如下:

1万+




被折叠的 条评论
为什么被折叠?



