-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (105 loc) · 3.61 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
var fetchScript = require('./lib/fetch-script')();
var LOADER_SRC = 'https://rawgit.com/lowlines/three-tgx-loader/master/three.tgxloader.js';
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
var config = {};
if (window.DESTINYMODELCONFIG) config = window.DESTINYMODELCONFIG;
if (!config.apiKey) config.apiKey = '';
/**
* Destiny Model component for A-Frame.
*/
AFRAME.registerComponent('destiny-model', {
schema: {
itemHash: { type: 'number' },
itemHashes: { type: 'array' },
shaderHash: { type: 'number', default: 0 },
shaderHashes: { type: 'array' },
game: { type: 'string', default: 'destiny2' },
apiKey: { type: 'string', default: config.apiKey },
platform: { type: 'string', default: 'mobile' },
d1Manifest: { type: 'string', default: config.d1Manifest },
d2Manifest: { type: 'string', default: config.d2Manifest },
isFemale: { type: 'boolean', default: false }
},
/**
* Set if component needs multiple instancing.
*/
multiple: false,
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
this.model = null;
this.loader = null;
this.loaderPromise = loadLoader().then(function () {
this.loader = new THREE.TGXLoader();
}.bind(this));
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function () {
var self = this;
var el = this.el;
var itemHash = this.data.itemHash;
var itemHashes = this.data.itemHashes || [itemHash];
var shaderHash = this.data.shaderHash;
var shaderHashes = this.data.shaderHashes || [shaderHash];
var isFemale = this.data.isFemale;
var apiKey = this.data.apiKey;
var d1Manifest = this.data.d1Manifest;
var d2Manifest = this.data.d2Manifest;
var game = this.data.game;
var platform = this.data.platform;
if (!itemHashes || !apiKey || !game
|| (game === 'destiny2' && platform === 'web')
|| (game === 'destiny2' && !d2Manifest)
|| (platform === 'mobile' && !d1Manifest)) { return; }
this.remove();
this.loaderPromise.then(function () {
THREE.TGXLoader.APIKey = apiKey;
THREE.TGXLoader.ManifestPath = d1Manifest;
THREE.TGXLoader.ManifestPath2 = d2Manifest;
THREE.TGXLoader.Platform = platform;
this.loader.load({itemHashes: itemHashes, shaderHashes: shaderHashes, game: game, isFemale: isFemale}, function tgxLoaded (geometry, materials) {
var mesh = new THREE.Mesh(geometry, new THREE.MultiMaterial(materials));
mesh.rotation.x = -90 * Math.PI / 180;
self.model = mesh;
el.setObject3D('mesh', self.model);
el.emit('model-loaded', {format: 'destiny', model: self.model});
});
}.bind(this));
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () {
if (!this.model) { return; }
this.el.removeObject3D('mesh');
},
/**
* Called on each scene tick.
*/
// tick: function (t) { },
/**
* Called when entity pauses.
* Use to stop or remove any dynamic or background behavior such as events.
*/
pause: function () { },
/**
* Called when entity resumes.
* Use to continue or add any dynamic or background behavior such as events.
*/
play: function () { }
});
var loadLoader = (function () {
var promise;
return function () {
promise = promise || fetchScript(LOADER_SRC);
return promise;
};
}());