From af5a0fef137af5271972545111927bb3f9eccea2 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Tue, 26 Oct 2021 08:50:20 +0200 Subject: [PATCH] Add node to mesh instance instead of transformation. --- sandbox/embed_selfhost_errors.html | 3 +- sandbox/embed_selfhost_externallibs.html | 3 +- sandbox/embed_selfhost_fullscreen.html | 3 +- sandbox/embed_selfhost_manual.html | 3 +- sandbox/embed_selfhost_multiple.html | 3 +- sandbox/embed_selfhost_single.html | 3 +- sandbox/embed_selfhost_single_scroll.html | 3 +- source/model/meshinstance.js | 45 +++++++++++++++++++---- source/model/meshinstanceid.js | 18 --------- source/model/model.js | 24 +++--------- source/model/node.js | 3 +- test/tests/model_test.js | 26 ++++++------- test/tests/modelutils_test.js | 20 +++++----- test/tests/node_test.js | 2 +- test/tests/quantities_test.js | 10 +++-- tools/config.json | 3 +- website/embed.html | 3 +- website/index.html | 3 +- 18 files changed, 85 insertions(+), 93 deletions(-) delete mode 100644 source/model/meshinstanceid.js diff --git a/sandbox/embed_selfhost_errors.html b/sandbox/embed_selfhost_errors.html index 3ea2d76..4400631 100644 --- a/sandbox/embed_selfhost_errors.html +++ b/sandbox/embed_selfhost_errors.html @@ -32,16 +32,15 @@ - + - diff --git a/sandbox/embed_selfhost_externallibs.html b/sandbox/embed_selfhost_externallibs.html index b32415f..f3febda 100644 --- a/sandbox/embed_selfhost_externallibs.html +++ b/sandbox/embed_selfhost_externallibs.html @@ -32,16 +32,15 @@ - + - diff --git a/sandbox/embed_selfhost_fullscreen.html b/sandbox/embed_selfhost_fullscreen.html index 182bec8..5ab20d6 100644 --- a/sandbox/embed_selfhost_fullscreen.html +++ b/sandbox/embed_selfhost_fullscreen.html @@ -31,16 +31,15 @@ - + - diff --git a/sandbox/embed_selfhost_manual.html b/sandbox/embed_selfhost_manual.html index 0de1d4c..0cd6cc7 100644 --- a/sandbox/embed_selfhost_manual.html +++ b/sandbox/embed_selfhost_manual.html @@ -32,16 +32,15 @@ - + - diff --git a/sandbox/embed_selfhost_multiple.html b/sandbox/embed_selfhost_multiple.html index c7d79f2..2b9f96d 100644 --- a/sandbox/embed_selfhost_multiple.html +++ b/sandbox/embed_selfhost_multiple.html @@ -32,16 +32,15 @@ - + - diff --git a/sandbox/embed_selfhost_single.html b/sandbox/embed_selfhost_single.html index 8a45298..db2daf4 100644 --- a/sandbox/embed_selfhost_single.html +++ b/sandbox/embed_selfhost_single.html @@ -31,16 +31,15 @@ - + - diff --git a/sandbox/embed_selfhost_single_scroll.html b/sandbox/embed_selfhost_single_scroll.html index b0a2467..2fe298a 100644 --- a/sandbox/embed_selfhost_single_scroll.html +++ b/sandbox/embed_selfhost_single_scroll.html @@ -31,16 +31,15 @@ - + - diff --git a/source/model/meshinstance.js b/source/model/meshinstance.js index 59c485d..768df9b 100644 --- a/source/model/meshinstance.js +++ b/source/model/meshinstance.js @@ -1,10 +1,29 @@ +OV.MeshInstanceId = class +{ + constructor (nodeId, meshIndex) + { + this.nodeId = nodeId; + this.meshIndex = meshIndex; + } + + IsEqual (rhs) + { + return this.nodeId === rhs.nodeId && this.meshIndex === rhs.meshIndex; + } + + GetKey () + { + return this.nodeId.toString () + ':' + this.meshIndex.toString (); + } +}; + OV.MeshInstance = class extends OV.Object3D { - constructor (mesh, transformation) + constructor (node, mesh) { super (); + this.node = node; this.mesh = mesh; - this.transformation = transformation; } VertexCount () @@ -29,11 +48,12 @@ OV.MeshInstance = class extends OV.Object3D EnumerateVertices (onVertex) { - if (this.transformation.IsIdentity ()) { + let transformation = this.node.GetWorldTransformation (); + if (transformation.IsIdentity ()) { this.mesh.EnumerateVertices (onVertex); } else { this.mesh.EnumerateVertices ((vertex) => { - const transformed = this.transformation.TransformCoord3D (vertex); + const transformed = transformation.TransformCoord3D (vertex); onVertex (transformed); }); } @@ -46,15 +66,24 @@ OV.MeshInstance = class extends OV.Object3D EnumerateTriangleVertices (onTriangleVertices) { - if (this.transformation.IsIdentity ()) { + let transformation = this.node.GetWorldTransformation (); + if (transformation.IsIdentity ()) { this.mesh.EnumerateTriangleVertices (onTriangleVertices); } else { this.mesh.EnumerateTriangleVertices ((v0, v1, v2) => { - const v0Transformed = this.transformation.TransformCoord3D (v0); - const v1Transformed = this.transformation.TransformCoord3D (v1); - const v2Transformed = this.transformation.TransformCoord3D (v2); + const v0Transformed = transformation.TransformCoord3D (v0); + const v1Transformed = transformation.TransformCoord3D (v1); + const v2Transformed = transformation.TransformCoord3D (v2); onTriangleVertices (v0Transformed, v1Transformed, v2Transformed); }); } } + + GetTransformedMesh () + { + let transformation = this.node.GetWorldTransformation (); + const transformed = OV.CloneMesh (this.mesh); + OV.TransformMesh (transformed, transformation); + return transformed; + } }; diff --git a/source/model/meshinstanceid.js b/source/model/meshinstanceid.js deleted file mode 100644 index 7686d78..0000000 --- a/source/model/meshinstanceid.js +++ /dev/null @@ -1,18 +0,0 @@ -OV.MeshInstanceId = class -{ - constructor (nodeId, meshIndex) - { - this.nodeId = nodeId; - this.meshIndex = meshIndex; - } - - IsEqual (rhs) - { - return this.nodeId === rhs.nodeId && this.meshIndex === rhs.meshIndex; - } - - GetKey () - { - return this.nodeId.toString () + ':' + this.meshIndex.toString (); - } -}; diff --git a/source/model/model.js b/source/model/model.js index 19d694d..61b6240 100644 --- a/source/model/model.js +++ b/source/model/model.js @@ -118,32 +118,20 @@ OV.Model = class extends OV.ModelObject3D EnumerateMeshInstances (onMeshInstance) { - function EnumerateNodeMeshInstances (model, node, transformation, onMeshInstance) - { - let nodeTransformation = node.GetTransformation ().Clone (); - nodeTransformation.Append (transformation); - - for (let childNode of node.GetChildNodes ()) { - EnumerateNodeMeshInstances (model, childNode, nodeTransformation, onMeshInstance); - } - + this.root.Enumerate ((node) => { for (let meshIndex of node.GetMeshIndices ()) { - let mesh = model.GetMesh (meshIndex); - let meshInstance = new OV.MeshInstance (mesh, nodeTransformation); + let mesh = this.GetMesh (meshIndex); + let meshInstance = new OV.MeshInstance (node, mesh); onMeshInstance (meshInstance); } - } - - let transformation = new OV.Transformation (); - EnumerateNodeMeshInstances (this, this.root, transformation, onMeshInstance); + }); } EnumerateTransformedMeshInstances (onMesh) { this.EnumerateMeshInstances ((meshInstance) => { - const mesh = OV.CloneMesh (meshInstance.mesh); - OV.TransformMesh (mesh, meshInstance.transformation); - onMesh (mesh); + const transformed = meshInstance.GetTransformedMesh (); + onMesh (transformed); }); } diff --git a/source/model/node.js b/source/model/node.js index fb00a66..be475ac 100644 --- a/source/model/node.js +++ b/source/model/node.js @@ -68,8 +68,7 @@ OV.Node = class let transformation = this.transformation.Clone (); let parent = this.parent; while (parent !== null) { - const parentTransformation = parent.transformation.Clone (); - transformation = parentTransformation.Append (transformation); + transformation.Append (parent.transformation); parent = parent.parent; } return transformation; diff --git a/test/tests/model_test.js b/test/tests/model_test.js index 7e2da6b..058e703 100644 --- a/test/tests/model_test.js +++ b/test/tests/model_test.js @@ -309,7 +309,7 @@ function CreateTranslatedRotatedCubesModel () rotatedNode.SetName ('Rotated'); let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 0.0, 1.0), Math.PI / 2.0); - rotatedNode.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w))); + rotatedNode.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateRotation (0.0, 0.0, 0.7071067811865475, 0.7071067811865476))); let translatedRotatedNode = new OV.Node (); translatedRotatedNode.SetName ('Translated and Rotated'); @@ -484,14 +484,14 @@ describe ('Node Hierarchy', function () { let boundingBox2 = OV.GetBoundingBox (meshInstances[1]); let boundingBox3 = OV.GetBoundingBox (meshInstances[2]); - assert (OV.CoordIsEqual3D (boundingBox1.min, new OV.Coord3D (2.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox1.max, new OV.Coord3D (3.0, 1.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox1.min, new OV.Coord3D (0.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox1.max, new OV.Coord3D (1.0, 1.0, 1.0))); - assert (OV.CoordIsEqual3D (boundingBox2.min, new OV.Coord3D (-1.0, 2.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox2.max, new OV.Coord3D (0.0, 3.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox2.min, new OV.Coord3D (2.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox2.max, new OV.Coord3D (3.0, 1.0, 1.0))); - assert (OV.CoordIsEqual3D (boundingBox3.min, new OV.Coord3D (0.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox3.max, new OV.Coord3D (1.0, 1.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox3.min, new OV.Coord3D (-1.0, 2.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox3.max, new OV.Coord3D (0.0, 3.0, 1.0))); }); it ('Enumerate transformed mesh instances', function () { @@ -531,13 +531,13 @@ describe ('Node Hierarchy', function () { let boundingBox2 = OV.GetBoundingBox (meshes[1]); let boundingBox3 = OV.GetBoundingBox (meshes[2]); - assert (OV.CoordIsEqual3D (boundingBox1.min, new OV.Coord3D (2.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox1.max, new OV.Coord3D (3.0, 1.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox1.min, new OV.Coord3D (0.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox1.max, new OV.Coord3D (1.0, 1.0, 1.0))); - assert (OV.CoordIsEqual3D (boundingBox2.min, new OV.Coord3D (-1.0, 2.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox2.max, new OV.Coord3D (0.0, 3.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox2.min, new OV.Coord3D (2.0, 0.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox2.max, new OV.Coord3D (3.0, 1.0, 1.0))); - assert (OV.CoordIsEqual3D (boundingBox3.min, new OV.Coord3D (0.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox3.max, new OV.Coord3D (1.0, 1.0, 1.0))); + assert (OV.CoordIsEqual3D (boundingBox3.min, new OV.Coord3D (-1.0, 2.0, 0.0))); + assert (OV.CoordIsEqual3D (boundingBox3.max, new OV.Coord3D (0.0, 3.0, 1.0))); }); }); diff --git a/test/tests/modelutils_test.js b/test/tests/modelutils_test.js index 9e02b74..7330783 100644 --- a/test/tests/modelutils_test.js +++ b/test/tests/modelutils_test.js @@ -4,7 +4,7 @@ var testUtils = require ('../utils/testutils.js'); describe ('Model Utils', function () { it ('Create Merged Model', function () { var model = new OV.Model (); - + var mesh1 = new OV.Mesh (); mesh1.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); mesh1.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); @@ -72,11 +72,11 @@ describe ('Model Utils', function () { normals : [0, 0, 1, 0, 0, 1, 0, 0, 1], uvs : [], mat : 0 - } + } ] } ] - }); + }); }); it ('Mesh Bounding Box', function () { @@ -89,15 +89,17 @@ describe ('Model Utils', function () { it ('Mesh Instance Bounding Box', function () { let cube = OV.GenerateCuboid (null, 1.0, 1.0, 1.0); let transformation = new OV.Transformation (new OV.Matrix ().CreateTranslation (2.0, 0.0, 0.0)); - let cubeInstance = new OV.MeshInstance (cube, transformation); + let node = new OV.Node (); + node.SetTransformation (transformation); + let cubeInstance = new OV.MeshInstance (node, cube); let cubeInstanceBounds = OV.GetBoundingBox (cubeInstance); assert (OV.CoordIsEqual3D (cubeInstanceBounds.min, new OV.Coord3D (2.0, 0.0, 0.0))); assert (OV.CoordIsEqual3D (cubeInstanceBounds.max, new OV.Coord3D (3.0, 1.0, 1.0))); - }); + }); it ('Model Bounding Box', function () { var model = new OV.Model (); - + var mesh1 = new OV.Mesh (); mesh1.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); mesh1.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); @@ -138,7 +140,7 @@ describe ('Model Utils', function () { for (let i = 0; i < topology.edges.length; i++) { assert.strictEqual (topology.edges[i].triangles.length, 2); } - }); + }); it ('Cube Topology Calculation', function () { let cube = testUtils.GetModelWithOneMesh (OV.GenerateCuboid (null, 1.0, 1.0, 1.0)); @@ -181,7 +183,7 @@ describe ('Model Utils', function () { let topology = OV.GetTopology (model); assert.strictEqual (topology.vertices.length, 15); assert (OV.IsSolid (model)); - }); + }); it ('Two Cubes Connecting in One Edge Topology Calculation', function () { const model = testUtils.GetTwoCubesConnectingInOneEdgeModel (); @@ -195,5 +197,5 @@ describe ('Model Utils', function () { let topology = OV.GetTopology (model); assert.strictEqual (topology.vertices.length, 12); assert (OV.IsSolid (model)); - }); + }); }); diff --git a/test/tests/node_test.js b/test/tests/node_test.js index 6ec2383..3f5c60b 100644 --- a/test/tests/node_test.js +++ b/test/tests/node_test.js @@ -118,7 +118,7 @@ describe ('Node', function() { let tr2 = new OV.Transformation (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w)); let tr3 = new OV.Transformation (new OV.Matrix ().CreateTranslation (0.0, 0.0, 2.0)); - let refTr = new OV.Transformation ().Append (tr1).Append (tr2).Append (tr3); + let refTr = new OV.Transformation ().Append (tr3).Append (tr2).Append (tr1); let node1 = new OV.Node (); node1.SetTransformation (tr1); diff --git a/test/tests/quantities_test.js b/test/tests/quantities_test.js index 417d939..e5eeb8a 100644 --- a/test/tests/quantities_test.js +++ b/test/tests/quantities_test.js @@ -18,7 +18,7 @@ describe ('Quantities', function () { it ('Two Cubes Connecting in One Vertex Volume Calculation', function () { const model = testUtils.GetTwoCubesConnectingInOneVertexModel (); assert (OV.IsEqual (OV.CalculateVolume (model), 2.0)); - }); + }); it ('Two Cubes Connecting in One Edge Volume Calculation', function () { const model = testUtils.GetTwoCubesConnectingInOneEdgeModel (); @@ -28,7 +28,7 @@ describe ('Quantities', function () { it ('Two Cubes Connecting in One Face Volume Calculation', function () { const model = testUtils.GetTwoCubesConnectingInOneFaceModel (); assert (OV.IsEqual (OV.CalculateVolume (model), 2.0)); - }); + }); it ('Cube with Wrongly Oriented Triangle Volume Calculation', function () { var mesh = new OV.Mesh (); @@ -90,8 +90,10 @@ describe ('Quantities', function () { it ('Cube Scaled Volume and Area Calculation', function () { const mesh = OV.GenerateCuboid (null, 1.0, 1.0, 1.0); const transformation = new OV.Transformation (new OV.Matrix ().CreateScale (2.0, 2.0, 2.0)); - const meshInstance = new OV.MeshInstance (mesh, transformation); + let node = new OV.Node (); + node.SetTransformation (transformation); + const meshInstance = new OV.MeshInstance (node, mesh); assert (OV.IsEqual (OV.CalculateVolume (meshInstance), 8.0)); assert (OV.IsEqual (OV.CalculateSurfaceArea (meshInstance), 24.0)); - }); + }); }); diff --git a/tools/config.json b/tools/config.json index a2a0fb9..f172988 100644 --- a/tools/config.json +++ b/tools/config.json @@ -28,16 +28,15 @@ "source/model/property.js", "source/model/object.js", "source/model/mesh.js", - "source/model/meshinstance.js", "source/model/meshbuffer.js", "source/model/node.js", + "source/model/meshinstance.js", "source/model/model.js", "source/model/topology.js", "source/model/modelutils.js", "source/model/modelfinalization.js", "source/model/quantities.js", "source/model/generator.js", - "source/model/meshinstanceid.js", "source/import/importerutils.js", "source/import/importerbase.js", "source/import/importerobj.js", diff --git a/website/embed.html b/website/embed.html index b1b1c5d..fa6ffc3 100644 --- a/website/embed.html +++ b/website/embed.html @@ -41,16 +41,15 @@ - + - diff --git a/website/index.html b/website/index.html index 5ae4517..c2081ee 100644 --- a/website/index.html +++ b/website/index.html @@ -41,16 +41,15 @@ - + -