@operato/scene-visualizer 1.2.31 → 1.2.37
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/CHANGELOG.md +18 -0
- package/db.sqlite +0 -0
- package/dist/three-modeller.d.ts +0 -0
- package/dist/three-modeller.js +2 -0
- package/dist/three-modeller.js.map +1 -0
- package/dist/three-space.d.ts +86 -0
- package/dist/three-space.js +566 -0
- package/dist/three-space.js.map +1 -0
- package/logs/.08636eb59927f12972f6774f5947c8507b3564c2-audit.json +3 -28
- package/logs/.5e5d741d8b7784a2fbad65eedc0fd46946aaf6f2-audit.json +3 -48
- package/logs/application-2023-05-20-07.log +6 -0
- package/logs/connections-2023-05-20-07.log +41 -0
- package/package.json +3 -2
- package/schema.gql +10 -0
- package/src/three-space.ts +732 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/logs/application-2023-04-30-10.log +0 -6
- package/logs/application-2023-04-30-12.log +0 -6
- package/logs/application-2023-04-30-14.log +0 -6
- package/logs/application-2023-04-30-16.log +0 -15
- package/logs/application-2023-04-30-20.log +0 -6
- package/logs/application-2023-04-30-22.log +0 -6
- package/logs/connections-2023-04-22-10.log +0 -82
- package/logs/connections-2023-04-22-11.log +0 -41
- package/logs/connections-2023-04-24-13.log +0 -82
- package/logs/connections-2023-04-24-14.log +0 -82
- package/logs/connections-2023-04-30-10.log +0 -41
- package/logs/connections-2023-04-30-12.log +0 -41
- package/logs/connections-2023-04-30-14.log +0 -41
- package/logs/connections-2023-04-30-16.log +0 -41
- package/logs/connections-2023-04-30-20.log +0 -41
- package/logs/connections-2023-04-30-22.log +0 -41
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © HatioLab Inc. All rights reserved.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Component, ComponentNature, Container, error, FPS, Layout, Properties } from '@hatiolab/things-scene'
|
|
6
|
+
import * as THREE from 'three'
|
|
7
|
+
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'
|
|
8
|
+
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js'
|
|
9
|
+
|
|
10
|
+
import ThreeControls from './three-controls'
|
|
11
|
+
import './three-layout'
|
|
12
|
+
import { RealObject } from './threed/real-object'
|
|
13
|
+
import { ThreeDimensionalContainer } from './threed/three-dimensional-container'
|
|
14
|
+
import { CSS3DRenderer } from 'three/examples/jsm/renderers/CSS3DRenderer'
|
|
15
|
+
import { createFloor } from './threed/floor/floor'
|
|
16
|
+
import { RealObjectDomElement } from './threed'
|
|
17
|
+
|
|
18
|
+
const NATURE = {
|
|
19
|
+
mutable: false,
|
|
20
|
+
resizable: true,
|
|
21
|
+
rotatable: true,
|
|
22
|
+
properties: [
|
|
23
|
+
{
|
|
24
|
+
type: 'select',
|
|
25
|
+
label: 'precision',
|
|
26
|
+
name: 'precision',
|
|
27
|
+
property: {
|
|
28
|
+
options: [
|
|
29
|
+
{
|
|
30
|
+
display: 'High',
|
|
31
|
+
value: 'highp'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
display: 'Medium',
|
|
35
|
+
value: 'mediump'
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
display: 'Low',
|
|
39
|
+
value: 'lowp'
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: 'checkbox',
|
|
46
|
+
label: 'anti-alias',
|
|
47
|
+
name: 'antialias',
|
|
48
|
+
property: 'antialias'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'checkbox',
|
|
52
|
+
label: 'controller',
|
|
53
|
+
name: 'controller',
|
|
54
|
+
property: 'controller'
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
help: 'scene/component/three-space'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const WEBGL_NO_SUPPORT_TEXT = 'WebGL no support'
|
|
61
|
+
|
|
62
|
+
export class ThreeSpace extends Container implements ThreeDimensionalContainer {
|
|
63
|
+
_scene3d?: THREE.Scene
|
|
64
|
+
_initalizeComplete?: Promise<void>
|
|
65
|
+
|
|
66
|
+
_mixer?: THREE.AnimationMixer
|
|
67
|
+
|
|
68
|
+
_camera?: THREE.PerspectiveCamera
|
|
69
|
+
_renderer3d?: THREE.WebGLRenderer
|
|
70
|
+
|
|
71
|
+
_mouse?: THREE.Vector2
|
|
72
|
+
|
|
73
|
+
_clock?: THREE.Clock
|
|
74
|
+
|
|
75
|
+
_controls?: any
|
|
76
|
+
_onFocus?: (e: FocusEvent) => void
|
|
77
|
+
_onResize?: (e: Event) => void
|
|
78
|
+
_noSupportWebgl: boolean = false
|
|
79
|
+
|
|
80
|
+
_raycaster = new THREE.Raycaster()
|
|
81
|
+
|
|
82
|
+
_lastFocused?: RealObject
|
|
83
|
+
_lastHovered?: RealObject
|
|
84
|
+
|
|
85
|
+
private _css3DScene?: THREE.Scene
|
|
86
|
+
private _css3DRenderer?: CSS3DRenderer
|
|
87
|
+
|
|
88
|
+
composer?: EffectComposer
|
|
89
|
+
|
|
90
|
+
containable(component: Component) {
|
|
91
|
+
return component.is3dish()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
is3dContainer() {
|
|
95
|
+
return true
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* THREE Object related .. */
|
|
99
|
+
|
|
100
|
+
createObjects(components: Component[]) {
|
|
101
|
+
for (let component of components) {
|
|
102
|
+
try {
|
|
103
|
+
var item = component.realObject
|
|
104
|
+
|
|
105
|
+
if (item) {
|
|
106
|
+
if (component.isHTMLElement()) {
|
|
107
|
+
this.css3DScene.add((item as RealObjectDomElement).object3d)
|
|
108
|
+
} else {
|
|
109
|
+
this._scene3d!.add(item.object3d)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} catch (err) {
|
|
113
|
+
error(err)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
destroy_scene3d() {
|
|
119
|
+
this.stop()
|
|
120
|
+
|
|
121
|
+
window.removeEventListener('focus', this._onFocus!)
|
|
122
|
+
window.removeEventListener('resize', this._onResize!)
|
|
123
|
+
|
|
124
|
+
if (this._renderer3d) this._renderer3d.clear()
|
|
125
|
+
|
|
126
|
+
if (this._controls?.autoRotate) {
|
|
127
|
+
this._controls.doAutoRotate(false)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
delete this._renderer3d
|
|
131
|
+
delete this._camera
|
|
132
|
+
delete this._controls
|
|
133
|
+
|
|
134
|
+
if (this._scene3d) {
|
|
135
|
+
let children = this._scene3d.children.slice()
|
|
136
|
+
for (let i in children) {
|
|
137
|
+
let child = children[i] as any
|
|
138
|
+
if (child.dispose) child.dispose()
|
|
139
|
+
if (child.geometry && child.geometry.dispose) child.geometry.dispose()
|
|
140
|
+
if (child.material && child.material.dispose) child.material.dispose()
|
|
141
|
+
if (child.texture && child.texture.dispose) child.texture.dispose()
|
|
142
|
+
this._scene3d.remove(child)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
delete this._scene3d
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
update() {
|
|
150
|
+
var { width, height } = this.bounds
|
|
151
|
+
|
|
152
|
+
var {
|
|
153
|
+
fov = 45,
|
|
154
|
+
near = 0.1,
|
|
155
|
+
far = 20000,
|
|
156
|
+
antialias = true,
|
|
157
|
+
precision = 'highp',
|
|
158
|
+
cameraX,
|
|
159
|
+
cameraY,
|
|
160
|
+
cameraZ,
|
|
161
|
+
exposure = 2.5
|
|
162
|
+
} = this.state
|
|
163
|
+
|
|
164
|
+
var components = this.components || []
|
|
165
|
+
|
|
166
|
+
// SCENE
|
|
167
|
+
this._scene3d = new THREE.Scene()
|
|
168
|
+
|
|
169
|
+
// CAMERA
|
|
170
|
+
var aspect = width / height
|
|
171
|
+
|
|
172
|
+
this._camera = new THREE.PerspectiveCamera(fov, aspect, near, far)
|
|
173
|
+
|
|
174
|
+
var cameraXPos = height * 0.8,
|
|
175
|
+
cameraYPos = width * 0.8,
|
|
176
|
+
cameraZPos = Math.min(500, Math.floor(Math.min(width, height)))
|
|
177
|
+
// cameraZPos = Math.floor(Math.min(width, height))
|
|
178
|
+
|
|
179
|
+
if (cameraX != undefined) cameraXPos = cameraX * width
|
|
180
|
+
if (cameraY != undefined) cameraYPos = cameraY * height
|
|
181
|
+
if (cameraZ != undefined) cameraZPos = cameraZ * Math.floor(Math.min(width, height))
|
|
182
|
+
|
|
183
|
+
this._camera.position.set(cameraXPos, cameraZPos, cameraYPos)
|
|
184
|
+
|
|
185
|
+
this._scene3d.add(this._camera)
|
|
186
|
+
this._camera.lookAt(this._scene3d.position)
|
|
187
|
+
this._camera.zoom = this.getState('zoom') * 0.01
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const canvas = document.createElement('canvas')
|
|
191
|
+
let context = canvas!.getContext('webgl2')!
|
|
192
|
+
|
|
193
|
+
var renderer3d = new THREE.WebGLRenderer({
|
|
194
|
+
canvas,
|
|
195
|
+
context,
|
|
196
|
+
precision,
|
|
197
|
+
alpha: true,
|
|
198
|
+
antialias
|
|
199
|
+
})
|
|
200
|
+
} catch (e) {
|
|
201
|
+
this._noSupportWebgl = true
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this._renderer3d = renderer3d
|
|
206
|
+
|
|
207
|
+
renderer3d!.autoClear = true
|
|
208
|
+
// @ts-ignore for three@0.150.1
|
|
209
|
+
renderer3d!.outputColorSpace = THREE.SRGBColorSpace
|
|
210
|
+
renderer3d!.useLegacyLights = false
|
|
211
|
+
renderer3d!.toneMappingExposure = Math.pow(exposure, 5.0)
|
|
212
|
+
renderer3d!.toneMapping = THREE.LinearToneMapping
|
|
213
|
+
renderer3d!.shadowMap.enabled = true
|
|
214
|
+
renderer3d!.setClearColor(0xffffff, 0) // transparent
|
|
215
|
+
renderer3d!.setSize(width, height)
|
|
216
|
+
|
|
217
|
+
// CONTROLS
|
|
218
|
+
this._controls = new (ThreeControls as any)(this._camera, this)
|
|
219
|
+
this._controls.cameraChanged = true
|
|
220
|
+
|
|
221
|
+
// Lights
|
|
222
|
+
var _hemiLight = new THREE.HemisphereLight(0xdddddd, 0x333333, 0.035)
|
|
223
|
+
var _directionalLight = new THREE.DirectionalLight(0xd6e1ff, 0.01)
|
|
224
|
+
var _pointLight1 = new THREE.PointLight(0xd6e1ff, 1, undefined, 2)
|
|
225
|
+
|
|
226
|
+
_pointLight1.power = cameraZPos * 50
|
|
227
|
+
_pointLight1.castShadow = true
|
|
228
|
+
_pointLight1.position.set(0, cameraZPos, 0)
|
|
229
|
+
_pointLight1.shadow.camera.near = 0.1
|
|
230
|
+
_pointLight1.shadow.camera.far = cameraZPos * 2
|
|
231
|
+
|
|
232
|
+
// const helper = new THREE.CameraHelper(_pointLight1.shadow.camera)
|
|
233
|
+
// this._scene3d?.add(helper)
|
|
234
|
+
|
|
235
|
+
this.scene3d!.add(_hemiLight)
|
|
236
|
+
this.scene3d!.add(_pointLight1)
|
|
237
|
+
this._camera.add(_directionalLight)
|
|
238
|
+
|
|
239
|
+
this._mouse = new THREE.Vector2()
|
|
240
|
+
|
|
241
|
+
this._clock = new THREE.Clock(true)
|
|
242
|
+
this._mixer = new THREE.AnimationMixer(this.scene3d!)
|
|
243
|
+
|
|
244
|
+
this.createObjects(components)
|
|
245
|
+
|
|
246
|
+
this._camera.updateProjectionMatrix()
|
|
247
|
+
|
|
248
|
+
const floor = createFloor(this)
|
|
249
|
+
// if (this._floor) {
|
|
250
|
+
// this.object3d.remove(this._floor)
|
|
251
|
+
// this._floor.clear()
|
|
252
|
+
// delete this._floor
|
|
253
|
+
// }
|
|
254
|
+
|
|
255
|
+
if (floor) {
|
|
256
|
+
// this._floor = floor
|
|
257
|
+
this._scene3d.add(floor)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// this.invalidate()
|
|
261
|
+
|
|
262
|
+
// postprocessing
|
|
263
|
+
|
|
264
|
+
var composer = (this.composer = new EffectComposer(renderer3d!))
|
|
265
|
+
composer.setSize(width, height)
|
|
266
|
+
|
|
267
|
+
const renderPass = new RenderPass(this._scene3d, this._camera)
|
|
268
|
+
composer.addPass(renderPass)
|
|
269
|
+
|
|
270
|
+
this._onFocus = (e: FocusEvent) => {
|
|
271
|
+
this.render_threed()
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
this._onResize = (e: Event) => {
|
|
275
|
+
this.resize()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
window.addEventListener('resize', this._onResize)
|
|
279
|
+
window.addEventListener('focus', this._onFocus)
|
|
280
|
+
|
|
281
|
+
this.buildOverlays()
|
|
282
|
+
this.resize()
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
resize() {
|
|
286
|
+
const { width, height } = this.bounds
|
|
287
|
+
|
|
288
|
+
this._camera!.aspect = width / height
|
|
289
|
+
|
|
290
|
+
this.renderer3d!.setSize(width, height)
|
|
291
|
+
this.css3DRenderer.setSize(width, height)
|
|
292
|
+
|
|
293
|
+
this.composer?.setSize(width, height)
|
|
294
|
+
this._camera!.updateProjectionMatrix()
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
init_scene3d() {
|
|
298
|
+
this.root.on('redraw', this.onredraw, this)
|
|
299
|
+
|
|
300
|
+
if (this._scene3d) {
|
|
301
|
+
this.destroy_scene3d()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
this.update()
|
|
305
|
+
|
|
306
|
+
this.onchangeData && this.onchangeData(this.data, this.data)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
threed_animate() {
|
|
310
|
+
if (!this._controls) {
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (this._mixer) {
|
|
315
|
+
this._mixer.update(this._clock!.getDelta())
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
this._controls.update()
|
|
319
|
+
|
|
320
|
+
if (this.state.autoRotate) {
|
|
321
|
+
this.invalidate()
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
this.render_threed()
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
stop() {}
|
|
328
|
+
|
|
329
|
+
get scene3d() {
|
|
330
|
+
if (!this._scene3d) {
|
|
331
|
+
this.init_scene3d()
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return this._scene3d
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
get renderer3d() {
|
|
338
|
+
return this._renderer3d
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
get mixer() {
|
|
342
|
+
return this._mixer
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
get zoom() {
|
|
346
|
+
return this._camera?.zoom
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* CSS3DRenderer용 THREE.Scene을 제공
|
|
351
|
+
*/
|
|
352
|
+
get css3DScene() {
|
|
353
|
+
if (!this._css3DScene) {
|
|
354
|
+
this._css3DScene = this.buildCSS3DScene()
|
|
355
|
+
}
|
|
356
|
+
return this._css3DScene
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
protected buildCSS3DScene() {
|
|
360
|
+
return new THREE.Scene()
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
protected disposeCSS3DScene() {
|
|
364
|
+
var children = [...this.css3DScene.children]
|
|
365
|
+
children.forEach(child => this.css3DScene.remove(child))
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* css3d-renderer */
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* css3d-renderer getter
|
|
372
|
+
*/
|
|
373
|
+
get css3DRenderer() {
|
|
374
|
+
if (!this._css3DRenderer) {
|
|
375
|
+
this._css3DRenderer = this.createCSS3DRenderer()
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return this._css3DRenderer
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
protected createCSS3DRenderer() {
|
|
382
|
+
var renderer = new CSS3DRenderer()
|
|
383
|
+
var div = renderer.domElement
|
|
384
|
+
div.style.position = 'absolute'
|
|
385
|
+
div.style.top = '0'
|
|
386
|
+
div.style.pointerEvents = 'none'
|
|
387
|
+
|
|
388
|
+
if (this.app.isViewMode) {
|
|
389
|
+
// disableAllUserEvents(div)
|
|
390
|
+
} else {
|
|
391
|
+
div.style.pointerEvents = 'none'
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return renderer
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
protected disposeCSS3DRenderer() {
|
|
398
|
+
// Nothing to do
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* element내부에 필요한 overlay들을 생성
|
|
403
|
+
* ViewRenderer에서는 CSS3DRenderer와 GLRendering을 위한 canvas를 오버레이로 만든다.
|
|
404
|
+
* Warn: this.element는 아직 만들어지지 않은 상태에 buildOverlays가 호출됨.
|
|
405
|
+
* @param into
|
|
406
|
+
*/
|
|
407
|
+
buildOverlays() {
|
|
408
|
+
this.element.appendChild(this.css3DRenderer.domElement)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
render_threed() {
|
|
412
|
+
if (this.composer) {
|
|
413
|
+
this.composer.render()
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/* Container Overides .. */
|
|
418
|
+
render(ctx: CanvasRenderingContext2D) {
|
|
419
|
+
if (this.app.isViewMode) {
|
|
420
|
+
this.setState('threed', true)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (this.state.threed && !this._noSupportWebgl && this._camera) {
|
|
424
|
+
this.css3DRenderer.render(this.css3DScene, this._camera!)
|
|
425
|
+
return
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
super.render(ctx)
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
postrender(ctx: CanvasRenderingContext2D) {
|
|
432
|
+
var { left, top, debug, threed } = this.state
|
|
433
|
+
|
|
434
|
+
var { width, height } = this.bounds
|
|
435
|
+
|
|
436
|
+
// ios에서 width, height에 소수점이 있으면 3d를 표현하지 못하는 문제가 있어 정수화
|
|
437
|
+
width = Math.floor(width)
|
|
438
|
+
height = Math.floor(height)
|
|
439
|
+
|
|
440
|
+
if (threed) {
|
|
441
|
+
if (!this._scene3d) {
|
|
442
|
+
this.init_scene3d()
|
|
443
|
+
this.render_threed()
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (this._noSupportWebgl) {
|
|
447
|
+
this._showWebglNoSupportText(ctx)
|
|
448
|
+
return
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (!this._renderer3d) {
|
|
452
|
+
return
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
var { width: rendererWidth, height: rendererHeight } = this._renderer3d.getSize(new THREE.Vector2())
|
|
456
|
+
|
|
457
|
+
ctx.drawImage(this._renderer3d.domElement, 0, 0, rendererWidth, rendererHeight, left, top, width, height)
|
|
458
|
+
|
|
459
|
+
if (debug) {
|
|
460
|
+
ctx.font = 100 + 'px Arial'
|
|
461
|
+
ctx.textAlign = 'center'
|
|
462
|
+
ctx.fillStyle = 'black'
|
|
463
|
+
ctx.globalAlpha = 0.5
|
|
464
|
+
ctx.fillText(String(FPS()), 100, 100)
|
|
465
|
+
}
|
|
466
|
+
} else {
|
|
467
|
+
super.postrender(ctx)
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
dispose() {
|
|
472
|
+
this.root.off('redraw', this.onredraw, this)
|
|
473
|
+
|
|
474
|
+
this.disposeCSS3DScene()
|
|
475
|
+
|
|
476
|
+
this.composer?.dispose()
|
|
477
|
+
this.destroy_scene3d()
|
|
478
|
+
|
|
479
|
+
super.dispose()
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
get layout() {
|
|
483
|
+
return Layout.get('three')
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
get nature(): ComponentNature {
|
|
487
|
+
return NATURE
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
getObjectByRaycast(): THREE.Object3D | undefined {
|
|
491
|
+
var intersects = this.getObjectsByRaycast()
|
|
492
|
+
|
|
493
|
+
if (intersects && intersects.length > 0) {
|
|
494
|
+
var object: THREE.Object3D | null = intersects[0].object
|
|
495
|
+
while (object) {
|
|
496
|
+
if (object.userData.context) {
|
|
497
|
+
return object
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
object = object.parent
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
getObjectsByRaycast() {
|
|
506
|
+
var vector = this._mouse
|
|
507
|
+
if (!this._camera) {
|
|
508
|
+
return
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
this._raycaster.setFromCamera(vector!, this._camera)
|
|
512
|
+
|
|
513
|
+
var intersects = this._raycaster.intersectObjects(this._scene3d!.children, true)
|
|
514
|
+
|
|
515
|
+
return intersects
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
_showWebglNoSupportText(context: CanvasRenderingContext2D) {
|
|
519
|
+
context.save()
|
|
520
|
+
|
|
521
|
+
var { width, height } = this.state
|
|
522
|
+
|
|
523
|
+
context.font = width / 20 + 'px Arial'
|
|
524
|
+
context.textAlign = 'center'
|
|
525
|
+
context.fillText(WEBGL_NO_SUPPORT_TEXT, width / 2 - width / 40, height / 2)
|
|
526
|
+
|
|
527
|
+
context.restore()
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/* Event Handlers */
|
|
531
|
+
|
|
532
|
+
onchange(after: Properties, before: Properties) {
|
|
533
|
+
if (after.hasOwnProperty('width') || after.hasOwnProperty('height') || after.hasOwnProperty('threed'))
|
|
534
|
+
this.destroy_scene3d()
|
|
535
|
+
|
|
536
|
+
if (after.hasOwnProperty('autoRotate')) {
|
|
537
|
+
if (this._controls) {
|
|
538
|
+
this._controls.doAutoRotate(after.autoRotate)
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (
|
|
543
|
+
after.hasOwnProperty('fov') ||
|
|
544
|
+
after.hasOwnProperty('near') ||
|
|
545
|
+
after.hasOwnProperty('far') ||
|
|
546
|
+
after.hasOwnProperty('zoom')
|
|
547
|
+
) {
|
|
548
|
+
if (this._camera) {
|
|
549
|
+
const { near, far, zoom, fov } = this.state
|
|
550
|
+
|
|
551
|
+
this._camera.near = near
|
|
552
|
+
this._camera.far = far
|
|
553
|
+
this._camera.zoom = zoom * 0.01
|
|
554
|
+
this._camera.fov = fov
|
|
555
|
+
this._camera.updateProjectionMatrix()
|
|
556
|
+
|
|
557
|
+
this._controls.cameraChanged = true
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
onmousedown(e: MouseEvent) {
|
|
563
|
+
if (this._controls) {
|
|
564
|
+
this._controls.onMouseDown(e)
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
onmouseup(e: MouseEvent) {
|
|
569
|
+
if (this._controls) {
|
|
570
|
+
if (this._lastFocused) {
|
|
571
|
+
;(this._lastFocused as any)._focused = false
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const { left, top, width, height } = this.state
|
|
575
|
+
|
|
576
|
+
var pointer = this.transcoordC2S(e.offsetX, e.offsetY)
|
|
577
|
+
|
|
578
|
+
this._mouse!.x = ((pointer.x - left) / width) * 2 - 1
|
|
579
|
+
this._mouse!.y = -((pointer.y - top) / height) * 2 + 1
|
|
580
|
+
|
|
581
|
+
var object = this.getObjectByRaycast()
|
|
582
|
+
var realObject = object?.userData.context as RealObject
|
|
583
|
+
|
|
584
|
+
if (realObject) {
|
|
585
|
+
;(realObject as any)._focused = true
|
|
586
|
+
;(realObject as any)._focusedAt = performance.now()
|
|
587
|
+
this._lastFocused = realObject
|
|
588
|
+
realObject.component?.trigger('click', e)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
e.stopPropagation()
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
onmousemove(e: MouseEvent) {
|
|
596
|
+
if (this._controls) {
|
|
597
|
+
const { left, top, width, height } = this.state
|
|
598
|
+
|
|
599
|
+
var pointer = this.transcoordC2S(e.offsetX, e.offsetY)
|
|
600
|
+
|
|
601
|
+
this._mouse!.x = ((pointer.x - left) / width) * 2 - 1
|
|
602
|
+
this._mouse!.y = -((pointer.y - top) / height) * 2 + 1
|
|
603
|
+
|
|
604
|
+
var object = this.getObjectByRaycast()
|
|
605
|
+
var realObject = object?.userData.context as RealObject
|
|
606
|
+
|
|
607
|
+
if (realObject !== this._lastHovered) {
|
|
608
|
+
if (this._lastHovered) {
|
|
609
|
+
this._lastHovered.component?.trigger('mouseleave', e)
|
|
610
|
+
delete this._lastHovered
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (realObject) {
|
|
614
|
+
this._lastHovered = realObject
|
|
615
|
+
this._lastHovered.component?.trigger('mouseenter', e)
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
this._controls.onMouseMove(e)
|
|
620
|
+
|
|
621
|
+
e.stopPropagation()
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
onmouseleave(e: MouseEvent) {}
|
|
626
|
+
|
|
627
|
+
onwheel(e: WheelEvent) {
|
|
628
|
+
if (this._controls) {
|
|
629
|
+
this.handleMouseWheel(e)
|
|
630
|
+
e.stopPropagation()
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
ondblclick(e: MouseEvent) {
|
|
635
|
+
if (this._controls) {
|
|
636
|
+
this.setState('zoom', this.get('zoom'))
|
|
637
|
+
this._controls.reset()
|
|
638
|
+
e.stopPropagation()
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
ondragstart(e: DragEvent) {
|
|
643
|
+
if (this._controls) {
|
|
644
|
+
var pointer = this.transcoordC2S(e.offsetX, e.offsetY)
|
|
645
|
+
var { left, top, width, height } = this.bounds
|
|
646
|
+
|
|
647
|
+
this._mouse!.x = ((pointer.x - left) / width) * 2 - 1
|
|
648
|
+
this._mouse!.y = -((pointer.y - top) / height) * 2 + 1
|
|
649
|
+
|
|
650
|
+
this._controls.onDragStart(e)
|
|
651
|
+
e.stopPropagation()
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
ondragmove(e: DragEvent) {
|
|
656
|
+
if (this._controls) {
|
|
657
|
+
this._controls.cameraChanged = true
|
|
658
|
+
this._controls.onDragMove(e)
|
|
659
|
+
e.stopPropagation()
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
ondragend(e: DragEvent) {
|
|
664
|
+
if (this._controls) {
|
|
665
|
+
this._controls.cameraChanged = true
|
|
666
|
+
this._controls.onDragEnd(e)
|
|
667
|
+
e.stopPropagation()
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
ontouchstart(e: TouchEvent) {
|
|
672
|
+
if (this._controls) {
|
|
673
|
+
this._controls.onTouchStart(e)
|
|
674
|
+
e.stopPropagation()
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
onpan(e: TouchEvent) {
|
|
679
|
+
if (this._controls) {
|
|
680
|
+
this._controls.cameraChanged = true
|
|
681
|
+
this._controls.onTouchMove(e)
|
|
682
|
+
e.stopPropagation()
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
ontouchend(e: TouchEvent) {
|
|
686
|
+
if (this._controls) {
|
|
687
|
+
this._controls.onTouchEnd(e)
|
|
688
|
+
e.stopPropagation()
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
onkeydown(e: KeyboardEvent) {
|
|
693
|
+
if (this._controls) {
|
|
694
|
+
this._controls.onKeyDown(e)
|
|
695
|
+
e.stopPropagation()
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
onpinch(e: TouchEvent) {
|
|
700
|
+
if (this._controls) {
|
|
701
|
+
var zoom = this.getState('zoom')
|
|
702
|
+
//@ts-ignore
|
|
703
|
+
zoom *= e.scale
|
|
704
|
+
|
|
705
|
+
if (zoom < 100) zoom = 100
|
|
706
|
+
|
|
707
|
+
this.setState('zoom', zoom)
|
|
708
|
+
e.stopPropagation()
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
ondoubletap() {
|
|
713
|
+
this._controls.reset()
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
handleMouseWheel(event: WheelEvent) {
|
|
717
|
+
var delta = 0
|
|
718
|
+
var zoom = this.getState('zoom')
|
|
719
|
+
|
|
720
|
+
delta = -event.deltaY
|
|
721
|
+
zoom += delta * 0.1
|
|
722
|
+
if (zoom < 100) zoom = 100
|
|
723
|
+
|
|
724
|
+
this.setState('zoom', zoom)
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
onredraw() {
|
|
728
|
+
this.threed_animate()
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
Component.register('3d-space', ThreeSpace)
|