-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
57 lines (48 loc) · 1.7 KB
/
math.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
const { vec2, vec3, vec4, quat, mat2, mat3, mat4 } = glMatrix;
const Vec2 = vec2.fromValues;
const Vec3 = vec3.fromValues;
const Vec4 = vec4.fromValues;
const Quat = quat.fromValues;
function calcRayDirection(wh_prop, iproj, iview) {
let t = vec4.transformMat4(
vec4.create(),
vec4.fromValues(wh_prop[0]*2-1, wh_prop[1]*2-1, 1, 1),
iproj
);
let s = vec3.fromValues(t[0]/t[3], t[1]/t[3], t[2]/t[3]);
vec3.normalize(s, s);
let r = vec4.transformMat4(
vec4.create(),
vec4.fromValues(s[0], s[1], s[2], 0),
iview
);
return vec3.fromValues(r[0], r[1], r[2]);
}
Float32Array.prototype.concat = function() {
var bytesPerIndex = 4,
buffers = Array.prototype.slice.call(arguments);
// add self
buffers.unshift(this);
buffers = buffers.map(function (item) {
if (item instanceof Float32Array) {
return item.buffer;
} else if (item instanceof ArrayBuffer) {
if (item.byteLength / bytesPerIndex % 1 !== 0) {
throw new Error('One of the ArrayBuffers is not from a Float32Array');
}
return item;
} else {
throw new Error('You can only concat Float32Array, or ArrayBuffers');
}
});
var concatenatedByteLength = buffers
.map(function (a) {return a.byteLength;})
.reduce(function (a,b) {return a + b;}, 0);
var concatenatedArray = new Float32Array(concatenatedByteLength / bytesPerIndex);
var offset = 0;
buffers.forEach(function (buffer, index) {
concatenatedArray.set(new Float32Array(buffer), offset);
offset += buffer.byteLength / bytesPerIndex;
});
return concatenatedArray;
};