Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions src/extras/DataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,13 @@ function _generateTables() {

}

// float32 to float16

/**
* Returns a half precision floating point value (FP16) from the given single
* precision floating point value (FP32).
*
* @param {number} val - A single precision floating point value.
* @return {number} The FP16 value.
*/
function toHalfFloat( val ) {

if ( Math.abs( val ) > 65504 ) console.warn( 'THREE.DataUtils.toHalfFloat(): Value out of range.' );
Expand All @@ -154,8 +159,13 @@ function toHalfFloat( val ) {

}

// float16 to float32

/**
* Returns a single precision floating point value (FP32) from the given half
* precision floating point value (FP16).
*
* @param {number} val - A half precision floating point value.
* @return {number} The FP32 value.
*/
function fromHalfFloat( val ) {

const m = val >> 10;
Expand All @@ -164,10 +174,40 @@ function fromHalfFloat( val ) {

}

const DataUtils = {
toHalfFloat: toHalfFloat,
fromHalfFloat: fromHalfFloat,
};
/**
* A class containing utility functions for data.
*
* @hideconstructor
*/
class DataUtils {

/**
* Returns a half precision floating point value (FP16) from the given single
* precision floating point value (FP32).
*
* @param {number} val - A single precision floating point value.
* @return {number} The FP16 value.
*/
static toHalfFloat( val ) {

return toHalfFloat( val );

}

/**
* Returns a single precision floating point value (FP32) from the given half
* precision floating point value (FP16).
*
* @param {number} val - A half precision floating point value.
* @return {number} The FP32 value.
*/
static fromHalfFloat( val ) {

return fromHalfFloat( val );

}

}

export {
toHalfFloat,
Expand Down
23 changes: 17 additions & 6 deletions src/extras/Earcut.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/**
* Port from https://github.com/mapbox/earcut (v2.2.4)
* An implementation of the earcut polygon triangulation algorithm. The code
* is a port of [mapbox/earcut]{@link https://github.com/mapbox/earcut mapbox/earcut} (v2.2.4).
*
* @hideconstructor
*/

const Earcut = {

triangulate: function ( data, holeIndices, dim = 2 ) {
class Earcut {

/**
* Triangulates the given shape definition by returning an array of triangles.
*
* @param {Array<number>} data - An array with 2D points.
* @param {Array<number>} holeIndices - An array with indices defining holes.
* @param {number} [dim=2] - The number of coordinates per vertex in the input array.
* @return {Array<number>} An array representing the triangulated faces. Each face is defined by three consecutive numbers
* representing vertex indices.
*/
static triangulate( data, holeIndices, dim = 2 ) {

const hasHoles = holeIndices && holeIndices.length;
const outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length;
Expand Down Expand Up @@ -46,7 +57,7 @@ const Earcut = {

}

};
}

// create a circular doubly linked list from polygon points in the specified winding order
function linkedList( data, start, end, dim, clockwise ) {
Expand Down
17 changes: 17 additions & 0 deletions src/extras/ImageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ import { SRGBToLinear } from '../math/ColorManagement.js';

let _canvas;

/**
* A class containing utility functions for images.
*
* @hideconstructor
*/
class ImageUtils {

/**
* Returns a data URI containing a representation of the given image.
*
* @param {(HTMLImageElement|HTMLCanvasElement)} image - The image object.
* @return {string} The data URI.
*/
static getDataURL( image ) {

if ( /^data:/i.test( image.src ) ) {
Expand Down Expand Up @@ -52,6 +63,12 @@ class ImageUtils {

}

/**
* Converts the given sRGB image data to linear color space.
*
* @param {(HTMLImageElement|HTMLCanvasElement|ImageBitmap|Object)} image - The image object.
* @return {HTMLCanvasElement|Object} The converted image.
*/
static sRGBToLinear( image ) {

if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
Expand Down
26 changes: 24 additions & 2 deletions src/extras/ShapeUtils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Earcut } from './Earcut.js';

/**
* A class containing utility functions for shapes.
*
* @hideconstructor
*/
class ShapeUtils {

// calculate area of the contour polygon

/**
* Calculate area of a ( 2D ) contour polygon.
*
* @param {Array<Vector2>} contour - An array of 2D points.
* @return {number} The area.
*/
static area( contour ) {

const n = contour.length;
Expand All @@ -19,12 +28,25 @@ class ShapeUtils {

}

/**
* Returns `true` if the given contour uses a clockwise winding order.
*
* @param {Array<Vector2>} pts - An array of 2D points defining a polyong.
* @return {boolean} Whether the given contour uses a clockwise winding order or not.
*/
static isClockWise( pts ) {

return ShapeUtils.area( pts ) < 0;

}

/**
* Triangluates the given shape definition.
*
* @param {Array<Vector2>} contour - An array of 2D points defining the contour.
* @param {Array<Array<Vector2>>} holes - An array that holds arrays of 2D points defining the holes.
* @return {Array<Array<number>>} An array that holds for each face definition an array with three indices.
*/
static triangulateShape( contour, holes ) {

const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
Expand Down
110 changes: 95 additions & 15 deletions src/extras/TextureUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { AlphaFormat, LuminanceFormat, LuminanceAlphaFormat, RedFormat, RedIntegerFormat, RGFormat, RGIntegerFormat, RGBFormat, RGBAFormat, RGBAIntegerFormat, RGB_S3TC_DXT1_Format, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, RGB_PVRTC_2BPPV1_Format, RGBA_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGBA_ETC2_EAC_Format, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGBA_BPTC_Format, RGB_BPTC_SIGNED_Format, RGB_BPTC_UNSIGNED_Format, RED_RGTC1_Format, SIGNED_RED_RGTC1_Format, RED_GREEN_RGTC2_Format, SIGNED_RED_GREEN_RGTC2_Format, UnsignedByteType, ByteType, UnsignedShortType, ShortType, HalfFloatType, UnsignedShort4444Type, UnsignedShort5551Type, UnsignedIntType, IntType, FloatType, UnsignedInt5999Type } from '../constants.js';

/**
* Scales the texture as large as possible within its surface without cropping
* or stretching the texture. The method preserves the original aspect ratio of
* the texture. Akin to CSS `object-fit: contain`
*
* @param {Texture} texture - The texture.
* @param {number} aspect - The texture's aspect ratio.
* @return {Texture} The updated texture.
*/
function contain( texture, aspect ) {

const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
Expand All @@ -26,6 +35,15 @@ function contain( texture, aspect ) {

}

/**
* Scales the texture to the smallest possible size to fill the surface, leaving
* no empty space. The method preserves the original aspect ratio of the texture.
* Akin to CSS `object-fit: cover`.
*
* @param {Texture} texture - The texture.
* @param {number} aspect - The texture's aspect ratio.
* @return {Texture} The updated texture.
*/
function cover( texture, aspect ) {

const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
Expand All @@ -52,6 +70,12 @@ function cover( texture, aspect ) {

}

/**
* Configures the texture to the default transformation. Akin to CSS `object-fit: fill`.
*
* @param {Texture} texture - The texture.
* @return {Texture} The updated texture.
*/
function fill( texture ) {

texture.repeat.x = 1;
Expand All @@ -64,17 +88,14 @@ function fill( texture ) {

}



/**
* Given the width, height, format, and type of a texture. Determines how many
* bytes must be used to represent the texture.
* Determines how many bytes must be used to represent the texture.
*
* @param {number} width
* @param {number} height
* @param {number} format
* @param {number} type
* @return {number} The number of bytes required to represent the texture.
* @param {number} width - The width of the texture.
* @param {number} height - The height of the texture.
* @param {number} format - The texture's format.
* @param {number} type - The texture's type.
* @return {number} The byte length.
*/
function getByteLength( width, height, format, type ) {

Expand Down Expand Up @@ -206,11 +227,70 @@ function getTextureTypeByteLength( type ) {

}

const TextureUtils = {
contain,
cover,
fill,
getByteLength
};
/**
* A class containing utility functions for textures.
*
* @hideconstructor
*/
class TextureUtils {

/**
* Scales the texture as large as possible within its surface without cropping
* or stretching the texture. The method preserves the original aspect ratio of
* the texture. Akin to CSS `object-fit: contain`
*
* @param {Texture} texture - The texture.
* @param {number} aspect - The texture's aspect ratio.
* @return {Texture} The updated texture.
*/
static contain( texture, aspect ) {

return contain( texture, aspect );

}

/**
* Scales the texture to the smallest possible size to fill the surface, leaving
* no empty space. The method preserves the original aspect ratio of the texture.
* Akin to CSS `object-fit: cover`.
*
* @param {Texture} texture - The texture.
* @param {number} aspect - The texture's aspect ratio.
* @return {Texture} The updated texture.
*/
static cover( texture, aspect ) {

return cover( texture, aspect );

}

/**
* Configures the texture to the default transformation. Akin to CSS `object-fit: fill`.
*
* @param {Texture} texture - The texture.
* @return {Texture} The updated texture.
*/
static fill( texture ) {

return fill( texture );

}

/**
* Determines how many bytes must be used to represent the texture.
*
* @param {number} width - The width of the texture.
* @param {number} height - The height of the texture.
* @param {number} format - The texture's format.
* @param {number} type - The texture's type.
* @return {number} The byte length.
*/
static getByteLength( width, height, format, type ) {

return getByteLength( width, height, format, type );

}

}

export { contain, cover, fill, getByteLength, TextureUtils };
4 changes: 2 additions & 2 deletions src/extras/core/Curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Curve {
* @abstract
* @param {number} t - A interpolation factor representing a position on the curve. Must be in the range `[0,1]`.
* @param {(Vector2|Vector3)} [optionalTarget] - The optional target vector the result is written to.
* @return {(Vector2|Vector3)} The position on the curve. It can be a 2D or 3D vector depending on the curve definition.
* @return {?(Vector2|Vector3)} The position on the curve. It can be a 2D or 3D vector depending on the curve definition.
*/
getPoint( /* t, optionalTarget */ ) {

Expand Down Expand Up @@ -134,7 +134,7 @@ class Curve {
/**
* Returns the total arc length of the curve.
*
* @return {number} The length of the curve..
* @return {number} The length of the curve.
*/
getLength() {

Expand Down
Loading
Loading