-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
297 lines (267 loc) · 8.25 KB
/
index.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
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
295
296
297
var tic = require('tic')();
var createAtlas = require('atlaspack');
function Texture(opts) {
if (!(this instanceof Texture)) return new Texture(opts || {});
this.game = opts.game;
this.THREE = this.game.THREE;
this.materials = [];
this.texturePath = opts.texturePath || '/textures/';
this.loading = 0;
this.options = defaults(opts || {}, {
crossOrigin: 'Anonymous',
materialParams: defaults(opts.materialParams || {}, {
ambient: 0xbbbbbb
}),
materialType: this.THREE.MeshLambertMaterial,
applyTextureParams: function(map) {
map.magFilter = this.THREE.NearestFilter;
map.minFilter = this.THREE.LinearMipMapLinearFilter;
}.bind(this)
});
// create a canvas for the texture atlas
this.canvas = document.createElement('canvas');
this.canvas.width = opts.atlasWidth || 512;
this.canvas.height = opts.atlasHeight || 512;
// create core atlas and texture
this.atlas = createAtlas(this.canvas);
this._atlasuv = false;
this._atlaskey = false;
this.texture = new this.THREE.Texture(this.canvas);
this.options.applyTextureParams(this.texture);
// load a first material for easy application to meshes
this.material = new this.options.materialType(this.options.materialParams);
this.material.map = this.texture;
this.material.transparent = true;
// a place for meshes to wait while textures are loading
this._meshQueue = [];
}
module.exports = Texture;
Texture.prototype.load = function(names, done) {
var self = this;
if (!Array.isArray(names)) names = [names];
done = done || function() {};
this.loading++;
var materialSlice = names.map(self._expandName);
self.materials = self.materials.concat(materialSlice);
// load onto the texture atlas
var load = Object.create(null);
materialSlice.forEach(function(mats) {
mats.forEach(function(mat) {
// todo: check if texture already exists
load[mat] = true;
});
});
each(Object.keys(load), self.pack.bind(self), function() {
self._afterLoading();
done(materialSlice);
});
};
Texture.prototype.pack = function(name, done) {
var self = this;
function pack(img) {
var node = self.atlas.pack(img);
if (node === false) {
self.atlas = self.atlas.expand(img);
}
done();
}
if (typeof name === 'string') {
var img = new Image();
img.id = name;
img.crossOrigin = self.options.crossOrigin;
img.src = self.texturePath + ext(name);
img.onload = function() {
pack(img);
};
img.onerror = function() {
console.error('Couldn\'t load URL [' + img.src + ']');
done();
};
} else {
pack(name);
}
return self;
};
Texture.prototype.find = function(name) {
var self = this;
var type = 0;
self.materials.forEach(function(mats, i) {
mats.forEach(function(mat) {
if (mat === name) {
type = i + 1;
return false;
}
});
if (type !== 0) return false;
});
return type;
};
Texture.prototype._expandName = function(name) {
if (name.top) return [name.back, name.front, name.top, name.bottom, name.left, name.right];
if (!Array.isArray(name)) name = [name];
// load the 0 texture to all
if (name.length === 1) name = [name[0],name[0],name[0],name[0],name[0],name[0]];
// 0 is top/bottom, 1 is sides
if (name.length === 2) name = [name[1],name[1],name[0],name[0],name[1],name[1]];
// 0 is top, 1 is bottom, 2 is sides
if (name.length === 3) name = [name[2],name[2],name[0],name[1],name[2],name[2]];
// 0 is top, 1 is bottom, 2 is front/back, 3 is left/right
if (name.length === 4) name = [name[2],name[2],name[0],name[1],name[3],name[3]];
return name;
};
Texture.prototype._afterLoading = function() {
var self = this;
setTimeout(function() {
self.loading--;
self._atlasuv = self.atlas.uv();
self._atlaskey = Object.create(null);
self.atlas.index().forEach(function(key) {
self._atlaskey[key.name] = key;
});
self.texture.needsUpdate = true;
self.material.needsUpdate = true;
//window.open(self.canvas.toDataURL());
if (self._meshQueue.length > 0) {
self._meshQueue.forEach(function(queue, i) {
self.paint.apply(queue.self, queue.args);
delete self._meshQueue[i];
});
}
}, 100);
};
Texture.prototype.paint = function(mesh, materials) {
var self = this;
// if were loading put into queue
if (self.loading > 0) {
self._meshQueue.push({self: self, args: arguments});
return false;
}
var isVoxelMesh = (materials) ? false : true;
if (!isVoxelMesh) materials = self._expandName(materials);
mesh.geometry.faces.forEach(function(face, i) {
if (mesh.geometry.faceVertexUvs[0].length < 1) return;
if (isVoxelMesh) {
var index = Math.floor(face.color.b*255 + face.color.g*255*255 + face.color.r*255*255*255);
materials = self.materials[index - 1];
if (!materials) materials = self.materials[0];
}
// BACK, FRONT, TOP, BOTTOM, LEFT, RIGHT
var name = materials[0] || '';
if (face.normal.z === 1) name = materials[1] || '';
else if (face.normal.y === 1) name = materials[2] || '';
else if (face.normal.y === -1) name = materials[3] || '';
else if (face.normal.x === -1) name = materials[4] || '';
else if (face.normal.x === 1) name = materials[5] || '';
var atlasuv = self._atlasuv[name];
if (!atlasuv) return;
// 0 -- 1
// | |
// 3 -- 2
// faces on these meshes are flipped vertically, so we map in reverse
// TODO: tops need rotate
if (isVoxelMesh) {
if (face.normal.z === -1 || face.normal.x === 1) {
atlasuv = uvrot(atlasuv, 90);
}
atlasuv = uvinvert(atlasuv);
} else {
atlasuv = uvrot(atlasuv, -90);
}
for (var j = 0; j < 4; j++) {
mesh.geometry.faceVertexUvs[0][i][j].set(atlasuv[j][0], 1 - atlasuv[j][1]);
}
});
mesh.geometry.uvsNeedUpdate = true;
};
Texture.prototype.sprite = function(name, w, h, cb) {
var self = this;
if (typeof w === 'function') { cb = w; w = null; }
if (typeof h === 'function') { cb = h; h = null; }
w = w || 16; h = h || w;
self.loading++;
var img = new Image();
img.src = self.texturePath + ext(name);
img.onerror = cb;
img.onload = function() {
var canvases = [];
for (var x = 0; x < img.width; x += w) {
for (var y = 0; y < img.height; y += h) {
var canvas = document.createElement('canvas');
canvas.width = w; canvas.height = h;
canvas.name = name + '_' + x + '_' + y;
canvas.getContext('2d').drawImage(img, x, y, w, h, 0, 0, w, h);
canvases.push(canvas);
}
}
var textures = [];
each(canvases, function(canvas, next) {
var tex = new Image();
tex.name = canvas.name;
tex.src = canvas.toDataURL();
tex.onload = function() {
self.pack(tex, next);
};
tex.onerror = next;
textures.push([
tex.name, tex.name, tex.name,
tex.name, tex.name, tex.name
]);
}, function() {
self._afterLoading();
delete canvases;
self.materials = self.materials.concat(textures);
cb(textures);
});
};
return self;
};
Texture.prototype.animate = function(mesh, names, delay) {
var self = this;
delay = delay || 1000;
if (!Array.isArray(names) || names.length < 2) return false;
var i = 0;
var mat = new this.options.materialType(this.options.materialParams);
mat.map = this.texture;
mat.transparent = true;
mat.needsUpdate = true;
tic.interval(function() {
self.paint(mesh, names[i % names.length]);
i++;
}, delay);
return mat;
};
Texture.prototype.tick = function(dt) {
tic.tick(dt);
};
function uvrot(coords, deg) {
if (deg === 0) return coords;
var c = [];
var i = (4 - Math.ceil(deg / 90)) % 4;
for (var j = 0; j < 4; j++) {
c.push(coords[i]);
if (i === 3) i = 0; else i++;
}
return c;
}
function uvinvert(coords) {
var c = coords.slice(0);
return [c[3], c[2], c[1], c[0]];
}
function ext(name) {
return (String(name).indexOf('.') !== -1) ? name : name + '.png';
}
function defaults(obj) {
[].slice.call(arguments, 1).forEach(function(from) {
if (from) for (var k in from) if (obj[k] == null) obj[k] = from[k];
});
return obj;
}
function each(arr, it, done) {
var count = 0;
arr.forEach(function(a) {
it(a, function() {
count++;
if (count >= arr.length) done();
});
});
}