Skip to content

Commit 04b1f83

Browse files
Achieve3318Daniel
andauthored
docs: improve JSDoc documentation in utils.js (#32743)
Co-authored-by: Daniel <daniel@contributor.dev>
1 parent 5532147 commit 04b1f83

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

src/utils.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Finds the minimum value in an array.
3+
*
4+
* @private
5+
* @param {Array<number>} array - The array to search for the minimum value.
6+
* @return {number} The minimum value in the array, or Infinity if the array is empty.
7+
*/
18
function arrayMin( array ) {
29

310
if ( array.length === 0 ) return Infinity;
@@ -14,6 +21,13 @@ function arrayMin( array ) {
1421

1522
}
1623

24+
/**
25+
* Finds the maximum value in an array.
26+
*
27+
* @private
28+
* @param {Array<number>} array - The array to search for the maximum value.
29+
* @return {number} The maximum value in the array, or -Infinity if the array is empty.
30+
*/
1731
function arrayMax( array ) {
1832

1933
if ( array.length === 0 ) return - Infinity;
@@ -30,6 +44,18 @@ function arrayMax( array ) {
3044

3145
}
3246

47+
/**
48+
* Checks if an array contains values that require Uint32 representation.
49+
*
50+
* This function determines whether the array contains any values >= 65535,
51+
* which would require a Uint32Array rather than a Uint16Array for proper storage.
52+
* The function iterates from the end of the array, assuming larger values are
53+
* typically located at the end.
54+
*
55+
* @private
56+
* @param {Array<number>} array - The array to check.
57+
* @return {boolean} True if the array contains values >= 65535, false otherwise.
58+
*/
3359
function arrayNeedsUint32( array ) {
3460

3561
// assumes larger values usually on last
@@ -44,6 +70,14 @@ function arrayNeedsUint32( array ) {
4470

4571
}
4672

73+
/**
74+
* Map of typed array constructor names to their constructors.
75+
* This mapping enables dynamic creation of typed arrays based on string type names.
76+
*
77+
* @private
78+
* @constant
79+
* @type {Object<string, TypedArrayConstructor>}
80+
*/
4781
const TYPED_ARRAYS = {
4882
Int8Array: Int8Array,
4983
Uint8Array: Uint8Array,
@@ -56,6 +90,14 @@ const TYPED_ARRAYS = {
5690
Float64Array: Float64Array
5791
};
5892

93+
/**
94+
* Creates a typed array of the specified type from the given buffer.
95+
*
96+
* @private
97+
* @param {string} type - The name of the typed array type (e.g., 'Float32Array', 'Uint16Array').
98+
* @param {ArrayBuffer} buffer - The buffer to create the typed array from.
99+
* @return {TypedArray} A new typed array of the specified type.
100+
*/
59101
function getTypedArray( type, buffer ) {
60102

61103
return new TYPED_ARRAYS[ type ]( buffer );
@@ -74,12 +116,31 @@ function isTypedArray( array ) {
74116

75117
}
76118

119+
/**
120+
* Creates an XHTML element with the specified tag name.
121+
*
122+
* This function uses the XHTML namespace to create DOM elements,
123+
* ensuring proper element creation in XML-based contexts.
124+
*
125+
* @private
126+
* @param {string} name - The tag name of the element to create (e.g., 'canvas', 'div').
127+
* @return {HTMLElement} The created XHTML element.
128+
*/
77129
function createElementNS( name ) {
78130

79131
return document.createElementNS( 'http://www.w3.org/1999/xhtml', name );
80132

81133
}
82134

135+
/**
136+
* Creates a canvas element configured for block display.
137+
*
138+
* This is a convenience function that creates a canvas element with
139+
* display style set to 'block', which is commonly used in three.js
140+
* rendering contexts to avoid inline element spacing issues.
141+
*
142+
* @return {HTMLCanvasElement} A canvas element with display set to 'block'.
143+
*/
83144
function createCanvasElement() {
84145

85146
const canvas = createElementNS( 'canvas' );
@@ -88,22 +149,59 @@ function createCanvasElement() {
88149

89150
}
90151

152+
/**
153+
* Internal cache for tracking warning messages to prevent duplicate warnings.
154+
*
155+
* @private
156+
* @type {Object<string, boolean>}
157+
*/
91158
const _cache = {};
92159

160+
/**
161+
* Custom console function handler for intercepting log, warn, and error calls.
162+
*
163+
* @private
164+
* @type {Function|null}
165+
*/
93166
let _setConsoleFunction = null;
94167

168+
/**
169+
* Sets a custom function to handle console output.
170+
*
171+
* This allows external code to intercept and handle console.log, console.warn,
172+
* and console.error calls made by three.js, which is useful for custom logging,
173+
* testing, or debugging workflows.
174+
*
175+
* @param {Function} fn - The function to handle console output. Should accept
176+
* (type, message, ...params) where type is 'log', 'warn', or 'error'.
177+
*/
95178
function setConsoleFunction( fn ) {
96179

97180
_setConsoleFunction = fn;
98181

99182
}
100183

184+
/**
185+
* Gets the currently set custom console function.
186+
*
187+
* @return {Function|null} The custom console function, or null if not set.
188+
*/
101189
function getConsoleFunction() {
102190

103191
return _setConsoleFunction;
104192

105193
}
106194

195+
/**
196+
* Logs an informational message with the 'THREE.' prefix.
197+
*
198+
* If a custom console function is set via setConsoleFunction(), it will be used
199+
* instead of the native console.log. The first parameter is treated as the
200+
* method name and is automatically prefixed with 'THREE.'.
201+
*
202+
* @param {...any} params - The message components. The first param is used as
203+
* the method name and prefixed with 'THREE.'.
204+
*/
107205
function log( ...params ) {
108206

109207
const message = 'THREE.' + params.shift();
@@ -120,6 +218,16 @@ function log( ...params ) {
120218

121219
}
122220

221+
/**
222+
* Logs a warning message with the 'THREE.' prefix.
223+
*
224+
* If a custom console function is set via setConsoleFunction(), it will be used
225+
* instead of the native console.warn. The first parameter is treated as the
226+
* method name and is automatically prefixed with 'THREE.'.
227+
*
228+
* @param {...any} params - The message components. The first param is used as
229+
* the method name and prefixed with 'THREE.'.
230+
*/
123231
function warn( ...params ) {
124232

125233
const message = 'THREE.' + params.shift();
@@ -136,6 +244,16 @@ function warn( ...params ) {
136244

137245
}
138246

247+
/**
248+
* Logs an error message with the 'THREE.' prefix.
249+
*
250+
* If a custom console function is set via setConsoleFunction(), it will be used
251+
* instead of the native console.error. The first parameter is treated as the
252+
* method name and is automatically prefixed with 'THREE.'.
253+
*
254+
* @param {...any} params - The message components. The first param is used as
255+
* the method name and prefixed with 'THREE.'.
256+
*/
139257
function error( ...params ) {
140258

141259
const message = 'THREE.' + params.shift();
@@ -152,6 +270,15 @@ function error( ...params ) {
152270

153271
}
154272

273+
/**
274+
* Logs a warning message only once, preventing duplicate warnings.
275+
*
276+
* This function maintains an internal cache of warning messages and will only
277+
* output each unique warning message once. Useful for warnings that may be
278+
* triggered repeatedly but should only be shown to the user once.
279+
*
280+
* @param {...any} params - The warning message components.
281+
*/
155282
function warnOnce( ...params ) {
156283

157284
const message = params.join( ' ' );
@@ -164,6 +291,20 @@ function warnOnce( ...params ) {
164291

165292
}
166293

294+
/**
295+
* Asynchronously probes for WebGL sync object completion.
296+
*
297+
* This function creates a promise that resolves when the WebGL sync object
298+
* signals completion or rejects if the sync operation fails. It uses polling
299+
* at the specified interval to check the sync status without blocking the
300+
* main thread. This is useful for GPU-CPU synchronization in WebGL contexts.
301+
*
302+
* @private
303+
* @param {WebGLRenderingContext|WebGL2RenderingContext} gl - The WebGL rendering context.
304+
* @param {WebGLSync} sync - The WebGL sync object to wait for.
305+
* @param {number} interval - The polling interval in milliseconds.
306+
* @return {Promise<void>} A promise that resolves when the sync completes or rejects if it fails.
307+
*/
167308
function probeAsync( gl, sync, interval ) {
168309

169310
return new Promise( function ( resolve, reject ) {
@@ -193,6 +334,18 @@ function probeAsync( gl, sync, interval ) {
193334

194335
}
195336

337+
/**
338+
* Converts a projection matrix from normalized device coordinates (NDC)
339+
* range [-1, 1] to [0, 1].
340+
*
341+
* This conversion is commonly needed when working with depth textures or
342+
* render targets that expect depth values in the [0, 1] range rather than
343+
* the standard OpenGL NDC range of [-1, 1]. The function modifies the
344+
* projection matrix in place.
345+
*
346+
* @private
347+
* @param {Matrix4} projectionMatrix - The projection matrix to convert (modified in place).
348+
*/
196349
function toNormalizedProjectionMatrix( projectionMatrix ) {
197350

198351
const m = projectionMatrix.elements;
@@ -205,6 +358,21 @@ function toNormalizedProjectionMatrix( projectionMatrix ) {
205358

206359
}
207360

361+
/**
362+
* Reverses the depth range of a projection matrix.
363+
*
364+
* This function inverts the depth mapping of a projection matrix, which is
365+
* useful for reversed-Z depth buffer techniques that can improve depth
366+
* precision. The function handles both perspective and orthographic projection
367+
* matrices differently and modifies the matrix in place.
368+
*
369+
* For perspective matrices (where m[11] === -1), the depth mapping is
370+
* reversed with an offset. For orthographic matrices, a simpler reversal
371+
* is applied.
372+
*
373+
* @private
374+
* @param {Matrix4} projectionMatrix - The projection matrix to reverse (modified in place).
375+
*/
208376
function toReversedProjectionMatrix( projectionMatrix ) {
209377

210378
const m = projectionMatrix.elements;

0 commit comments

Comments
 (0)