-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsketch2three.js
110 lines (83 loc) · 2.74 KB
/
sketch2three.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
var Sketch2three = (function() {
function Sketch2three() {
var self = this;
self.slices = [];
self.opts = {};
self.url = null;
var geometry = new THREE.PlaneGeometry( 1, 1, 10, 0 );
this.makeMesh = function(node) {
var path = self.getPath() + '/' + node.name;
var props = node.properties;
var texture = THREE.ImageUtils.loadTexture(path);
var material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true
//wireframe: true
});
var mesh = new THREE.Mesh(geometry.clone(), material);
var scale = self.pixelScale;
var y = props.y + (props.height / 2);
var x = props.x + (props.width / 2);
mesh.name = node.name;
mesh.scale.set(props.width * scale, props.height * scale, 1);
mesh.position.set(x * scale, -y * scale, 0);
mesh.userData = props;
return mesh;
}
this.getPath = function() {
return self.url.substring(0, self.url.lastIndexOf('/'));
}
this.load = function(url, opts) {
self.opts = opts || {};
self.pixelScale = self.opts.pixelScale || 1;
return new Promise(function(resolve, reject) {
if (!url) {
reject('must point to a sketch json file.')
}
self.url = url;
function loadJson(url) {
return new Promise( function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(xhr.response);
}
xhr.onerror = function() {
reject(new Error('Some kind of network error, XHR failed.'))
}
xhr.open('GET', url);
xhr.send();
})
};
function propertyTest(currentObject) {
for (var property in currentObject) {
if (currentObject[property].hasOwnProperty('format')) {
self.slices.push({
name: property,
properties: currentObject[property]
})
} else {
propertyTest(currentObject[property]);
}
}
}
loadJson(url)
.then(function(response) {
return JSON.parse(response);
}, function(err) {
reject('Error parsing json');
})
.then(function(data) {
propertyTest(data)
var filtered = self.slices.filter(function(slice) {
return (self.opts.exclude.indexOf(slice.name) === -1) ? true : false;
});
var slices = filtered.map(function(slice) {
return self.makeMesh(slice);
});
resolve(slices)
})
})
}
}
return new Sketch2three();
})();