forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-hexgrid.js
52 lines (50 loc) · 1.23 KB
/
a-hexgrid.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
const vg = require('../../lib/hex-grid.min.js');
const defaultHexGrid = require('../../lib/default-hex-grid');
/**
* Hex grid.
*/
module.exports.Primitive = AFRAME.registerPrimitive('a-hexgrid', {
defaultComponents: {
'hexgrid': {}
},
mappings: {
src: 'hexgrid.src'
}
});
module.exports.Component = AFRAME.registerComponent('hexgrid', {
dependencies: ['material'],
schema: {
src: {type: 'asset'}
},
init: function () {
const data = this.data;
if (data.src) {
fetch(data.src)
.then((response) => response.json())
.then((json) => this.addMesh(json));
} else {
this.addMesh(defaultHexGrid);
}
},
addMesh: function (json) {
const grid = new vg.HexGrid();
grid.fromJSON(json);
const board = new vg.Board(grid);
board.generateTilemap();
this.el.setObject3D('mesh', board.group);
this.addMaterial();
},
addMaterial: function () {
const materialComponent = this.el.components.material;
const material = (materialComponent || {}).material;
if (!material) return;
this.el.object3D.traverse((node) => {
if (node.isMesh) {
node.material = material;
}
});
},
remove: function () {
this.el.removeObject3D('mesh');
}
});