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