From 5bc13469f1ff5a5592dc7dc39109d06993dcf6b5 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Thu, 11 Mar 2021 10:02:56 -0500 Subject: [PATCH 1/3] Create troisjs object/group in separate component --- src/core/Object3D.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/Object3D.js b/src/core/Object3D.js index 22e57d1..5635d7d 100644 --- a/src/core/Object3D.js +++ b/src/core/Object3D.js @@ -27,7 +27,15 @@ export default { if (this.lookAt) this.o3d.lookAt(this.lookAt.x, this.lookAt.y, this.lookAt.z); watch(() => this.lookAt, (v) => { this.o3d.lookAt(v.x, v.y, v.z); }, { deep: true }); - if (this.$parent.add) this.$parent.add(this.o3d); + // traverse parents until we find something we can add to + let parent = this.$parent; + while (parent) { + if (parent.add) { + parent.add(this.o3d); + break; + } + parent = parent.$parent; + } }, add(o) { this.o3d.add(o); }, remove(o) { this.o3d.remove(o); }, From e47f6094e8f4cedda875b811d6256c5dd29b32d3 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Thu, 11 Mar 2021 17:15:52 -0500 Subject: [PATCH 2/3] Removes successfully --- src/core/Object3D.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/core/Object3D.js b/src/core/Object3D.js index 5635d7d..88c0f19 100644 --- a/src/core/Object3D.js +++ b/src/core/Object3D.js @@ -13,7 +13,15 @@ export default { // can't use setup because it will not be used in sub components // setup() {}, unmounted() { - if (this.$parent.remove) this.$parent.remove(this.o3d); + // traverse ancestors until we find something we can remove from + let parent = this.$parent; + while (parent) { + if (parent.remove) { + parent.remove(this.o3d); + break; + } + parent = parent.$parent; + } }, methods: { initObject3D(o3d) { @@ -27,7 +35,7 @@ export default { if (this.lookAt) this.o3d.lookAt(this.lookAt.x, this.lookAt.y, this.lookAt.z); watch(() => this.lookAt, (v) => { this.o3d.lookAt(v.x, v.y, v.z); }, { deep: true }); - // traverse parents until we find something we can add to + // traverse ancestors until we find something we can add to let parent = this.$parent; while (parent) { if (parent.add) { From e92a62b45b773095ac2b275f0004301d1bf39c87 Mon Sep 17 00:00:00 2001 From: Kevin LEVRON Date: Thu, 11 Mar 2021 23:58:05 +0100 Subject: [PATCH 3/3] few adjustments --- src/core/Object3D.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/core/Object3D.js b/src/core/Object3D.js index 88c0f19..1355204 100644 --- a/src/core/Object3D.js +++ b/src/core/Object3D.js @@ -13,15 +13,7 @@ export default { // can't use setup because it will not be used in sub components // setup() {}, unmounted() { - // traverse ancestors until we find something we can remove from - let parent = this.$parent; - while (parent) { - if (parent.remove) { - parent.remove(this.o3d); - break; - } - parent = parent.$parent; - } + if (this._parent) this._parent.remove(this.o3d); }, methods: { initObject3D(o3d) { @@ -31,19 +23,20 @@ export default { bindProp(this, 'rotation', this.o3d); bindProp(this, 'scale', this.o3d); - // fix lookat.x + // TODO : fix lookat.x if (this.lookAt) this.o3d.lookAt(this.lookAt.x, this.lookAt.y, this.lookAt.z); watch(() => this.lookAt, (v) => { this.o3d.lookAt(v.x, v.y, v.z); }, { deep: true }); - // traverse ancestors until we find something we can add to let parent = this.$parent; while (parent) { if (parent.add) { parent.add(this.o3d); + this._parent = parent; break; } parent = parent.$parent; } + if (!this._parent) console.error('Missing parent (Scene, Group...)'); }, add(o) { this.o3d.add(o); }, remove(o) { this.o3d.remove(o); },