Skip to content

Commit

Permalink
Merge pull request #6788 from AnalyticalGraphicsInc/fix-polygon-tangents
Browse files Browse the repository at this point in the history
Fix tangent/bitangent when stRotation is applied
  • Loading branch information
bagnell authored Jul 11, 2018
2 parents b32bc17 + 18aefea commit 2fa8660
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Change Log
* Fixed `PolygonGeometry` when using `VertexFormat.POSITION_ONLY`, `perPositionHeight` and `extrudedHeight` [#6790](expect(https://github.com/AnalyticalGraphicsInc/cesium/pull/6790)
* 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 a bug where 3D Tilesets using the `region` bounding volume don't get transformed when the tileset's `modelMatrix` changes. [6755](https://github.com/AnalyticalGraphicsInc/cesium/pull/6755)
* 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

Expand Down
18 changes: 15 additions & 3 deletions Source/Core/EllipseGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -104,8 +105,19 @@ 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);
Expand Down Expand Up @@ -152,7 +164,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;
Expand Down
19 changes: 15 additions & 4 deletions Source/Core/PolygonGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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;
Expand Down Expand Up @@ -134,8 +135,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;
Expand Down Expand Up @@ -206,14 +217,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);
}
Expand Down

0 comments on commit 2fa8660

Please sign in to comment.