-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (75 loc) · 2.86 KB
/
index.js
File metadata and controls
83 lines (75 loc) · 2.86 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
function Texture(names, opts) {
if (!(this instanceof Texture)) return new Texture(names, opts);
opts = opts || {};
if (!isArray(name)) {
opts = names;
names = null;
}
this.THREE = opts.THREE || require('three');
this.texturePath = opts.texturePath || '/textures/';
if (names) this.loadTextures(names);
}
module.exports = Texture;
Texture.prototype.loadTexture = function(data) {
var self = this;
if (typeof data === 'string') data = [data];
if (!isArray(data)) {
data = [data.back, data.front, data.top, data.bottom, data.left, data.right];
}
// load the 0 texture to all
if (data.length === 1) data = [data[0],data[0],data[0],data[0],data[0],data[0]];
// 0 is top/bottom, 1 is sides
if (data.length === 2) data = [data[1],data[1],data[0],data[0],data[1],data[1]];
// 0 is top, 1 is bottom, 2 is sides
if (data.length === 3) data = [data[2],data[2],data[0],data[1],data[2],data[2]];
// 0 is top, 1 is bottom, 2 is front/back, 3 is left/right
if (data.length === 4) data = [data[2],data[2],data[0],data[1],data[3],data[3]];
data = data.map(function(name, i) {
var tex = self.THREE.ImageUtils.loadTexture(self.texturePath + ext(name));
tex.magFilter = self.THREE.NearestFilter;
tex.minFilter = self.THREE.LinearMipMapLinearFilter;
tex.wrapT = self.THREE.RepeatWrapping;
tex.wrapS = self.THREE.RepeatWrapping;
return new self.THREE.MeshLambertMaterial({
map: tex,
ambient: 0xbbbbbb
});
});
return (self._loadingMulti !== true) ? new self.THREE.MeshFaceMaterial(data) : data;
};
Texture.prototype.loadTextures = function(names) {
var self = this;
self._loadingMulti = true;
self.material = new self.THREE.MeshFaceMaterial(
[].concat.apply([], names.map(function(name) {
return self.loadTexture(name);
}))
);
self._loadingMulti = false;
return self.material;
};
Texture.prototype.applyTextures = function(geom) {
var self = this;
if (!self.material) return;
var textures = self.material.materials;
geom.faces.forEach(function(face) {
var c = face.vertexColors[0];
var index = Math.floor(c.b*255 + c.g*255*255 + c.r*255*255*255);
index = (Math.max(0, index - 1) % (textures.length / 6)) * 6;
// BACK, FRONT, TOP, BOTTOM, LEFT, RIGHT
if (face.normal.z === 1) index += 1;
else if (face.normal.y === 1) index += 2;
else if (face.normal.y === -1) index += 3;
else if (face.normal.x === -1) index += 4;
else if (face.normal.x === 1) index += 5;
// todo: figure out why left and back need a -90 rotation
face.materialIndex = index;
})
};
function ext(name) {
return (name.indexOf('.') !== -1) ? name : name + '.png';
}
// copied from https://github.com/joyent/node/blob/master/lib/util.js#L433
function isArray(ar) {
return Array.isArray(ar) || (typeof ar === 'object' && Object.prototype.toString.call(ar) === '[object Array]');
}