1
- window . AnimationWrapper =
2
- class AnimationWrapper extends URDFViewer {
1
+ import * as THREE from 'three' ;
2
+ import URDFViewer from 'urdf-loader' ;
3
+
4
+ // import URDFViewer from 'urdf-loader/urdf-viewer-element.js';
5
+
6
+ export default
7
+ class AnimationWrapper extends URDFViewer {
3
8
4
9
constructor ( ) {
5
10
@@ -30,7 +35,7 @@ class AnimationWrapper extends URDFViewer {
30
35
31
36
_setRecorder ( quality , speed ) {
32
37
this . action . reset ( ) ;
33
- this . mixer . update ( this . clock . getDelta ( ) ) ; // Jump to first frame before record
38
+ this . mixer . update ( this . clock . getDelta ( ) ) ; // Jump to first frame before record
34
39
this . gif . abort ( ) ;
35
40
this . gif . width = null ;
36
41
this . gif . height = null ;
@@ -47,15 +52,15 @@ class AnimationWrapper extends URDFViewer {
47
52
_addGIFConverterGUI ( ) {
48
53
if ( this . gui == null ) this . gui = new dat . GUI ( ) ;
49
54
this . gif . on ( 'progress' , p => gifAPI [ 'converting =' ] = p * 100 ) ;
50
- this . mixer . addEventListener ( 'loop' , ( ) => {
55
+ this . mixer . addEventListener ( 'loop' , ( ) => {
51
56
if ( this . recording ) {
52
57
this . recording = false ;
53
58
this . action . paused = true ;
54
59
this . gif . render ( ) ;
55
60
}
56
61
} ) ;
57
62
this . updateRecordBar = ( ) => gifAPI [ 'recording =' ] = this . action . time ;
58
- let folder = this . gui . addFolder ( 'Record ' + this . track . name ) ,
63
+ let folder = this . gui . addFolder ( 'Record ' + this . track . name ) ,
59
64
gifAPI = {
60
65
'record()' : ( ) => {
61
66
this . _setRecorder ( gifAPI . quality , gifAPI . speed ) ;
@@ -68,7 +73,7 @@ class AnimationWrapper extends URDFViewer {
68
73
speed : 1 ,
69
74
quality : 10
70
75
} ;
71
-
76
+
72
77
folder . add ( gifAPI , 'speed' , 0.1 , 10 ) ;
73
78
folder . add ( gifAPI , 'quality' , 1 , 10 , 1 ) ;
74
79
folder . add ( gifAPI , 'record()' ) ;
@@ -95,14 +100,24 @@ class AnimationWrapper extends URDFViewer {
95
100
addURDF ( data ) {
96
101
97
102
super . urdf = data . urdf ;
98
- super . package = data . packagesContainingMeshes . join ( ', ' ) || '' ;
103
+
104
+ if ( Object . prototype . toString . call ( data . urdfPkgs ) === "[object String]" )
105
+
106
+ new THREE . FileLoader ( ) . load ( data . urdfPkgs , pkgs => {
107
+ super . package = parse_rosinstall ( pkgs )
108
+ } ) ;
109
+
110
+ else {
111
+ let pkgs = data . urdfPkgs || [ ] ;
112
+ super . package = pkgs . join ( ', ' ) ;
113
+ }
99
114
}
100
115
101
116
addAnimation ( data ) {
102
117
103
118
this . addEventListener ( 'geometry-loaded' , ( ) => {
104
119
105
- new THREE . FileLoader ( ) . load ( data . animation , anim => {
120
+ new THREE . FileLoader ( ) . load ( data . animation , anim => {
106
121
107
122
// Load recorded robot motion into a Three.js clipAction
108
123
const fading = data . fading || 0.0 ;
@@ -115,20 +130,20 @@ class AnimationWrapper extends URDFViewer {
115
130
//if (document.title == this.defaultTitle) document.title = this.track.name + ' animation';
116
131
117
132
this . action = this . mixer . clipAction ( this . track ) ;
118
- this . status = { recording : false } ;
133
+ this . status = { recording : false } ;
119
134
this . action . fadeIn ( fading ) . play ( ) ;
120
135
121
136
if ( this . gui == null ) this . gui = new dat . GUI ( ) ;
122
- if ( controlGUI ) { addControlGUI ( this . gui , this . action , this . track , fading ) ; }
137
+ if ( controlGUI ) { addControlGUI ( this . gui , this . action , this . track , fading ) ; }
123
138
124
- this . dispatchEvent ( new CustomEvent ( 'animation-loaded' , { bubbles : true , cancelable : true , composed : true } ) ) ;
139
+ this . dispatchEvent ( new CustomEvent ( 'animation-loaded' , { bubbles : true , cancelable : true , composed : true } ) ) ;
125
140
126
141
let animate = function ( ) {
127
142
requestAnimationFrame ( animate ) ;
128
- this . mixer . update ( this . clock . getDelta ( ) ) ;
143
+ this . mixer . update ( this . clock . getDelta ( ) ) ;
129
144
130
145
if ( this . recording ) {
131
- this . gif . addFrame ( this . canvas , { delay : this . delay , copy : true } ) ;
146
+ this . gif . addFrame ( this . canvas , { delay : this . delay , copy : true } ) ;
132
147
this . updateRecordBar ( ) ;
133
148
}
134
149
} . bind ( this ) ;
@@ -144,15 +159,15 @@ class AnimationWrapper extends URDFViewer {
144
159
this . canvas = document . getElementsByTagName ( 'canvas' ) [ 0 ] ;
145
160
146
161
this . gif = new GIF ( { // https://github.com/jnordberg/gif.js
147
- repeat : data . repeate || 0 , // 1 = no repeat, 0 = forever
148
- workers : data . workers || 2 ,
162
+ repeat : data . repeate || 0 , // 1 = no repeat, 0 = forever
163
+ workers : data . workers || 2 ,
149
164
workerScript : data . workerScript || 'gif.worker.js' ,
150
- background : data . background || '#fff' ,
165
+ background : data . background || '#fff' ,
151
166
//width: this.canvas.width,
152
167
//height: this.canvas.height,
153
- transparent : this . transparent || null , // hex color, 0x00FF00 = green
154
- dither : data . dither || false , // e.g. FloydSteinberg-serpentine
155
- debug : data . debug || false ,
168
+ transparent : this . transparent || null , // hex color, 0x00FF00 = green
169
+ dither : data . dither || false , // e.g. FloydSteinberg-serpentine
170
+ debug : data . debug || false ,
156
171
} ) ;
157
172
this . gif . on ( 'finished' , blob => window . open ( URL . createObjectURL ( blob ) ) ) ;
158
173
@@ -183,27 +198,43 @@ class AnimationWrapper extends URDFViewer {
183
198
} ;
184
199
185
200
186
- function addControlGUI ( gui , action , track , fading ) {
201
+ function addControlGUI ( gui , action , track , fading ) {
187
202
// modified code of https://threejs.org/examples/#webgl_animation_skinning_morph
188
- let folder = gui . addFolder ( 'Control ' + track . name ) ,
203
+ let folder = gui . addFolder ( 'Control ' + track . name ) ,
189
204
API = {
190
205
'play()' : ( ) => action . play ( ) ,
191
206
'stop()' : ( ) => action . stop ( ) ,
192
207
'reset()' : ( ) => action . reset ( ) ,
193
208
get 'time =' ( ) { return action !== null ? action . time : 0 ; } ,
194
- set 'time =' ( value ) { action . time = value ; } ,
209
+ set 'time =' ( value ) { action . time = value ; } ,
195
210
get 'paused =' ( ) { return action !== null && action . paused ; } ,
196
- set 'paused =' ( value ) { action . paused = value ; } ,
211
+ set 'paused =' ( value ) { action . paused = value ; } ,
197
212
get 'enabled =' ( ) { return action !== null && action . enabled ; } ,
198
- 'fade in()' : ( ) => action . reset ( ) . fadeIn ( fading ) . play ( )
213
+ 'fade in()' : ( ) => action . reset ( ) . fadeIn ( fading ) . play ( )
199
214
} ;
200
215
201
- folder . add ( API , 'play()' ) ;
202
- folder . add ( API , 'stop()' ) ;
203
- folder . add ( API , 'reset()' ) ;
204
- folder . add ( API , 'time =' , 0 , track . duration ) . listen ( ) ;
205
- folder . add ( API , 'paused =' ) . listen ( ) ;
206
- folder . add ( API , 'fade in()' ) ;
216
+ folder . add ( API , 'play()' ) ;
217
+ folder . add ( API , 'stop()' ) ;
218
+ folder . add ( API , 'reset()' ) ;
219
+ folder . add ( API , 'time =' , 0 , track . duration ) . listen ( ) ;
220
+ folder . add ( API , 'paused =' ) . listen ( ) ;
221
+ folder . add ( API , 'fade in()' ) ;
222
+ }
223
+
224
+ function parse_rosinstall ( pkgs ) {
225
+
226
+ function gitraw ( uri ) {
227
+ const base = "https://raw.githubusercontent.com/" ;
228
+ const repo = uri . split ( "https://github.com/" ) [ 1 ] . split ( ".git" ) [ 0 ] ;
229
+ return base + repo ;
230
+ }
231
+
232
+ //Hardcoded for git
233
+ function g ( obj , key ) { return obj [ "git" ] [ key ] }
234
+ let l = "local-name" , u = "uri" , v = "version" ;
235
+
236
+ return jsyaml . load ( pkgs ) . map ( i => `${ g ( i , l ) } : ${ gitraw ( g ( i , u ) ) } /${ g ( i , v ) } /${ g ( i , l ) } ` ) ;
237
+
207
238
}
208
239
209
240
customElements . define ( 'urdf-viewer' , AnimationWrapper ) ;
0 commit comments