geometry_helper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2017 The Draco Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. /**
  16. * @fileoverview Helper class implementing various utilities for THREE.js
  17. * geometry.
  18. */
  19. function GeometryHelper() {}
  20. GeometryHelper.prototype = {
  21. constructor: GeometryHelper,
  22. // Computes vertex normals on THREE.js buffer geometry, even when the mesh
  23. // uses triangle strip connectivity.
  24. computeVertexNormals: function (bufferGeometry) {
  25. if (bufferGeometry.drawMode === THREE.TrianglesDrawMode) {
  26. bufferGeometry.computeVertexNormals();
  27. return;
  28. } else if (bufferGeometry.drawMode === THREE.TriangleStripDrawMode) {
  29. if (bufferGeometry.attributes.position === undefined) {
  30. return;
  31. }
  32. const inPositions = bufferGeometry.attributes.position.array;
  33. if (bufferGeometry.attributes.normal === undefined) {
  34. bufferGeometry.addAttribute(
  35. 'normal',
  36. new THREE.BufferAttribute(new Float32Array(inPositions.length),
  37. 3));
  38. } else {
  39. // Reset existing normals to zero.
  40. const array = bufferGeometry.attributes.normal.array;
  41. for (let i = 0; i < array.length; ++i) {
  42. array[ i ] = 0;
  43. }
  44. }
  45. let outNormals = bufferGeometry.attributes.normal.array;
  46. let pos0 = new THREE.Vector3();
  47. let pos1 = new THREE.Vector3();
  48. let pos2 = new THREE.Vector3();
  49. let posDif0 = new THREE.Vector3(), posDif1 = new THREE.Vector3();
  50. let localNormal = new THREE.Vector3();
  51. const stripIndices = bufferGeometry.index.array;
  52. for (let i = 2; i < stripIndices.length; ++i) {
  53. let index0 = stripIndices[i - 2] * 3;
  54. let index1 = stripIndices[i - 1] * 3;
  55. let index2 = stripIndices[i] * 3;
  56. // Skip degenerate triangles.
  57. if (index0 === index1 || index0 === index2 || index1 === index2) {
  58. continue;
  59. }
  60. if ((i & 1) !== 0) {
  61. // Swap index 1 and 0 on odd indexed triangles.
  62. const tmpIndex = index1;
  63. index1 = index2;
  64. index2 = tmpIndex;
  65. }
  66. // Get position values.
  67. pos0.fromArray(inPositions, index0);
  68. pos1.fromArray(inPositions, index1);
  69. pos2.fromArray(inPositions, index2);
  70. // Position differences
  71. posDif0.subVectors(pos2, pos0);
  72. posDif1.subVectors(pos1, pos0);
  73. // Weighted normal.
  74. localNormal.crossVectors(posDif1, posDif0);
  75. // Update normals on vertices
  76. outNormals[index0] += localNormal.x;
  77. outNormals[index0 + 1] += localNormal.y;
  78. outNormals[index0 + 2] += localNormal.z;
  79. outNormals[index1] += localNormal.x;
  80. outNormals[index1 + 1] += localNormal.y;
  81. outNormals[index1 + 2] += localNormal.z;
  82. outNormals[index2] += localNormal.x;
  83. outNormals[index2 + 1] += localNormal.y;
  84. outNormals[index2 + 2] += localNormal.z;
  85. }
  86. bufferGeometry.normalizeNormals();
  87. bufferGeometry.attributes.normal.needsUpdate = true;
  88. }
  89. },
  90. };