forked from mkuzdal/ScottSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.js
174 lines (144 loc) · 5.79 KB
/
light.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var MAX_LIGHT_COUNT = 5;
class lightHandler {
constructor (_scene) {
this.lightCount = 0;
this.lightSources = [];
this.scene = _scene;
}
addSource (light) {
if (this.lightCount >= MAX_LIGHT_COUNT)
return;
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i] === light) {
return;
}
}
light.lightID = this.lightCount;
this.lightCount++;
this.lightSources.push (light);
}
removeSource (light) {
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i] === light) {
this.lightSources.splice (i, 1);
this.lightCount--;
return;
}
}
}
setupAll () {
for (var i = 0; i < this.lightCount; i++) {
this.lightSources[i].setup ();
}
if (this.lightSources.length) {
gl.uniformMatrix4fv (lightProjectionMatrixLoc, false, this.lightSources[0].projectionMatrix);
gl.uniformMatrix4fv (lightMatrixLoc, false, this.lightSources[0].view);
}
}
setupByTag (tag) {
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i].tag == tag) {
this.lightSources[i].setup ();
}
}
}
activateAll () {
for (var i = 0; i < this.lightCount; i++) {
this.lightSources[i].active = true;
}
}
activateByTag (tag) {
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i].tag == tag) {
this.lightSources[i].active = true;
}
}
}
deactivateAll () {
for (var i = 0; i < this.lightCount; i++) {
this.lightSources[i].active = false;
}
}
deactivateByTag (tag) {
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i].tag == tag) {
this.lightSources[i].active = false;
}
}
}
toggleAll () {
for (var i = 0; i < this.lightCount; i++) {
this.lightSources[i].active = !this.lightSources[i].active;
}
}
toggleByTag (tag) {
for (var i = 0; i < this.lightCount; i++) {
if (this.lightSources[i].tag == tag) {
this.lightSources[i].active = !this.lightSources[i].active;
}
}
}
makePrimarySource (light) {
var temp = this.lightSources[0];
this.lightSources[0] = light
this.addSource (temp);
}
}
/** light: an abstraction for a light object. Lights have a transform component defining
* its orientation and position in space as well as the ambient, diffuse and specular values
* that define it.
*/
class light {
/** constructor: builds an instance of a light object with given attributes.
* @param { transform } transform: the orientation and position of the light source.
* @param { vec4 } ambient: the ambient value for the light
* @param { vec4 } diffuse: the diffuse value for the light.
* @param { vec4 } specular: the specular value for the light.
*/
constructor (_transform, _at, _ambient, _diffuse, _specular, _bias) {
this.transform = _transform || new transform ();
this.at = _at || vec3.fromValues (0.0, 0.0, 0.0);
this.up = vec3.create ();
vec3.sub (this.up, this.at, this.transform.position);
this.up = vec3.fromValues (-this.up[1], this.up[0], this.up[2]);
this.ambient = _ambient || vec4.fromValues (0.2, 0.2, 0.2, 1.0);
this.diffuse = _diffuse || vec4.fromValues (0.4, 0.4, 0.4, 1.0);
this.specular = _specular || vec4.fromValues (0.6, 0.6, 0.6, 1.0);
this.tag = "light"
this.active = true;
this.shadows = true;
this.bias = _bias || 0.007;
this.projectionMatrix = mat4.create ();
this.view = mat4.create ();
}
/** setup: sets up the lightposition uniform in the vertex shader.
*/
setup () {
var pos = [ this.transform.position[0], this.transform.position[1], this.transform.position[2], 1.0 ];
gl.uniform4fv (gl.getUniformLocation (program, "fLightPosition[" + this.lightID + "]"), pos);
gl.uniform4fv (gl.getUniformLocation (program, "fAmbientLight[" + this.lightID + "]"), this.ambient);
if (this.active) {
gl.uniform4fv (gl.getUniformLocation (program, "fDiffuseLight[" + this.lightID + "]"), this.diffuse);
gl.uniform4fv (gl.getUniformLocation (program, "fSpecularLight[" + this.lightID + "]"), this.specular);
} else {
gl.uniform4fv (gl.getUniformLocation (program, "fDiffuseLight[" + this.lightID + "]"), vec4.fromValues (0.0, 0.0, 0.0, 0.0));
gl.uniform4fv (gl.getUniformLocation (program, "fSpecularLight[" + this.lightID + "]"), vec4.fromValues (0.0, 0.0, 0.0, 0.0));
}
this.setPerspective ();
this.setLightMatrix ();
gl.uniformMatrix4fv (gl.getUniformLocation (program, "lightProjectionMatrix[" + this.lightID + "]"), false, this.projectionMatrix);
gl.uniformMatrix4fv (gl.getUniformLocation (program, "lightMatrix[" + this.lightID + "]"), false, this.view);
gl.uniform1f (gl.getUniformLocation (program, "fLightBias[" + this.lightID + "]"), this.bias);
}
/** setPerspective: sets the perspective projection matrix.
*/
setPerspective () {
//mat4.ortho (this.projectionMatrix, -10.0, 10.0, -10.0, 10.0, 1s.0, 256.0);
mat4.perspective (this.projectionMatrix, Math.PI * 90.0 / 180, OFFSCREEN_WIDTH / OFFSCREEN_HEIGHT, 1.0, 256.0);
}
/** setLightMatrix: sets the light view matrix.
*/
setLightMatrix () {
mat4.lookAt (this.view, this.transform.position, this.at, this.up);
}
}