@@ -11,6 +11,27 @@ import {
11
11
Vector3 ,
12
12
} from 'three' ;
13
13
14
+ /** @module BufferGeometryUtils */
15
+
16
+ /**
17
+ * Computes vertex tangents using the MikkTSpace algorithm. MikkTSpace generates the same tangents consistently,
18
+ * and is used in most modelling tools and normal map bakers. Use MikkTSpace for materials with normal maps,
19
+ * because inconsistent tangents may lead to subtle visual issues in the normal map, particularly around mirrored
20
+ * UV seams.
21
+ *
22
+ * In comparison to this method, {@link BufferGeometry#computeTangents} (a custom algorithm) generates tangents that
23
+ * probably will not match the tangents in other software. The custom algorithm is sufficient for general use with a
24
+ * custom material, and may be faster than MikkTSpace.
25
+ *
26
+ * Returns the original BufferGeometry. Indexed geometries will be de-indexed. Requires position, normal, and uv attributes.
27
+ *
28
+ * @param {BufferGeometry } geometry - The geometry to compute tangents for.
29
+ * @param {Object } MikkTSpace - Instance of `examples/jsm/libs/mikktspace.module.js`, or `mikktspace` npm package.
30
+ * Aawait `MikkTSpace.ready` before use.
31
+ * @param {boolean } [negateSign=true] - Whether to negate the sign component (.w) of each tangent.
32
+ * Required for normal map conventions in some formats, including glTF.
33
+ * @return {BufferGeometry } The updated geometry.
34
+ */
14
35
function computeMikkTSpaceTangents ( geometry , MikkTSpace , negateSign = true ) {
15
36
16
37
if ( ! MikkTSpace || ! MikkTSpace . isReady ) {
@@ -100,9 +121,11 @@ function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) {
100
121
}
101
122
102
123
/**
103
- * @param {Array<BufferGeometry> } geometries
104
- * @param {boolean } useGroups
105
- * @return {?BufferGeometry }
124
+ * Merges a set of geometries into a single instance. All geometries must have compatible attributes.
125
+ *
126
+ * @param {Array<BufferGeometry> } geometries - The geometries to merge.
127
+ * @param {boolean } [useGroups=false] - Whether to use groups or not.
128
+ * @return {?BufferGeometry } The merged geometry. Returns `null` if the merge does not succeed.
106
129
*/
107
130
function mergeGeometries ( geometries , useGroups = false ) {
108
131
@@ -296,8 +319,11 @@ function mergeGeometries( geometries, useGroups = false ) {
296
319
}
297
320
298
321
/**
299
- * @param {Array<BufferAttribute> } attributes
300
- * @return {?BufferAttribute }
322
+ * Merges a set of attributes into a single instance. All attributes must have compatible properties and types.
323
+ * Instances of {@link InterleavedBufferAttribute} are not supported.
324
+ *
325
+ * @param {Array<BufferAttribute> } attributes - The attributes to merge.
326
+ * @return {?BufferAttribute } The merged attribute. Returns `null` if the merge does not succeed.
301
327
*/
302
328
function mergeAttributes ( attributes ) {
303
329
@@ -389,10 +415,12 @@ function mergeAttributes( attributes ) {
389
415
}
390
416
391
417
/**
392
- * @param {BufferAttribute } attribute
393
- * @return {BufferAttribute }
418
+ * Performs a deep clone of the given buffer attribute.
419
+ *
420
+ * @param {BufferAttribute } attribute - The attribute to clone.
421
+ * @return {BufferAttribute } The cloned attribute.
394
422
*/
395
- export function deepCloneAttribute ( attribute ) {
423
+ function deepCloneAttribute ( attribute ) {
396
424
397
425
if ( attribute . isInstancedInterleavedBufferAttribute || attribute . isInterleavedBufferAttribute ) {
398
426
@@ -411,8 +439,11 @@ export function deepCloneAttribute( attribute ) {
411
439
}
412
440
413
441
/**
414
- * @param {Array<BufferAttribute> } attributes
415
- * @return {Array<InterleavedBufferAttribute> }
442
+ * Interleaves a set of attributes and returns a new array of corresponding attributes that share a
443
+ * single {@link InterleavedBuffer} instance. All attributes must have compatible types.
444
+ *
445
+ * @param {Array<BufferAttribute> } attributes - The attributes to interleave.
446
+ * @return {Array<InterleavedBufferAttribute> } An array of interleaved attributes. If interleave does not succeed, the method returns `null`.
416
447
*/
417
448
function interleaveAttributes ( attributes ) {
418
449
@@ -475,8 +506,13 @@ function interleaveAttributes( attributes ) {
475
506
476
507
}
477
508
478
- // returns a new, non-interleaved version of the provided attribute
479
- export function deinterleaveAttribute ( attribute ) {
509
+ /**
510
+ * Returns a new, non-interleaved version of the given attribute.
511
+ *
512
+ * @param {InterleavedBufferAttribute } attribute - The interleaved attribute.
513
+ * @return {BufferAttribute } The non-interleaved attribute.
514
+ */
515
+ function deinterleaveAttribute ( attribute ) {
480
516
481
517
const cons = attribute . data . array . constructor ;
482
518
const count = attribute . count ;
@@ -523,8 +559,12 @@ export function deinterleaveAttribute( attribute ) {
523
559
524
560
}
525
561
526
- // deinterleaves all attributes on the geometry
527
- export function deinterleaveGeometry ( geometry ) {
562
+ /**
563
+ * Deinterleaves all attributes on the given geometry.
564
+ *
565
+ * @param {BufferGeometry } geometry - The geometry to deinterleave.
566
+ */
567
+ function deinterleaveGeometry ( geometry ) {
528
568
529
569
const attributes = geometry . attributes ;
530
570
const morphTargets = geometry . morphTargets ;
@@ -567,8 +607,10 @@ export function deinterleaveGeometry( geometry ) {
567
607
}
568
608
569
609
/**
570
- * @param {BufferGeometry } geometry
571
- * @return {number }
610
+ * Returns the amount of bytes used by all attributes to represent the geometry.
611
+ *
612
+ * @param {BufferGeometry } geometry - The geometry.
613
+ * @return {number } The estimate bytes used.
572
614
*/
573
615
function estimateBytesUsed ( geometry ) {
574
616
@@ -590,9 +632,11 @@ function estimateBytesUsed( geometry ) {
590
632
}
591
633
592
634
/**
593
- * @param {BufferGeometry } geometry
594
- * @param {number } tolerance
595
- * @return {BufferGeometry }
635
+ * Returns a new geometry with vertices for which all similar vertex attributes (within tolerance) are merged.
636
+ *
637
+ * @param {BufferGeometry } geometry - The geometry to merge vertices for.
638
+ * @param {number } [tolerance=1e-4] - The tolerance value.
639
+ * @return {BufferGeometry } - The new geometry with merged vertices.
596
640
*/
597
641
function mergeVertices ( geometry , tolerance = 1e-4 ) {
598
642
@@ -753,9 +797,12 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
753
797
}
754
798
755
799
/**
756
- * @param {BufferGeometry } geometry
757
- * @param {number } drawMode
758
- * @return {BufferGeometry }
800
+ * Returns a new indexed geometry based on `TrianglesDrawMode` draw mode.
801
+ * This mode corresponds to the `gl.TRIANGLES` primitive in WebGL.
802
+ *
803
+ * @param {BufferGeometry } geometry - The geometry to convert.
804
+ * @param {number } drawMode - The current draw mode.
805
+ * @return {BufferGeometry } The new geometry using `TrianglesDrawMode`.
759
806
*/
760
807
function toTrianglesDrawMode ( geometry , drawMode ) {
761
808
@@ -864,9 +911,13 @@ function toTrianglesDrawMode( geometry, drawMode ) {
864
911
865
912
/**
866
913
* Calculates the morphed attributes of a morphed/skinned BufferGeometry.
867
- * Helpful for Raytracing or Decals.
868
- * @param {Mesh | Line | Points } object An instance of Mesh, Line or Points.
869
- * @return {Object } An Object with original position/normal attributes and morphed ones.
914
+ *
915
+ * Helpful for Raytracing or Decals (i.e. a `DecalGeometry` applied to a morphed Object with a `BufferGeometry`
916
+ * will use the original `BufferGeometry`, not the morphed/skinned one, generating an incorrect result.
917
+ * Using this function to create a shadow `Object3`D the `DecalGeometry` can be correctly generated).
918
+ *
919
+ * @param {Mesh|Line|Points } object - The 3D object tocompute morph attributes for.
920
+ * @return {Object } An object with original position/normal attributes and morphed ones.
870
921
*/
871
922
function computeMorphedAttributes ( object ) {
872
923
@@ -1142,6 +1193,12 @@ function computeMorphedAttributes( object ) {
1142
1193
1143
1194
}
1144
1195
1196
+ /**
1197
+ * Merges the {@link BufferGeometry#groups} for the given geometry.
1198
+ *
1199
+ * @param {BufferGeometry } geometry - The geometry to modify.
1200
+ * @return {BufferGeometry } - The updated geometry
1201
+ */
1145
1202
function mergeGroups ( geometry ) {
1146
1203
1147
1204
if ( geometry . groups . length === 0 ) {
@@ -1244,15 +1301,14 @@ function mergeGroups( geometry ) {
1244
1301
1245
1302
}
1246
1303
1247
-
1248
1304
/**
1249
1305
* Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
1250
1306
* non-indexed geometry. Returns the geometry with smooth normals everywhere except
1251
1307
* faces that meet at an angle greater than the crease angle.
1252
1308
*
1253
- * @param {BufferGeometry } geometry
1254
- * @param {number } [creaseAngle]
1255
- * @return {BufferGeometry }
1309
+ * @param {BufferGeometry } geometry - The geometry to modify.
1310
+ * @param {number } [creaseAngle=Math.PI/3] - The crease angle in radians.
1311
+ * @return {BufferGeometry } - The updated geometry
1256
1312
*/
1257
1313
function toCreasedNormals ( geometry , creaseAngle = Math . PI / 3 /* 60 degrees */ ) {
1258
1314
@@ -1363,6 +1419,9 @@ export {
1363
1419
computeMikkTSpaceTangents ,
1364
1420
mergeGeometries ,
1365
1421
mergeAttributes ,
1422
+ deepCloneAttribute ,
1423
+ deinterleaveAttribute ,
1424
+ deinterleaveGeometry ,
1366
1425
interleaveAttributes ,
1367
1426
estimateBytesUsed ,
1368
1427
mergeVertices ,
0 commit comments