-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasgn3.js
More file actions
294 lines (229 loc) · 6.89 KB
/
asgn3.js
File metadata and controls
294 lines (229 loc) · 6.89 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
let gl;
let camera;
let a_Position, a_UV;
let u_ModelMatrix, u_ViewMatrix, u_ProjMatrix;
let u_Sampler0, u_Sampler1;
let u_whichTexture;
let u_BaseColor;
let u_texColorWeight;
let map = [];
let animals = [];
let collected = 0;
const WORLD_SIZE = 32;
function main() {
const canvas = document.getElementById('webgl');
gl = canvas.getContext('webgl');
gl.enable(gl.DEPTH_TEST);
initShaders(gl, VSHADER, FSHADER);
initBuffers();
initTextures();
camera = new Camera(canvas);
generateWorld();
spawnAnimals();
initControls();
render();
}
const VSHADER = `
attribute vec4 a_Position;
attribute vec2 a_UV;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ProjMatrix;
varying vec2 v_UV;
void main() {
gl_Position = u_ProjMatrix * u_ViewMatrix * u_ModelMatrix * a_Position;
v_UV = a_UV;
}
`;
const FSHADER = `
precision mediump float;
uniform sampler2D u_Sampler0;
uniform sampler2D u_Sampler1;
uniform int u_whichTexture;
uniform vec4 u_BaseColor;
uniform float u_texColorWeight;
varying vec2 v_UV;
void main() {
vec4 texColor;
if(u_whichTexture == 0)
texColor = texture2D(u_Sampler0, v_UV);
else
texColor = texture2D(u_Sampler1, v_UV);
gl_FragColor = (1.0 - u_texColorWeight) * u_BaseColor
+ u_texColorWeight * texColor;
}
`;
function initBuffers() {
let vertices = new Float32Array([
// position // UV
-0.5,-0.5,0.5, 0,0,
0.5,-0.5,0.5, 1,0,
0.5,0.5,0.5, 1,1,
-0.5,-0.5,0.5, 0,0,
0.5,0.5,0.5, 1,1,
-0.5,0.5,0.5, 0,1,
-0.5,-0.5,-0.5, 1,0,
-0.5,0.5,-0.5, 1,1,
0.5,0.5,-0.5, 0,1,
-0.5,-0.5,-0.5, 1,0,
0.5,0.5,-0.5, 0,1,
0.5,-0.5,-0.5, 0,0,
-0.5,0.5,-0.5, 0,0,
-0.5,0.5,0.5, 0,1,
0.5,0.5,0.5, 1,1,
-0.5,0.5,-0.5, 0,0,
0.5,0.5,0.5, 1,1,
0.5,0.5,-0.5, 1,0,
-0.5,-0.5,-0.5, 0,0,
0.5,-0.5,-0.5, 1,0,
0.5,-0.5,0.5, 1,1,
-0.5,-0.5,-0.5, 0,0,
0.5,-0.5,0.5, 1,1,
-0.5,-0.5,0.5, 0,1,
]);
let buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
a_Position = gl.getAttribLocation(gl.program, 'a_Position');
a_UV = gl.getAttribLocation(gl.program, 'a_UV');
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 20, 0);
gl.enableVertexAttribArray(a_Position);
gl.vertexAttribPointer(a_UV, 2, gl.FLOAT, false, 20, 12);
gl.enableVertexAttribArray(a_UV);
u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix');
u_ProjMatrix = gl.getUniformLocation(gl.program, 'u_ProjMatrix');
u_Sampler0 = gl.getUniformLocation(gl.program, 'u_Sampler0');
u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1');
u_whichTexture = gl.getUniformLocation(gl.program, 'u_whichTexture');
u_BaseColor = gl.getUniformLocation(gl.program, 'u_BaseColor');
u_texColorWeight = gl.getUniformLocation(gl.program, 'u_texColorWeight');
}
function initTextures() {
loadTexture('textures/wall.jpg', 0);
loadTexture('textures/grass.jpg', 1);
}
function loadTexture(path, unit) {
let texture = gl.createTexture();
let image = new Image();
image.onload = function() {
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
gl.activeTexture(gl.TEXTURE0 + unit);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB,
gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.LINEAR);
if(unit == 0)
gl.uniform1i(u_Sampler0, 0);
else
gl.uniform1i(u_Sampler1, 1);
};
image.src = path;
}
function generateWorld() {
for(let x=0;x<WORLD_SIZE;x++){
map[x]=[];
for(let z=0;z<WORLD_SIZE;z++){
if(x==0 || z==0 || x==31 || z==31)
map[x][z]=4;
else
map[x][z]=Math.floor(Math.random()*3);
}
}
}
function spawnAnimals(){
for(let i=0;i<5;i++){
animals.push({
x: Math.floor(Math.random()*WORLD_SIZE),
z: Math.floor(Math.random()*WORLD_SIZE)
});
}
}
function render(){
gl.clearColor(0.5,0.7,1,1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.uniformMatrix4fv(u_ViewMatrix,false,camera.viewMatrix.elements);
gl.uniformMatrix4fv(u_ProjMatrix,false,camera.projectionMatrix.elements);
drawSky();
drawGround();
drawWorld();
drawAnimals();
requestAnimationFrame(render);
}
function drawCube(x,y,z,textureID){
let model = new Matrix4();
model.setTranslate(x,y,z);
gl.uniformMatrix4fv(u_ModelMatrix,false,model.elements);
gl.uniform1i(u_whichTexture, textureID);
gl.uniform4f(u_BaseColor, 1,1,1,1);
gl.uniform1f(u_texColorWeight, 1);
gl.drawArrays(gl.TRIANGLES,0,24);
}
function drawWorld(){
for(let x=0;x<WORLD_SIZE;x++){
for(let z=0;z<WORLD_SIZE;z++){
for(let y=0;y<map[x][z];y++){
drawCube(x,y,z,0);
}
}
}
}
function drawGround(){
let model = new Matrix4();
model.setTranslate(16,-0.5,16);
model.scale(32,0.1,32);
gl.uniformMatrix4fv(u_ModelMatrix,false,model.elements);
gl.uniform1i(u_whichTexture,1);
gl.uniform1f(u_texColorWeight,1);
gl.drawArrays(gl.TRIANGLES,0,24);
}
function drawSky(){
let model = new Matrix4();
model.setTranslate(16,10,16);
model.scale(1000,1000,1000);
gl.uniformMatrix4fv(u_ModelMatrix,false,model.elements);
gl.uniform1f(u_texColorWeight,0);
gl.uniform4f(u_BaseColor,0.4,0.7,1,1);
gl.drawArrays(gl.TRIANGLES,0,24);
}
function drawAnimals(){
for(let a of animals){
let model = new Matrix4();
model.setTranslate(a.x, map[a.x][a.z]+0.5, a.z);
gl.uniformMatrix4fv(u_ModelMatrix,false,model.elements);
gl.uniform1f(u_texColorWeight,0);
gl.uniform4f(u_BaseColor,1,1,0,1);
gl.drawArrays(gl.TRIANGLES,0,24);
}
}
function initControls(){
document.onkeydown = (e)=>{
if(e.key=="w") camera.moveForward();
if(e.key=="s") camera.moveBackwards();
if(e.key=="a") camera.moveLeft();
if(e.key=="d") camera.moveRight();
if(e.key=="q") camera.rotate(-3,0);
if(e.key=="e") camera.rotate(3,0);
if(e.key=="f") modifyBlock(1);
if(e.key=="g") modifyBlock(-1);
};
let lastX;
document.onmousemove = (e)=>{
if(lastX==undefined){ lastX=e.clientX; return; }
let dx = e.clientX-lastX;
lastX = e.clientX;
if(dx>0) camera.panRight();
else camera.panLeft();
};
}
function modifyBlock(delta){
let fx = Math.floor(camera.at.elements[0]);
let fz = Math.floor(camera.at.elements[2]);
if(fx>=0 && fx<32 && fz>=0 && fz<32){
map[fx][fz]+=delta;
if(map[fx][fz]<0) map[fx][fz]=0;
if(map[fx][fz]>4) map[fx][fz]=4;
}
}