-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
97 lines (87 loc) · 2.16 KB
/
index.js
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
var int8 = new Int8Array(4);
var int32 = new Int32Array(int8.buffer, 0, 1);
var float32 = new Float32Array(int8.buffer, 0, 1);
/**
* A singleton for number utilities.
* @class NumberUtil
*/
var NumberUtil = function() {
};
/**
* Returns a float representation of the given int bits. ArrayBuffer
* is used for the conversion.
*
* @method intBitsToFloat
* @static
* @param {Number} i the int to cast
* @return {Number} the float
*/
NumberUtil.intBitsToFloat = function(i) {
int32[0] = i;
return float32[0];
};
/**
* Returns the int bits from the given float. ArrayBuffer is used
* for the conversion.
*
* @method floatToIntBits
* @static
* @param {Number} f the float to cast
* @return {Number} the int bits
*/
NumberUtil.floatToIntBits = function(f) {
float32[0] = f;
return int32[0];
};
/**
* Encodes ABGR int as a float, with slight precision loss.
*
* @method intToFloatColor
* @static
* @param {Number} value an ABGR packed integer
*/
NumberUtil.intToFloatColor = function(value) {
return NumberUtil.intBitsToFloat( value & 0xfeffffff );
};
/**
* Returns a float encoded ABGR value from the given RGBA
* bytes (0 - 255). Useful for saving bandwidth in vertex data.
*
* @method colorToFloat
* @static
* @param {Number} r the Red byte (0 - 255)
* @param {Number} g the Green byte (0 - 255)
* @param {Number} b the Blue byte (0 - 255)
* @param {Number} a the Alpha byte (0 - 255)
* @return {Float32} a Float32 of the RGBA color
*/
NumberUtil.colorToFloat = function(r, g, b, a) {
var bits = (a << 24 | b << 16 | g << 8 | r);
return NumberUtil.intToFloatColor(bits);
};
/**
* Returns true if the number is a power-of-two.
*
* @method isPowerOfTwo
* @param {Number} n the number to test
* @return {Boolean} true if power-of-two
*/
NumberUtil.isPowerOfTwo = function(n) {
return (n & (n - 1)) === 0;
};
/**
* Returns the next highest power-of-two from the specified number.
*
* @param {Number} n the number to test
* @return {Number} the next highest power of two
*/
NumberUtil.nextPowerOfTwo = function(n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n+1;
};
module.exports = NumberUtil;