-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (111 loc) · 2.99 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
/* global AFRAME */
// import local version of LDrawLoader for now
require('./LDrawLoader.js');
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Bricks component for A-Frame.
*/
AFRAME.registerComponent('bricks', {
schema: {
src: { type: "model" },
steps: { type: "int" },
},
init: function () {
this.model = null;
this.ldrawLoader = new THREE.LDrawLoader();
this.isBuggy = AFRAME.version !== "1.0.4-blocks-mod";
},
update: function (oldData) {
// load new model if the source has changed
if(this.data.src !== oldData.src) {
this.resetMesh();
this.loadModel(this.data.src, this.data.steps);
}
if((this.data.steps !== oldData.steps) && this.model) {
this.setStep(this.data.steps);
}
},
remove: function () {
if (!this.model) {
return;
}
this.resetMesh();
},
resetMesh: function () {
this.el.removeObject3D("mesh");
},
pause: function() {
// hide the model in the aframe inspector
// (since a Three.js bug in Mesh.intersect results in an infinte loop)
if(this.isBuggy) {
console.log("Three.js has a bug that prevents previews of Brick models.");
console.log("Using Edges-only preview in the Inspector");
if(this.data.steps) {
// hide faces of all bricks
console.log("CHILDREN", this.model.children);
this.model.children.forEach( brick => this.hide(brick) );
} else {
// hide faces of the model
this.show(this.model);
}
}
},
play: function() {
// show the model again
if(this.isBuggy) {
if(this.data.steps) {
this.model.children.forEach( brick => this.show(brick) );
} else {
this.show(this.model);
}
}
},
show: function(obj) {
// show the brick surface
obj.children[0].visible = true;
},
hide: function(obj) {
// hide the brick surface
obj.children[0].visible = false;
},
loadModel: function (url, steps) {
let el = this.el;
let ldrawLoader = this.ldrawLoader;
// separate objects into individual building steps
ldrawLoader.separateObjects = true;
ldrawLoader.load(url, (model) => {
console.log("Construction Steps", model.userData.numConstructionSteps );
console.log(model);
this.model = model;
model.scale.y = -1;
if( steps ) {
this.setStep( steps );
}
el.setObject3D('mesh', model);
el.emit("model-loaded", { format: "ldraw", model: model });
});
},
setStep: function(step) {
this.model.traverse( c => {
if ( c.isGroup ) {
c.visible = c.userData.constructionStep <= step;
}
});
}
});
// register block
let meshMixin = AFRAME.primitives.getMeshMixin();
AFRAME.registerPrimitive(
"a-bricks",
AFRAME.utils.extendDeep({}, meshMixin, {
defaultComponents: {
"bricks": {},
},
mappings: {
src: "bricks.src",
steps: "bricks.steps"
},
})
);