From 95ec11b59907734f8b12799b57d7048f109d240b Mon Sep 17 00:00:00 2001 From: Albertus Angga <albertusangga.98@gmail.com> Date: Fri, 17 Apr 2020 18:17:22 +0700 Subject: [PATCH] Fix parent child bugs and connect parent-child matrix --- experiment-3/model.js | 12 +++++++----- experiment-3/no-2.js | 1 + experiment-3/object-node.js | 9 ++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/experiment-3/model.js b/experiment-3/model.js index ccd552e..861ae74 100644 --- a/experiment-3/model.js +++ b/experiment-3/model.js @@ -22,7 +22,7 @@ class Model { this.node = ObjectNode.getOrCreate(name).updateWith({ model: this, - parentName: !!parent + parent: !!parentName ? ObjectNode.getOrCreate(parentName) : undefined, }); @@ -63,9 +63,11 @@ class Model { // objectNodesList and objectNameToId is a global variable // I know, it looks dirty to call those variables here // but this is the fastest way to call it. - console.log(this.name); - if (this.node.hasParent) { - var parentNode = thihs.node.parent; + + // If the node already has parent and the model is already initialized + if (this.node.hasParent && !!this.node.parent.model) { + var parentNode = this.node.parent; + console.log('parent: ', parentNode); var parentMatrix = parentNode.model.fullTransformMatrix; this.fullTransformMatrix = m4.multiply( parentMatrix, @@ -138,7 +140,7 @@ class Model { gl.uniformMatrix4fv( gl.modelMatrixLoc, false, - flatten(this.transformationMatrix) + flatten(this.fullTransformMatrix) ); gl.drawArrays(gl.TRIANGLES, this.bufferStartIndex, this.vertexCount); } diff --git a/experiment-3/no-2.js b/experiment-3/no-2.js index 272c0b6..4edbaa1 100644 --- a/experiment-3/no-2.js +++ b/experiment-3/no-2.js @@ -157,6 +157,7 @@ function initObjects() { }); if (!model.node.hasParent) rootNodes.push(model.node); }); + rootNodes.forEach((rootNode) => rootNode.updateTransformations()); } function initCanvasAndGL() { diff --git a/experiment-3/object-node.js b/experiment-3/object-node.js index fae0726..feb0aa3 100644 --- a/experiment-3/object-node.js +++ b/experiment-3/object-node.js @@ -10,7 +10,7 @@ class ObjectNode { this.key = key; this.parent = null; this.children = []; - this.model = {}; + this.model = null; } updateWith({ model, parent }) { @@ -36,4 +36,11 @@ class ObjectNode { this.model.render(gl); this.children.forEach(child => child.render(gl)); } + + updateTransformations() { + this.model.updateMatrices(); + this.children.forEach(children => { + children.updateTransformations() + }); + } } -- GitLab