diff --git a/source/import/importero3dv.js b/source/import/importero3dv.js index a056580..007d510 100644 --- a/source/import/importero3dv.js +++ b/source/import/importero3dv.js @@ -73,22 +73,6 @@ OV.ImporterO3dv = class extends OV.ImporterBase genParams.SetMaterial (meshContent.material); } - if (meshContent.transformation !== undefined) { - let translation = new OV.Coord3D (0.0, 0.0, 0.0); - let rotation = new OV.Quaternion (0.0, 0.0, 0.0, 1.0); - let scale = new OV.Coord3D (1.0, 1.0, 1.0); - if (meshContent.transformation.translation !== undefined) { - translation = OV.ArrayToCoord3D (meshContent.transformation.translation); - } - if (meshContent.transformation.rotation !== undefined) { - rotation = OV.ArrayToQuaternion (meshContent.transformation.rotation); - } - if (meshContent.transformation.scale !== undefined) { - scale = OV.ArrayToCoord3D (meshContent.transformation.scale); - } - genParams.SetTransformation (translation, rotation, scale); - } - let parameters = meshContent.parameters; if (parameters === undefined) { return; @@ -121,10 +105,30 @@ OV.ImporterO3dv = class extends OV.ImporterBase let radius = OV.ValueOrDefault (parameters.radius, 1.0); mesh = OV.GeneratePlatonicSolid (genParams, parameters.solid_type, radius); } - if (mesh !== null) { - this.ImportProperties (mesh, meshContent); - this.model.AddMesh (mesh); + + if (mesh === null) { + return; } + + if (meshContent.transformation !== undefined) { + let translation = new OV.Coord3D (0.0, 0.0, 0.0); + let rotation = new OV.Quaternion (0.0, 0.0, 0.0, 1.0); + let scale = new OV.Coord3D (1.0, 1.0, 1.0); + if (meshContent.transformation.translation !== undefined) { + translation = OV.ArrayToCoord3D (meshContent.transformation.translation); + } + if (meshContent.transformation.rotation !== undefined) { + rotation = OV.ArrayToQuaternion (meshContent.transformation.rotation); + } + if (meshContent.transformation.scale !== undefined) { + scale = OV.ArrayToCoord3D (meshContent.transformation.scale); + } + let matrix = new OV.Matrix ().ComposeTRS (translation, rotation, scale); + mesh.SetTransformation (new OV.Transformation (matrix)); + } + + this.ImportProperties (mesh, meshContent); + this.model.AddMesh (mesh); } ImportNode (nodeContent, meshNode) diff --git a/source/model/mesh.js b/source/model/mesh.js index 0d4ce3c..d670157 100644 --- a/source/model/mesh.js +++ b/source/model/mesh.js @@ -7,6 +7,7 @@ OV.Mesh = class extends OV.ModelObject3D this.normals = []; this.uvs = []; this.triangles = []; + this.transformation = new OV.Transformation (); } VertexCount () @@ -88,10 +89,27 @@ OV.Mesh = class extends OV.ModelObject3D return this.triangles[index]; } + SetTransformation (transformation) + { + this.transformation = transformation; + } + + GetTransformation () + { + return this.transformation; + } + EnumerateVertices (onVertex) { - for (const vertex of this.vertices) { - onVertex (vertex); + if (this.transformation.IsIdentity ()) { + for (const vertex of this.vertices) { + onVertex (vertex); + } + } else { + for (const vertex of this.vertices) { + const transformed = this.transformation.TransformCoord3D (vertex); + onVertex (transformed); + } } } @@ -104,11 +122,20 @@ OV.Mesh = class extends OV.ModelObject3D EnumerateTriangleVertices (onTriangleVertices) { - for (const triangle of this.triangles) { - let v0 = this.vertices[triangle.v0]; - let v1 = this.vertices[triangle.v1]; - let v2 = this.vertices[triangle.v2]; - onTriangleVertices (v0, v1, v2); + if (this.transformation.IsIdentity ()) { + for (const triangle of this.triangles) { + let v0 = this.vertices[triangle.v0]; + let v1 = this.vertices[triangle.v1]; + let v2 = this.vertices[triangle.v2]; + onTriangleVertices (v0, v1, v2); + } + } else { + for (const triangle of this.triangles) { + const v0Transformed = this.transformation.TransformCoord3D (this.vertices[triangle.v0]); + const v1Transformed = this.transformation.TransformCoord3D (this.vertices[triangle.v1]); + const v2Transformed = this.transformation.TransformCoord3D (this.vertices[triangle.v2]); + onTriangleVertices (v0Transformed, v1Transformed, v2Transformed); + } } } }; diff --git a/source/threejs/threeconverter.js b/source/threejs/threeconverter.js index f461ec3..e1f0f1d 100644 --- a/source/threejs/threeconverter.js +++ b/source/threejs/threeconverter.js @@ -272,6 +272,10 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks) } let threeMesh = new THREE.Mesh (threeGeometry, meshThreeMaterials); + let matrix = mesh.GetTransformation ().GetMatrix (); + let threeMatrix = new THREE.Matrix4 ().fromArray (matrix.Get ()); + threeMesh.applyMatrix4 (threeMatrix); + threeMesh.userData = { originalMeshId : meshInstanceId, originalMaterials : meshOriginalMaterials, diff --git a/test/tests/importero3dv_test.js b/test/tests/importero3dv_test.js index e0eb213..1393db7 100644 --- a/test/tests/importero3dv_test.js +++ b/test/tests/importero3dv_test.js @@ -78,6 +78,10 @@ describe ('O3dv Importer', function () { assert.strictEqual (mesh.GetPropertyGroup (0).PropertyCount (), 5); } + let boundingBox = OV.GetBoundingBox (model); + assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-0.5773502691896258, -1.0, -1.0))); + assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (6.85065080835204, 1.0, 3.9341723589627158))); + done (); }); }); diff --git a/test/tests/mesh_test.js b/test/tests/mesh_test.js index aad621d..8b2b6b9 100644 --- a/test/tests/mesh_test.js +++ b/test/tests/mesh_test.js @@ -95,4 +95,35 @@ describe ('Mesh', function() { assert (OV.CoordIsEqual3D (mesh.GetVertex (2), new OV.Coord3D (0.0, 1.0, 3.0))); assert (OV.CoordIsEqual3D (mesh.GetNormal (0), new OV.Coord3D (-1.0, 0.0, 0.0))); }); + + it ('Mesh Transformation', function () { + var mesh = new OV.Mesh (); + mesh.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + mesh.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + mesh.AddVertex (new OV.Coord3D (1.0, 1.0, 0.0)); + mesh.AddNormal (new OV.Coord3D (0.0, 0.0, 1.0)); + mesh.AddTextureUV (new OV.Coord2D (0.0, 0.0)); + mesh.AddTextureUV (new OV.Coord2D (1.0, 0.0)); + mesh.AddTextureUV (new OV.Coord2D (1.0, 1.0)); + var triangle = new OV.Triangle (0, 1, 2); + triangle.SetNormals (0, 0, 0); + triangle.SetTextureUVs (0, 1, 2); + mesh.AddTriangle (triangle); + + let rotation = OV.QuaternionFromAxisAngle (new OV.Coord3D (0.0, 1.0, 0.0), -Math.PI / 2.0); + let transformation = new OV.Transformation (); + transformation.AppendMatrix (new OV.Matrix ().CreateScale (2.0, 1.0, 1.0)); + transformation.AppendMatrix (new OV.Matrix ().CreateRotation (rotation.x, rotation.y, rotation.z, rotation.w)); + transformation.AppendMatrix (new OV.Matrix ().CreateTranslation (0.0, 0.0, 1.0)); + mesh.SetTransformation (transformation); + + let transformedVertices = []; + mesh.EnumerateVertices ((vertex) => { + transformedVertices.push (vertex); + }) + + assert (OV.CoordIsEqual3D (transformedVertices[0], new OV.Coord3D (0.0, 0.0, 1.0))); + assert (OV.CoordIsEqual3D (transformedVertices[1], new OV.Coord3D (0.0, 0.0, 3.0))); + assert (OV.CoordIsEqual3D (transformedVertices[2], new OV.Coord3D (0.0, 1.0, 3.0))); + }); });