Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 3x 3x 3x 3x 11x 11x 11x 11x 11x 11x 3x 3x 3x 3x 3x 134x 134x 134x 134x 134x 134x 134x 134x 134x 3x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 2x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 2x 1x 16x 16x 6x 10x 3x 7x 3x 4x 1x 16x 6x 10x 6x 6x 4x | import defined from "./defined.js";
import IndexDatatype from "./IndexDatatype.js";
import PrimitiveType from "./PrimitiveType.js";
/**
* Functions for generating indices for model wireframes. The indices are
* outputted as typed arrays, which can then be put into buffers for rendering.
*
* @namespace WireframeIndexGenerator
* @private
*/
const WireframeIndexGenerator = {};
function createWireframeFromTriangles(vertexCount) {
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
vertexCount * 2,
);
const length = vertexCount;
let index = 0;
for (let i = 0; i < length; i += 3) {
wireframeIndices[index++] = i;
wireframeIndices[index++] = i + 1;
wireframeIndices[index++] = i + 1;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = i;
}
return wireframeIndices;
}
function createWireframeFromTriangleIndices(vertexCount, originalIndices) {
const originalIndicesCount = originalIndices.length;
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
originalIndicesCount * 2,
);
let index = 0;
for (let i = 0; i < originalIndicesCount; i += 3) {
const point0 = originalIndices[i];
const point1 = originalIndices[i + 1];
const point2 = originalIndices[i + 2];
wireframeIndices[index++] = point0;
wireframeIndices[index++] = point1;
wireframeIndices[index++] = point1;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = point0;
}
return wireframeIndices;
}
function createWireframeFromTriangleStrip(vertexCount) {
const numberOfTriangles = vertexCount - 2;
const wireframeIndicesCount = 2 + numberOfTriangles * 4;
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
wireframeIndicesCount,
);
let index = 0;
// Handle the first edge
wireframeIndices[index++] = 0;
wireframeIndices[index++] = 1;
// Add two edges for every triangle in the strip
for (let i = 0; i < numberOfTriangles; i++) {
wireframeIndices[index++] = i + 1;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = i;
}
return wireframeIndices;
}
function createWireframeFromTriangleStripIndices(vertexCount, originalIndices) {
const originalIndicesCount = originalIndices.length;
const numberOfTriangles = originalIndicesCount - 2;
const wireframeIndicesCount = 2 + numberOfTriangles * 4;
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
wireframeIndicesCount,
);
let index = 0;
// Handle the first edge
wireframeIndices[index++] = originalIndices[0];
wireframeIndices[index++] = originalIndices[1];
// Add two edges for every triangle in the strip
for (let i = 0; i < numberOfTriangles; i++) {
const point0 = originalIndices[i];
const point1 = originalIndices[i + 1];
const point2 = originalIndices[i + 2];
wireframeIndices[index++] = point1;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = point0;
}
return wireframeIndices;
}
function createWireframeFromTriangleFan(vertexCount) {
const numberOfTriangles = vertexCount - 2;
const wireframeIndicesCount = 2 + numberOfTriangles * 4;
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
wireframeIndicesCount,
);
let index = 0;
// Handle the first edge
wireframeIndices[index++] = 0;
wireframeIndices[index++] = 1;
// Add two edges for every triangle in the fan
for (let i = 0; i < numberOfTriangles; i++) {
wireframeIndices[index++] = i + 1;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = i + 2;
wireframeIndices[index++] = 0;
}
return wireframeIndices;
}
function createWireframeFromTriangleFanIndices(vertexCount, originalIndices) {
const originalIndicesCount = originalIndices.length;
const numberOfTriangles = originalIndicesCount - 2;
const wireframeIndicesCount = 2 + numberOfTriangles * 4;
const wireframeIndices = IndexDatatype.createTypedArray(
vertexCount,
wireframeIndicesCount,
);
let index = 0;
// Handle the first edge
const firstPoint = originalIndices[0];
wireframeIndices[index++] = firstPoint;
wireframeIndices[index++] = originalIndices[1];
// Add two edges for every triangle in the fan
for (let i = 0; i < numberOfTriangles; i++) {
const point1 = originalIndices[i + 1];
const point2 = originalIndices[i + 2];
wireframeIndices[index++] = point1;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = point2;
wireframeIndices[index++] = firstPoint;
}
return wireframeIndices;
}
/**
* Generates a wireframe index buffer for a primitive, either by reindexing the existing indices
* or creating them from scratch if the model had none.
*
* @param {PrimitiveType} primitiveType The primitive type.
* @param {number} vertexCount The number of vertices in the primitive.
* @param {Uint8Array|Uint16Array|Uint32Array} [originalIndices] A typed array containing the original indices of the primitive.
*
* @return {Uint16Array|Uint32Array} A typed array with the wireframe indices, or undefined if the primitive type does not use triangles.
*
* @private
*/
WireframeIndexGenerator.createWireframeIndices = function (
primitiveType,
vertexCount,
originalIndices,
) {
const hasOriginalIndices = defined(originalIndices);
if (primitiveType === PrimitiveType.TRIANGLES) {
return hasOriginalIndices
? createWireframeFromTriangleIndices(vertexCount, originalIndices)
: createWireframeFromTriangles(vertexCount);
}
if (primitiveType === PrimitiveType.TRIANGLE_STRIP) {
return hasOriginalIndices
? createWireframeFromTriangleStripIndices(vertexCount, originalIndices)
: createWireframeFromTriangleStrip(vertexCount);
}
if (primitiveType === PrimitiveType.TRIANGLE_FAN) {
return hasOriginalIndices
? createWireframeFromTriangleFanIndices(vertexCount, originalIndices)
: createWireframeFromTriangleFan(vertexCount);
}
return undefined;
};
/**
* Gets the number of indices in the wireframe index buffer of a primitive type.
*
* @param {PrimitiveType} primitiveType The primitive type.
* @param {number} originalCount The original number of vertices or indices in the primitive.
* @return {number} The number of indices in the primitive's wireframe.
*
* @private
*/
WireframeIndexGenerator.getWireframeIndicesCount = function (
primitiveType,
originalCount,
) {
// For TRIANGLES, the wireframe takes every triangle (i.e. three of the original
// indices) and turns it into lines. Each triangle has three lines, and each line
// requires two indices, so the final count is twice the original.
if (primitiveType === PrimitiveType.TRIANGLES) {
return originalCount * 2;
}
// For TRIANGLE_STRIP and TRIANGLE_FAN, the number of triangles in the primitive
// is equal to the total number of vertices minus two. This is because after the
// first edge is specified by the first two indices, every point afterwards
// contributes two more edges with two of the previous points, forming a new triangle.
// Each of these edges requires two indices, so each triangle in the primitive
// results in four indices in addition to the first two.
if (
primitiveType === PrimitiveType.TRIANGLE_STRIP ||
primitiveType === PrimitiveType.TRIANGLE_FAN
) {
const numberOfTriangles = originalCount - 2;
return 2 + numberOfTriangles * 4;
}
return originalCount;
};
export default WireframeIndexGenerator;
|