-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationWrapper.js
More file actions
266 lines (213 loc) · 9.11 KB
/
Copy pathAnimationWrapper.js
File metadata and controls
266 lines (213 loc) · 9.11 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import * as THREE from 'three';
import URDFViewer from 'urdf-loader';
import URDFLoader from 'urdf-loader/src/URDFLoader.js';
export default
class AnimationWrapper extends URDFViewer {
constructor() {
super();
// removed shader bug https://github.com/mrdoob/three.js/issues/9716
this.renderer.context.getShaderInfoLog = function () { return ''; };
if (typeof this.urdfLoader === "undefined") {
// URDF-loader version after 0.6.0
this.loadMeshFunc = (path, manager, done) => {
new URDFLoader(manager).defaultMeshLoader(path, manager, obj => {
for (let i = obj.children.length - 1; i >= 0; i--) {
if (obj.children[i].type !== 'Mesh') {
obj.remove(obj.children[i]);
}
}
done(obj);
});
};
}
else {
// URDF-loader version before 0.6.0
const meshOnlyLoader_ = this.urdfLoader.defaultMeshLoader.bind(this.urdfLoader);
this.urdfLoader.defaultMeshLoader = (path, ext, done) => {
meshOnlyLoader_(path, ext, obj => {
for (let i = obj.children.length - 1; i >= 0; i--) {
if (obj.children[i].type !== 'Mesh') {
obj.remove(obj.children[i]);
}
}
done(obj);
});
};
}
this.clock = new THREE.Clock();
this.controls.enablePan = true;
this.controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
this.controls.dampingFactor = 0.25;
this.controls.screenSpacePanning = false;
this.controls.maxPolarAngle = Math.PI / 2;
// this.controls.removeEventListener('change', () => this.recenter());
}
_setRecorder(quality, speed) {
this.action.reset();
this.mixer.update(this.clock.getDelta()); // Jump to first frame before record
this.gif.abort();
this.gif.width = null;
this.gif.height = null;
this.gif.frames = []; // Remove previous recorded frames
this.gif.setOptions({
quality: quality,
width: this.canvas.width,
height: this.canvas.height
});
this.mixer.timeScale = speed;
this.action.paused = false;
}
renderLoop() {
requestAnimationFrame(this.renderLoop.bind(this));
this.mixer.update(this.clock.getDelta());
if (this.recording) {
this.gif.addFrame(this.canvas, { delay: this.delay, copy: true });
this.updateRecordBar();
}
}
_addGIFConverterGUI() {
if (this.gui == null) this.gui = new dat.GUI();
this.gif.on('progress', p => gifAPI['converting ='] = p * 100);
this.mixer.addEventListener('loop', () => {
if (this.recording) {
this.recording = false;
this.action.paused = true;
this.gif.render();
}
});
this.updateRecordBar = () => gifAPI['recording ='] = this.action.time;
let folder = this.gui.addFolder('Record ' + this.track.name),
gifAPI = {
'record()': () => {
this._setRecorder(gifAPI.quality, gifAPI.speed);
gifAPI['recording ='] = 0;
gifAPI['converting ='] = 0;
this.recording = true;
},
'recording =': 0,
'converting =': 0,
speed: 1,
quality: 10
};
folder.add(gifAPI, 'speed', 0.1, 10);
folder.add(gifAPI, 'quality', 1, 10, 1);
folder.add(gifAPI, 'record()');
let rec = folder.add(gifAPI, 'recording =', 0, this.track.duration).listen();
let conv = folder.add(gifAPI, 'converting =', 0, 100).listen();
rec.domElement.style.pointerEvents = 'none';
conv.domElement.style.pointerEvents = 'none';
}
setCamera(data) {
this.camera.fov = data.fov;
this.camera.position.set(...data.position);
}
setLight(data) {
this.directionalLight.position.set(...data.position);
this.directionalLight.shadow.bias = data.shadow.bias;
this.directionalLight.shadow.mapSize.width = data.shadow.mapSize.width;
this.directionalLight.shadow.mapSize.height = data.shadow.mapSize.width;
}
addURDF(data) {
this.setUrdf = (urdf) => {
super.urdf = urdf;
};
if (Object.prototype.toString.call(data.urdfPkgs) === '[object String]')
new THREE.FileLoader().load(data.urdfPkgs, pkgs => {
super.package = parseRosinstall(pkgs);
this.setUrdf(data.urdf); // once the packages are parsed
});
else {
let pkgs = data.urdfPkgs || [];
super.package = pkgs.join(', ');
this.setUrdf(data.urdf);
}
}
addAnimation(data) {
this.addEventListener('geometry-loaded', () => {
new THREE.FileLoader().load(data.animation, anim => {
// Load recorded robot motion into a Three.js clipAction
const fading = data.fading || 0.0;
const controlGUI = data.controlGUI | false;
const loopOnce = data.loopOnce | false;
this.mixer = new THREE.AnimationMixer(this.world);
this.track = THREE.AnimationClip.parse(JSON.parse(anim));
this.action = this.mixer.clipAction(this.track);
this.action.fadeIn(fading).play();
if (loopOnce) {
this.action.setLoop(THREE.LoopOnce);
this.action.clampWhenFinished = true;
}
if (this.gui == null) this.gui = new dat.GUI();
if (controlGUI) addControlGUI(this.gui, this.action, this.track, fading);
this.dispatchEvent(new CustomEvent('animation-loaded', { bubbles: true, cancelable: true, composed: true }));
this.renderLoop();
});
}, { once: true });
}
addGIFConverter(data) {
this.addEventListener('animation-loaded', () => {
this.canvas = document.getElementsByTagName('canvas')[0];
this.gif = new GIF({ // https://github.com/jnordberg/gif.js
repeat: data.repeate || 0, // 1 = no repeat, 0 = forever
workers: data.workers || 2,
workerScript: data.workerScript || 'gif.worker.js',
background: data.background || '#fff',
transparent: this.transparent || null, // hex color, 0x00FF00 = green
dither: data.dither || false, // e.g. FloydSteinberg-serpentine
debug: data.debug || false,
});
this.gif.on('finished', blob => window.open(URL.createObjectURL(blob)));
this.delay = data.delay || 500;
this.renderer.setClearColor(0xffffff); //white bg for recorded gif
this._addGIFConverterGUI();
});// TODO check if "once" is better
}
addWorld(data) {
const urdf = data.urdf;
const pkgs = data.packagesContainingMeshes.join(', ') || '';
this.urdfLoader.load(urdf, pkgs,
obj => {
obj.traverse(c => {
if (c.type === 'Mesh') {
c.castShadow = true;
c.receiveShadow = true;
}
});
this.scene.add(obj);
}
);
}
}
function addControlGUI(gui, action, track, fading) {
// modified code of https://threejs.org/examples/#webgl_animation_skinning_morph
let folder = gui.addFolder('Control ' + track.name),
API = {
'play()': () => action.play(),
'stop()': () => action.stop(),
'reset()': () => action.reset(),
get 'time ='() { return action !== null ? action.time : 0; },
set 'time ='(value) { action.time = value; },
get 'paused ='() { return action !== null && action.paused; },
set 'paused ='(value) { action.paused = value; },
get 'enabled ='() { return action !== null && action.enabled; },
'fade in()': () => action.reset().fadeIn(fading).play()
};
folder.add(API, 'play()');
folder.add(API, 'stop()');
folder.add(API, 'reset()');
folder.add(API, 'time =', 0, track.duration).listen();
folder.add(API, 'paused =').listen();
folder.add(API, 'fade in()');
}
function parseRosinstall(pkgs) {
function gitraw(uri) {
const base = 'https://raw.githubusercontent.com/';
const repo = uri.split('https://github.com/')[1].split('.git')[0];
return base + repo;
}
//Hardcoded for git
function g(obj, key) { return obj['git'][key]; }
let l = 'local-name', u = 'uri', v = 'version';
return jsyaml.load(pkgs).map(i => `${g(i, l)}: ${gitraw(g(i, u))}/${g(i, v)}/${g(i, l)}`);
}
customElements.define('urdf-viewer', AnimationWrapper);