@thanhthbm/scene-editor 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 +169 -0
- package/dist/index.css +921 -0
- package/dist/index.js +14076 -0
- package/package.json +49 -0
- package/src/editor/css/main.css +1030 -0
- package/src/editor/js/Animation.js +618 -0
- package/src/editor/js/AnimationResizer.js +73 -0
- package/src/editor/js/Command.js +40 -0
- package/src/editor/js/Config.js +86 -0
- package/src/editor/js/Editor.js +1006 -0
- package/src/editor/js/EditorControls.js +489 -0
- package/src/editor/js/GLTFImportDialog.js +112 -0
- package/src/editor/js/History.js +321 -0
- package/src/editor/js/Loader.js +1162 -0
- package/src/editor/js/LoaderUtils.js +90 -0
- package/src/editor/js/Menubar.Add.js +559 -0
- package/src/editor/js/Menubar.Edit.js +155 -0
- package/src/editor/js/Menubar.File.js +585 -0
- package/src/editor/js/Menubar.Help.js +73 -0
- package/src/editor/js/Menubar.Render.js +858 -0
- package/src/editor/js/Menubar.Status.js +51 -0
- package/src/editor/js/Menubar.View.js +193 -0
- package/src/editor/js/Menubar.js +29 -0
- package/src/editor/js/Player.js +53 -0
- package/src/editor/js/Resizer.js +65 -0
- package/src/editor/js/Script.js +532 -0
- package/src/editor/js/Selector.js +129 -0
- package/src/editor/js/Sidebar.Geometry.BoxGeometry.js +121 -0
- package/src/editor/js/Sidebar.Geometry.BufferGeometry.js +124 -0
- package/src/editor/js/Sidebar.Geometry.CapsuleGeometry.js +109 -0
- package/src/editor/js/Sidebar.Geometry.CircleGeometry.js +97 -0
- package/src/editor/js/Sidebar.Geometry.CylinderGeometry.js +121 -0
- package/src/editor/js/Sidebar.Geometry.DodecahedronGeometry.js +73 -0
- package/src/editor/js/Sidebar.Geometry.ExtrudeGeometry.js +196 -0
- package/src/editor/js/Sidebar.Geometry.IcosahedronGeometry.js +73 -0
- package/src/editor/js/Sidebar.Geometry.LatheGeometry.js +98 -0
- package/src/editor/js/Sidebar.Geometry.Modifiers.js +73 -0
- package/src/editor/js/Sidebar.Geometry.OctahedronGeometry.js +74 -0
- package/src/editor/js/Sidebar.Geometry.PlaneGeometry.js +97 -0
- package/src/editor/js/Sidebar.Geometry.RingGeometry.js +121 -0
- package/src/editor/js/Sidebar.Geometry.ShapeGeometry.js +76 -0
- package/src/editor/js/Sidebar.Geometry.SphereGeometry.js +133 -0
- package/src/editor/js/Sidebar.Geometry.TetrahedronGeometry.js +74 -0
- package/src/editor/js/Sidebar.Geometry.TextGeometry.js +136 -0
- package/src/editor/js/Sidebar.Geometry.TorusGeometry.js +109 -0
- package/src/editor/js/Sidebar.Geometry.TorusKnotGeometry.js +121 -0
- package/src/editor/js/Sidebar.Geometry.TubeGeometry.js +135 -0
- package/src/editor/js/Sidebar.Geometry.js +471 -0
- package/src/editor/js/Sidebar.Material.BooleanProperty.js +60 -0
- package/src/editor/js/Sidebar.Material.ColorProperty.js +87 -0
- package/src/editor/js/Sidebar.Material.ConstantProperty.js +62 -0
- package/src/editor/js/Sidebar.Material.MapProperty.js +277 -0
- package/src/editor/js/Sidebar.Material.NumberProperty.js +60 -0
- package/src/editor/js/Sidebar.Material.Program.js +73 -0
- package/src/editor/js/Sidebar.Material.RangeValueProperty.js +63 -0
- package/src/editor/js/Sidebar.Material.js +751 -0
- package/src/editor/js/Sidebar.Object.js +892 -0
- package/src/editor/js/Sidebar.Project.App.js +218 -0
- package/src/editor/js/Sidebar.Project.Materials.js +82 -0
- package/src/editor/js/Sidebar.Project.Renderer.js +193 -0
- package/src/editor/js/Sidebar.Project.Resources.js +242 -0
- package/src/editor/js/Sidebar.Project.js +21 -0
- package/src/editor/js/Sidebar.Properties.js +73 -0
- package/src/editor/js/Sidebar.Scene.js +643 -0
- package/src/editor/js/Sidebar.Script.js +129 -0
- package/src/editor/js/Sidebar.Settings.History.js +146 -0
- package/src/editor/js/Sidebar.Settings.Shortcuts.js +198 -0
- package/src/editor/js/Sidebar.Settings.js +58 -0
- package/src/editor/js/Sidebar.js +41 -0
- package/src/editor/js/Storage.js +98 -0
- package/src/editor/js/Strings.js +2718 -0
- package/src/editor/js/TextureParametersDialog.js +293 -0
- package/src/editor/js/Toolbar.js +75 -0
- package/src/editor/js/Viewport.Controls.js +98 -0
- package/src/editor/js/Viewport.Info.js +144 -0
- package/src/editor/js/Viewport.Pathtracer.js +27 -0
- package/src/editor/js/Viewport.ViewHelper.js +41 -0
- package/src/editor/js/Viewport.XR.js +7 -0
- package/src/editor/js/Viewport.js +982 -0
- package/src/editor/js/commands/AddObjectCommand.js +68 -0
- package/src/editor/js/commands/AddScriptCommand.js +75 -0
- package/src/editor/js/commands/Commands.js +24 -0
- package/src/editor/js/commands/MoveObjectCommand.js +117 -0
- package/src/editor/js/commands/MultiCmdsCommand.js +85 -0
- package/src/editor/js/commands/RemoveObjectCommand.js +88 -0
- package/src/editor/js/commands/RemoveScriptCommand.js +81 -0
- package/src/editor/js/commands/SetColorCommand.js +73 -0
- package/src/editor/js/commands/SetGeometryCommand.js +86 -0
- package/src/editor/js/commands/SetGeometryValueCommand.js +70 -0
- package/src/editor/js/commands/SetMaterialColorCommand.js +87 -0
- package/src/editor/js/commands/SetMaterialCommand.js +80 -0
- package/src/editor/js/commands/SetMaterialMapCommand.js +144 -0
- package/src/editor/js/commands/SetMaterialRangeCommand.js +92 -0
- package/src/editor/js/commands/SetMaterialValueCommand.js +91 -0
- package/src/editor/js/commands/SetMaterialVectorCommand.js +88 -0
- package/src/editor/js/commands/SetPositionCommand.js +84 -0
- package/src/editor/js/commands/SetRotationCommand.js +84 -0
- package/src/editor/js/commands/SetScaleCommand.js +84 -0
- package/src/editor/js/commands/SetSceneCommand.js +104 -0
- package/src/editor/js/commands/SetScriptValueCommand.js +80 -0
- package/src/editor/js/commands/SetShadowValueCommand.js +73 -0
- package/src/editor/js/commands/SetTextureParametersCommand.js +143 -0
- package/src/editor/js/commands/SetUuidCommand.js +70 -0
- package/src/editor/js/commands/SetValueCommand.js +75 -0
- package/src/editor/js/libs/acorn/acorn.js +3236 -0
- package/src/editor/js/libs/acorn/acorn_loose.js +1299 -0
- package/src/editor/js/libs/acorn/walk.js +344 -0
- package/src/editor/js/libs/app/index.html +51 -0
- package/src/editor/js/libs/app.js +14 -0
- package/src/editor/js/libs/codemirror/addon/dialog.css +32 -0
- package/src/editor/js/libs/codemirror/addon/dialog.js +163 -0
- package/src/editor/js/libs/codemirror/addon/show-hint.css +36 -0
- package/src/editor/js/libs/codemirror/addon/show-hint.js +529 -0
- package/src/editor/js/libs/codemirror/addon/tern.css +87 -0
- package/src/editor/js/libs/codemirror/addon/tern.js +750 -0
- package/src/editor/js/libs/codemirror/codemirror.css +344 -0
- package/src/editor/js/libs/codemirror/codemirror.js +9849 -0
- package/src/editor/js/libs/codemirror/mode/glsl.js +233 -0
- package/src/editor/js/libs/codemirror/mode/javascript.js +959 -0
- package/src/editor/js/libs/codemirror/theme/monokai.css +41 -0
- package/src/editor/js/libs/esprima.js +6401 -0
- package/src/editor/js/libs/jsonlint.js +453 -0
- package/src/editor/js/libs/signals.min.js +14 -0
- package/src/editor/js/libs/tern-threejs/build-defs.js +233 -0
- package/src/editor/js/libs/ternjs/comment.js +87 -0
- package/src/editor/js/libs/ternjs/def.js +588 -0
- package/src/editor/js/libs/ternjs/doc_comment.js +401 -0
- package/src/editor/js/libs/ternjs/infer.js +1635 -0
- package/src/editor/js/libs/ternjs/polyfill.js +80 -0
- package/src/editor/js/libs/ternjs/signal.js +26 -0
- package/src/editor/js/libs/ternjs/tern.js +993 -0
- package/src/editor/js/libs/ui.js +1305 -0
- package/src/editor/js/libs/ui.three.js +946 -0
- package/src/events.js +273 -0
- package/src/index.js +284 -0
- package/src/presets/machines.js +239 -0
package/src/events.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_STATUS_COLORS = {
|
|
4
|
+
run: 0x2f9e6b,
|
|
5
|
+
idle: 0xe0a800,
|
|
6
|
+
err: 0xc0392b,
|
|
7
|
+
stop: 0xe0a800,
|
|
8
|
+
error: 0xc0392b,
|
|
9
|
+
off: 0x64748b
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function applyUserData( object, payload ) {
|
|
13
|
+
|
|
14
|
+
const ud = object.userData || ( object.userData = {} );
|
|
15
|
+
for ( const key in payload ) {
|
|
16
|
+
|
|
17
|
+
if ( key === 'uuid' || key === 'topic' ) continue;
|
|
18
|
+
ud[ key ] = payload[ key ];
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function applyBeacon( object, status, statusColors ) {
|
|
25
|
+
|
|
26
|
+
const hex = statusColors && statusColors[ status ];
|
|
27
|
+
if ( hex === undefined ) return;
|
|
28
|
+
const color = new THREE.Color( hex );
|
|
29
|
+
object.traverse( ( child ) => {
|
|
30
|
+
|
|
31
|
+
if ( child.userData && child.userData.role === 'beacon' && child.material ) {
|
|
32
|
+
|
|
33
|
+
child.material.color.copy( color );
|
|
34
|
+
child.material.emissive.copy( color );
|
|
35
|
+
child.material.emissiveIntensity = 0.85;
|
|
36
|
+
child.material.needsUpdate = true;
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
} );
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function defaultHandler( editor ) {
|
|
45
|
+
|
|
46
|
+
return function ( object, payload ) {
|
|
47
|
+
|
|
48
|
+
applyUserData( object, payload );
|
|
49
|
+
if ( payload && payload.status !== undefined ) {
|
|
50
|
+
|
|
51
|
+
applyBeacon( object, payload.status, DEFAULT_STATUS_COLORS );
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
editor.signals.objectChanged.dispatch( object );
|
|
55
|
+
editor.signals.sceneGraphChanged.dispatch();
|
|
56
|
+
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function EventBridge( editor ) {
|
|
62
|
+
|
|
63
|
+
this.editor = editor;
|
|
64
|
+
this._bindings = new Map(); // uuid -> binding
|
|
65
|
+
this._ws = null;
|
|
66
|
+
this._wsUrl = null;
|
|
67
|
+
this._onWsMessage = null;
|
|
68
|
+
this._onWsClose = null;
|
|
69
|
+
this._reconnectMs = 0;
|
|
70
|
+
this._reconnectTimer = null;
|
|
71
|
+
this._closedByUser = false;
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
EventBridge.prototype = {
|
|
76
|
+
|
|
77
|
+
constructor: EventBridge,
|
|
78
|
+
|
|
79
|
+
bind: function ( object, options = {} ) {
|
|
80
|
+
|
|
81
|
+
if ( ! object || ! object.uuid ) return this;
|
|
82
|
+
this._bindings.set( object.uuid, {
|
|
83
|
+
object,
|
|
84
|
+
topic: options.topic || object.userData.topic || null,
|
|
85
|
+
match: options.match || null,
|
|
86
|
+
handler: options.handler || defaultHandler( this.editor ),
|
|
87
|
+
statusColors: options.statusColors || DEFAULT_STATUS_COLORS
|
|
88
|
+
} );
|
|
89
|
+
return this;
|
|
90
|
+
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
bindByUuid: function ( uuid, options ) {
|
|
94
|
+
|
|
95
|
+
const object = this.editor.objectByUuid( uuid );
|
|
96
|
+
if ( object ) this.bind( object, options );
|
|
97
|
+
return object;
|
|
98
|
+
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
detach: function ( object ) {
|
|
102
|
+
|
|
103
|
+
if ( object && object.uuid ) this._bindings.delete( object.uuid );
|
|
104
|
+
return this;
|
|
105
|
+
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
clear: function () {
|
|
109
|
+
|
|
110
|
+
this._bindings.clear();
|
|
111
|
+
return this;
|
|
112
|
+
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
dispatch: function ( uuid, payload ) {
|
|
116
|
+
|
|
117
|
+
const binding = this._bindings.get( uuid );
|
|
118
|
+
if ( binding ) binding.handler( binding.object, payload || {}, this.editor );
|
|
119
|
+
return !! binding;
|
|
120
|
+
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
dispatchMatch: function ( predicate, payload ) {
|
|
124
|
+
|
|
125
|
+
let n = 0;
|
|
126
|
+
this._bindings.forEach( ( binding ) => {
|
|
127
|
+
|
|
128
|
+
if ( predicate( binding.object, binding.topic ) ) {
|
|
129
|
+
|
|
130
|
+
binding.handler( binding.object, payload || {}, this.editor );
|
|
131
|
+
n ++;
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
} );
|
|
136
|
+
return n;
|
|
137
|
+
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
dispatchTopic: function ( topic, payload ) {
|
|
141
|
+
|
|
142
|
+
return this.dispatchMatch( ( _object, t ) => t === topic, payload );
|
|
143
|
+
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
getBindings: function () {
|
|
147
|
+
|
|
148
|
+
return Array.from( this._bindings.values() );
|
|
149
|
+
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
applyOnObject: function ( object, payload, statusColors ) {
|
|
153
|
+
|
|
154
|
+
applyUserData( object, payload || {} );
|
|
155
|
+
if ( payload && payload.status !== undefined ) {
|
|
156
|
+
|
|
157
|
+
applyBeacon( object, payload.status, statusColors || DEFAULT_STATUS_COLORS );
|
|
158
|
+
|
|
159
|
+
}
|
|
160
|
+
this.editor.signals.objectChanged.dispatch( object );
|
|
161
|
+
this.editor.signals.sceneGraphChanged.dispatch();
|
|
162
|
+
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
connectWS: function ( url, options = {} ) {
|
|
166
|
+
|
|
167
|
+
const self = this;
|
|
168
|
+
this._wsUrl = url;
|
|
169
|
+
this._closedByUser = false;
|
|
170
|
+
this._reconnectMs = options.reconnectMs || 0;
|
|
171
|
+
this._closeWs();
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
|
|
175
|
+
this._ws = new WebSocket( url );
|
|
176
|
+
|
|
177
|
+
} catch ( e ) {
|
|
178
|
+
|
|
179
|
+
console.warn( 'EventBridge.connectWS failed:', e.message );
|
|
180
|
+
return this;
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
this._onWsMessage = function ( event ) {
|
|
185
|
+
|
|
186
|
+
let payload;
|
|
187
|
+
try { payload = JSON.parse( event.data ); } catch ( e ) { payload = {}; }
|
|
188
|
+
const uuid = payload.uuid;
|
|
189
|
+
const topic = payload.topic;
|
|
190
|
+
if ( uuid ) {
|
|
191
|
+
|
|
192
|
+
self.dispatch( uuid, payload );
|
|
193
|
+
return;
|
|
194
|
+
|
|
195
|
+
}
|
|
196
|
+
if ( topic ) {
|
|
197
|
+
|
|
198
|
+
self.dispatchTopic( topic, payload );
|
|
199
|
+
return;
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
self._dispatchBroadcast( payload );
|
|
203
|
+
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
this._onWsClose = function () {
|
|
207
|
+
|
|
208
|
+
if ( self._closedByUser ) return;
|
|
209
|
+
if ( self._reconnectMs > 0 ) {
|
|
210
|
+
|
|
211
|
+
clearTimeout( self._reconnectTimer );
|
|
212
|
+
self._reconnectTimer = setTimeout( () => self.connectWS( self._wsUrl, self._reconnectMs ? { reconnectMs: self._reconnectMs } : {} ), self._reconnectMs );
|
|
213
|
+
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
this._ws.addEventListener( 'message', this._onWsMessage );
|
|
219
|
+
this._ws.addEventListener( 'close', this._onWsClose );
|
|
220
|
+
return this;
|
|
221
|
+
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
sendWS: function ( data ) {
|
|
225
|
+
|
|
226
|
+
if ( this._ws && this._ws.readyState === WebSocket.OPEN ) {
|
|
227
|
+
|
|
228
|
+
this._ws.send( typeof data === 'string' ? data : JSON.stringify( data ) );
|
|
229
|
+
return true;
|
|
230
|
+
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
disconnectWS: function () {
|
|
237
|
+
|
|
238
|
+
this._closedByUser = true;
|
|
239
|
+
this._closeWs();
|
|
240
|
+
return this;
|
|
241
|
+
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
_closeWs: function () {
|
|
245
|
+
|
|
246
|
+
if ( this._ws ) {
|
|
247
|
+
|
|
248
|
+
this._ws.removeEventListener( 'message', this._onWsMessage );
|
|
249
|
+
this._ws.removeEventListener( 'close', this._onWsClose );
|
|
250
|
+
try { this._ws.close(); } catch ( e ) {}
|
|
251
|
+
this._ws = null;
|
|
252
|
+
|
|
253
|
+
}
|
|
254
|
+
clearTimeout( this._reconnectTimer );
|
|
255
|
+
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
_dispatchBroadcast: function ( payload ) {
|
|
259
|
+
|
|
260
|
+
this._bindings.forEach( ( binding ) => binding.handler( binding.object, payload || {}, this.editor ) );
|
|
261
|
+
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
destroy: function () {
|
|
265
|
+
|
|
266
|
+
this._closeWs();
|
|
267
|
+
this._bindings.clear();
|
|
268
|
+
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
export { EventBridge, DEFAULT_STATUS_COLORS };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
|
|
3
|
+
import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
|
|
4
|
+
|
|
5
|
+
import { Editor } from './editor/js/Editor.js';
|
|
6
|
+
import { Viewport } from './editor/js/Viewport.js';
|
|
7
|
+
import { Toolbar } from './editor/js/Toolbar.js';
|
|
8
|
+
import { Sidebar } from './editor/js/Sidebar.js';
|
|
9
|
+
import { Menubar } from './editor/js/Menubar.js';
|
|
10
|
+
import { Resizer } from './editor/js/Resizer.js';
|
|
11
|
+
|
|
12
|
+
import { AddObjectCommand } from './editor/js/commands/AddObjectCommand.js';
|
|
13
|
+
|
|
14
|
+
import './editor/css/main.css';
|
|
15
|
+
|
|
16
|
+
import { EventBridge } from './events.js';
|
|
17
|
+
import { registerBuiltinPresets, registerPreset, createPreset, PRESETS } from './presets/machines.js';
|
|
18
|
+
|
|
19
|
+
THREE.ObjectLoader.registerGeometry( 'TextGeometry', TextGeometry );
|
|
20
|
+
|
|
21
|
+
const ROOT_STYLE = [
|
|
22
|
+
'position:relative',
|
|
23
|
+
'width:100%',
|
|
24
|
+
'height:100%',
|
|
25
|
+
'min-height:240px',
|
|
26
|
+
'overflow:hidden',
|
|
27
|
+
'display:block'
|
|
28
|
+
].join( ';' );
|
|
29
|
+
|
|
30
|
+
const DEFAULT_EXPORT_NAME = 'scene.glb';
|
|
31
|
+
|
|
32
|
+
function downloadBlob( blob, filename ) {
|
|
33
|
+
|
|
34
|
+
const url = URL.createObjectURL( blob );
|
|
35
|
+
const a = document.createElement( 'a' );
|
|
36
|
+
a.href = url;
|
|
37
|
+
a.download = filename || DEFAULT_EXPORT_NAME;
|
|
38
|
+
a.dispatchEvent( new MouseEvent( 'click' ) );
|
|
39
|
+
setTimeout( () => URL.revokeObjectURL( url ), 1000 );
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class SceneEditor {
|
|
44
|
+
|
|
45
|
+
constructor( options = {} ) {
|
|
46
|
+
|
|
47
|
+
this.options = options;
|
|
48
|
+
this._commands = options.commands !== undefined ? options.commands : true;
|
|
49
|
+
this._destroyed = false;
|
|
50
|
+
this._onWindowResize = this._onWindowResize.bind( this );
|
|
51
|
+
|
|
52
|
+
this.editor = new Editor();
|
|
53
|
+
this.signals = this.editor.signals;
|
|
54
|
+
|
|
55
|
+
this.root = document.createElement( 'div' );
|
|
56
|
+
this.root.className = 'scene-editor-root';
|
|
57
|
+
this.root.setAttribute( 'style', ROOT_STYLE );
|
|
58
|
+
|
|
59
|
+
const editor = this.editor;
|
|
60
|
+
const root = this.root;
|
|
61
|
+
const viewport = new Viewport( editor );
|
|
62
|
+
const toolbar = new Toolbar( editor );
|
|
63
|
+
const sidebar = new Sidebar( editor );
|
|
64
|
+
const menubar = new Menubar( editor );
|
|
65
|
+
const resizer = new Resizer( editor );
|
|
66
|
+
|
|
67
|
+
this._doms = { viewport, toolbar, sidebar, menubar };
|
|
68
|
+
root.appendChild( menubar.dom );
|
|
69
|
+
root.appendChild( viewport.dom );
|
|
70
|
+
root.appendChild( toolbar.dom );
|
|
71
|
+
root.appendChild( sidebar.dom );
|
|
72
|
+
root.appendChild( resizer.dom );
|
|
73
|
+
|
|
74
|
+
this._ro = new ResizeObserver( () => this._onWindowResize() );
|
|
75
|
+
this._ro.observe( root );
|
|
76
|
+
|
|
77
|
+
this.events = new EventBridge( this.editor );
|
|
78
|
+
|
|
79
|
+
editor.signals.windowResize.dispatch();
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
mount( container ) {
|
|
84
|
+
|
|
85
|
+
if ( ! container ) throw new Error( 'SceneEditor.mount: container element required' );
|
|
86
|
+
const parent = typeof container === 'string'
|
|
87
|
+
? document.querySelector( container )
|
|
88
|
+
: container;
|
|
89
|
+
if ( ! parent ) throw new Error( 'SceneEditor.mount: container not found' );
|
|
90
|
+
parent.appendChild( this.root );
|
|
91
|
+
this.editor.signals.windowResize.dispatch();
|
|
92
|
+
return this;
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getScene() { return this.editor.scene; }
|
|
97
|
+
|
|
98
|
+
getViewport() { return this.editor.viewportCamera; }
|
|
99
|
+
|
|
100
|
+
getEditor() { return this.editor; }
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
getSceneJSON() {
|
|
104
|
+
|
|
105
|
+
return this.editor.toJSON();
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async setSceneJSON( json ) {
|
|
110
|
+
|
|
111
|
+
const data = typeof json === 'string' ? JSON.parse( json ) : json;
|
|
112
|
+
this.editor.clear();
|
|
113
|
+
await this.editor.fromJSON( data );
|
|
114
|
+
this.editor.signals.windowResize.dispatch();
|
|
115
|
+
return this;
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
getObjectByUuid( uuid ) {
|
|
121
|
+
|
|
122
|
+
return this.editor.objectByUuid( uuid );
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
selectByUuid( uuid ) {
|
|
127
|
+
|
|
128
|
+
this.editor.selectByUuid( uuid );
|
|
129
|
+
return this;
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getSelected() {
|
|
134
|
+
|
|
135
|
+
return this.editor.selected;
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
getSelection() {
|
|
141
|
+
|
|
142
|
+
return this.editor.getSelection ? this.editor.getSelection() : [ this.editor.selected ];
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
selectMultiple( objects ) {
|
|
147
|
+
|
|
148
|
+
if ( this.editor.selectMultiple ) this.editor.selectMultiple( objects );
|
|
149
|
+
else if ( objects && objects.length ) this.editor.select( objects[ 0 ] );
|
|
150
|
+
return this;
|
|
151
|
+
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
addToSelection( object ) {
|
|
155
|
+
|
|
156
|
+
if ( this.editor.addToSelection ) this.editor.addToSelection( object );
|
|
157
|
+
else this.editor.select( object );
|
|
158
|
+
return this;
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
mergeSelectionIntoGroup( name ) {
|
|
163
|
+
|
|
164
|
+
if ( this.editor.mergeSelectionIntoGroup ) return this.editor.mergeSelectionIntoGroup( name );
|
|
165
|
+
return null;
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
ungroupBySelection() {
|
|
170
|
+
|
|
171
|
+
if ( this.editor.ungroupBySelection ) return this.editor.ungroupBySelection();
|
|
172
|
+
return [];
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
addObject( object ) {
|
|
178
|
+
|
|
179
|
+
this.editor.execute( new AddObjectCommand( this.editor, object ) );
|
|
180
|
+
return object;
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
addPreset( name, x = 0, z = 0, params ) {
|
|
185
|
+
|
|
186
|
+
const object = createPreset( name, x, z, params );
|
|
187
|
+
if ( object ) this.addObject( object );
|
|
188
|
+
return object || null;
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
registerPreset( name, factory ) {
|
|
193
|
+
|
|
194
|
+
return registerPreset( name, factory );
|
|
195
|
+
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
presets() {
|
|
199
|
+
|
|
200
|
+
return Object.keys( PRESETS );
|
|
201
|
+
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
async exportGLB( rootObject ) {
|
|
206
|
+
|
|
207
|
+
const obj = rootObject || this.editor.scene;
|
|
208
|
+
const exporter = new GLTFExporter();
|
|
209
|
+
return new Promise( ( resolve, reject ) => {
|
|
210
|
+
|
|
211
|
+
exporter.parse( obj, ( result ) => resolve( result ), ( error ) => reject( error ), { binary: true } );
|
|
212
|
+
|
|
213
|
+
} );
|
|
214
|
+
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async exportGLBFile( rootObject, filename ) {
|
|
218
|
+
|
|
219
|
+
const buffer = await this.exportGLB( rootObject );
|
|
220
|
+
const blob = new Blob( [ buffer ], { type: 'model/gltf-binary' } );
|
|
221
|
+
downloadBlob( blob, filename );
|
|
222
|
+
return blob;
|
|
223
|
+
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
async exportGLTF( rootObject ) {
|
|
228
|
+
|
|
229
|
+
const obj = rootObject || this.editor.scene;
|
|
230
|
+
const exporter = new GLTFExporter();
|
|
231
|
+
return new Promise( ( resolve, reject ) => {
|
|
232
|
+
|
|
233
|
+
exporter.parse( obj, ( result ) => resolve( JSON.stringify( result, null, 2 ) ),
|
|
234
|
+
( error ) => reject( error ), { binary: false } );
|
|
235
|
+
|
|
236
|
+
} );
|
|
237
|
+
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
on( signalName, handler ) {
|
|
242
|
+
|
|
243
|
+
const sig = this.signals[ signalName ];
|
|
244
|
+
if ( ! sig ) throw new Error( 'SceneEditor.on: unknown signal "' + signalName + '"' );
|
|
245
|
+
sig.add( handler );
|
|
246
|
+
return () => sig.remove( handler );
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
off( signalName, handler ) {
|
|
251
|
+
|
|
252
|
+
const sig = this.signals[ signalName ];
|
|
253
|
+
if ( sig ) sig.remove( handler );
|
|
254
|
+
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
_onWindowResize() {
|
|
258
|
+
|
|
259
|
+
if ( this._destroyed ) return;
|
|
260
|
+
this.signals.windowResize.dispatch();
|
|
261
|
+
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
destroy() {
|
|
265
|
+
|
|
266
|
+
if ( this._destroyed ) return;
|
|
267
|
+
this._destroyed = true;
|
|
268
|
+
this._ro.disconnect();
|
|
269
|
+
this.signals.editorCleared.dispatch();
|
|
270
|
+
if ( this.root.parentNode ) this.root.parentNode.removeChild( this.root );
|
|
271
|
+
this.events.destroy();
|
|
272
|
+
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export {
|
|
278
|
+
SceneEditor,
|
|
279
|
+
EventBridge,
|
|
280
|
+
registerBuiltinPresets,
|
|
281
|
+
registerPreset,
|
|
282
|
+
createPreset,
|
|
283
|
+
PRESETS
|
|
284
|
+
};
|