From e889d20d368eca624a2497a051ce8f85460d0c2e Mon Sep 17 00:00:00 2001 From: hpinkos Date: Tue, 10 Jul 2018 17:15:15 -0400 Subject: [PATCH 1/2] fix tangents when stRotation is applied --- CHANGES.md | 1 + Source/Core/EllipseGeometry.js | 19 ++++++++++++++++--- Source/Core/PolygonGeometry.js | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7414ce2bb570..9c1119620569 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ Change Log #### Fixes :wrench: * Fixed bug causing billboards and labels to appear the wrong size when switching scene modes [#6745](https://github.com/AnalyticalGraphicsInc/cesium/issues/6745) * Fixed a bug that was preventing 3D Tilesets on the opposite side of the globe from being occluded [#6714](https://github.com/AnalyticalGraphicsInc/cesium/issues/6714) +* Fixed `PolygonGeometry` and `EllipseGeometry` tangent and bitangent attributes when a texture rotation is used [#6788](https://github.com/AnalyticalGraphicsInc/cesium/pull/6788) ### 1.47 - 2018-07-02 diff --git a/Source/Core/EllipseGeometry.js b/Source/Core/EllipseGeometry.js index 9c855da8566d..fe210104f2aa 100644 --- a/Source/Core/EllipseGeometry.js +++ b/Source/Core/EllipseGeometry.js @@ -62,6 +62,7 @@ define([ var scratchCartesian4 = new Cartesian3(); var texCoordScratch = new Cartesian2(); var textureMatrixScratch = new Matrix3(); + var tangentMatrixScratch = new Matrix3(); var quaternionScratch = new Quaternion(); var scratchNormal = new Cartesian3(); @@ -104,8 +105,20 @@ define([ var geodeticNormal = ellipsoid.scaleToGeodeticSurface(center, scratchCartesian1); ellipsoid.geodeticSurfaceNormal(geodeticNormal, geodeticNormal); - var rotation = Quaternion.fromAxisAngle(geodeticNormal, stRotation, quaternionScratch); - var textureMatrix = Matrix3.fromQuaternion(rotation, textureMatrixScratch); + + var textureMatrix = textureMatrixScratch; + var tangentMatrix = tangentMatrixScratch; + if (stRotation !== 0) { + var rotation = Quaternion.fromAxisAngle(geodeticNormal, stRotation, quaternionScratch); + textureMatrix = Matrix3.fromQuaternion(rotation, textureMatrix); + + rotation = Quaternion.fromAxisAngle(geodeticNormal, -stRotation, quaternionScratch); + tangentMatrix = Matrix3.fromQuaternion(rotation, tangentMatrix); + } else { + textureMatrix = Matrix3.clone(Matrix3.IDENTITY, textureMatrix); + tangentMatrix = Matrix3.clone(Matrix3.IDENTITY, tangentMatrix); + } + var minTexCoord = Cartesian2.fromElements(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, scratchMinTexCoord); var maxTexCoord = Cartesian2.fromElements(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, scratchMaxTexCoord); @@ -152,7 +165,7 @@ define([ if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) { if (vertexFormat.tangent || vertexFormat.bitangent) { tangent = Cartesian3.normalize(Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent), tangent); - Matrix3.multiplyByVector(textureMatrix, tangent, tangent); + Matrix3.multiplyByVector(tangentMatrix, tangent, tangent); } if (vertexFormat.normal) { normals[i] = normal.x; diff --git a/Source/Core/PolygonGeometry.js b/Source/Core/PolygonGeometry.js index 6d6477cbf709..fcafb60b38ee 100644 --- a/Source/Core/PolygonGeometry.js +++ b/Source/Core/PolygonGeometry.js @@ -126,6 +126,7 @@ define([ var appendTextureCoordinatesCartesian3 = new Cartesian3(); var appendTextureCoordinatesQuaternion = new Quaternion(); var appendTextureCoordinatesMatrix3 = new Matrix3(); + var tangentMatrixScratch = new Matrix3(); function computeAttributes(options) { var vertexFormat = options.vertexFormat; @@ -170,8 +171,18 @@ define([ var bitangent = scratchBitangent; var recomputeNormal = true; - var rotation = Quaternion.fromAxisAngle(tangentPlane._plane.normal, stRotation, appendTextureCoordinatesQuaternion); - var textureMatrix = Matrix3.fromQuaternion(rotation, appendTextureCoordinatesMatrix3); + var textureMatrix = appendTextureCoordinatesMatrix3; + var tangentRotationMatrix = tangentMatrixScratch; + if (stRotation !== 0.0) { + var rotation = Quaternion.fromAxisAngle(tangentPlane._plane.normal, stRotation, appendTextureCoordinatesQuaternion); + textureMatrix = Matrix3.fromQuaternion(rotation, textureMatrix); + + rotation = Quaternion.fromAxisAngle(tangentPlane._plane.normal, -stRotation, appendTextureCoordinatesQuaternion); + tangentRotationMatrix = Matrix3.fromQuaternion(rotation, tangentRotationMatrix); + } else { + textureMatrix = Matrix3.clone(Matrix3.IDENTITY, textureMatrix); + tangentRotationMatrix = Matrix3.clone(Matrix3.IDENTITY, tangentRotationMatrix); + } var bottomOffset = 0; var bottomOffset2 = 0; @@ -242,14 +253,14 @@ define([ if (perPositionHeight) { scratchPerPosNormal = Cartesian3.fromArray(normals, attrIndex, scratchPerPosNormal); scratchPerPosTangent = Cartesian3.cross(Cartesian3.UNIT_Z, scratchPerPosNormal, scratchPerPosTangent); - scratchPerPosTangent = Cartesian3.normalize(Matrix3.multiplyByVector(textureMatrix, scratchPerPosTangent, scratchPerPosTangent), scratchPerPosTangent); + scratchPerPosTangent = Cartesian3.normalize(Matrix3.multiplyByVector(tangentRotationMatrix, scratchPerPosTangent, scratchPerPosTangent), scratchPerPosTangent); if (vertexFormat.bitangent) { scratchPerPosBitangent = Cartesian3.normalize(Cartesian3.cross(scratchPerPosNormal, scratchPerPosTangent, scratchPerPosBitangent), scratchPerPosBitangent); } } tangent = Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent); - tangent = Cartesian3.normalize(Matrix3.multiplyByVector(textureMatrix, tangent, tangent), tangent); + tangent = Cartesian3.normalize(Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent), tangent); if (vertexFormat.bitangent) { bitangent = Cartesian3.normalize(Cartesian3.cross(normal, tangent, bitangent), bitangent); } From 1dc169d8937c5ab2317576c177a122a6e673b48e Mon Sep 17 00:00:00 2001 From: hpinkos Date: Tue, 10 Jul 2018 18:03:28 -0400 Subject: [PATCH 2/2] eslint --- Source/Core/EllipseGeometry.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Core/EllipseGeometry.js b/Source/Core/EllipseGeometry.js index fe210104f2aa..aaa0e3a630c5 100644 --- a/Source/Core/EllipseGeometry.js +++ b/Source/Core/EllipseGeometry.js @@ -119,7 +119,6 @@ define([ tangentMatrix = Matrix3.clone(Matrix3.IDENTITY, tangentMatrix); } - var minTexCoord = Cartesian2.fromElements(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, scratchMinTexCoord); var maxTexCoord = Cartesian2.fromElements(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, scratchMaxTexCoord);