forked from MDFL64/vbsp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.js
205 lines (147 loc) · 4.89 KB
/
post.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
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
var color_table = {};
Module.ready = function(f) {
var module = this;
var color_req = new XMLHttpRequest();
color_req.onreadystatechange = function () {
if (this.readyState == 4){
if (this.status != 200) {
console.log("[vbsp.js] Warning! Failed to download color table! Only simple color guessing will be used.");
} else {
color_table = JSON.parse(this.response);
}
if (module.calledRun) {
f();
} else {
// Defer until ready!
module.onRuntimeInitialized = f;
}
}
}
color_req.open('GET',"colors.json",true);
color_req.send();
}
var _loadMap = Module.cwrap("loadMap", null, ["number","number"]);
var map_request;
Module.setCam = Module.cwrap("setCam", null, ["number","number","number","number","number"]);
var _setSkybox = Module.cwrap("setSkybox", null, ["number","number","number","number"]);
var _setModel = Module.cwrap("setModel", null, ["number","number","number","number"]);
var _setSkyColor = Module.cwrap("setSkyColor", null, ["number","number","number"]);
var _setAmbient = Module.cwrap("setAmbient", null, ["number","number","number"]);
var _setLight = Module.cwrap("setLight", null, ["number","number","number"]);
var _setLightAngle = Module.cwrap("setLightAngle", null, ["number","number"]);
var load_div;
function setLoadPercent(x,text,color) {
var w = Module.canvas.width;
var h = Module.canvas.height;
load_div.style.width = w*x;
load_div.style.height = h/10;
load_div.style.fontSize = h/10 + "px";
load_div.textContent = text;
load_div.style.backgroundColor = color;
}
Module.loadMap = function(url) {
console.log("[vbsp.js] Downloading \""+url+"\"...");
if (map_request) {
map_request.onreadystatechange = null;
map_request.abort();
}
map_request = new XMLHttpRequest();
map_request.responseType = "arraybuffer";
map_request.onreadystatechange = function () {
if (this.readyState == 4){
if (this.status != 200) {
console.log("[vbsp.js] Fatal! Failed to download map!");
map_request = null;
setLoadPercent(1,"Download failed!","red");
return;
}
setLoadPercent(1,"Initializing...","orange");
setTimeout(function() {
var map_data = map_request.response;
var map_size = map_data.byteLength;
var map_ptr = Module._malloc(map_size);
new Uint8Array(Module.buffer, map_ptr, map_size).set(new Uint8Array(map_data));
console.group("[vbsp.js] Initializing \""+url+"\"...");
console.time("Completed in");
_loadMap(map_ptr,map_size); // note that this always returns 1 atm, there is no indication of failure!
console.timeEnd("Completed in");
console.groupEnd();
Module._free(map_ptr);
map_request = null;
setLoadPercent(0);
});
}
};
map_request.addEventListener("progress",function(e) {
if (e.lengthComputable) {
var percent = e.loaded / e.total;
if (percent!=1)
setLoadPercent(percent,"Downloading...","yellow");
}
});
map_request.open('GET',url,true);
map_request.send();
}
var _initRenderer = Module.cwrap("initRenderer", null, ["number","number"]);
Module.initRenderer = function(div) {
div.style.position = "relative";
Module.canvas = document.createElement("canvas");
div.appendChild(Module.canvas);
Module.canvas.style.cursor = "move";
load_div = document.createElement("div");
div.appendChild(load_div);
load_div.style.position = "absolute";
load_div.style.bottom = 0;
console.log("[vbsp.js] Starting renderer.");
_initRenderer(div.offsetWidth,div.offsetHeight);
setLoadPercent(1,"Ready!","lime");
}
function guess_color(name) {
/*if (name.indexOf("concrete") > -1)
return colors.concrete;
if (name.indexOf("cement") > -1)
return colors.concrete;
if (name.indexOf("pavement") > -1)
return colors.concrete_dark;
if (name.indexOf("plaster") > -1)
return colors.plaster;
if (name.indexOf("metal") > -1)
return 0x24201b;
if (name.indexOf("brick") > -1)
return colors.brick;
if (name.indexOf("wood") > -1)
return colors.wood;
if (name.indexOf("plastic") > -1)
return colors.white;
if (name.indexOf("glass") > -1 || name.indexOf("window") > -1)
return colors.glass;
if (name.indexOf("mud") > -1)
return colors.mud;
if (name.indexOf("sand") > -1)
return colors.sand;
if (name.indexOf("dirt") > -1)
return colors.mud;
if (name.indexOf("grass") > -1)
return colors.grass;
if (name.indexOf("stone") > -1)
return colors.concrete;
if (name.indexOf("water") > -1)
return colors.water;
if (name.indexOf("slime") > -1)
return 0x808000;
if (name.indexOf("button") > -1)
return 0xFF0000;
if (name.indexOf("sign") > -1)
return 0x008002;
/*if (name.indexOf("red") > -1)
return 0xEB5B5B;
if (name.indexOf("blue") > -1)
return 0x5B92EB;*/
if (name.indexOf("gravel") > -1)
return 5788488;
if (name.indexOf("black") > -1)
return 0;
//console.log(">>> "+name);
return 0xFFFFFF;
}
//Module.load = Module.cwrap("load", "number", []);