diff --git a/demo/index.html b/demo/index.html
index 56b3f76..7ce619b 100644
--- a/demo/index.html
+++ b/demo/index.html
@@ -93,6 +93,7 @@
maptalks.three plugin Demos
+ layer-add-remove
mesh dispose
layer dispose
diff --git a/demo/layer-add-remove.html b/demo/layer-add-remove.html
new file mode 100644
index 0000000..4992801
--- /dev/null
+++ b/demo/layer-add-remove.html
@@ -0,0 +1,193 @@
+
+
+
+
+ osm buildings demo
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index 67f2f9b..a80c1e3 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -130,6 +130,7 @@ class ThreeLayer extends maptalks.CanvasLayer {
_identifyBaseObjectEventsThis: Function;
_zoomendThis: Function;
_emptyIdentifyThis: Function;
+ _meshes: Array = [];
constructor(id: string, options: BaseLayerOptionType) {
super(id, options);
@@ -707,6 +708,7 @@ class ThreeLayer extends maptalks.CanvasLayer {
}
}
}
+ this._meshes = [];
return this;
}
@@ -826,6 +828,10 @@ class ThreeLayer extends maptalks.CanvasLayer {
} else if (mesh instanceof THREE.Object3D) {
scene.add(mesh);
}
+ const index = this._meshes.indexOf(mesh);
+ if (index === -1) {
+ this._meshes.push(mesh);
+ }
});
this._zoomend();
if (render) {
@@ -871,6 +877,15 @@ class ThreeLayer extends maptalks.CanvasLayer {
} else if (mesh instanceof THREE.Object3D) {
scene.remove(mesh);
}
+ for (let i = 0, len = this._meshes.length; i < len; i++) {
+ const object3d = this._meshes[i];
+ if (!object3d) {
+ continue;
+ }
+ if (object3d === mesh) {
+ this._meshes.splice(i, 1);
+ }
+ }
});
if (render) {
const renderer = this._getRenderer();
@@ -1372,6 +1387,11 @@ class ThreeLayer extends maptalks.CanvasLayer {
return this;
}
+ _addBaseObjectsWhenInit() {
+ this.addMesh(this._meshes);
+ return this;
+ }
+
_callbackBaseObjectAnimation() {
const layer = this;
if (layer._animationBaseObjectMap) {
@@ -1474,6 +1494,7 @@ class ThreeRenderer extends maptalks.renderer.CanvasLayerRenderer {
scene.add(camera);
this.pick = new GPUPick(this.layer);
BaseObjectTaskManager.star();
+ this.layer._addBaseObjectsWhenInit();
}
onCanvasCreate() {