Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: No longer double vertices in GeometryHelper.addBackfaceTriangles #455

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions webgl-geometries/GeometryHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,39 +528,18 @@ GeometryHelper.trianglesToLines = function triangleToLines(indices, out) {
};

/**
* Adds a reverse order triangle for every triangle in the mesh. Adds extra vertices
* and indices to input arrays.
* Adds a reverse order triangle for every triangle in the mesh. Adds extra
* indices to input array.
*
* @static
* @method
*
* @param {Array} vertices X, Y, Z positions of all vertices in the geometry
* @param {Array} indices Indices of all faces on the geometry
* @return {undefined} undefined
*/
GeometryHelper.addBackfaceTriangles = function addBackfaceTriangles(vertices, indices) {
var nFaces = indices.length / 3;

var maxIndex = 0;
var i = indices.length;
while (i--) if (indices[i] > maxIndex) maxIndex = indices[i];

maxIndex++;

for (i = 0; i < nFaces; i++) {
var indexOne = indices[i * 3],
indexTwo = indices[i * 3 + 1],
indexThree = indices[i * 3 + 2];

indices.push(indexOne + maxIndex, indexThree + maxIndex, indexTwo + maxIndex);
}

// Iterating instead of .slice() here to avoid max call stack issue.

var nVerts = vertices.length;
for (i = 0; i < nVerts; i++) {
vertices.push(vertices[i]);
}
GeometryHelper.addBackfaceTriangles = function addBackfaceTriangles(indices) {
for (var face = 0, len = indices.length; face < len; face += 3)
indices.push(indices[face], indices[face + 2], indices[face + 1]);
};

module.exports = GeometryHelper;
5 changes: 2 additions & 3 deletions webgl-geometries/primitives/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ function Circle (options) {
var detail = options.detail || 30;
var buffers = getCircleBuffers(detail, true);

if (options.backface !== false) {
GeometryHelper.addBackfaceTriangles(buffers.vertices, buffers.indices);
}
if (options.backface !== false)
GeometryHelper.addBackfaceTriangles(buffers.indices);

var textureCoords = getCircleTexCoords(buffers.vertices);
var normals = GeometryHelper.computeNormals(buffers.vertices, buffers.indices);
Expand Down
5 changes: 2 additions & 3 deletions webgl-geometries/primitives/Cylinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ function Cylinder (options) {
Cylinder.generator.bind(null, radius)
);

if (options.backface !== false) {
GeometryHelper.addBackfaceTriangles(buffers.vertices, buffers.indices);
}
if (options.backface !== false)
GeometryHelper.addBackfaceTriangles(buffers.indices);

options.buffers = [
{ name: 'a_pos', data: buffers.vertices },
Expand Down
5 changes: 2 additions & 3 deletions webgl-geometries/primitives/ParametricCone.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ function ParametricCone (options) {
ParametricCone.generator.bind(null, radius)
);

if (options.backface !== false) {
GeometryHelper.addBackfaceTriangles(buffers.vertices, buffers.indices);
}
if (options.backface !== false)
GeometryHelper.addBackfaceTriangles(buffers.indices);

options.buffers = [
{ name: 'a_pos', data: buffers.vertices },
Expand Down
3 changes: 1 addition & 2 deletions webgl-geometries/primitives/Plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ function Plane(options) {
}

if (options.backface !== false) {
GeometryHelper.addBackfaceTriangles(vertices, indices);
GeometryHelper.addBackfaceTriangles(indices);

// duplicate texture coordinates as well

var len = textureCoords.length;
for (i = 0; i < len; i++) textureCoords.push(textureCoords[i]);
}
Expand Down
5 changes: 2 additions & 3 deletions webgl-geometries/primitives/Triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ function Triangle (options) {

while(--detail) GeometryHelper.subdivide(indices, vertices, textureCoords);

if (options.backface !== false) {
GeometryHelper.addBackfaceTriangles(vertices, indices);
}
if (options.backface !== false)
GeometryHelper.addBackfaceTriangles(indices);

normals = GeometryHelper.computeNormals(vertices, indices);

Expand Down