Skip to content

Commit

Permalink
Fix normal calculation for CylinderGeometry
Browse files Browse the repository at this point in the history
The calculated normals of the curved side of the cylinder are wrong when the bottom and top radius are not equal, as the code assumes that the normal is always at right angles to the cylinder axis. This commit handles the more general case where the radii at the ends of the cylinder are not equal.
  • Loading branch information
felixpalmer authored Jul 31, 2018
1 parent 62b2312 commit c3eaacb
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Source/Core/CylinderGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,17 @@ define([
var tangentIndex = 0;
var bitangentIndex = 0;

var theta = Math.atan2(bottomRadius - topRadius, length);
var normal = normalScratch;
normal.z = 0;
normal.z = Math.sin(theta);
var normalScale = Math.cos(theta);
var tangent = tangentScratch;
var bitangent = bitangentScratch;

for (i = 0; i < slices; i++) {
var angle = i / slices * CesiumMath.TWO_PI;
var x = Math.cos(angle);
var y = Math.sin(angle);
var x = normalScale * Math.cos(angle);
var y = normalScale * Math.sin(angle);
if (computeNormal) {
normal.x = x;
normal.y = y;
Expand All @@ -241,12 +243,12 @@ define([
}

if (vertexFormat.normal) {
normals[normalIndex++] = x;
normals[normalIndex++] = y;
normals[normalIndex++] = 0;
normals[normalIndex++] = x;
normals[normalIndex++] = y;
normals[normalIndex++] = 0;
normals[normalIndex++] = normal.x;
normals[normalIndex++] = normal.y;
normals[normalIndex++] = normal.z;
normals[normalIndex++] = normal.x;
normals[normalIndex++] = normal.y;
normals[normalIndex++] = normal.z;
}

if (vertexFormat.tangent) {
Expand Down

0 comments on commit c3eaacb

Please sign in to comment.