urdf-loader-babylonjs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +592 -0
- package/package.json +53 -0
- package/src/URDFClasses.d.ts +80 -0
- package/src/URDFClasses.js +566 -0
- package/src/URDFDragControls.js +385 -0
- package/src/URDFLoader.d.ts +45 -0
- package/src/URDFLoader.js +769 -0
- package/src/urdf-manipulator-element.js +158 -0
- package/src/urdf-viewer-element.js +607 -0
- package/umd/URDFLoader.js +1337 -0
- package/umd/URDFLoader.js.map +1 -0
- package/umd/urdf-manipulator-element.js +548 -0
- package/umd/urdf-manipulator-element.js.map +1 -0
- package/umd/urdf-viewer-element.js +614 -0
- package/umd/urdf-viewer-element.js.map +1 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import { Engine, Scene, ArcRotateCamera, HemisphericLight, DirectionalLight, ShadowGenerator, MeshBuilder, StandardMaterial, Color3, Color4, Vector3, Quaternion, Mesh, Material, TransformNode } from '@babylonjs/core';
|
|
2
|
+
import URDFLoader from './URDFLoader.js';
|
|
3
|
+
|
|
4
|
+
// urdf-viewer element
|
|
5
|
+
// Loads and displays a 3D view of a URDF-formatted robot
|
|
6
|
+
|
|
7
|
+
// Events
|
|
8
|
+
// urdf-change: Fires when the URDF has finished loading and getting processed
|
|
9
|
+
// urdf-processed: Fires when the URDF has finished loading and getting processed
|
|
10
|
+
// geometry-loaded: Fires when all the geometry has been fully loaded
|
|
11
|
+
// ignore-limits-change: Fires when the 'ignore-limits' attribute changes
|
|
12
|
+
// angle-change: Fires when an angle changes
|
|
13
|
+
export default
|
|
14
|
+
class URDFViewer extends HTMLElement {
|
|
15
|
+
|
|
16
|
+
static get observedAttributes() {
|
|
17
|
+
|
|
18
|
+
return ['package', 'urdf', 'up', 'display-shadow', 'ambient-color', 'ignore-limits', 'show-collision'];
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get package() { return this.getAttribute('package') || ''; }
|
|
23
|
+
set package(val) { this.setAttribute('package', val); }
|
|
24
|
+
|
|
25
|
+
get urdf() { return this.getAttribute('urdf') || ''; }
|
|
26
|
+
set urdf(val) { this.setAttribute('urdf', val); }
|
|
27
|
+
|
|
28
|
+
get ignoreLimits() { return this.hasAttribute('ignore-limits') || false; }
|
|
29
|
+
set ignoreLimits(val) { val ? this.setAttribute('ignore-limits', val) : this.removeAttribute('ignore-limits'); }
|
|
30
|
+
|
|
31
|
+
get up() { return this.getAttribute('up') || '+Z'; }
|
|
32
|
+
set up(val) { this.setAttribute('up', val); }
|
|
33
|
+
|
|
34
|
+
get displayShadow() { return this.hasAttribute('display-shadow') || false; }
|
|
35
|
+
set displayShadow(val) { val ? this.setAttribute('display-shadow', '') : this.removeAttribute('display-shadow'); }
|
|
36
|
+
|
|
37
|
+
get ambientColor() { return this.getAttribute('ambient-color') || '#8ea0a8'; }
|
|
38
|
+
set ambientColor(val) { val ? this.setAttribute('ambient-color', val) : this.removeAttribute('ambient-color'); }
|
|
39
|
+
|
|
40
|
+
get autoRedraw() { return this.hasAttribute('auto-redraw') || false; }
|
|
41
|
+
set autoRedraw(val) { val ? this.setAttribute('auto-redraw', true) : this.removeAttribute('auto-redraw'); }
|
|
42
|
+
|
|
43
|
+
get noAutoRecenter() { return this.hasAttribute('no-auto-recenter') || false; }
|
|
44
|
+
set noAutoRecenter(val) { val ? this.setAttribute('no-auto-recenter', true) : this.removeAttribute('no-auto-recenter'); }
|
|
45
|
+
|
|
46
|
+
get showCollision() { return this.hasAttribute('show-collision') || false; }
|
|
47
|
+
set showCollision(val) { val ? this.setAttribute('show-collision', true) : this.removeAttribute('show-collision'); }
|
|
48
|
+
|
|
49
|
+
get jointValues() {
|
|
50
|
+
|
|
51
|
+
const values = {};
|
|
52
|
+
if (this.robot) {
|
|
53
|
+
|
|
54
|
+
for (const name in this.robot.joints) {
|
|
55
|
+
|
|
56
|
+
const joint = this.robot.joints[name];
|
|
57
|
+
values[name] = joint.jointValue.length === 1 ? joint.angle : [...joint.jointValue];
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return values;
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
set jointValues(val) { this.setJointValues(val); }
|
|
67
|
+
|
|
68
|
+
get angles() {
|
|
69
|
+
|
|
70
|
+
return this.jointValues;
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
set angles(v) {
|
|
74
|
+
|
|
75
|
+
this.jointValues = v;
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* Lifecycle Functions */
|
|
80
|
+
constructor() {
|
|
81
|
+
|
|
82
|
+
super();
|
|
83
|
+
|
|
84
|
+
this._requestId = 0;
|
|
85
|
+
this._dirty = false;
|
|
86
|
+
this._loadScheduled = false;
|
|
87
|
+
this.robot = null;
|
|
88
|
+
this.loadMeshFunc = null;
|
|
89
|
+
this.urlModifierFunc = null;
|
|
90
|
+
|
|
91
|
+
// Create a canvas element for Babylon.js
|
|
92
|
+
const canvas = document.createElement('canvas');
|
|
93
|
+
this._canvas = canvas;
|
|
94
|
+
|
|
95
|
+
// Engine setup
|
|
96
|
+
const engine = new Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
|
|
97
|
+
this.engine = engine;
|
|
98
|
+
|
|
99
|
+
// Scene setup
|
|
100
|
+
const scene = new Scene(engine);
|
|
101
|
+
scene.useRightHandedSystem = true;
|
|
102
|
+
scene.clearColor = new Color4(0, 0, 0, 0);
|
|
103
|
+
|
|
104
|
+
// Ambient light
|
|
105
|
+
const ambientLight = new HemisphericLight('ambientLight', new Vector3(0, 1, 0), scene);
|
|
106
|
+
const c3 = this._parseColor(this.ambientColor);
|
|
107
|
+
ambientLight.diffuse = c3;
|
|
108
|
+
ambientLight.groundColor = Color3.Lerp(Color3.Black(), c3, 0.5);
|
|
109
|
+
ambientLight.intensity = 0.5;
|
|
110
|
+
|
|
111
|
+
// Directional light
|
|
112
|
+
const dirLight = new DirectionalLight('dirLight', new Vector3(-4, -10, -1), scene);
|
|
113
|
+
dirLight.intensity = Math.PI;
|
|
114
|
+
dirLight.position = new Vector3(4, 10, 1);
|
|
115
|
+
|
|
116
|
+
// Shadow generator
|
|
117
|
+
this._shadowGenerator = new ShadowGenerator(2048, dirLight);
|
|
118
|
+
this._shadowGenerator.useBlurExponentialShadowMap = true;
|
|
119
|
+
this._shadowGenerator.bias = 0.001;
|
|
120
|
+
|
|
121
|
+
// Camera setup (ArcRotateCamera has built-in orbit controls)
|
|
122
|
+
const camera = new ArcRotateCamera('camera', -Math.PI / 2, Math.PI / 3, 10, Vector3.Zero(), scene);
|
|
123
|
+
camera.minZ = 0.1;
|
|
124
|
+
camera.maxZ = 1000;
|
|
125
|
+
camera.lowerRadiusLimit = 0.25;
|
|
126
|
+
camera.upperRadiusLimit = 50;
|
|
127
|
+
camera.wheelPrecision = 5;
|
|
128
|
+
camera.panningSensibility = 50;
|
|
129
|
+
camera.attachControl(canvas, true);
|
|
130
|
+
|
|
131
|
+
// World node for up-axis rotation
|
|
132
|
+
const world = new TransformNode('world', scene);
|
|
133
|
+
world.rotationQuaternion = new Quaternion();
|
|
134
|
+
|
|
135
|
+
// Ground plane for shadows
|
|
136
|
+
const ground = MeshBuilder.CreateGround('ground', { width: 400, height: 400 }, scene);
|
|
137
|
+
const groundMaterial = new StandardMaterial('groundMat', scene);
|
|
138
|
+
groundMaterial.diffuseColor = Color3.Black();
|
|
139
|
+
groundMaterial.specularColor = Color3.Black();
|
|
140
|
+
groundMaterial.alpha = 0.25;
|
|
141
|
+
ground.material = groundMaterial;
|
|
142
|
+
ground.receiveShadows = true;
|
|
143
|
+
ground.position.y = -0.5;
|
|
144
|
+
ground.isPickable = false;
|
|
145
|
+
|
|
146
|
+
this.scene = scene;
|
|
147
|
+
this.babylonScene = scene;
|
|
148
|
+
this.world = world;
|
|
149
|
+
this.camera = camera;
|
|
150
|
+
this.controls = camera; // camera is also the controls in Babylon.js
|
|
151
|
+
this.ground = ground;
|
|
152
|
+
this.directionalLight = dirLight;
|
|
153
|
+
this.ambientLight = ambientLight;
|
|
154
|
+
|
|
155
|
+
this._setUp(this.up);
|
|
156
|
+
|
|
157
|
+
this._collisionMaterial = new StandardMaterial('collisionMat', scene);
|
|
158
|
+
this._collisionMaterial.diffuseColor = new Color3(1.0, 0.745, 0.22);
|
|
159
|
+
this._collisionMaterial.alpha = 0.35;
|
|
160
|
+
this._collisionMaterial.transparencyMode = Material.MATERIAL_ALPHABLEND;
|
|
161
|
+
|
|
162
|
+
// Render loop
|
|
163
|
+
engine.runRenderLoop(() => {
|
|
164
|
+
|
|
165
|
+
if (this.parentNode) {
|
|
166
|
+
|
|
167
|
+
this.updateSize();
|
|
168
|
+
|
|
169
|
+
if (this._dirty || this.autoRedraw) {
|
|
170
|
+
|
|
171
|
+
if (!this.noAutoRecenter) {
|
|
172
|
+
|
|
173
|
+
this._updateEnvironment();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
this._dirty = false;
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
scene.render();
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
_parseColor(colorStr) {
|
|
189
|
+
|
|
190
|
+
if (!colorStr) return new Color3(0.56, 0.63, 0.66);
|
|
191
|
+
try {
|
|
192
|
+
|
|
193
|
+
// Expand shorthand hex
|
|
194
|
+
if (colorStr.length === 4) {
|
|
195
|
+
|
|
196
|
+
colorStr = '#' + colorStr[1] + colorStr[1] + colorStr[2] + colorStr[2] + colorStr[3] + colorStr[3];
|
|
197
|
+
|
|
198
|
+
}
|
|
199
|
+
return Color3.FromHexString(colorStr);
|
|
200
|
+
|
|
201
|
+
} catch {
|
|
202
|
+
|
|
203
|
+
return new Color3(0.56, 0.63, 0.66);
|
|
204
|
+
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
connectedCallback() {
|
|
210
|
+
|
|
211
|
+
// Add our initialize styles for the element if they haven't
|
|
212
|
+
// been added yet
|
|
213
|
+
if (!this.constructor._styletag) {
|
|
214
|
+
|
|
215
|
+
const styletag = document.createElement('style');
|
|
216
|
+
styletag.innerHTML =
|
|
217
|
+
`
|
|
218
|
+
${ this.tagName } { display: block; }
|
|
219
|
+
${ this.tagName } canvas {
|
|
220
|
+
width: 100%;
|
|
221
|
+
height: 100%;
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
document.head.appendChild(styletag);
|
|
225
|
+
this.constructor._styletag = styletag;
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// add the canvas
|
|
230
|
+
if (this.childElementCount === 0) {
|
|
231
|
+
|
|
232
|
+
this.appendChild(this._canvas);
|
|
233
|
+
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this.updateSize();
|
|
237
|
+
requestAnimationFrame(() => this.updateSize());
|
|
238
|
+
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
disconnectedCallback() {
|
|
242
|
+
|
|
243
|
+
this.engine.stopRenderLoop();
|
|
244
|
+
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
attributeChangedCallback(attr, oldval, newval) {
|
|
248
|
+
|
|
249
|
+
this._updateCollisionVisibility();
|
|
250
|
+
if (!this.noAutoRecenter) {
|
|
251
|
+
this.recenter();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
switch (attr) {
|
|
255
|
+
|
|
256
|
+
case 'package':
|
|
257
|
+
case 'urdf': {
|
|
258
|
+
|
|
259
|
+
this._scheduleLoad();
|
|
260
|
+
break;
|
|
261
|
+
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
case 'up': {
|
|
265
|
+
|
|
266
|
+
this._setUp(this.up);
|
|
267
|
+
break;
|
|
268
|
+
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
case 'ambient-color': {
|
|
272
|
+
|
|
273
|
+
const c3 = this._parseColor(this.ambientColor);
|
|
274
|
+
this.ambientLight.diffuse = c3;
|
|
275
|
+
this.ambientLight.groundColor = Color3.Lerp(Color3.Black(), c3, 0.5);
|
|
276
|
+
break;
|
|
277
|
+
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
case 'ignore-limits': {
|
|
281
|
+
|
|
282
|
+
this._setIgnoreLimits(this.ignoreLimits, true);
|
|
283
|
+
break;
|
|
284
|
+
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* Public API */
|
|
292
|
+
updateSize() {
|
|
293
|
+
|
|
294
|
+
const w = this.clientWidth;
|
|
295
|
+
const h = this.clientHeight;
|
|
296
|
+
|
|
297
|
+
if (w > 0 && h > 0) {
|
|
298
|
+
|
|
299
|
+
this._canvas.width = w * window.devicePixelRatio;
|
|
300
|
+
this._canvas.height = h * window.devicePixelRatio;
|
|
301
|
+
this.engine.resize();
|
|
302
|
+
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
redraw() {
|
|
308
|
+
|
|
309
|
+
this._dirty = true;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
recenter() {
|
|
313
|
+
|
|
314
|
+
this._updateEnvironment();
|
|
315
|
+
this.redraw();
|
|
316
|
+
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Set the joint with jointName to
|
|
320
|
+
// angle in degrees
|
|
321
|
+
setJointValue(jointName, ...values) {
|
|
322
|
+
|
|
323
|
+
if (!this.robot) return;
|
|
324
|
+
if (!this.robot.joints[jointName]) return;
|
|
325
|
+
|
|
326
|
+
if (this.robot.joints[jointName].setJointValue(...values)) {
|
|
327
|
+
|
|
328
|
+
this.redraw();
|
|
329
|
+
this.dispatchEvent(new CustomEvent('angle-change', { bubbles: true, cancelable: true, detail: jointName }));
|
|
330
|
+
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
setJointValues(values) {
|
|
336
|
+
|
|
337
|
+
for (const name in values) {
|
|
338
|
+
|
|
339
|
+
if (Array.isArray(values[name])) {
|
|
340
|
+
|
|
341
|
+
this.setJointValue(name, ...values[name]);
|
|
342
|
+
|
|
343
|
+
} else {
|
|
344
|
+
|
|
345
|
+
this.setJointValue(name, values[name]);
|
|
346
|
+
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/* Private Functions */
|
|
354
|
+
_updateEnvironment() {
|
|
355
|
+
|
|
356
|
+
const robot = this.robot;
|
|
357
|
+
if (!robot) return;
|
|
358
|
+
|
|
359
|
+
// Compute bounding info from visual meshes
|
|
360
|
+
let min = new Vector3(Infinity, Infinity, Infinity);
|
|
361
|
+
let max = new Vector3(-Infinity, -Infinity, -Infinity);
|
|
362
|
+
|
|
363
|
+
const processNode = (node) => {
|
|
364
|
+
|
|
365
|
+
if (node.getBoundingInfo && node instanceof Mesh && node.getTotalVertices() > 0) {
|
|
366
|
+
|
|
367
|
+
node.computeWorldMatrix(true);
|
|
368
|
+
const bi = node.getBoundingInfo();
|
|
369
|
+
min = Vector3.Minimize(min, bi.boundingBox.minimumWorld);
|
|
370
|
+
max = Vector3.Maximize(max, bi.boundingBox.maximumWorld);
|
|
371
|
+
|
|
372
|
+
}
|
|
373
|
+
if (node.getChildren) {
|
|
374
|
+
|
|
375
|
+
node.getChildren().forEach(processNode);
|
|
376
|
+
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
robot.traverse(c => {
|
|
382
|
+
if (c.isURDFVisual) {
|
|
383
|
+
|
|
384
|
+
processNode(c);
|
|
385
|
+
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
if (min.x === Infinity) return;
|
|
390
|
+
|
|
391
|
+
const center = Vector3.Center(min, max);
|
|
392
|
+
this.camera.target.y = center.y;
|
|
393
|
+
this.ground.position.y = min.y - 1e-3;
|
|
394
|
+
|
|
395
|
+
const dirLight = this.directionalLight;
|
|
396
|
+
|
|
397
|
+
if (this.displayShadow) {
|
|
398
|
+
|
|
399
|
+
const radius = Vector3.Distance(min, max) / 2;
|
|
400
|
+
dirLight.shadowMinZ = -radius * 3;
|
|
401
|
+
dirLight.shadowMaxZ = radius * 3;
|
|
402
|
+
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
_scheduleLoad() {
|
|
408
|
+
|
|
409
|
+
// if our current model is already what's being requested
|
|
410
|
+
// or has been loaded then early out
|
|
411
|
+
if (this._prevload === `${ this.package }|${ this.urdf }`) return;
|
|
412
|
+
this._prevload = `${ this.package }|${ this.urdf }`;
|
|
413
|
+
|
|
414
|
+
// if we're already waiting on a load then early out
|
|
415
|
+
if (this._loadScheduled) return;
|
|
416
|
+
this._loadScheduled = true;
|
|
417
|
+
|
|
418
|
+
if (this.robot) {
|
|
419
|
+
|
|
420
|
+
this.robot.traverse(c => c.dispose && c.dispose());
|
|
421
|
+
this.robot = null;
|
|
422
|
+
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
requestAnimationFrame(() => {
|
|
426
|
+
|
|
427
|
+
this._loadUrdf(this.package, this.urdf);
|
|
428
|
+
this._loadScheduled = false;
|
|
429
|
+
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Watch the package and urdf field and load the robot model.
|
|
435
|
+
// This should _only_ be called from _scheduleLoad because that
|
|
436
|
+
// ensures the that current robot has been removed
|
|
437
|
+
_loadUrdf(pkg, urdf) {
|
|
438
|
+
|
|
439
|
+
this.dispatchEvent(new CustomEvent('urdf-change', { bubbles: true, cancelable: true, composed: true }));
|
|
440
|
+
|
|
441
|
+
if (urdf) {
|
|
442
|
+
|
|
443
|
+
// Keep track of this request and make
|
|
444
|
+
// sure it doesn't get overwritten by
|
|
445
|
+
// a subsequent one
|
|
446
|
+
this._requestId++;
|
|
447
|
+
const requestId = this._requestId;
|
|
448
|
+
|
|
449
|
+
const updateMaterials = mesh => {
|
|
450
|
+
|
|
451
|
+
mesh.traverse(c => {
|
|
452
|
+
|
|
453
|
+
if (c instanceof Mesh) {
|
|
454
|
+
|
|
455
|
+
// Add to shadow generator
|
|
456
|
+
this._shadowGenerator.addShadowCaster(c);
|
|
457
|
+
c.receiveShadows = true;
|
|
458
|
+
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
if (pkg.includes(':') && (pkg.split(':')[1].substring(0, 2)) !== '//') {
|
|
466
|
+
|
|
467
|
+
pkg = pkg.split(',').reduce((map, value) => {
|
|
468
|
+
|
|
469
|
+
const split = value.split(/:/).filter(x => !!x);
|
|
470
|
+
const pkgName = split.shift().trim();
|
|
471
|
+
const pkgPath = split.join(':').trim();
|
|
472
|
+
map[pkgName] = pkgPath;
|
|
473
|
+
|
|
474
|
+
return map;
|
|
475
|
+
|
|
476
|
+
}, {});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
let robot = null;
|
|
480
|
+
const loader = new URDFLoader(this.scene);
|
|
481
|
+
loader.packages = pkg;
|
|
482
|
+
if (this.loadMeshFunc) {
|
|
483
|
+
|
|
484
|
+
loader.loadMeshCb = this.loadMeshFunc;
|
|
485
|
+
|
|
486
|
+
}
|
|
487
|
+
loader.fetchOptions = { mode: 'cors', credentials: 'same-origin' };
|
|
488
|
+
loader.parseCollision = true;
|
|
489
|
+
|
|
490
|
+
loader.manager.onLoad = () => {
|
|
491
|
+
|
|
492
|
+
// If another request has come in to load a new
|
|
493
|
+
// robot, then ignore this one
|
|
494
|
+
if (this._requestId !== requestId) {
|
|
495
|
+
|
|
496
|
+
if (robot) robot.traverse(c => c.dispose && c.dispose());
|
|
497
|
+
return;
|
|
498
|
+
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
this.robot = robot;
|
|
502
|
+
robot.parent = this.world;
|
|
503
|
+
updateMaterials(robot);
|
|
504
|
+
|
|
505
|
+
this._setIgnoreLimits(this.ignoreLimits);
|
|
506
|
+
this._updateCollisionVisibility();
|
|
507
|
+
|
|
508
|
+
this.dispatchEvent(new CustomEvent('urdf-processed', { bubbles: true, cancelable: true, composed: true }));
|
|
509
|
+
this.dispatchEvent(new CustomEvent('geometry-loaded', { bubbles: true, cancelable: true, composed: true }));
|
|
510
|
+
|
|
511
|
+
this.recenter();
|
|
512
|
+
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
loader.load(urdf, model => robot = model);
|
|
516
|
+
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
_updateCollisionVisibility() {
|
|
522
|
+
|
|
523
|
+
const showCollision = this.showCollision;
|
|
524
|
+
const collisionMaterial = this._collisionMaterial;
|
|
525
|
+
const robot = this.robot;
|
|
526
|
+
|
|
527
|
+
if (robot === null) return;
|
|
528
|
+
|
|
529
|
+
const colliders = [];
|
|
530
|
+
robot.traverse(c => {
|
|
531
|
+
|
|
532
|
+
if (c.isURDFCollider) {
|
|
533
|
+
|
|
534
|
+
c.setEnabled(showCollision);
|
|
535
|
+
colliders.push(c);
|
|
536
|
+
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
colliders.forEach(coll => {
|
|
542
|
+
|
|
543
|
+
coll.traverse(c => {
|
|
544
|
+
|
|
545
|
+
if (c instanceof Mesh) {
|
|
546
|
+
|
|
547
|
+
c.isPickable = false;
|
|
548
|
+
c.material = collisionMaterial;
|
|
549
|
+
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Watch the coordinate frame and update the
|
|
559
|
+
// rotation of the scene to match
|
|
560
|
+
_setUp(up) {
|
|
561
|
+
|
|
562
|
+
if (!up) up = '+Z';
|
|
563
|
+
up = up.toUpperCase();
|
|
564
|
+
const sign = up.replace(/[^-+]/g, '')[0] || '+';
|
|
565
|
+
const axis = up.replace(/[^XYZ]/gi, '')[0] || 'Z';
|
|
566
|
+
|
|
567
|
+
if (!this.world) return;
|
|
568
|
+
|
|
569
|
+
const PI = Math.PI;
|
|
570
|
+
const HALFPI = PI / 2;
|
|
571
|
+
|
|
572
|
+
let rx = 0, rz = 0;
|
|
573
|
+
const ry = 0;
|
|
574
|
+
if (axis === 'X') { rz = sign === '+' ? HALFPI : -HALFPI; }
|
|
575
|
+
if (axis === 'Z') { rx = sign === '+' ? -HALFPI : HALFPI; }
|
|
576
|
+
if (axis === 'Y') { rx = sign === '+' ? 0 : PI; }
|
|
577
|
+
|
|
578
|
+
this.world.rotationQuaternion = Quaternion.RotationYawPitchRoll(ry, rx, rz);
|
|
579
|
+
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// Updates the current robot's angles to ignore
|
|
583
|
+
// joint limits or not
|
|
584
|
+
_setIgnoreLimits(ignore, dispatch = false) {
|
|
585
|
+
|
|
586
|
+
if (this.robot) {
|
|
587
|
+
|
|
588
|
+
Object
|
|
589
|
+
.values(this.robot.joints)
|
|
590
|
+
.forEach(joint => {
|
|
591
|
+
|
|
592
|
+
joint.ignoreLimits = ignore;
|
|
593
|
+
joint.setJointValue(...joint.jointValue);
|
|
594
|
+
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (dispatch) {
|
|
600
|
+
|
|
601
|
+
this.dispatchEvent(new CustomEvent('ignore-limits-change', { bubbles: true, cancelable: true, composed: true }));
|
|
602
|
+
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
}
|