This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
/
_MatrixStack.js
136 lines (118 loc) · 2.66 KB
/
_MatrixStack.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
*
* You can modify and use this source freely
* only for the development of application related Live2D.
*
* (c) Live2D Inc. All rights reserved.
*/
/**
* EYHN 修改
*
* Copyright © 2016 - 2017 EYHN
*/
// Modified by xiazeyu.
/**
* @desc A matrix stack releated to draw the model
*/
function MatrixStack() {}
MatrixStack.matrixStack = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
MatrixStack.depth = 0;
MatrixStack.currentMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
MatrixStack.tmp = new Array(16);
/**
* @name reset
* @desc reset the stack
* @param null
* @returns null
* @memberOf MatrixStack
*/
MatrixStack.reset = function(){
this.depth = 0;
}
/**
* @name loadIdentity
* @desc reset values in the stack to whether it can be divisible by 5
* @param null
* @returns null
* @memberOf MatrixStack
*/
MatrixStack.loadIdentity = function(){
var thisRef = this;
for (var i = 0; i < 16; i++){
thisRef.currentMatrix[i] = (i % 5 == 0) ? 1 : 0;
}
}
/**
* @name push
* @desc push a new element into the stack
* @param null
* @returns null
* @memberOf MatrixStack
*/
MatrixStack.push = function(){
var thisRef = this;
// var offset = thisRef.depth * 16;
var nextOffset = (thisRef.depth + 1) * 16;
if (thisRef.matrixStack.length < nextOffset + 16){
thisRef.matrixStack.length = nextOffset + 16;
}
for (var i = 0; i < 16; i++){
thisRef.matrixStack[nextOffset + i] = thisRef.currentMatrix[i];
}
thisRef.depth++;
}
/**
* @name pop
* @desc pop an element from the stack
* @param null
* @returns null
* @memberOf MatrixStack
*/
MatrixStack.pop = function(){
var thisRef = this;
thisRef.depth--;
if (thisRef.depth < 0){ // stack is underflow?????
myError("Invalid matrix stack.");
thisRef.depth = 0;
}
var offset = thisRef.depth * 16;
for (var i = 0; i < 16; i++){
thisRef.currentMatrix[i] = thisRef.matrixStack[offset + i];
}
}
/**
* @name getMatrix
* @desc return the current matrix stack
* @param null
* @returns {Array} current matrix stack
* @memberOf MatrixStack
*/
MatrixStack.getMatrix = function(){
return this.currentMatrix;
}
/**
* @name multMatrix
* @desc matrix multiplication, save to the currentMatrix
* @param null
* @returns null
* @memberOf MatrixStack
*/
MatrixStack.multMatrix = function(matNew)
{
var thisRef = this;
var i, j, k;
for (i = 0; i < 16; i++){
thisRef.tmp[i] = 0;
}
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
for (k = 0; k < 4; k++){
thisRef.tmp[i + j * 4] += thisRef.currentMatrix[i + k * 4] * matNew[k + j * 4];
}
}
}
for (i = 0; i < 16; i++){
thisRef.currentMatrix[i] = thisRef.tmp[i];
}
}
module.exports = MatrixStack;