lythreeframe 1.0.5 → 1.0.7

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.
Files changed (136) hide show
  1. package/dist/bundle.cjs.js +2782 -0
  2. package/dist/bundle.esm.js +2747 -0
  3. package/dist/index.d.ts +42 -0
  4. package/dist/lythreeframe/AssetManagement/AssetDefines.d.ts +6 -0
  5. package/dist/lythreeframe/AssetManagement/AssetManager.d.ts +23 -0
  6. package/dist/lythreeframe/AssetManagement/AssetPointer/AssetPointer.d.ts +9 -0
  7. package/dist/lythreeframe/AssetManagement/Task/Task.d.ts +7 -0
  8. package/dist/lythreeframe/Container/SmartPointer.d.ts +10 -0
  9. package/dist/lythreeframe/Defines.d.ts +4 -0
  10. package/dist/lythreeframe/Delegate.d.ts +8 -0
  11. package/dist/lythreeframe/Factory/CameraFactory.d.ts +20 -0
  12. package/dist/lythreeframe/Factory/RendererFactory.d.ts +2 -0
  13. package/dist/lythreeframe/Frame/Controller.d.ts +40 -0
  14. package/dist/lythreeframe/Frame/Viewport.d.ts +50 -0
  15. package/dist/lythreeframe/Frame/World.d.ts +18 -0
  16. package/dist/lythreeframe/Geometry/PlaneGeometry.d.ts +3 -0
  17. package/dist/lythreeframe/Geometry/TriangleGeometry.d.ts +3 -0
  18. package/dist/lythreeframe/Library/ContainerLibrary.d.ts +3 -0
  19. package/dist/lythreeframe/Library/Math.d.ts +27 -0
  20. package/dist/lythreeframe/Library/ResourceLibrary.d.ts +6 -0
  21. package/dist/lythreeframe/Object/Actor.d.ts +86 -0
  22. package/dist/lythreeframe/Object/Actors/Camera/CameraActor.d.ts +4 -0
  23. package/dist/lythreeframe/Object/Actors/Light/DirectionalLightActor.d.ts +7 -0
  24. package/dist/lythreeframe/Object/Actors/Shape/BoxActor.d.ts +5 -0
  25. package/dist/lythreeframe/Object/Actors/Shape/PlaneActor.d.ts +9 -0
  26. package/dist/lythreeframe/Object/BaseObject.d.ts +10 -0
  27. package/dist/lythreeframe/Object/Components/2D/2DComponent.d.ts +13 -0
  28. package/dist/lythreeframe/Object/Components/Camera/CameraComponent.d.ts +26 -0
  29. package/dist/lythreeframe/Object/Components/Component.d.ts +17 -0
  30. package/dist/lythreeframe/Object/Components/Light/DirectionalLight/DirectionalLightComponent.d.ts +15 -0
  31. package/dist/lythreeframe/Object/Components/Light/LightComponent.d.ts +12 -0
  32. package/dist/lythreeframe/Object/Components/Mesh/InstanceMesh/InstanceMeshComponent.d.ts +7 -0
  33. package/dist/lythreeframe/Object/Components/Mesh/InstanceMesh/MultiInstanceMeshComponent.d.ts +13 -0
  34. package/dist/lythreeframe/Object/Components/Mesh/MeshComponent.d.ts +22 -0
  35. package/dist/lythreeframe/Object/Components/Mesh/Shape/BoxComponent.d.ts +5 -0
  36. package/dist/lythreeframe/Object/Components/Mesh/Shape/PlaneComponent.d.ts +5 -0
  37. package/dist/lythreeframe/Object/Components/Mesh/Shape/SphereComponent.d.ts +5 -0
  38. package/dist/lythreeframe/Object/Components/Mesh/Sprite/SpriteComponent.d.ts +8 -0
  39. package/dist/lythreeframe/Object/Components/SceneComponent.d.ts +66 -0
  40. package/dist/lythreeframe/Object/PawnV2/FirstPerson.d.ts +5 -0
  41. package/dist/lythreeframe/Object/PawnV2/Oribital.d.ts +12 -0
  42. package/dist/lythreeframe/Object/PawnV2/Pawn.d.ts +14 -0
  43. package/dist/lythreeframe/Object/PawnV2/TransformControl.d.ts +46 -0
  44. package/dist/lythreeframe/PostProcess/Param/Bloom.d.ts +6 -0
  45. package/dist/lythreeframe/PostProcess/Param/DOF.d.ts +6 -0
  46. package/dist/lythreeframe/PostProcess/Param/Denoise.d.ts +7 -0
  47. package/dist/lythreeframe/PostProcess/Param/GTAO.d.ts +11 -0
  48. package/dist/lythreeframe/PostProcess/Param/LensFlare.d.ts +6 -0
  49. package/dist/lythreeframe/PostProcess/Param/MotionBlur.d.ts +4 -0
  50. package/dist/lythreeframe/PostProcess/Param/Outline.d.ts +17 -0
  51. package/dist/lythreeframe/PostProcess/Param/SSR.d.ts +6 -0
  52. package/dist/lythreeframe/PostProcess/Param/ToneMapping.d.ts +9 -0
  53. package/dist/lythreeframe/PostProcess/PostProcessParam.d.ts +16 -0
  54. package/dist/lythreeframe/PostProcess/WebGPUPostProcessFactory.d.ts +43 -0
  55. package/dist/lythreeframe/Shader/Postprocess/ColorShader.d.ts +29 -0
  56. package/dist/lythreeframe/ThreeJsApp.d.ts +35 -0
  57. package/package.json +26 -14
  58. package/dist/index.ts +0 -80
  59. package/rollup.config.js +0 -31
  60. package/src/draco/README.md +0 -32
  61. package/src/draco/draco_decoder.js +0 -34
  62. package/src/draco/draco_decoder.wasm +0 -0
  63. package/src/draco/draco_encoder.js +0 -33
  64. package/src/draco/draco_wasm_wrapper.js +0 -117
  65. package/src/draco/gltf/draco_decoder.js +0 -33
  66. package/src/draco/gltf/draco_decoder.wasm +0 -0
  67. package/src/draco/gltf/draco_encoder.js +0 -33
  68. package/src/draco/gltf/draco_wasm_wrapper.js +0 -116
  69. package/src/index.ts +0 -44
  70. package/src/lythreeframe/AssetManagement/AssetDefines.ts +0 -8
  71. package/src/lythreeframe/AssetManagement/AssetManager.ts +0 -288
  72. package/src/lythreeframe/AssetManagement/AssetPointer/AssetPointer.ts +0 -41
  73. package/src/lythreeframe/AssetManagement/Task/Task.ts +0 -24
  74. package/src/lythreeframe/Container/SmartPointer.ts +0 -54
  75. package/src/lythreeframe/Defines.ts +0 -11
  76. package/src/lythreeframe/Delegate.ts +0 -37
  77. package/src/lythreeframe/Factory/CameraFactory.ts +0 -81
  78. package/src/lythreeframe/Factory/RendererFactory.ts +0 -7
  79. package/src/lythreeframe/Frame/Controller.ts +0 -261
  80. package/src/lythreeframe/Frame/Viewport.ts +0 -516
  81. package/src/lythreeframe/Frame/World.ts +0 -59
  82. package/src/lythreeframe/Frame.ts +0 -511
  83. package/src/lythreeframe/Geometry/PlaneGeometry.ts +0 -53
  84. package/src/lythreeframe/Geometry/TriangleGeometry.ts +0 -50
  85. package/src/lythreeframe/Library/ContainerLibrary.ts +0 -21
  86. package/src/lythreeframe/Library/MaterialLibrary.ts +0 -292
  87. package/src/lythreeframe/Library/Math.ts +0 -154
  88. package/src/lythreeframe/Library/ResourceLibrary.ts +0 -21
  89. package/src/lythreeframe/Object/Actor.ts +0 -571
  90. package/src/lythreeframe/Object/Actors/Camera/CameraActor.ts +0 -11
  91. package/src/lythreeframe/Object/Actors/Light/DirectionalLightActor.ts +0 -18
  92. package/src/lythreeframe/Object/Actors/Shape/BoxActor.ts +0 -20
  93. package/src/lythreeframe/Object/Actors/Shape/CurveActor.ts +0 -11
  94. package/src/lythreeframe/Object/Actors/Shape/PlaneActor.ts +0 -27
  95. package/src/lythreeframe/Object/Actors/Shape/TubeActor.ts +0 -37
  96. package/src/lythreeframe/Object/BaseObject.ts +0 -45
  97. package/src/lythreeframe/Object/Components/2D/2DComponent.ts +0 -64
  98. package/src/lythreeframe/Object/Components/Camera/CameraComponent.ts +0 -113
  99. package/src/lythreeframe/Object/Components/Component.ts +0 -67
  100. package/src/lythreeframe/Object/Components/Light/DirectionalLight/DirectionalLightComponent.ts +0 -78
  101. package/src/lythreeframe/Object/Components/Light/LightComponent.ts +0 -54
  102. package/src/lythreeframe/Object/Components/Mesh/InstanceMesh/InstanceMeshComponent.ts +0 -39
  103. package/src/lythreeframe/Object/Components/Mesh/InstanceMesh/MultiInstanceMeshComponent.ts +0 -115
  104. package/src/lythreeframe/Object/Components/Mesh/Line/CurveComponent.ts +0 -221
  105. package/src/lythreeframe/Object/Components/Mesh/MeshComponent.ts +0 -162
  106. package/src/lythreeframe/Object/Components/Mesh/Shape/BoxComponent.ts +0 -17
  107. package/src/lythreeframe/Object/Components/Mesh/Shape/PlaneComponent.ts +0 -10
  108. package/src/lythreeframe/Object/Components/Mesh/Shape/SphereComponent.ts +0 -10
  109. package/src/lythreeframe/Object/Components/Mesh/Sprite/SpriteComponent.ts +0 -32
  110. package/src/lythreeframe/Object/Components/SceneComponent.ts +0 -811
  111. package/src/lythreeframe/Object/Controller/Controller.ts +0 -764
  112. package/src/lythreeframe/Object/Pawn/CameraStatus.ts +0 -262
  113. package/src/lythreeframe/Object/Pawn/FirstPerson.ts +0 -230
  114. package/src/lythreeframe/Object/Pawn/Oribital.ts +0 -276
  115. package/src/lythreeframe/Object/Pawn/PawnBase.ts +0 -189
  116. package/src/lythreeframe/Object/Pawn/TopView.ts +0 -205
  117. package/src/lythreeframe/Object/Pawn/TransformControl.ts +0 -215
  118. package/src/lythreeframe/Object/Pawn/staticCamera.ts +0 -80
  119. package/src/lythreeframe/Object/PawnV2/FirstPerson.ts +0 -12
  120. package/src/lythreeframe/Object/PawnV2/Oribital.ts +0 -45
  121. package/src/lythreeframe/Object/PawnV2/Pawn.ts +0 -50
  122. package/src/lythreeframe/Object/PawnV2/TransformControl.ts +0 -202
  123. package/src/lythreeframe/PostProcess/Param/Bloom.ts +0 -12
  124. package/src/lythreeframe/PostProcess/Param/DOF.ts +0 -13
  125. package/src/lythreeframe/PostProcess/Param/Denoise.ts +0 -14
  126. package/src/lythreeframe/PostProcess/Param/GTAO.ts +0 -21
  127. package/src/lythreeframe/PostProcess/Param/LensFlare.ts +0 -11
  128. package/src/lythreeframe/PostProcess/Param/MotionBlur.ts +0 -8
  129. package/src/lythreeframe/PostProcess/Param/Outline.ts +0 -20
  130. package/src/lythreeframe/PostProcess/Param/SSR.ts +0 -11
  131. package/src/lythreeframe/PostProcess/Param/ToneMapping.ts +0 -31
  132. package/src/lythreeframe/PostProcess/PostProcessParam.ts +0 -26
  133. package/src/lythreeframe/PostProcess/WebGPUPostProcessFactory.ts +0 -217
  134. package/src/lythreeframe/Shader/Postprocess/ColorShader.ts +0 -49
  135. package/src/lythreeframe/ThreeJsApp.ts +0 -124
  136. package/tsconfig.json +0 -105
@@ -0,0 +1,2747 @@
1
+ import { MathUtils, Group, Vector3, Box3, Quaternion, Euler, Matrix4, Mesh, BufferGeometry, Material, Texture, LoadingManager, FileLoader, Scene, NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping, NeutralToneMapping, Vector2, Raycaster, PerspectiveCamera, OrthographicCamera, Clock, DirectionalLight, MeshStandardMaterial, BoxGeometry, MeshBasicMaterial, PlaneGeometry, SphereGeometry, Object3D } from 'three';
2
+ import { DRACOLoader, GLTFLoader, CSS2DRenderer, OrbitControls } from 'three/examples/jsm/Addons.js';
3
+ import { pass, mrt, output, uniform, velocity, metalness, transformedNormalView, blendColor, time, oscSine } from 'three/tsl';
4
+ import { WebGPURenderer, PostProcessing, Color } from 'three/webgpu';
5
+ import { bloom } from 'three/examples/jsm/tsl/display/BloomNode.js';
6
+ import { denoise } from 'three/examples/jsm/tsl/display/DenoiseNode.js';
7
+ import { dof } from 'three/examples/jsm/tsl/display/DepthOfFieldNode.js';
8
+ import { ao } from 'three/examples/jsm/tsl/display/GTAONode.js';
9
+ import { ssr } from 'three/examples/jsm/tsl/display/SSRNode.js';
10
+ import { lensflare } from 'three/examples/jsm/tsl/display/LensflareNode.js';
11
+ import { outline } from 'three/examples/jsm/tsl/display/OutlineNode.js';
12
+ import { motionBlur } from 'three/examples/jsm/tsl/display/MotionBlur.js';
13
+ import { fxaa } from 'three/examples/jsm/tsl/display/FXAANode.js';
14
+ import { smaa } from 'three/examples/jsm/tsl/display/SMAANode.js';
15
+ import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
16
+ import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls';
17
+ import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js';
18
+
19
+ class LYLoadTask {
20
+ constructor(paths, onLoaded) {
21
+ this.paths = paths;
22
+ this.onLoaded = onLoaded;
23
+ }
24
+ get Paths() {
25
+ return this.paths;
26
+ }
27
+ onLoadingFinished(loaded) {
28
+ if (this.onLoaded) {
29
+ this.onLoaded(loaded);
30
+ }
31
+ }
32
+ }
33
+
34
+ /*
35
+ * virtual class of 3D Object, should not be use directly.
36
+ */
37
+ class BaseObject {
38
+ get IsTickable() {
39
+ return this.bCanTick;
40
+ }
41
+ set IsTickable(bCanTick) {
42
+ this.bCanTick = bCanTick;
43
+ }
44
+ get uuid() {
45
+ return this._uuid;
46
+ }
47
+ constructor(uuid = MathUtils.generateUUID()) {
48
+ this.bCanTick = false;
49
+ this.bCanTick = false;
50
+ this._uuid = uuid;
51
+ }
52
+ tick(deltaTime) {
53
+ if (!this.bCanTick) {
54
+ return;
55
+ }
56
+ }
57
+ destroy() {
58
+ }
59
+ }
60
+
61
+ class Component extends BaseObject {
62
+ set parentActor(value) {
63
+ this._parentActor = value;
64
+ }
65
+ get parentActor() {
66
+ return this._parentActor;
67
+ }
68
+ get threeObject() {
69
+ return this.obj;
70
+ }
71
+ set threeObject(newThreeObject) {
72
+ this.obj = newThreeObject;
73
+ if (this.obj) {
74
+ this.obj.userData["LYObject"] = this;
75
+ }
76
+ }
77
+ get name() {
78
+ return this._name;
79
+ }
80
+ set name(name) {
81
+ this._name = name;
82
+ }
83
+ constructor(newThreeObject) {
84
+ super(newThreeObject.uuid);
85
+ this.obj = null;
86
+ this._parentActor = null;
87
+ this._name = "Component";
88
+ this._parentActor = null;
89
+ this.threeObject = newThreeObject;
90
+ this._name = `${newThreeObject.type}Component`;
91
+ }
92
+ destroyObject() {
93
+ if (!this.obj) {
94
+ return;
95
+ }
96
+ }
97
+ destroy() {
98
+ this.destroyObject();
99
+ }
100
+ }
101
+
102
+ // export const LYAttachmentRules =
103
+ // {
104
+ // KeepWorld: 1,
105
+ // KeepRelative: 2,
106
+ // }
107
+ var AttachmentRules;
108
+ (function (AttachmentRules) {
109
+ AttachmentRules[AttachmentRules["KeepWorld"] = 1] = "KeepWorld";
110
+ AttachmentRules[AttachmentRules["KeepRelative"] = 2] = "KeepRelative";
111
+ })(AttachmentRules || (AttachmentRules = {}));
112
+
113
+ class SceneComponent extends Component {
114
+ set parentActor(value) {
115
+ this.childrenComponents.forEach((elem) => {
116
+ elem.parentActor = value;
117
+ });
118
+ this._parentActor = value;
119
+ }
120
+ get parentActor() {
121
+ // 通过 _parentActor 私有字段访问父级 Actor
122
+ return this._parentActor;
123
+ }
124
+ constructor(newThreeObject) {
125
+ super(newThreeObject);
126
+ this.bCanHover = false;
127
+ this.bCanClick = false;
128
+ this.world = null;
129
+ this._name = "SceneComponent";
130
+ }
131
+ createDefaultThreeObject() {
132
+ this.threeObject = new Group();
133
+ }
134
+ get isVisible() {
135
+ if (!this.threeObject) {
136
+ return false;
137
+ }
138
+ return this.threeObject.visible;
139
+ }
140
+ setVisible(bVisible) {
141
+ if (!this.threeObject) {
142
+ throw Error("threeObject is invalid");
143
+ }
144
+ this.threeObject.visible = bVisible;
145
+ if (this.world) {
146
+ this.world.viewport.markRenderStateDirty();
147
+ }
148
+ }
149
+ setLayers(layer) {
150
+ if (!this.threeObject) {
151
+ throw Error("threeObject is invalid");
152
+ }
153
+ this.threeObject.layers.set(layer);
154
+ let childComps = this.childrenComponents;
155
+ childComps.forEach((elem) => {
156
+ elem.setLayers(layer);
157
+ });
158
+ }
159
+ getBoundsCenterPositionWS() {
160
+ let ret = new Vector3();
161
+ this.getBounds().getCenter(ret);
162
+ return this.getWorldPosition();
163
+ }
164
+ getBoundsTopCenterPositionWS() {
165
+ let ret = new Vector3();
166
+ let bounds = this.getBounds();
167
+ bounds.getCenter(ret);
168
+ ret.y = bounds.max.y;
169
+ return ret;
170
+ }
171
+ getBoundsBottomCenterPositionWS() {
172
+ let ret = new Vector3();
173
+ let bounds = this.getBounds();
174
+ bounds.getCenter(ret);
175
+ ret.y = bounds.min.y;
176
+ return ret;
177
+ }
178
+ getBounds() {
179
+ if (!this.threeObject) {
180
+ throw Error("threeObject is invalid");
181
+ }
182
+ let ret = new Box3();
183
+ ret.setFromObject(this.threeObject, false);
184
+ if (ret.isEmpty()) {
185
+ ret.min = ret.max = this.getWorldPosition();
186
+ }
187
+ return ret;
188
+ }
189
+ get parentComponent() {
190
+ var _a, _b;
191
+ if (!this.threeObject) {
192
+ throw Error("threeObject is invalid");
193
+ }
194
+ let target = (_b = (_a = this.threeObject) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.userData.LYObject;
195
+ if (!target) {
196
+ return null;
197
+ }
198
+ if (!(target instanceof SceneComponent)) {
199
+ return null;
200
+ }
201
+ if (target.parentActor !== this.parentActor) {
202
+ return null;
203
+ }
204
+ return target;
205
+ }
206
+ get childrenComponents() {
207
+ if (!this.threeObject) {
208
+ throw Error("threeObject is invalid");
209
+ }
210
+ let childrens = [];
211
+ for (let i = 0; i < this.threeObject.children.length; ++i) {
212
+ if (!this.threeObject.children[i].userData.LYObject) {
213
+ console.warn("One of children component has no parentActor", this.threeObject.children[i].userData.LYObject, this.threeObject.children[i]);
214
+ continue;
215
+ }
216
+ if (this.threeObject.children[i].userData.LYObject.parentActor === this.parentActor) {
217
+ childrens.push(this.threeObject.children[i].userData.LYObject);
218
+ }
219
+ }
220
+ return childrens;
221
+ }
222
+ onAddedToWorld(world) {
223
+ this.world = world;
224
+ let childrens = this.childrenComponents;
225
+ for (let i = 0; i < childrens.length; ++i) {
226
+ childrens[i].onAddedToWorld(this.world);
227
+ }
228
+ }
229
+ destroy() {
230
+ this.isHoverEnabled = false;
231
+ this.isClickEnabled = false;
232
+ this.detachFromParentActor();
233
+ this.detachFromParentComponent();
234
+ this.destroyChildren();
235
+ this.destroyObject();
236
+ if (this.world) {
237
+ this.world.viewport.markRenderStateDirty();
238
+ }
239
+ }
240
+ destroyObject() {
241
+ if (!this.threeObject) {
242
+ return;
243
+ }
244
+ if (this.threeObject.parent) {
245
+ this.threeObject.parent.remove(this.threeObject);
246
+ }
247
+ super.destroyObject();
248
+ }
249
+ // Local Transforms
250
+ // // Position
251
+ getPosition() {
252
+ if (!this.threeObject) {
253
+ throw Error("threeObject is invalid");
254
+ }
255
+ if (this.threeObject.matrixAutoUpdate) {
256
+ return this.threeObject.position.clone();
257
+ }
258
+ else {
259
+ const position = new Vector3();
260
+ const quaternion = new Quaternion();
261
+ const scale = new Vector3();
262
+ this.threeObject.matrix.decompose(position, quaternion, scale);
263
+ return position;
264
+ }
265
+ }
266
+ setPosition(...args) {
267
+ if (!this.threeObject) {
268
+ throw Error("threeObject is invalid");
269
+ }
270
+ let newPosition = null;
271
+ if (args.length === 1 && args[0] instanceof Vector3) {
272
+ newPosition = new Vector3().copy(args[0]);
273
+ }
274
+ else if (args.length === 3 &&
275
+ typeof args[0] === "number" &&
276
+ typeof args[1] === "number" &&
277
+ typeof args[2] === "number") {
278
+ newPosition = new Vector3(args[0], args[1], args[2]);
279
+ }
280
+ if (!newPosition) {
281
+ return;
282
+ }
283
+ this.threeObject.position.copy(newPosition);
284
+ if (!this.threeObject.matrixAutoUpdate) {
285
+ this.threeObject.matrix.setPosition(newPosition.x, newPosition.y, newPosition.z);
286
+ }
287
+ this.threeObject.updateMatrix();
288
+ this.threeObject.updateMatrixWorld(true);
289
+ if (this.world) {
290
+ this.world.viewport.markRenderStateDirty();
291
+ }
292
+ }
293
+ // // Rotation
294
+ getRotation() {
295
+ if (!this.threeObject) {
296
+ throw Error("threeObject is invalid");
297
+ }
298
+ if (this.threeObject.matrixAutoUpdate) {
299
+ return this.threeObject.rotation.clone();
300
+ }
301
+ else {
302
+ const position = new Vector3();
303
+ const quaternion = new Quaternion();
304
+ const scale = new Vector3();
305
+ this.threeObject.matrix.decompose(position, quaternion, scale);
306
+ return new Euler().setFromQuaternion(quaternion);
307
+ }
308
+ }
309
+ setRotation(...args) {
310
+ if (!this.threeObject) {
311
+ throw Error("threeObject is invalid");
312
+ }
313
+ let newRotation = null;
314
+ if (args[0] instanceof Euler) {
315
+ newRotation = new Euler().copy(args[0]);
316
+ }
317
+ if (typeof (args[0]) === "number" && typeof (args[1]) === "number" && typeof (args[2]) === "number") {
318
+ newRotation = new Euler(args[0], args[1], args[2]);
319
+ }
320
+ if (!newRotation) {
321
+ return;
322
+ }
323
+ this.threeObject.rotation.copy(newRotation);
324
+ this.threeObject.updateMatrix();
325
+ this.threeObject.updateMatrixWorld(true);
326
+ if (this.world) {
327
+ this.world.viewport.markRenderStateDirty();
328
+ }
329
+ }
330
+ // // quat
331
+ getQuaternion() {
332
+ if (!this.threeObject) {
333
+ throw Error("threeObject is invalid");
334
+ }
335
+ if (this.threeObject.matrixAutoUpdate) {
336
+ return this.threeObject.quaternion.clone();
337
+ }
338
+ else {
339
+ const position = new Vector3();
340
+ const quaternion = new Quaternion();
341
+ const scale = new Vector3();
342
+ this.threeObject.matrix.decompose(position, quaternion, scale);
343
+ return quaternion;
344
+ }
345
+ }
346
+ setQuaternion(...args) {
347
+ if (!this.threeObject) {
348
+ throw Error("threeObject is invalid");
349
+ }
350
+ let newQuat = null;
351
+ if (args[0] instanceof Quaternion) {
352
+ newQuat = new Quaternion().copy(args[0]);
353
+ }
354
+ if (typeof args[0] === "number" && typeof args[1] === "number" && typeof args[2] === "number" && typeof args[3] === "number") {
355
+ newQuat = new Quaternion(args[0], args[1], args[2], args[3]);
356
+ }
357
+ if (!newQuat) {
358
+ console.error("setQuaternion failed,param is invalid", args);
359
+ return;
360
+ }
361
+ this.threeObject.quaternion.copy(newQuat);
362
+ if (!this.threeObject.matrixAutoUpdate) {
363
+ const position = new Vector3();
364
+ const quaternion = new Quaternion();
365
+ const scale = new Vector3();
366
+ this.threeObject.matrix.decompose(position, quaternion, scale);
367
+ this.threeObject.matrix.compose(position, newQuat, scale);
368
+ }
369
+ if (this.world) {
370
+ this.world.viewport.markRenderStateDirty();
371
+ }
372
+ this.threeObject.updateMatrix();
373
+ this.threeObject.updateMatrixWorld(true);
374
+ }
375
+ // // Scale
376
+ getScale() {
377
+ if (!this.threeObject) {
378
+ throw Error("threeObject is invalid");
379
+ }
380
+ if (this.threeObject.matrixAutoUpdate) {
381
+ return this.threeObject.scale.clone();
382
+ }
383
+ else {
384
+ const position = new Vector3();
385
+ const quaternion = new Quaternion();
386
+ const scale = new Vector3();
387
+ this.threeObject.matrix.decompose(position, quaternion, scale);
388
+ return scale;
389
+ }
390
+ }
391
+ setScale(...args) {
392
+ if (!this.threeObject) {
393
+ throw Error("threeObject is invalid");
394
+ }
395
+ let newScale = null;
396
+ if (args[0] instanceof Vector3) {
397
+ newScale = new Vector3().copy(args[0]);
398
+ }
399
+ if (typeof (args[0]) === "number" && typeof (args[1]) === "number" && typeof (args[2]) === "number") {
400
+ newScale = new Vector3(args[0], args[1], args[2]);
401
+ }
402
+ if (!newScale) {
403
+ return;
404
+ }
405
+ this.threeObject.scale.copy(newScale);
406
+ // if(!this.threeObject.matrixAutoUpdate)
407
+ // {
408
+ // const position = new Vector3();
409
+ // const quaternion = new Quaternion()
410
+ // const scale = new Vector3();
411
+ // this.threeObject.matrix.decompose(position, quaternion, scale)
412
+ // this.threeObject.matrix.compose(position, quaternion, new Vector3(param1, param2, param3))
413
+ // }
414
+ this.threeObject.updateMatrix();
415
+ this.threeObject.updateMatrixWorld(true);
416
+ if (this.world) {
417
+ this.world.viewport.markRenderStateDirty();
418
+ }
419
+ }
420
+ // matrix
421
+ getMatrix() {
422
+ if (!this.threeObject) {
423
+ throw Error("threeObject is invalid");
424
+ }
425
+ return this.threeObject.matrix;
426
+ }
427
+ setMatrix(matrix) {
428
+ var _a;
429
+ if (!this.threeObject) {
430
+ throw Error("threeObject is invalid");
431
+ }
432
+ // this.threeObject.matrix.copy(matrix)
433
+ matrix.decompose(this.threeObject.position, this.threeObject.quaternion, this.threeObject.scale);
434
+ this.threeObject.updateMatrix();
435
+ this.threeObject.updateMatrixWorld(true);
436
+ (_a = this.world) === null || _a === void 0 ? void 0 : _a.viewport.markRenderStateDirty();
437
+ }
438
+ // World Transform
439
+ // // WorldPosition
440
+ getWorldPosition() {
441
+ if (!this.threeObject) {
442
+ throw Error("threeObject is invalid");
443
+ }
444
+ let location = new Vector3();
445
+ this.threeObject.getWorldPosition(location);
446
+ return location;
447
+ }
448
+ // // rotation(euler)
449
+ getWorldRotation() {
450
+ if (!this.threeObject) {
451
+ throw Error("threeObject is invalid");
452
+ }
453
+ let rotation = new Quaternion();
454
+ this.threeObject.getWorldQuaternion(rotation);
455
+ return new Euler().setFromQuaternion(rotation);
456
+ }
457
+ // // rotation(quat)
458
+ getWorldQuaternion() {
459
+ if (!this.threeObject) {
460
+ throw Error("threeObject is invalid");
461
+ }
462
+ let rotation = new Quaternion();
463
+ this.threeObject.getWorldQuaternion(rotation);
464
+ return rotation;
465
+ }
466
+ // // Scale
467
+ getWorldScale() {
468
+ if (!this.threeObject) {
469
+ throw Error("threeObject is invalid");
470
+ }
471
+ let scale = new Vector3();
472
+ this.threeObject.getWorldScale(scale);
473
+ return scale;
474
+ }
475
+ // // Matrix
476
+ getWorldMatrix() {
477
+ if (!this.threeObject) {
478
+ throw Error("threeObject is invalid");
479
+ }
480
+ this.threeObject.updateMatrixWorld(true);
481
+ return this.threeObject.matrixWorld.clone();
482
+ }
483
+ setWorldMatrix(newMatrix) {
484
+ if (!this.threeObject) {
485
+ throw Error("threeObject is invalid");
486
+ }
487
+ if (this.threeObject.parent) {
488
+ this.threeObject.parent.updateMatrixWorld(true);
489
+ // 计算父对象的逆世界矩阵
490
+ const parentInverseWorldMatrix = this.threeObject.parent.matrixWorld.clone().invert();
491
+ // 计算物体的本地矩阵
492
+ const cLocalMatrix = new Matrix4().multiplyMatrices(parentInverseWorldMatrix, newMatrix);
493
+ // 更新物体的本地矩阵
494
+ this.setMatrix(cLocalMatrix);
495
+ // this.threeObject.matrix.copy(cLocalMatrix);
496
+ // this.threeObject.matrixAutoUpdate = false;
497
+ // this.threeObject.matrix.decompose(this.threeObject.position, this.threeObject.quaternion, this.threeObject.scale);
498
+ }
499
+ else {
500
+ this.setMatrix(newMatrix);
501
+ // // 没有父对象时直接设置世界矩阵为本地矩阵
502
+ // this.threeObject.matrix.copy(newMatrix);
503
+ // this.threeObject.matrixAutoUpdate = false;
504
+ }
505
+ // 更新世界矩阵
506
+ this.threeObject.updateMatrixWorld(true);
507
+ if (this.world) {
508
+ // 强制场景重新渲染
509
+ this.world.viewport.markRenderStateDirty();
510
+ }
511
+ }
512
+ getWorldForwardDirection() {
513
+ if (!this.threeObject) {
514
+ throw Error("threeObject is invalid");
515
+ }
516
+ let ret = new Vector3();
517
+ this.threeObject.getWorldDirection(ret);
518
+ return ret;
519
+ }
520
+ getWorldUpDirection() {
521
+ if (!this.threeObject) {
522
+ throw Error("threeObject is invalid");
523
+ }
524
+ let ret = new Vector3(0, 1, 0);
525
+ ret.applyQuaternion(this.getWorldQuaternion());
526
+ return ret;
527
+ }
528
+ getWorldRightDirection() {
529
+ if (!this.threeObject) {
530
+ throw Error("threeObject is invalid");
531
+ }
532
+ let ret = new Vector3(1, 0, 0);
533
+ ret.applyQuaternion(this.getWorldQuaternion());
534
+ return ret;
535
+ }
536
+ worldToLocal(vec) {
537
+ if (!this.threeObject) {
538
+ throw Error("threeObject is invalid");
539
+ }
540
+ return this.threeObject.worldToLocal(vec);
541
+ }
542
+ localToWorld(vec) {
543
+ if (!this.threeObject) {
544
+ throw Error("threeObject is invalid");
545
+ }
546
+ return this.threeObject.localToWorld(vec);
547
+ }
548
+ // Attachment
549
+ // // to another component
550
+ // this function will not set newComponent's parentActor
551
+ attachComponent(newComponent) {
552
+ if (!this.threeObject) {
553
+ throw Error("threeObject is invalid");
554
+ }
555
+ if (!newComponent.threeObject) {
556
+ throw Error("newComponent threeObject is invalid");
557
+ }
558
+ this.threeObject.add(newComponent.threeObject);
559
+ if (this.world) {
560
+ newComponent.onAddedToWorld(this.world);
561
+ }
562
+ }
563
+ detachComponent(target) {
564
+ if (!this.threeObject) {
565
+ throw Error("threeObject is invalid");
566
+ }
567
+ if (!target.threeObject) {
568
+ throw Error("target threeObject is invalid");
569
+ }
570
+ this.threeObject.remove(target.threeObject);
571
+ }
572
+ // newComponent's parentActor will be the same as this
573
+ addChildComponent(newComponent, attachmentRule = AttachmentRules.KeepRelative) {
574
+ if (!this.threeObject) {
575
+ throw Error("threeObject is invalid");
576
+ }
577
+ if (!newComponent.threeObject) {
578
+ throw Error("newComponent threeObject is invalid");
579
+ }
580
+ newComponent.parentActor = this.parentActor;
581
+ if (attachmentRule = AttachmentRules.KeepWorld) {
582
+ let worldMatrix = newComponent.getWorldMatrix();
583
+ this.threeObject.add(newComponent.threeObject);
584
+ newComponent.setWorldMatrix(worldMatrix);
585
+ }
586
+ else {
587
+ this.threeObject.add(newComponent.threeObject);
588
+ }
589
+ if (this.world) {
590
+ newComponent.onAddedToWorld(this.world);
591
+ }
592
+ this.threeObject.updateMatrix();
593
+ this.threeObject.updateMatrixWorld(true);
594
+ }
595
+ removeChildComponent(targetComponent) {
596
+ if (!this.threeObject) {
597
+ throw Error("threeObject is invalid");
598
+ }
599
+ if (!targetComponent.threeObject) {
600
+ throw Error("targetComponent threeObject is invalid");
601
+ }
602
+ this.threeObject.remove(targetComponent.threeObject);
603
+ targetComponent.destroy();
604
+ }
605
+ detachFromParentComponent() {
606
+ if (this.parentComponent) {
607
+ this.parentComponent.removeChildComponent(this);
608
+ }
609
+ }
610
+ detachFromParentActor() {
611
+ if (this.parentActor) {
612
+ this.parentActor = null;
613
+ }
614
+ }
615
+ destroyChildren() {
616
+ if (!this.threeObject) {
617
+ return;
618
+ }
619
+ for (let i = 0; i < this.threeObject.children.length; ++i) {
620
+ this.threeObject.children[i].userData.LYObject.destroy();
621
+ }
622
+ }
623
+ /* horver */
624
+ set isHoverEnabled(bCanHorver) {
625
+ this.bCanHover = bCanHorver;
626
+ let children = this.childrenComponents;
627
+ for (let i = 0; i < children.length; ++i) {
628
+ children[i].isHoverEnabled = this.bCanHover;
629
+ }
630
+ }
631
+ get isHoverEnabled() {
632
+ return this.bCanHover;
633
+ }
634
+ // click
635
+ set isClickEnabled(bCanClick) {
636
+ this.bCanClick = bCanClick;
637
+ let children = this.childrenComponents;
638
+ for (let i = 0; i < children.length; ++i) {
639
+ children[i].isClickEnabled = this.bCanClick;
640
+ }
641
+ }
642
+ get isClickEnabled() {
643
+ return this.bCanClick;
644
+ }
645
+ onHorveringBegin() {
646
+ if (!this.isHoverEnabled) {
647
+ return;
648
+ }
649
+ if (this.parentActor) {
650
+ let comp = this;
651
+ this.parentActor.onComponentHorveringBegin(comp);
652
+ }
653
+ }
654
+ onHorveringEnd() {
655
+ if (!this.isHoverEnabled) {
656
+ return;
657
+ }
658
+ if (this.parentActor) {
659
+ let comp = this;
660
+ this.parentActor.onComponentHorveringEnd(comp);
661
+ }
662
+ }
663
+ /* click */
664
+ onClicked() {
665
+ if (this.parentActor) {
666
+ let comp = this;
667
+ this.parentActor.onComponentClicked(comp);
668
+ }
669
+ }
670
+ onDoubleClicked() {
671
+ if (this.parentActor) {
672
+ let comp = this;
673
+ this.parentActor.onComponentDoubleClicked(comp);
674
+ }
675
+ }
676
+ }
677
+
678
+ class MeshComponent extends SceneComponent {
679
+ get threeObject() {
680
+ return this.obj;
681
+ }
682
+ set threeObject(newThreeObject) {
683
+ this.obj = newThreeObject;
684
+ if (this.obj) {
685
+ this.obj.userData["LYObject"] = this;
686
+ }
687
+ }
688
+ get geometry() {
689
+ return this.threeObject ? this.threeObject.geometry : null;
690
+ }
691
+ set geometry(geo) {
692
+ if (!this.threeObject) {
693
+ return;
694
+ }
695
+ this.threeObject.geometry = geo;
696
+ }
697
+ get material() {
698
+ return this.threeObject ? this.threeObject.material : null;
699
+ }
700
+ set material(newMat) {
701
+ if (!this.threeObject) {
702
+ return;
703
+ }
704
+ this.threeObject.material = newMat;
705
+ if (this.world) {
706
+ this.world.viewport.markRenderStateDirty();
707
+ }
708
+ }
709
+ constructor(...args) {
710
+ let newMesh = null;
711
+ if (args[0] instanceof Mesh) {
712
+ newMesh = args[0];
713
+ }
714
+ else {
715
+ if (args[0] instanceof BufferGeometry && (args[1] instanceof Material || Array.isArray(args[1]))) {
716
+ newMesh = new Mesh(args[0], args[1]);
717
+ }
718
+ else {
719
+ throw new Error('Invalid arguments for A constructor');
720
+ }
721
+ }
722
+ super(newMesh);
723
+ this.obj = null;
724
+ this.threeObject = newMesh;
725
+ LYAssetManager.Get().checkMeshResource(newMesh);
726
+ }
727
+ set castShadow(bCast) {
728
+ if (this.threeObject)
729
+ this.threeObject.castShadow = bCast;
730
+ }
731
+ get castShadow() {
732
+ return this.threeObject ? this.threeObject.castShadow : false;
733
+ }
734
+ set receiveShadow(bReceive) {
735
+ if (!this.threeObject) {
736
+ return;
737
+ }
738
+ this.threeObject.receiveShadow = bReceive;
739
+ }
740
+ get receiveShadow() {
741
+ return this.threeObject ? this.threeObject.receiveShadow : false;
742
+ }
743
+ getBoundsCenterPosition() {
744
+ let ret = new Vector3();
745
+ this.getBounds().getCenter(ret);
746
+ return ret;
747
+ }
748
+ getBoundsTopCenterPosition() {
749
+ let ret = new Vector3();
750
+ let bounds = this.getBounds();
751
+ bounds.getCenter(ret);
752
+ ret.y = bounds.max.y;
753
+ return ret;
754
+ }
755
+ getBoundsBottomCenterPosition() {
756
+ let ret = new Vector3();
757
+ let bounds = this.getBounds();
758
+ bounds.getCenter(ret);
759
+ ret.y = bounds.min.y;
760
+ return ret;
761
+ }
762
+ destroyObject() {
763
+ if (this.world && this.world.scene && this.threeObject) {
764
+ this.world.scene.remove(this.threeObject);
765
+ }
766
+ let assetManager = LYAssetManager.Get();
767
+ const mats = Array.isArray(this.material) ? this.material : [this.material];
768
+ mats.forEach((material) => {
769
+ if (material instanceof Material) {
770
+ Object.entries(material).forEach(([key, value]) => {
771
+ if (value instanceof Texture) {
772
+ assetManager.releaseAsset(value);
773
+ value = null;
774
+ }
775
+ });
776
+ assetManager.releaseAsset(material);
777
+ }
778
+ });
779
+ this.material = [];
780
+ if (this.geometry instanceof BufferGeometry) {
781
+ assetManager.releaseAsset(this.geometry);
782
+ this.geometry = null;
783
+ }
784
+ }
785
+ }
786
+
787
+ class TSmartPointer {
788
+ constructor(value, referenceCount = 1) {
789
+ this.referenceCount = 1;
790
+ this.value = null;
791
+ this.value = value;
792
+ this.referenceCount = referenceCount;
793
+ }
794
+ getRefCount() {
795
+ return this.referenceCount;
796
+ }
797
+ addRef(count = 1) {
798
+ if (this.value !== null) {
799
+ this.referenceCount += count;
800
+ }
801
+ }
802
+ release() {
803
+ if (this.value !== null) {
804
+ this.referenceCount--;
805
+ console.log(`Reference count decreased to: ${this.referenceCount}`);
806
+ if (this.referenceCount === 0) {
807
+ console.log("No more references. Deleting object.");
808
+ this.value = null;
809
+ }
810
+ }
811
+ }
812
+ forceRelease() {
813
+ if (this.value !== null) {
814
+ this.value = null;
815
+ this.referenceCount = 0;
816
+ }
817
+ }
818
+ getValue() {
819
+ return this.value;
820
+ }
821
+ }
822
+
823
+ class TAssetPointer extends TSmartPointer {
824
+ get uuid() {
825
+ return this._uuid;
826
+ }
827
+ constructor(value, referenceCount = 1) {
828
+ super(value, referenceCount);
829
+ this._uuid = value.uuid;
830
+ }
831
+ release() {
832
+ if (this.value !== null) {
833
+ this.referenceCount--;
834
+ if (this.referenceCount === 0) {
835
+ this.value.dispose();
836
+ this.value = null;
837
+ }
838
+ }
839
+ }
840
+ forceRelease() {
841
+ if (this.value !== null) {
842
+ this.value.dispose();
843
+ this.value = null;
844
+ this.referenceCount = 0;
845
+ }
846
+ }
847
+ }
848
+
849
+ // this class should be a singleton
850
+ class LYAssetManager {
851
+ constructor() {
852
+ this.assetPointer = new Map();
853
+ this.loadingManager = new LoadingManager();
854
+ this.dracoLoader = new DRACOLoader(this.loadingManager);
855
+ this.dracoLoader.setDecoderPath(`../../draco/`);
856
+ this.dracoLoader.setDecoderConfig({ type: "js" });
857
+ this.dracoLoader.preload();
858
+ this.gltfLoader = new GLTFLoader(this.loadingManager);
859
+ this.gltfLoader.setDRACOLoader(this.dracoLoader);
860
+ }
861
+ get LoadingManager() {
862
+ return this.loadingManager;
863
+ }
864
+ static Get() {
865
+ return assetManager;
866
+ }
867
+ static ClearAssets() {
868
+ assetManager.clearAssets();
869
+ }
870
+ convertThreeObjectToLYObject(parentLYComponent, threejsObject) {
871
+ let location = threejsObject.position.clone();
872
+ let rotation = threejsObject.rotation.clone();
873
+ let scale = threejsObject.scale.clone();
874
+ let newComp = null;
875
+ let children = threejsObject.children;
876
+ threejsObject.children = [];
877
+ if (threejsObject.type === "Group" || threejsObject.type === "Object3D") {
878
+ newComp = new SceneComponent(threejsObject);
879
+ }
880
+ else if (threejsObject.isMesh) {
881
+ let obj = threejsObject;
882
+ newComp = new MeshComponent(obj);
883
+ // assetManager.#collectMatAndGeoInMesh(threejsObject)
884
+ threejsObject.layers.set(0);
885
+ }
886
+ else {
887
+ newComp = new SceneComponent(threejsObject);
888
+ }
889
+ if (newComp === null) {
890
+ threejsObject.children = children;
891
+ //threejsObject.parent = parent
892
+ console.log("Unprocess", threejsObject);
893
+ return null;
894
+ }
895
+ parentLYComponent.addChildComponent(newComp);
896
+ newComp.setPosition(location);
897
+ newComp.setRotation(rotation);
898
+ newComp.setScale(scale);
899
+ for (let i = 0; i < children.length; ++i) {
900
+ this.convertThreeObjectToLYObject(newComp, children[i]);
901
+ }
902
+ return threejsObject.userData.LYObject;
903
+ }
904
+ collectResourcesAndReferences(gltf) {
905
+ function countResource(map, resource) {
906
+ if (!map.has(resource)) {
907
+ map.set(resource, 1);
908
+ }
909
+ else {
910
+ map.set(resource, map.get(resource) + 1);
911
+ }
912
+ }
913
+ const resources = {
914
+ geometries: new Map(),
915
+ materials: new Map(),
916
+ textures: new Map()
917
+ };
918
+ gltf.scene.traverse((child) => {
919
+ if (child.geometry && child.geometry instanceof BufferGeometry) {
920
+ countResource(resources.geometries, child.geometry);
921
+ }
922
+ if (child.material) {
923
+ const mats = Array.isArray(child.material) ? child.material : [child.material];
924
+ mats.forEach((mat) => {
925
+ if (mat) {
926
+ countResource(resources.materials, mat);
927
+ Object.entries(mat).forEach(([key, value]) => {
928
+ if (value instanceof Texture) {
929
+ countResource(resources.textures, value);
930
+ }
931
+ });
932
+ }
933
+ });
934
+ }
935
+ });
936
+ function createPointer(manager, map) {
937
+ map.forEach((value, key) => {
938
+ manager.addAsset(key, value);
939
+ });
940
+ }
941
+ createPointer(this, resources.geometries);
942
+ createPointer(this, resources.materials);
943
+ createPointer(this, resources.textures);
944
+ // console.log('Geometries:', Array.from(resources.geometries.entries()));
945
+ // console.log('Materials:', Array.from(resources.materials.entries()));
946
+ // console.log('Textures:', Array.from(resources.textures.entries()));
947
+ // console.log('assetPointer:', this.assetPointer);
948
+ }
949
+ checkMeshResource(mesh) {
950
+ if (mesh.geometry) {
951
+ if (!mesh.geometry.userData["assetPointer"]) {
952
+ this.addAsset(mesh.geometry);
953
+ }
954
+ }
955
+ if (mesh.material) {
956
+ const mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
957
+ mats.forEach((material) => {
958
+ if (material) {
959
+ if (!material.userData["assetPointer"]) {
960
+ this.addAsset(material);
961
+ }
962
+ Object.entries(material).forEach(([key, value]) => {
963
+ if (value instanceof Texture) {
964
+ if (!value.userData["assetPointer"]) {
965
+ this.addAsset(value);
966
+ }
967
+ }
968
+ });
969
+ }
970
+ });
971
+ }
972
+ }
973
+ loadMultiGLTFAsGroup(tasks, onProgress = null, onAllFinished = null) {
974
+ this.loadingManager.onLoad = onAllFinished ? () => {
975
+ this.loadingManager.onLoad = () => {
976
+ };
977
+ onAllFinished();
978
+ } : () => {
979
+ this.loadingManager.onLoad = () => {
980
+ };
981
+ };
982
+ this.loadingManager.onProgress = onProgress ? (url, loaded, total) => {
983
+ onProgress(url, loaded, total);
984
+ } : () => {
985
+ };
986
+ tasks.forEach((task) => {
987
+ task.Paths.forEach((path) => {
988
+ const onGlbLoaded = (glb) => {
989
+ task.onLoadingFinished(glb);
990
+ this.collectResourcesAndReferences(glb);
991
+ };
992
+ try {
993
+ this.gltfLoader.load(path, (glb) => {
994
+ onGlbLoaded(glb);
995
+ });
996
+ }
997
+ catch (e) {
998
+ console.error(e, path);
999
+ }
1000
+ });
1001
+ });
1002
+ }
1003
+ loadFile(filepath = '', onLoadFinished) {
1004
+ const loader = new FileLoader();
1005
+ loader.load(filepath, (data) => {
1006
+ onLoadFinished(data);
1007
+ });
1008
+ }
1009
+ addAsset(asset, referenceCount = 1) {
1010
+ let pointer = null;
1011
+ if (!asset.userData["assetPointer"]) {
1012
+ pointer = new TAssetPointer(asset, referenceCount);
1013
+ asset.userData["assetPointer"] = pointer;
1014
+ }
1015
+ else {
1016
+ pointer = asset.userData["assetPointer"];
1017
+ pointer.addRef(referenceCount);
1018
+ }
1019
+ if (pointer) {
1020
+ this.assetPointer.set(pointer.uuid, pointer);
1021
+ }
1022
+ }
1023
+ releaseAsset(asset) {
1024
+ // let uuid = asset.uuid
1025
+ let p = asset.userData["assetPointer"];
1026
+ if (p) {
1027
+ p.release();
1028
+ }
1029
+ else {
1030
+ asset.dispose();
1031
+ }
1032
+ }
1033
+ clearAssets() {
1034
+ let pointers = Array.from(this.assetPointer.values());
1035
+ pointers.forEach((p) => {
1036
+ p.forceRelease();
1037
+ });
1038
+ this.assetPointer.clear();
1039
+ }
1040
+ }
1041
+ let assetManager = new LYAssetManager();
1042
+
1043
+ class Delegate {
1044
+ constructor() {
1045
+ this.functions = [];
1046
+ }
1047
+ broadcast(...args) {
1048
+ this.functions.forEach((func) => func(...args));
1049
+ }
1050
+ add(func) {
1051
+ this.functions.push(func);
1052
+ }
1053
+ remove(func) {
1054
+ const index = this.functions.indexOf(func);
1055
+ if (index >= 0) {
1056
+ this.functions.splice(index, 1);
1057
+ }
1058
+ else {
1059
+ console.warn("function not found");
1060
+ }
1061
+ }
1062
+ clear() {
1063
+ this.functions = [];
1064
+ }
1065
+ }
1066
+
1067
+ const DefaultPostProcessParam = {
1068
+ toneMapping: {
1069
+ exposure: 1,
1070
+ toneMapping: "AgX"
1071
+ }
1072
+ };
1073
+
1074
+ class World {
1075
+ get scene() {
1076
+ return this._scene;
1077
+ }
1078
+ get viewport() {
1079
+ return this.app.viewport;
1080
+ }
1081
+ get controller() {
1082
+ return this.app.controller;
1083
+ }
1084
+ constructor(app) {
1085
+ this.actors = new Set();
1086
+ this.app = app;
1087
+ this._scene = new Scene();
1088
+ }
1089
+ init() {
1090
+ }
1091
+ tick(deltaTime) {
1092
+ this.actors.forEach((elem) => {
1093
+ elem.tick(deltaTime);
1094
+ });
1095
+ }
1096
+ addActor(actor) {
1097
+ if (!actor.rootComponent.threeObject) {
1098
+ throw Error("actor.threeObject is null");
1099
+ }
1100
+ actor.removeFromParent();
1101
+ this.scene.add(actor.rootComponent.threeObject);
1102
+ this.actors.add(actor);
1103
+ actor.onAddedToWorld(this);
1104
+ this.viewport.markRenderStateDirty();
1105
+ }
1106
+ destroy() {
1107
+ }
1108
+ }
1109
+
1110
+ const DefaultToneMappingParams = {
1111
+ exposure: 1.0,
1112
+ toneMapping: 'AgX',
1113
+ };
1114
+ const ToneMappingOptions = new Map([
1115
+ ["None", NoToneMapping],
1116
+ ["Linear", LinearToneMapping],
1117
+ ["Reinhard", ReinhardToneMapping],
1118
+ ["Cineon", CineonToneMapping],
1119
+ ["ACESFilmic", ACESFilmicToneMapping],
1120
+ ["AgX", AgXToneMapping],
1121
+ ["Neutral", NeutralToneMapping]
1122
+ ]);
1123
+
1124
+ class WebGPUPostProcessFactory {
1125
+ // scene pass
1126
+ static constructScenePass(scene, camera) {
1127
+ const scenePass = pass(scene, camera);
1128
+ scenePass.setMRT(mrt({
1129
+ output: output,
1130
+ }));
1131
+ return scenePass;
1132
+ }
1133
+ static updateScenePass(scenePass, outputNodes) {
1134
+ let oldMRT = scenePass.getMRT();
1135
+ let newMRT = mrt(outputNodes);
1136
+ if (oldMRT) {
1137
+ scenePass.setMRT(oldMRT.merge(newMRT));
1138
+ }
1139
+ else {
1140
+ scenePass.setMRT(newMRT);
1141
+ }
1142
+ console.log(scenePass.getMRT(), "MRTTTTTTT");
1143
+ }
1144
+ // bloom pass
1145
+ static constructBloomPass(scenePass, params) {
1146
+ const scenePassColor = scenePass.getTextureNode('output');
1147
+ const bloomPass = bloom(scenePassColor);
1148
+ WebGPUPostProcessFactory.updateBloomPass(bloomPass, params);
1149
+ return bloomPass;
1150
+ }
1151
+ static updateBloomPass(bloomPass, params) {
1152
+ bloomPass.threshold.value = params.threshold;
1153
+ bloomPass.strength.value = params.strength;
1154
+ bloomPass.radius.value = params.radius;
1155
+ }
1156
+ // DOF pass
1157
+ static constructDOFPass(scenePass, params) {
1158
+ const scenePassColor = scenePass.getTextureNode('output');
1159
+ const scenePassViewZ = scenePass.getViewZNode();
1160
+ return dof(scenePassColor, scenePassViewZ, uniform(params.focus), uniform(params.aperture).mul(0.00001), uniform(params.maxblur));
1161
+ }
1162
+ static updateDOFPass(dofPass, params) {
1163
+ dofPass.focus.value = params.focus;
1164
+ dofPass.aperture.value = params.aperture * 0.00001;
1165
+ dofPass.maxblur.value = params.maxblur;
1166
+ }
1167
+ // motion blur
1168
+ static constructMotionBlurPass(lastNode, scenePass, param) {
1169
+ WebGPUPostProcessFactory.updateScenePass(scenePass, { velocity });
1170
+ const scenePassVelocity = scenePass.getTextureNode('velocity').mul(param.blurAmount);
1171
+ const scenePassColor = scenePass.getTextureNode('output');
1172
+ const motionBlurPass = motionBlur(scenePassColor, scenePassVelocity);
1173
+ return motionBlurPass;
1174
+ }
1175
+ // ssr
1176
+ static constructSSRPass(scenePass, param) {
1177
+ WebGPUPostProcessFactory.updateScenePass(scenePass, {
1178
+ normal: transformedNormalView,
1179
+ metalness: metalness,
1180
+ });
1181
+ const scenePassColor = scenePass.getTextureNode('output');
1182
+ const scenePassNormal = scenePass.getTextureNode('normal');
1183
+ const scenePassDepth = scenePass.getTextureNode('depth');
1184
+ const scenePassMetalness = scenePass.getTextureNode('metalness');
1185
+ const ssrPass = ssr(scenePassColor, scenePassDepth, scenePassNormal, scenePassMetalness, scenePass.camera);
1186
+ ssrPass.resolutionScale = 1.0;
1187
+ WebGPUPostProcessFactory.updateSSRPass(ssrPass, param);
1188
+ return ssrPass;
1189
+ }
1190
+ static updateSSRPass(ssrPass, params) {
1191
+ ssrPass.maxDistance.value = params.maxDistance;
1192
+ ssrPass.opacity.value = params.opacity;
1193
+ ssrPass.thickness.value = params.thickness;
1194
+ }
1195
+ // GTAO
1196
+ static constructGTAOPass(scenePass, params) {
1197
+ WebGPUPostProcessFactory.updateScenePass(scenePass, {
1198
+ normal: transformedNormalView,
1199
+ });
1200
+ const scenePassNormal = scenePass.getTextureNode('normal');
1201
+ const scenePassDepth = scenePass.getTextureNode('depth');
1202
+ const aoPass = ao(scenePassDepth, scenePassNormal, scenePass.camera);
1203
+ aoPass.resolutionScale = 0.5;
1204
+ WebGPUPostProcessFactory.updateGTAOPass(aoPass, params);
1205
+ return aoPass;
1206
+ }
1207
+ static updateGTAOPass(gtaoPass, params) {
1208
+ gtaoPass.distanceExponent.value = params.distanceExponent;
1209
+ gtaoPass.distanceFallOff.value = params.distanceFallOff;
1210
+ gtaoPass.radius.value = params.radius;
1211
+ gtaoPass.scale.value = params.scale;
1212
+ gtaoPass.thickness.value = params.thickness;
1213
+ }
1214
+ static constructDenoisePass(scenePass, inputNode, params) {
1215
+ WebGPUPostProcessFactory.updateScenePass(scenePass, {
1216
+ normal: transformedNormalView,
1217
+ });
1218
+ const scenePassNormal = scenePass.getTextureNode('normal');
1219
+ const scenePassDepth = scenePass.getTextureNode('depth');
1220
+ const denoisePass = denoise(inputNode, scenePassDepth, scenePassNormal, scenePass.camera);
1221
+ WebGPUPostProcessFactory.updateDenoisePass(denoisePass, params);
1222
+ return denoisePass;
1223
+ }
1224
+ static updateDenoisePass(denoisePass, params) {
1225
+ denoisePass.radius.value = params.denoiseRadius;
1226
+ denoisePass.lumaPhi.value = params.lumaPhi;
1227
+ denoisePass.depthPhi.value = params.depthPhi;
1228
+ denoisePass.normalPhi.value = params.normalPhi;
1229
+ }
1230
+ static constructGTAODenoisePass(scenePass, gtaoPass, params) {
1231
+ return WebGPUPostProcessFactory.constructDenoisePass(scenePass, gtaoPass.getTextureNode(), params.denoiseParam);
1232
+ }
1233
+ static updateGTAODenoisePass(denoisePass, params) {
1234
+ WebGPUPostProcessFactory.updateDenoisePass(denoisePass, params.denoiseParam);
1235
+ }
1236
+ static constructLensFlarePass(inputNode, params) {
1237
+ const lensflarePass = lensflare(inputNode, {
1238
+ threshold: uniform(params.threshold),
1239
+ ghostAttenuationFactor: uniform(params.ghostAttenuationFactor),
1240
+ ghostSpacing: uniform(params.ghostSpacing),
1241
+ });
1242
+ return lensflarePass;
1243
+ }
1244
+ static updateLensFlarPass(lensflarePass, params) {
1245
+ lensflarePass.thresholdNode.value = params.threshold;
1246
+ lensflarePass.ghostSpacingNode.value = params.ghostSpacing;
1247
+ lensflarePass.ghostAttenuationFactorNode.value = params.ghostAttenuationFactor;
1248
+ }
1249
+ static constructOutlinePass(scene, camera, outlineObjects, param) {
1250
+ const outlinePass = outline(scene, camera, {
1251
+ selectedObjects: outlineObjects,
1252
+ edgeGlow: param.edgeGlow,
1253
+ edgeThickness: param.edgeThickness
1254
+ });
1255
+ return outlinePass;
1256
+ }
1257
+ static updateOutlinePass(outlinePass, outlineObjects, params) {
1258
+ outlinePass.edgeGlowNode.value = params.edgeGlow;
1259
+ outlinePass.edgeThicknessNode.value = params.edgeThickness;
1260
+ outlinePass.selectedObjects = outlineObjects;
1261
+ }
1262
+ // fxaa
1263
+ static constructFXAAPass(inputNode) {
1264
+ return fxaa(inputNode);
1265
+ }
1266
+ // smaa
1267
+ static constructSMAAPass(inputNode) {
1268
+ return smaa(inputNode);
1269
+ }
1270
+ }
1271
+
1272
+ const DefaultOutlineParams = {
1273
+ edgeStrength: 3.0,
1274
+ edgeGlow: 1.0,
1275
+ edgeThickness: 1.0,
1276
+ pulsePeriod: 0.0,
1277
+ visibleEdgeColor: 0xffffff,
1278
+ hiddenEdgeColor: 0xffffff,
1279
+ };
1280
+
1281
+ class Viewport {
1282
+ get canvas() {
1283
+ if (!this._canvas) {
1284
+ throw Error("Canvas is not initialized");
1285
+ }
1286
+ return this._canvas;
1287
+ }
1288
+ get renderer() {
1289
+ if (!this._renderer) {
1290
+ throw Error("Renderer is not initialized");
1291
+ }
1292
+ return this._renderer;
1293
+ }
1294
+ get app() {
1295
+ if (!this._app) {
1296
+ throw Error("App is not initialized");
1297
+ }
1298
+ return this._app;
1299
+ }
1300
+ constructor(app, elementId, rendererParam = { antialias: true }, postProcessParam = {}) {
1301
+ this._renderer = null;
1302
+ this.labelRenderer = null;
1303
+ this._app = null;
1304
+ this.resizeObserver = null;
1305
+ this._canvas = null;
1306
+ this.isRenderStateDirty = true;
1307
+ this.postProcessParam = {};
1308
+ this.postProcessing = null;
1309
+ this.scenePass = null;
1310
+ this.bloomPass = null;
1311
+ this.dofPass = null;
1312
+ this.GTAOPass = null;
1313
+ this.denoiseGTAOPass = null;
1314
+ // private denoiseOutlinePass: ShaderNodeObject<DenoiseNode> | null = null;
1315
+ this.ssrPass = null;
1316
+ this.outlinePass = null;
1317
+ // private motionBlurPass: ShaderNodeObject<Node> | null = null;
1318
+ this.fxaaPass = null;
1319
+ this.smaaPass = null;
1320
+ this.outlineObjects = [];
1321
+ this.postProcessParam = postProcessParam;
1322
+ this._app = app;
1323
+ let element = document.getElementById(elementId);
1324
+ if (!element) {
1325
+ throw Error(`Can not find domElement which id is ${elementId}`);
1326
+ }
1327
+ this._renderer = new WebGPURenderer(rendererParam);
1328
+ this._renderer.setPixelRatio(window.devicePixelRatio);
1329
+ this._renderer.setSize(element.clientWidth, element.clientHeight);
1330
+ this._renderer.shadowMap.enabled = true;
1331
+ this.labelRenderer = new CSS2DRenderer();
1332
+ this.labelRenderer.setSize(element.clientWidth, element.clientHeight);
1333
+ this.labelRenderer.domElement.style.pointerEvents = 'none';
1334
+ this.labelRenderer.domElement.style.position = 'absolute';
1335
+ this.labelRenderer.domElement.style.top = '0px';
1336
+ this.labelRenderer.domElement.className = 'scene-labelRenderer';
1337
+ element.appendChild(this.labelRenderer.domElement);
1338
+ element.appendChild(this._renderer.domElement);
1339
+ this._canvas = element;
1340
+ this.resizeObserver = new ResizeObserver((entries) => {
1341
+ for (let entry of entries) {
1342
+ if (entry.contentBoxSize) {
1343
+ this.onWindowResize();
1344
+ }
1345
+ }
1346
+ });
1347
+ this.resizeObserver.observe(this._canvas);
1348
+ this.setupPostProcess();
1349
+ }
1350
+ init() {
1351
+ }
1352
+ setupPostProcess() {
1353
+ if (this.postProcessParam.toneMapping) {
1354
+ let toneMapping = ToneMappingOptions.get(this.postProcessParam.toneMapping.toneMapping);
1355
+ if (toneMapping) {
1356
+ this.renderer.toneMapping = toneMapping;
1357
+ }
1358
+ this.renderer.toneMappingExposure = this.postProcessParam.toneMapping.exposure;
1359
+ }
1360
+ if (!(Object.keys(this.postProcessParam).some(key => ['bloom', 'dof', 'gtao', 'ssr', "outline"].includes(key)))) {
1361
+ this.destroyPostProcess();
1362
+ return;
1363
+ }
1364
+ if (!this.postProcessing) {
1365
+ this.postProcessing = new PostProcessing(this.renderer);
1366
+ }
1367
+ if (!this.scenePass) {
1368
+ this.scenePass = WebGPUPostProcessFactory.constructScenePass(this.app.world.scene, this.app.camera);
1369
+ }
1370
+ let finalNode = this.scenePass.getTextureNode('output');
1371
+ // bloom
1372
+ if (this.postProcessParam.bloom) {
1373
+ if (!this.bloomPass) {
1374
+ this.bloomPass = WebGPUPostProcessFactory.constructBloomPass(this.scenePass, this.postProcessParam.bloom);
1375
+ }
1376
+ else {
1377
+ WebGPUPostProcessFactory.updateBloomPass(this.bloomPass, this.postProcessParam.bloom);
1378
+ }
1379
+ finalNode = finalNode.add(this.bloomPass);
1380
+ }
1381
+ else {
1382
+ if (this.bloomPass) {
1383
+ this.bloomPass.dispose();
1384
+ this.bloomPass = null;
1385
+ }
1386
+ }
1387
+ // DOF
1388
+ if (this.postProcessParam.dof) {
1389
+ if (!this.dofPass) {
1390
+ this.dofPass = WebGPUPostProcessFactory.constructDOFPass(this.scenePass, this.postProcessParam.dof);
1391
+ }
1392
+ else {
1393
+ WebGPUPostProcessFactory.updateDOFPass(this.dofPass, this.postProcessParam.dof);
1394
+ }
1395
+ finalNode = this.dofPass;
1396
+ }
1397
+ else {
1398
+ if (this.dofPass) {
1399
+ this.dofPass.dispose();
1400
+ this.dofPass = null;
1401
+ }
1402
+ }
1403
+ // motion blur
1404
+ // if (this.postProcessParam.motionBlur)
1405
+ // {
1406
+ // if (!this.motionBlurPass)
1407
+ // {
1408
+ // this.motionBlurPass = WebGPUPostProcessFactory.constructMotionBlurPass(finalNode, this.scenePass, this.postProcessParam.motionBlur);
1409
+ // }
1410
+ // const vignette = screenUV.distance(.5).remap(.6, 1).mul(2).clamp().oneMinus();
1411
+ // finalNode = mix(this.motionBlurPass, this.scenePass.getTextureNode("velocity"), 0).mul(vignette);
1412
+ // }
1413
+ // else
1414
+ // {
1415
+ // if (this.motionBlurPass)
1416
+ // {
1417
+ // this.motionBlurPass.dispose();
1418
+ // this.motionBlurPass = null;
1419
+ // }
1420
+ // }
1421
+ // ssr
1422
+ if (this.postProcessParam.ssr) {
1423
+ if (!this.ssrPass) {
1424
+ this.ssrPass = WebGPUPostProcessFactory.constructSSRPass(this.scenePass, this.postProcessParam.ssr);
1425
+ }
1426
+ else {
1427
+ WebGPUPostProcessFactory.updateSSRPass(this.ssrPass, this.postProcessParam.ssr);
1428
+ }
1429
+ finalNode = blendColor(finalNode, this.ssrPass);
1430
+ }
1431
+ // GTAO
1432
+ if (this.postProcessParam.gtao) {
1433
+ if (!this.GTAOPass) {
1434
+ this.GTAOPass = WebGPUPostProcessFactory.constructGTAOPass(this.scenePass, this.postProcessParam.gtao);
1435
+ }
1436
+ else {
1437
+ WebGPUPostProcessFactory.updateGTAOPass(this.GTAOPass, this.postProcessParam.gtao);
1438
+ }
1439
+ if (this.postProcessParam.gtao.denoised) {
1440
+ if (!this.denoiseGTAOPass) {
1441
+ this.denoiseGTAOPass = WebGPUPostProcessFactory.constructGTAODenoisePass(this.scenePass, this.GTAOPass, this.postProcessParam.gtao);
1442
+ }
1443
+ else {
1444
+ WebGPUPostProcessFactory.updateGTAOPass(this.GTAOPass, this.postProcessParam.gtao);
1445
+ }
1446
+ finalNode = this.denoiseGTAOPass.mul(finalNode);
1447
+ }
1448
+ else {
1449
+ finalNode = this.GTAOPass.getTextureNode().mul(finalNode);
1450
+ if (this.denoiseGTAOPass) {
1451
+ this.denoiseGTAOPass.dispose();
1452
+ this.denoiseGTAOPass = null;
1453
+ }
1454
+ }
1455
+ }
1456
+ else {
1457
+ if (this.GTAOPass) {
1458
+ this.GTAOPass.dispose();
1459
+ this.GTAOPass = null;
1460
+ }
1461
+ if (this.denoiseGTAOPass) {
1462
+ this.denoiseGTAOPass.dispose();
1463
+ this.denoiseGTAOPass = null;
1464
+ }
1465
+ }
1466
+ // outline
1467
+ if (this.postProcessParam.outline) {
1468
+ if (!this.outlinePass) {
1469
+ this.outlinePass = WebGPUPostProcessFactory.constructOutlinePass(this.app.world.scene, this.scenePass.camera, this.outlineObjects, this.postProcessParam.outline);
1470
+ }
1471
+ else {
1472
+ WebGPUPostProcessFactory.updateOutlinePass(this.outlinePass, this.outlineObjects, this.postProcessParam.outline);
1473
+ }
1474
+ const { visibleEdge, hiddenEdge } = this.outlinePass;
1475
+ const pulsePeriod = uniform(this.postProcessParam.outline.pulsePeriod);
1476
+ const period = time.div(pulsePeriod).mul(2);
1477
+ const osc = oscSine(period).mul(.5).add(.5);
1478
+ const outlineColor = visibleEdge.mul(uniform(new Color(this.postProcessParam.outline.visibleEdgeColor))).add(hiddenEdge.mul(uniform(new Color(this.postProcessParam.outline.hiddenEdgeColor)))).mul(this.postProcessParam.outline.edgeStrength);
1479
+ const outlinePulse = pulsePeriod.greaterThan(0).select(outlineColor.mul(osc), outlineColor);
1480
+ // if(!this.denoiseOutlinePass)
1481
+ // {
1482
+ // this.denoiseOutlinePass = WebGPUPostProcessFactory.constructDenoisePass(this.scenePass, outlinePulse, DefaultDenoiseParam);
1483
+ // }
1484
+ // outlinePulse = this.denoiseOutlinePass.mul(outlinePulse)
1485
+ // finalNode = this.denoiseOutlinePass.mul(outlinePulse).add(finalNode);
1486
+ finalNode = outlinePulse.add(finalNode);
1487
+ // finalNode = this.denoiseOutlinePass.mul(outlinePulse).add(finalNode);
1488
+ }
1489
+ else {
1490
+ if (this.outlinePass) {
1491
+ this.outlinePass.dispose();
1492
+ this.outlinePass = null;
1493
+ }
1494
+ }
1495
+ if (this.postProcessParam.aa) {
1496
+ if (this.postProcessParam.aa === "fxaa") {
1497
+ this.fxaaPass = WebGPUPostProcessFactory.constructFXAAPass(finalNode);
1498
+ finalNode = this.fxaaPass;
1499
+ }
1500
+ if (this.postProcessParam.aa === "smaa") {
1501
+ this.smaaPass = WebGPUPostProcessFactory.constructSMAAPass(finalNode);
1502
+ finalNode = this.smaaPass;
1503
+ }
1504
+ }
1505
+ else {
1506
+ if (this.fxaaPass) {
1507
+ this.fxaaPass.dispose();
1508
+ this.fxaaPass = null;
1509
+ }
1510
+ if (this.smaaPass) {
1511
+ this.smaaPass.dispose();
1512
+ this.smaaPass = null;
1513
+ }
1514
+ }
1515
+ console.log(this.postProcessParam, "sssssssssssssssssssss");
1516
+ this.postProcessing.outputNode = finalNode;
1517
+ this.markRenderStateDirty();
1518
+ }
1519
+ updateToneMappingParam(params) {
1520
+ this.postProcessParam.toneMapping = params;
1521
+ this.setupPostProcess();
1522
+ this.markRenderStateDirty();
1523
+ }
1524
+ updateBloomPass(params) {
1525
+ this.postProcessParam.bloom = params;
1526
+ this.setupPostProcess();
1527
+ }
1528
+ updateGTAOParam(params) {
1529
+ this.postProcessParam.gtao = params;
1530
+ this.setupPostProcess();
1531
+ }
1532
+ updateDOFParam(params) {
1533
+ this.postProcessParam.dof = params;
1534
+ this.setupPostProcess();
1535
+ }
1536
+ updateSSRParam(params) {
1537
+ this.postProcessParam.ssr = params;
1538
+ this.setupPostProcess();
1539
+ }
1540
+ updateOutlineParam(params) {
1541
+ this.postProcessParam.outline = params;
1542
+ this.setupPostProcess();
1543
+ }
1544
+ addOutlineObject(obj) {
1545
+ if (!this.outlineObjects.includes(obj)) {
1546
+ this.outlineObjects.push(obj);
1547
+ }
1548
+ if (!this.postProcessParam.outline) {
1549
+ this.postProcessParam.outline = DefaultOutlineParams;
1550
+ this.setupPostProcess();
1551
+ }
1552
+ else {
1553
+ this.markRenderStateDirty();
1554
+ }
1555
+ }
1556
+ removeOutlineObject(obj) {
1557
+ if (!this.postProcessParam.outline) {
1558
+ this.outlineObjects = [];
1559
+ return;
1560
+ }
1561
+ const index = this.outlineObjects.indexOf(obj);
1562
+ if (index > -1) {
1563
+ this.outlineObjects.splice(index, 1);
1564
+ }
1565
+ this.markRenderStateDirty();
1566
+ }
1567
+ destroyPostProcess() {
1568
+ if (this.bloomPass) {
1569
+ this.bloomPass.dispose();
1570
+ this.bloomPass = null;
1571
+ }
1572
+ if (this.dofPass) {
1573
+ this.dofPass.dispose();
1574
+ this.dofPass = null;
1575
+ }
1576
+ if (this.GTAOPass) {
1577
+ this.GTAOPass.dispose();
1578
+ this.GTAOPass = null;
1579
+ }
1580
+ if (this.denoiseGTAOPass) {
1581
+ this.denoiseGTAOPass.dispose();
1582
+ this.denoiseGTAOPass = null;
1583
+ }
1584
+ if (this.ssrPass) {
1585
+ this.ssrPass.dispose();
1586
+ this.ssrPass = null;
1587
+ }
1588
+ if (this.outlinePass) {
1589
+ this.outlinePass.dispose();
1590
+ this.outlinePass = null;
1591
+ }
1592
+ this.outlineObjects = [];
1593
+ if (this.scenePass) {
1594
+ this.scenePass.dispose();
1595
+ this.scenePass = null;
1596
+ }
1597
+ if (this.postProcessing) {
1598
+ this.postProcessing.dispose();
1599
+ this.postProcessing = null;
1600
+ }
1601
+ }
1602
+ onWindowResize() {
1603
+ console.log("resize");
1604
+ let ele = this._canvas;
1605
+ if (!ele) {
1606
+ return;
1607
+ }
1608
+ this.app.onWindowResize(ele.clientWidth, ele.clientHeight);
1609
+ this.renderer.setSize(ele.clientWidth, ele.clientHeight);
1610
+ if (this.labelRenderer)
1611
+ this.labelRenderer.setSize(ele.clientWidth, ele.clientHeight);
1612
+ this.markRenderStateDirty();
1613
+ }
1614
+ markRenderStateDirty() {
1615
+ this.isRenderStateDirty = true;
1616
+ }
1617
+ render() {
1618
+ if (!this.isRenderStateDirty) {
1619
+ return;
1620
+ }
1621
+ if (this.postProcessing) {
1622
+ this.postProcessing.render();
1623
+ }
1624
+ else { //console.log("render renderer");
1625
+ this.renderer.render(this.app.world.scene, this.app.camera);
1626
+ }
1627
+ this.isRenderStateDirty = false;
1628
+ }
1629
+ destroy() {
1630
+ var _a;
1631
+ this.destroyPostProcess();
1632
+ this.renderer.setAnimationLoop(null);
1633
+ if (this.resizeObserver) {
1634
+ this.resizeObserver.disconnect();
1635
+ this.resizeObserver = null;
1636
+ }
1637
+ if (this.labelRenderer) {
1638
+ this.labelRenderer.domElement.remove();
1639
+ this.labelRenderer = null;
1640
+ }
1641
+ if (this._renderer) {
1642
+ this.renderer.domElement.remove();
1643
+ this._renderer.dispose();
1644
+ this._renderer = null;
1645
+ }
1646
+ (_a = this.postProcessing) === null || _a === void 0 ? void 0 : _a.dispose();
1647
+ this._canvas = null;
1648
+ this._app = null;
1649
+ this._renderer = null;
1650
+ this.outlineObjects = [];
1651
+ }
1652
+ }
1653
+
1654
+ class Pawn {
1655
+ get camera() {
1656
+ return this.controller.camera;
1657
+ }
1658
+ set enabled(value) {
1659
+ this.control.enabled = value;
1660
+ }
1661
+ get control() {
1662
+ if (!this._control) {
1663
+ throw new Error("Control not initialized");
1664
+ }
1665
+ return this._control;
1666
+ }
1667
+ constructor(controller) {
1668
+ this._control = null;
1669
+ this.isTickEnabled = false;
1670
+ this.controller = controller;
1671
+ }
1672
+ tick(deltaTime) {
1673
+ return;
1674
+ }
1675
+ possess() {
1676
+ }
1677
+ unpossess() {
1678
+ }
1679
+ destroy() {
1680
+ }
1681
+ }
1682
+
1683
+ class Orbital extends Pawn {
1684
+ get control() {
1685
+ if (!this._control) {
1686
+ throw new Error("Control not initialized");
1687
+ }
1688
+ return this._control;
1689
+ }
1690
+ constructor(controller) {
1691
+ super(controller);
1692
+ this.changeEvent = () => { this.onChange(); };
1693
+ this._control = new OrbitControls(controller.camera, controller.viewPort.canvas);
1694
+ this.control.target.set(0, 0, 0);
1695
+ controller.camera.position.set(0, 4, 4);
1696
+ this.control.update();
1697
+ }
1698
+ possess() {
1699
+ super.possess();
1700
+ this.control.addEventListener('change', this.changeEvent);
1701
+ }
1702
+ unpossess() {
1703
+ super.unpossess();
1704
+ this.control.removeEventListener("change", this.changeEvent);
1705
+ }
1706
+ onChange() {
1707
+ this.controller.viewPort.markRenderStateDirty();
1708
+ }
1709
+ }
1710
+
1711
+ class Controller {
1712
+ get camera() {
1713
+ return this.app.camera;
1714
+ }
1715
+ get world() {
1716
+ return this.app.world;
1717
+ }
1718
+ get viewPort() {
1719
+ return this.app.viewport;
1720
+ }
1721
+ get pawn() {
1722
+ if (!this._pawn) {
1723
+ throw Error("pawn is null");
1724
+ }
1725
+ return this._pawn;
1726
+ }
1727
+ get onClickNothingDelegate() {
1728
+ return this._onClickNothingDelegate;
1729
+ }
1730
+ constructor(app) {
1731
+ this.prepareClickComponent = null;
1732
+ this._pawn = null;
1733
+ this.onPointerMove = (event) => { this.onPointerMoveEvent(event); };
1734
+ this.onPointerEnter = (event) => { this.onPointerEnterEvent(event); };
1735
+ this.onPointerLeave = (event) => { this.onPointerLeaveEvent(event); };
1736
+ this.onPointerUp = (event) => { this.onPointerUpEvent(event); };
1737
+ this.onPointerDown = (event) => { this.onPointerDownEvent(event); };
1738
+ this.pointerPosition = new Vector2();
1739
+ this.doubleClickDelay = 250; // 双击判定时间间隔(毫秒)
1740
+ this.leftClickTimer = null;
1741
+ this.pointerLeftDownPosition = new Vector2();
1742
+ this.hoveringComponent = null;
1743
+ this._pointButtonIsDown = new Set();
1744
+ this._onClickNothingDelegate = new Delegate();
1745
+ this.app = app;
1746
+ this._pawn = new Orbital(this);
1747
+ this.pawn.possess();
1748
+ this.raycaster = new Raycaster();
1749
+ if (!this.viewPort.canvas) {
1750
+ throw Error("canvas is null");
1751
+ }
1752
+ this.viewPort.canvas.addEventListener("pointermove", this.onPointerMove);
1753
+ this.viewPort.canvas.addEventListener("pointerenter", this.onPointerEnter);
1754
+ this.viewPort.canvas.addEventListener("pointerleave", this.onPointerLeave);
1755
+ this.viewPort.canvas.addEventListener("pointerup", this.onPointerUp);
1756
+ this.viewPort.canvas.addEventListener("pointerdown", this.onPointerDown);
1757
+ }
1758
+ init() {
1759
+ }
1760
+ tick(deltaTime) {
1761
+ this.pawn.tick(deltaTime);
1762
+ }
1763
+ destroy() {
1764
+ if (this.leftClickTimer) {
1765
+ clearTimeout(this.leftClickTimer);
1766
+ this.leftClickTimer = null;
1767
+ }
1768
+ if (this.viewPort.canvas) {
1769
+ this.viewPort.canvas.removeEventListener("pointermove", this.onPointerMove);
1770
+ this.viewPort.canvas.removeEventListener("pointerenter", this.onPointerEnter);
1771
+ this.viewPort.canvas.removeEventListener("pointerleave", this.onPointerLeave);
1772
+ this.viewPort.canvas.removeEventListener("pointerup", this.onPointerUp);
1773
+ this.viewPort.canvas.removeEventListener("pointerdown", this.onPointerDown);
1774
+ }
1775
+ this.pawn.unpossess();
1776
+ this.pawn.destroy();
1777
+ this._pawn = null;
1778
+ }
1779
+ onPointerMoveEvent(event) {
1780
+ var _a, _b;
1781
+ if (!this.viewPort.canvas) {
1782
+ throw Error("canvas is null");
1783
+ }
1784
+ const canvasRect = this.viewPort.canvas.getBoundingClientRect();
1785
+ const offsetX = canvasRect.left;
1786
+ const offsetY = canvasRect.top;
1787
+ const pointer = new Vector2(((event.clientX - offsetX) / this.viewPort.canvas.clientWidth) * 2 - 1, 1 - ((event.clientY - offsetY) / this.viewPort.canvas.clientHeight) * 2);
1788
+ this.pointerPosition = pointer;
1789
+ if (this._pointButtonIsDown.size > 0) {
1790
+ return;
1791
+ }
1792
+ const hits = this.getHitResultUnderCursor();
1793
+ if (hits) {
1794
+ let component = hits.object.userData["LYObject"];
1795
+ if (component != this.hoveringComponent) {
1796
+ (_a = this.hoveringComponent) === null || _a === void 0 ? void 0 : _a.onHorveringEnd();
1797
+ this.hoveringComponent = null;
1798
+ if (component instanceof SceneComponent && component.isHoverEnabled) {
1799
+ component.onHorveringBegin();
1800
+ this.hoveringComponent = component;
1801
+ }
1802
+ }
1803
+ }
1804
+ else {
1805
+ (_b = this.hoveringComponent) === null || _b === void 0 ? void 0 : _b.onHorveringEnd();
1806
+ this.hoveringComponent = null;
1807
+ }
1808
+ }
1809
+ onPointerUpEvent(event) {
1810
+ this._pointButtonIsDown.delete(event.button);
1811
+ if (event.button === 0) {
1812
+ const pointerOffset = new Vector2().subVectors(this.pointerLeftDownPosition, this.pointerPosition).length();
1813
+ if (pointerOffset > 0.005) {
1814
+ if (this.leftClickTimer) {
1815
+ window.clearTimeout(this.leftClickTimer);
1816
+ this.leftClickTimer = null;
1817
+ }
1818
+ return;
1819
+ }
1820
+ if (!this.leftClickTimer) {
1821
+ const hit = this.getHitResultUnderCursor();
1822
+ let component = hit ? hit.object.userData["LYObject"] : null;
1823
+ if (component && component instanceof SceneComponent && component.isClickEnabled) {
1824
+ this.prepareClickComponent = component;
1825
+ this.leftClickTimer = window.setTimeout(() => {
1826
+ this.leftClickTimer = null;
1827
+ if (this.prepareClickComponent) {
1828
+ this.prepareClickComponent.onClicked();
1829
+ this.prepareClickComponent = null;
1830
+ }
1831
+ }, this.doubleClickDelay);
1832
+ }
1833
+ else {
1834
+ this._onClickNothingDelegate.broadcast();
1835
+ }
1836
+ }
1837
+ else {
1838
+ console.log("up 2, ", this.prepareClickComponent);
1839
+ window.clearTimeout(this.leftClickTimer);
1840
+ this.leftClickTimer = null;
1841
+ if (this.prepareClickComponent) {
1842
+ this.prepareClickComponent.onDoubleClicked();
1843
+ }
1844
+ else {
1845
+ this._onClickNothingDelegate.broadcast();
1846
+ }
1847
+ this.prepareClickComponent = null;
1848
+ }
1849
+ }
1850
+ }
1851
+ onPointerDownEvent(event) {
1852
+ this._pointButtonIsDown.add(event.button);
1853
+ if (event.button === 0) {
1854
+ this.pointerLeftDownPosition = this.pointerPosition.clone();
1855
+ }
1856
+ }
1857
+ onPointerEnterEvent(event) {
1858
+ if (!this.viewPort.canvas) {
1859
+ throw Error("canvas is null");
1860
+ }
1861
+ this.viewPort.canvas.addEventListener("pointermove", this.onPointerMove);
1862
+ this.viewPort.canvas.addEventListener("pointerup", this.onPointerUp);
1863
+ this.viewPort.canvas.addEventListener("pointerdown", this.onPointerDown);
1864
+ }
1865
+ onPointerLeaveEvent(event) {
1866
+ if (!this.viewPort.canvas) {
1867
+ throw Error("canvas is null");
1868
+ }
1869
+ this.viewPort.canvas.removeEventListener("pointermove", this.onPointerMove);
1870
+ this.viewPort.canvas.removeEventListener("pointerup", this.onPointerUp);
1871
+ this.viewPort.canvas.removeEventListener("pointerdown", this.onPointerDown);
1872
+ }
1873
+ getHitResultUnderCursor() {
1874
+ return this.getHitResultFromScreenPoint(this.pointerPosition.x, this.pointerPosition.y);
1875
+ }
1876
+ getHitResultFromScreenPoint(x, y) {
1877
+ this.raycaster.setFromCamera(new Vector2(x, y), this.camera);
1878
+ let out = this.raycaster.intersectObjects(this.world.scene.children, true);
1879
+ for (let i = 0; i < out.length; i++) {
1880
+ if (out[i].object.userData["rayIgnored"]) {
1881
+ continue;
1882
+ }
1883
+ return out[i];
1884
+ }
1885
+ return null;
1886
+ }
1887
+ }
1888
+
1889
+ class CameraFactory {
1890
+ static createCamera(param) {
1891
+ let cam = null;
1892
+ if ("fov" in param.param && "aspect" in param.param) {
1893
+ cam = new PerspectiveCamera(param.param.fov, param.param.aspect, param.near, param.far);
1894
+ }
1895
+ if ("left" in param.param && "right" in param.param && "top" in param.param && "bottom" in param.param) {
1896
+ cam = new OrthographicCamera(param.param.left, param.param.right, param.param.top, param.param.bottom, param.near, param.far);
1897
+ }
1898
+ if (!cam) {
1899
+ console.error("create camera failed");
1900
+ debugger;
1901
+ throw Error("create camera failed");
1902
+ }
1903
+ return cam;
1904
+ }
1905
+ static updataCamera(param, camera) {
1906
+ if ("fov" in param.param && "aspect" in param.param) {
1907
+ if (camera instanceof PerspectiveCamera) {
1908
+ camera.near = param.near;
1909
+ camera.far = param.far;
1910
+ camera.fov = param.param.fov;
1911
+ camera.aspect = param.param.aspect;
1912
+ return camera;
1913
+ }
1914
+ else {
1915
+ return new PerspectiveCamera(param.param.fov, param.param.aspect, param.near, param.far);
1916
+ }
1917
+ }
1918
+ if ("left" in param.param && "right" in param.param && "top" in param.param && "bottom" in param.param) {
1919
+ if (camera instanceof OrthographicCamera) {
1920
+ camera.near = param.near;
1921
+ camera.far = param.far;
1922
+ camera.left = param.param.left;
1923
+ camera.right = param.param.right;
1924
+ camera.top = param.param.top;
1925
+ camera.bottom = param.param.bottom;
1926
+ return camera;
1927
+ }
1928
+ else {
1929
+ return new OrthographicCamera(param.param.left, param.param.right, param.param.top, param.param.bottom, param.near, param.far);
1930
+ }
1931
+ }
1932
+ }
1933
+ }
1934
+
1935
+ const DefaultRenderParam = {
1936
+ antialias: true,
1937
+ };
1938
+ const DefaultCameraParam = {
1939
+ near: 0.1,
1940
+ far: 1000,
1941
+ param: {
1942
+ fov: 50,
1943
+ aspect: 1,
1944
+ }
1945
+ };
1946
+ const DefaultAppParam = {
1947
+ renderParam: DefaultRenderParam,
1948
+ cameraParam: DefaultCameraParam,
1949
+ postProcessParam: DefaultPostProcessParam,
1950
+ };
1951
+ class ThreeJsApp {
1952
+ get camera() {
1953
+ return this._camera;
1954
+ }
1955
+ get clock() {
1956
+ return this._clock;
1957
+ }
1958
+ get world() {
1959
+ return this._world;
1960
+ }
1961
+ get viewport() {
1962
+ return this._viewport;
1963
+ }
1964
+ get controller() {
1965
+ return this._controller;
1966
+ }
1967
+ get worldClass() {
1968
+ return World;
1969
+ }
1970
+ get viewportClass() {
1971
+ return Viewport;
1972
+ }
1973
+ get controllerClass() {
1974
+ return Controller;
1975
+ }
1976
+ constructor(elementId, appParam = DefaultAppParam) {
1977
+ this._clock = new Clock();
1978
+ this._camera = CameraFactory.createCamera(appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam);
1979
+ this._world = new this.worldClass(this);
1980
+ this._viewport = new this.viewportClass(this, elementId, appParam.renderParam ? appParam.renderParam : DefaultRenderParam, appParam.postProcessParam ? appParam.postProcessParam : DefaultPostProcessParam);
1981
+ this._controller = new this.controllerClass(this);
1982
+ this.viewport.renderer.setAnimationLoop(() => {
1983
+ this.tick();
1984
+ });
1985
+ this.init();
1986
+ }
1987
+ init() {
1988
+ this.controller.init();
1989
+ this.world.init();
1990
+ this.viewport.init();
1991
+ }
1992
+ tick() {
1993
+ const delta = this._clock.getDelta();
1994
+ this._controller.tick(delta);
1995
+ this.world.tick(delta);
1996
+ this.viewport.render();
1997
+ }
1998
+ onWindowResize(width, height) {
1999
+ if (this.camera instanceof PerspectiveCamera) {
2000
+ this.camera.aspect = width / height;
2001
+ }
2002
+ // if(this.camera instanceof OrthographicCamera)
2003
+ // {
2004
+ // // to do
2005
+ // }
2006
+ this.camera.updateProjectionMatrix();
2007
+ }
2008
+ destroy() {
2009
+ this.world.destroy();
2010
+ this.controller.destroy();
2011
+ this.viewport.destroy();
2012
+ }
2013
+ }
2014
+
2015
+ class Actor extends BaseObject {
2016
+ get world() {
2017
+ return this._world;
2018
+ }
2019
+ get name() {
2020
+ console.log("getname", this._name);
2021
+ return this._name;
2022
+ }
2023
+ set name(name) {
2024
+ this._name = name;
2025
+ }
2026
+ get rootComponent() {
2027
+ if (!this._rootComponent) {
2028
+ throw Error("rootComponent is invalid");
2029
+ }
2030
+ return this._rootComponent;
2031
+ }
2032
+ set rootComponent(newRoot) {
2033
+ newRoot.detachFromParentComponent();
2034
+ newRoot.parentActor = this;
2035
+ if (this._rootComponent) {
2036
+ let child = this.rootComponent.childrenComponents;
2037
+ for (let i = 0; i < child.length; ++i) {
2038
+ newRoot.addChildComponent(child[i]);
2039
+ }
2040
+ this.rootComponent.destroy();
2041
+ }
2042
+ this._rootComponent = newRoot;
2043
+ }
2044
+ get childrenComponents() {
2045
+ return this.rootComponent.childrenComponents;
2046
+ }
2047
+ get childActors() {
2048
+ var _a;
2049
+ let ret = [];
2050
+ (_a = this.rootComponent.threeObject) === null || _a === void 0 ? void 0 : _a.children.forEach((elem) => {
2051
+ var _a;
2052
+ let p = (_a = elem.userData["LYObject"]) === null || _a === void 0 ? void 0 : _a.parentActor;
2053
+ if (p !== this && p instanceof Actor) {
2054
+ ret.push(p);
2055
+ }
2056
+ });
2057
+ return ret;
2058
+ }
2059
+ get parentActor() {
2060
+ var _a, _b;
2061
+ let parent = (_b = (_a = this.rootComponent.threeObject) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.userData["LYObject"].parentActor;
2062
+ return parent === this ? null : parent;
2063
+ }
2064
+ get isVisible() {
2065
+ return this.rootComponent ? this.rootComponent.isVisible : false;
2066
+ }
2067
+ constructor(myThreeObject = null) {
2068
+ super();
2069
+ this._name = "Actor";
2070
+ this._rootComponent = null;
2071
+ this._world = null;
2072
+ this.onClickedEvent = [];
2073
+ this.onDoubleClickedEvent = [];
2074
+ this.onHoverBeginEvent = [];
2075
+ this.onHoverEndEvent = [];
2076
+ this.constructRootComponent(myThreeObject);
2077
+ }
2078
+ constructRootComponent(myThreeObject = null) {
2079
+ let tObject = myThreeObject ? myThreeObject : new Group();
2080
+ if (tObject.isMesh) {
2081
+ let mesh = tObject;
2082
+ this._rootComponent = new MeshComponent(mesh);
2083
+ this.rootComponent.parentActor = this;
2084
+ }
2085
+ else {
2086
+ this._rootComponent = new SceneComponent(tObject);
2087
+ this.rootComponent.parentActor = this;
2088
+ }
2089
+ }
2090
+ getBoundsCenterPosition(bInWorldSpace = true) {
2091
+ let ret = new Vector3();
2092
+ this.getBounds().getCenter(ret);
2093
+ if (!bInWorldSpace) {
2094
+ return ret.sub(this.getPosition());
2095
+ }
2096
+ else {
2097
+ return ret;
2098
+ }
2099
+ }
2100
+ getBoundsTopCenterPosition(bInWorldSpace = true) {
2101
+ let ret = new Vector3();
2102
+ let bounds = this.rootComponent.getBounds();
2103
+ bounds.getCenter(ret);
2104
+ ret.y = bounds.max.y;
2105
+ if (!bInWorldSpace) {
2106
+ return ret.sub(this.getPosition());
2107
+ }
2108
+ else {
2109
+ return ret;
2110
+ }
2111
+ }
2112
+ getBoundsBottomCenterPosition(bInWorldSpace = true) {
2113
+ let ret = new Vector3();
2114
+ let bounds = this.getBounds();
2115
+ bounds.getCenter(ret);
2116
+ ret.y = bounds.min.y;
2117
+ if (!bInWorldSpace) {
2118
+ return ret.sub(this.getPosition());
2119
+ }
2120
+ else {
2121
+ return ret;
2122
+ }
2123
+ }
2124
+ getBounds() {
2125
+ return this.rootComponent.getBounds();
2126
+ }
2127
+ destroy() {
2128
+ let childActors = this.childActors;
2129
+ childActors.forEach((elem) => {
2130
+ elem.destroy();
2131
+ });
2132
+ this.rootComponent.destroy();
2133
+ }
2134
+ setVisible(bVisible) {
2135
+ this.rootComponent.setVisible(bVisible);
2136
+ }
2137
+ setLayers(layer, bAffectChildActor = true) {
2138
+ this.rootComponent.setLayers(layer);
2139
+ if (bAffectChildActor) {
2140
+ this.childActors.forEach((child) => {
2141
+ child.setLayers(layer, bAffectChildActor);
2142
+ });
2143
+ }
2144
+ }
2145
+ tick(deltaTime) {
2146
+ super.tick(deltaTime);
2147
+ this.rootComponent.tick(deltaTime);
2148
+ }
2149
+ // Local Transforms
2150
+ // // Position
2151
+ getPosition() {
2152
+ return this.rootComponent.getPosition();
2153
+ }
2154
+ setPosition(...args) {
2155
+ if (args.length === 1) {
2156
+ this.rootComponent.setPosition(args[0]);
2157
+ }
2158
+ else {
2159
+ this.rootComponent.setPosition(args[0], args[1], args[2]);
2160
+ }
2161
+ }
2162
+ // // Rotation (euler)
2163
+ getRotation() {
2164
+ return this.rootComponent.getRotation();
2165
+ }
2166
+ setRotation(...args) {
2167
+ if (args.length === 1) {
2168
+ this.rootComponent.setRotation(args[0]);
2169
+ }
2170
+ else {
2171
+ this.rootComponent.setRotation(args[0], args[1], args[2]);
2172
+ }
2173
+ }
2174
+ // // quat
2175
+ getQuaternion() {
2176
+ return this.rootComponent.getQuaternion();
2177
+ }
2178
+ setQuaternion(...args) {
2179
+ if (args.length === 1) {
2180
+ this.rootComponent.setQuaternion(args[0]);
2181
+ }
2182
+ else {
2183
+ this.rootComponent.setQuaternion(args[0], args[1], args[2], args[3]);
2184
+ }
2185
+ }
2186
+ // // Scale
2187
+ getScale() {
2188
+ return this.rootComponent.getScale();
2189
+ }
2190
+ setScale(...args) {
2191
+ if (args.length === 1) {
2192
+ this.rootComponent.setScale(args[0]);
2193
+ }
2194
+ else {
2195
+ this.rootComponent.setScale(args[0], args[1], args[2]);
2196
+ }
2197
+ }
2198
+ // World Transform
2199
+ // // Matrix
2200
+ getWorldMatrix() {
2201
+ return this.rootComponent.getWorldMatrix();
2202
+ }
2203
+ setWorldMatrix(newMatrix) {
2204
+ this.rootComponent.setWorldMatrix(newMatrix);
2205
+ }
2206
+ // // WorldPosition
2207
+ getWorldPosition() {
2208
+ return this.rootComponent.getWorldPosition();
2209
+ }
2210
+ // setWorldPosition(param1, param2 = undefined, param3 = undefined)
2211
+ // {
2212
+ // }
2213
+ // // rotation(euler)
2214
+ getWorldRotation() {
2215
+ return this.rootComponent.getWorldRotation();
2216
+ }
2217
+ // // rotation(quat)
2218
+ getWorldQuaternion() {
2219
+ return this.rootComponent.getWorldQuaternion();
2220
+ }
2221
+ // // Scale
2222
+ getWorldScale() {
2223
+ return this.rootComponent.getWorldScale();
2224
+ }
2225
+ getWorldForwardDirection() {
2226
+ return this.rootComponent.getWorldForwardDirection();
2227
+ }
2228
+ getWorldUpDirection() {
2229
+ return this.rootComponent.getWorldUpDirection();
2230
+ }
2231
+ getWorldRightDirection() {
2232
+ return this.rootComponent.getWorldRightDirection();
2233
+ }
2234
+ addLocalOffset(axis, value) {
2235
+ let pos = this.getPosition();
2236
+ this.setPosition(pos.x + axis.x * value, pos.y + axis.y * value, pos.z + axis.z * value);
2237
+ }
2238
+ worldToLocal(vec) {
2239
+ return this.rootComponent.worldToLocal(vec);
2240
+ }
2241
+ localToWorld(vec) {
2242
+ return this.rootComponent.localToWorld(vec);
2243
+ }
2244
+ getComponents() {
2245
+ return this.rootComponent.childrenComponents;
2246
+ }
2247
+ addComponent(newComponent, attachmentRule = AttachmentRules.KeepRelative) {
2248
+ this.rootComponent.addChildComponent(newComponent, attachmentRule);
2249
+ if (this._world) {
2250
+ newComponent.onAddedToWorld(this._world);
2251
+ }
2252
+ }
2253
+ removeComponent(targetComponent) {
2254
+ if (targetComponent === this._rootComponent) {
2255
+ return;
2256
+ }
2257
+ this.rootComponent.removeChildComponent(targetComponent);
2258
+ }
2259
+ clearComponent() {
2260
+ this.rootComponent.destroyChildren();
2261
+ }
2262
+ addChildActor(newActor, attachmentRule = AttachmentRules.KeepWorld) {
2263
+ if (!(newActor.rootComponent)) {
2264
+ console.error("Actor :: addChildActor : the 'newActor' you want to add has no rootcomponent");
2265
+ return;
2266
+ }
2267
+ if (newActor.parentActor === this) {
2268
+ return;
2269
+ }
2270
+ if (attachmentRule === AttachmentRules.KeepRelative) {
2271
+ this.rootComponent.attachComponent(newActor.rootComponent);
2272
+ }
2273
+ if (attachmentRule === AttachmentRules.KeepWorld) {
2274
+ let worldMatrix = newActor.getWorldMatrix();
2275
+ this.rootComponent.attachComponent(newActor.rootComponent);
2276
+ newActor.setWorldMatrix(worldMatrix);
2277
+ }
2278
+ if (this._world) {
2279
+ this._world.viewport.markRenderStateDirty();
2280
+ }
2281
+ }
2282
+ removeChildActor(target) {
2283
+ let worldMatrix = target.getWorldMatrix();
2284
+ this.rootComponent.detachComponent(target.rootComponent);
2285
+ target.setWorldMatrix(worldMatrix);
2286
+ }
2287
+ removeFromParent() {
2288
+ if (this.parentActor) {
2289
+ this.parentActor.removeChildActor(this);
2290
+ }
2291
+ }
2292
+ onAddedToWorld(world) {
2293
+ this._world = world;
2294
+ this.rootComponent.onAddedToWorld(this._world);
2295
+ this.childActors.forEach((elem) => {
2296
+ elem.onAddedToWorld(world);
2297
+ });
2298
+ }
2299
+ onRemovedFromWorld() {
2300
+ this._world = null;
2301
+ }
2302
+ get isHoverEnabled() {
2303
+ return this.rootComponent.isHoverEnabled;
2304
+ }
2305
+ set isHoverEnabled(bCanHorver) {
2306
+ this.rootComponent.isHoverEnabled = bCanHorver;
2307
+ }
2308
+ get isClickEnabled() {
2309
+ return this.rootComponent.isClickEnabled;
2310
+ }
2311
+ set isClickEnabled(bCanHorver) {
2312
+ this.rootComponent.isClickEnabled = bCanHorver;
2313
+ }
2314
+ // hovering begin
2315
+ addHoveringBeginEvent(newFunc) {
2316
+ this.onHoverBeginEvent.push(newFunc);
2317
+ }
2318
+ removeHoveringBeginEvent(target) {
2319
+ let index = this.onHoverBeginEvent.indexOf(target);
2320
+ if (index >= 0) {
2321
+ this.onHoverBeginEvent.splice(index, 1);
2322
+ }
2323
+ }
2324
+ clearHoveringBeginEvent() {
2325
+ this.onHoverBeginEvent = [];
2326
+ }
2327
+ onComponentHorveringBegin(component) {
2328
+ for (let i = 0; i < this.onHoverBeginEvent.length; ++i) {
2329
+ this.onHoverBeginEvent[i](component);
2330
+ }
2331
+ }
2332
+ // hovering end
2333
+ addHoveringEndEvent(newFunc) {
2334
+ this.onHoverEndEvent.push(newFunc);
2335
+ }
2336
+ removeHoveringEndEvent(target) {
2337
+ let index = this.onHoverEndEvent.indexOf(target);
2338
+ if (index >= 0) {
2339
+ this.onHoverEndEvent.splice(index, 1);
2340
+ }
2341
+ }
2342
+ clearHoveringEndEvent() {
2343
+ this.onHoverEndEvent = [];
2344
+ }
2345
+ onComponentHorveringEnd(component) {
2346
+ // console.log("onComponentHorveringEnd", this)
2347
+ for (let i = 0; i < this.onHoverEndEvent.length; ++i) {
2348
+ this.onHoverEndEvent[i](component);
2349
+ }
2350
+ }
2351
+ //click
2352
+ addClickEvent(newFunc) {
2353
+ this.onClickedEvent.push(newFunc);
2354
+ }
2355
+ removeClickEvent(target) {
2356
+ let index = this.onClickedEvent.indexOf(target);
2357
+ if (index >= 0) {
2358
+ this.onClickedEvent.splice(index, 1);
2359
+ }
2360
+ }
2361
+ clearClickEvent() {
2362
+ this.onClickedEvent = [];
2363
+ }
2364
+ onComponentClicked(component) {
2365
+ //console.log("onComponentClicked", this.Name, component)
2366
+ for (let i = 0; i < this.onClickedEvent.length; ++i) {
2367
+ this.onClickedEvent[i](component);
2368
+ }
2369
+ }
2370
+ // double click
2371
+ addDoubleClickEvent(newFunc) {
2372
+ this.onDoubleClickedEvent.push(newFunc);
2373
+ }
2374
+ removeDoubleClickEvent(target) {
2375
+ let index = this.onClickedEvent.indexOf(target);
2376
+ if (index >= 0) {
2377
+ this.onDoubleClickedEvent.splice(index, 1);
2378
+ }
2379
+ }
2380
+ onComponentDoubleClicked(component) {
2381
+ //console.log("onComponentClicked", this.Name, component)
2382
+ for (let i = 0; i < this.onDoubleClickedEvent.length; ++i) {
2383
+ this.onDoubleClickedEvent[i](component);
2384
+ }
2385
+ }
2386
+ clearDoubleClickEvent() {
2387
+ this.onDoubleClickedEvent = [];
2388
+ }
2389
+ }
2390
+
2391
+ /*
2392
+ * virtual class of light, should not be use directly.
2393
+ */
2394
+ class LightComponent extends SceneComponent {
2395
+ get threeObject() {
2396
+ if (!this.obj) {
2397
+ throw Error("three object is invalid");
2398
+ }
2399
+ return this.obj;
2400
+ }
2401
+ set threeObject(newThreeObject) {
2402
+ this.obj = newThreeObject;
2403
+ if (this.obj) {
2404
+ this.obj.userData["LYObject"] = this;
2405
+ }
2406
+ }
2407
+ get color() {
2408
+ return this.threeObject.color.getHex();
2409
+ }
2410
+ set color(color) {
2411
+ this.threeObject.color.set(color);
2412
+ }
2413
+ get intensity() {
2414
+ return this.threeObject.intensity;
2415
+ }
2416
+ set intensity(intensity) {
2417
+ this.threeObject.intensity = intensity;
2418
+ }
2419
+ constructor(light) {
2420
+ super(light);
2421
+ this.obj = light;
2422
+ }
2423
+ }
2424
+
2425
+ class DirectionalLightComponent extends LightComponent {
2426
+ get threeObject() {
2427
+ if (!this.obj) {
2428
+ throw Error("three object is invalid");
2429
+ }
2430
+ return this.obj;
2431
+ }
2432
+ set threeObject(newThreeObject) {
2433
+ this.obj = newThreeObject;
2434
+ if (this.obj) {
2435
+ this.obj.userData["LYObject"] = this;
2436
+ }
2437
+ }
2438
+ get castShadow() {
2439
+ return this.threeObject.castShadow;
2440
+ }
2441
+ set castShadow(value) {
2442
+ this.threeObject.castShadow = value;
2443
+ }
2444
+ constructor(color = 0xffffff, intensity = 10) {
2445
+ let obj = new DirectionalLight(color, intensity);
2446
+ super(obj);
2447
+ this.obj = obj;
2448
+ obj.castShadow = true;
2449
+ }
2450
+ setPosition(...args) {
2451
+ if (args.length === 1) {
2452
+ super.setPosition(args[0]);
2453
+ }
2454
+ else {
2455
+ super.setPosition(args[0], args[1], args[2]);
2456
+ }
2457
+ this.update();
2458
+ }
2459
+ update() {
2460
+ if (this.world)
2461
+ this.world.viewport.markRenderStateDirty();
2462
+ }
2463
+ onAddedToWorld(world) {
2464
+ if (!this.threeObject) {
2465
+ throw Error("three object is invalid");
2466
+ }
2467
+ super.onAddedToWorld(world);
2468
+ }
2469
+ destroy() {
2470
+ super.destroy();
2471
+ }
2472
+ }
2473
+
2474
+ class DirectionalLightActor extends Actor {
2475
+ constructor(color = 0xffffff, intensity = 1) {
2476
+ super();
2477
+ this.lightComponent = new DirectionalLightComponent(color, intensity);
2478
+ this.rootComponent = this.lightComponent;
2479
+ this.lightComponent.castShadow = true;
2480
+ }
2481
+ }
2482
+
2483
+ class BoxComponent extends MeshComponent {
2484
+ constructor(width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1, material = new MeshStandardMaterial()) {
2485
+ super(new BoxGeometry(width, height, depth, widthSegments, heightSegments, depthSegments), material);
2486
+ }
2487
+ }
2488
+
2489
+ class BoxActor extends Actor {
2490
+ constructor(width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1, material = new MeshBasicMaterial()) {
2491
+ super();
2492
+ this.addComponent(new BoxComponent(width, height, depth, widthSegments, heightSegments, depthSegments, material));
2493
+ }
2494
+ }
2495
+
2496
+ class PlaneComponent extends MeshComponent {
2497
+ constructor(width, height, material, widthSegments = 1, heightSegments = 1) {
2498
+ super(new PlaneGeometry(width, height, widthSegments, heightSegments), material);
2499
+ }
2500
+ }
2501
+
2502
+ class PlaneActor extends Actor {
2503
+ get planeComponent() {
2504
+ return this._planeComponent;
2505
+ }
2506
+ constructor(width, height, material = new MeshBasicMaterial(), widthSegments = 1, heightSegments = 1) {
2507
+ super();
2508
+ this._planeComponent = new PlaneComponent(width, height, material, widthSegments, heightSegments);
2509
+ this._planeComponent.setRotation(Math.PI * 0.5 * -1, 0, 0);
2510
+ this.addComponent(this._planeComponent);
2511
+ this._planeComponent.receiveShadow = true;
2512
+ }
2513
+ destroy() {
2514
+ var _a;
2515
+ (_a = this._planeComponent) === null || _a === void 0 ? void 0 : _a.destroy();
2516
+ this._planeComponent = null;
2517
+ super.destroy();
2518
+ }
2519
+ }
2520
+
2521
+ const DefaultBloomParam = {
2522
+ threshold: 0,
2523
+ strength: 1,
2524
+ radius: 0,
2525
+ };
2526
+
2527
+ const DefaultDenoiseParam = {
2528
+ denoiseRadius: 5,
2529
+ lumaPhi: 5,
2530
+ depthPhi: 5,
2531
+ normalPhi: 5
2532
+ };
2533
+
2534
+ const DefaultDOFParam = {
2535
+ focus: 500.0,
2536
+ aperture: 5,
2537
+ maxblur: 0.01
2538
+ };
2539
+
2540
+ const DefaultGTAOParam = {
2541
+ distanceExponent: 1,
2542
+ distanceFallOff: 1,
2543
+ radius: 0.25,
2544
+ scale: 1,
2545
+ thickness: 1,
2546
+ denoised: true,
2547
+ denoiseParam: DefaultDenoiseParam,
2548
+ };
2549
+
2550
+ const DefaultSSRParam = {
2551
+ maxDistance: 0.5,
2552
+ opacity: 1,
2553
+ thickness: 0.015,
2554
+ };
2555
+
2556
+ class SphereComponent extends MeshComponent {
2557
+ constructor(radius, material = new MeshBasicMaterial(), widthSegments = 32, heightSegments = 16) {
2558
+ super(new SphereGeometry(radius, widthSegments, heightSegments), material);
2559
+ }
2560
+ }
2561
+
2562
+ class LabelComponent extends SceneComponent {
2563
+ get threeObject() {
2564
+ return this.obj;
2565
+ }
2566
+ set threeObject(newThreeObject) {
2567
+ this.obj = newThreeObject;
2568
+ if (this.obj) {
2569
+ this.obj.userData["LYObject"] = this;
2570
+ }
2571
+ }
2572
+ constructor(domElement, center = new Vector2(0.5, 1)) {
2573
+ let obj = new CSS2DObject(domElement);
2574
+ super(obj);
2575
+ this.obj = obj;
2576
+ obj.center = center;
2577
+ }
2578
+ set isHoverEnabled(bCanHorver) {
2579
+ return;
2580
+ }
2581
+ set isClickEnabled(bCanClick) {
2582
+ return;
2583
+ }
2584
+ setVisible(bVisible) {
2585
+ super.setVisible(bVisible);
2586
+ }
2587
+ destroy() {
2588
+ if (this.threeObject.element && this.threeObject.element.parentNode) {
2589
+ this.threeObject.element.innerHTML = '';
2590
+ this.threeObject.element.remove();
2591
+ }
2592
+ super.destroy();
2593
+ }
2594
+ }
2595
+ // example
2596
+ // const labelStyle = document.createElement( 'div' );
2597
+ //
2598
+ // labelStyle.textContent = "this.POIData.name";
2599
+ // labelStyle.className = 'label';
2600
+ // labelStyle.style.color = 'white'
2601
+ // labelStyle.style.backgroundColor = 'transparent';
2602
+ // labelStyle.style.fontSize = "10px";
2603
+ // labelStyle.style.pointerEvents = 'auto';
2604
+
2605
+ class FirstPerson extends Pawn {
2606
+ constructor(controller) {
2607
+ super(controller);
2608
+ this._control = new PointerLockControls(controller.camera, controller.viewPort.canvas);
2609
+ }
2610
+ }
2611
+
2612
+ class TransformGizmo extends Pawn {
2613
+ get control() {
2614
+ if (!this._control) {
2615
+ throw new Error("Control not initialized");
2616
+ }
2617
+ return this._control;
2618
+ }
2619
+ constructor(controller) {
2620
+ super(controller);
2621
+ this._control = null;
2622
+ this.helperObject = new Object3D();
2623
+ this.targets = [];
2624
+ this.primaryTarget = null;
2625
+ this.targetMatrixMap = new Map();
2626
+ this.onDraggingChangedEvent = (param) => { this.onDraggingChanged(param); };
2627
+ this.onObjectChangeEvent = () => { this.onObjectChanged(); };
2628
+ this.onChangeEvent = () => { this.onChange(); };
2629
+ this.onUpdateFunctions = [];
2630
+ this.onDraggingFunctions = [];
2631
+ this._control = new TransformControls(controller.camera, controller.viewPort.canvas);
2632
+ this.control.getHelper().traverse((elem) => {
2633
+ elem.userData["rayIgnored"] = true;
2634
+ });
2635
+ this.helperObject.name = "TransformHelperObject";
2636
+ }
2637
+ possess() {
2638
+ var _a;
2639
+ (_a = this.controller.world.scene) === null || _a === void 0 ? void 0 : _a.add(this.helperObject);
2640
+ this.control.attach(this.helperObject);
2641
+ this.control.addEventListener('change', this.onChangeEvent);
2642
+ this.control.addEventListener('dragging-changed', this.onDraggingChangedEvent);
2643
+ this.control.addEventListener('objectChange', this.onObjectChangeEvent);
2644
+ }
2645
+ unpossess() {
2646
+ var _a;
2647
+ this.control.removeEventListener('change', this.onChangeEvent);
2648
+ this.control.removeEventListener('dragging-changed', this.onDraggingChangedEvent);
2649
+ this.control.removeEventListener('objectChange', this.onObjectChangeEvent);
2650
+ this.control.detach();
2651
+ (_a = this.controller.world.scene) === null || _a === void 0 ? void 0 : _a.remove(this.helperObject);
2652
+ }
2653
+ onChange() {
2654
+ this.controller.viewPort.markRenderStateDirty();
2655
+ }
2656
+ onDraggingChanged(event) {
2657
+ if (this.primaryTarget) {
2658
+ let tMatrix = this.primaryTarget.getMatrixInWorld();
2659
+ tMatrix.decompose(this.helperObject.position, this.helperObject.quaternion, this.helperObject.scale);
2660
+ }
2661
+ this.onDraggingFunctions.forEach((elem) => {
2662
+ elem(event.value);
2663
+ });
2664
+ }
2665
+ onObjectChanged() {
2666
+ if (this.primaryTarget) {
2667
+ //const TA1 = this.primaryTarget.getMatrixInWorld();
2668
+ this.helperObject.updateMatrixWorld(true);
2669
+ const TA2 = this.helperObject.matrixWorld.clone();
2670
+ this.primaryTarget.onTransforming(TA2);
2671
+ // const TA1Inverse = TA1.clone().invert();
2672
+ //const transformMatrix = new Matrix4().multiplyMatrices(TA2, TA1Inverse);
2673
+ this.targets.forEach((tar) => {
2674
+ if (tar !== this.primaryTarget) {
2675
+ let t = this.targetMatrixMap.get(tar);
2676
+ if (t) {
2677
+ let TB2 = new Matrix4().multiplyMatrices(TA2, t); // B
2678
+ tar.onTransforming(TB2);
2679
+ }
2680
+ }
2681
+ });
2682
+ }
2683
+ this.onUpdateFunctions.forEach((elem) => {
2684
+ elem();
2685
+ });
2686
+ }
2687
+ set enable(newEnable) {
2688
+ this.control.enabled = newEnable;
2689
+ }
2690
+ setTarget(primaryTarget, multiTargets, onUpdate = null, onDragging = null) {
2691
+ var _a;
2692
+ this.targetMatrixMap.clear();
2693
+ let tMatrix = primaryTarget.getMatrixInWorld();
2694
+ tMatrix.decompose(this.helperObject.position, this.helperObject.quaternion, this.helperObject.scale);
2695
+ (_a = this.controller.world.scene) === null || _a === void 0 ? void 0 : _a.add(this.control.getHelper());
2696
+ // this.control.attach(this.helperObject);
2697
+ this.primaryTarget = primaryTarget;
2698
+ this.targets = multiTargets;
2699
+ this.targets.forEach((elem) => {
2700
+ const tarMatrix = elem.getMatrixInWorld();
2701
+ const TB_local = tMatrix.clone().invert().multiply(tarMatrix);
2702
+ this.targetMatrixMap.set(elem, TB_local);
2703
+ });
2704
+ if (onUpdate) {
2705
+ this.onUpdateFunctions.push(onUpdate);
2706
+ }
2707
+ if (onUpdate) {
2708
+ this.onUpdateFunctions.push(onUpdate);
2709
+ }
2710
+ if (onDragging) {
2711
+ this.onDraggingFunctions.push(onDragging);
2712
+ }
2713
+ this.isTickEnabled = true;
2714
+ }
2715
+ getMode() {
2716
+ return this.control.getMode();
2717
+ }
2718
+ setMode(mode) {
2719
+ this.control.setMode(mode);
2720
+ }
2721
+ setSpace(space) {
2722
+ this.control.setSpace(space);
2723
+ }
2724
+ setAxis(axis) {
2725
+ this.control.showX = axis ? axis === null || axis === void 0 ? void 0 : axis.includes("X") : false;
2726
+ this.control.showY = axis ? axis === null || axis === void 0 ? void 0 : axis.includes("Y") : false;
2727
+ this.control.showZ = axis ? axis === null || axis === void 0 ? void 0 : axis.includes("Z") : false;
2728
+ }
2729
+ setSize(size) {
2730
+ this.control.setSize(size);
2731
+ }
2732
+ destroy() {
2733
+ var _a;
2734
+ this.isTickEnabled = false;
2735
+ this.unpossess();
2736
+ this.targetMatrixMap.clear();
2737
+ this.targets = [];
2738
+ this.primaryTarget = null;
2739
+ this.onUpdateFunctions = [];
2740
+ this.onDraggingFunctions = [];
2741
+ (_a = this._control) === null || _a === void 0 ? void 0 : _a.dispose();
2742
+ this._control = null;
2743
+ super.destroy();
2744
+ }
2745
+ }
2746
+
2747
+ export { Actor, AttachmentRules, BoxActor, BoxComponent, Controller, DefaultAppParam, DefaultBloomParam, DefaultCameraParam, DefaultDOFParam, DefaultDenoiseParam, DefaultGTAOParam, DefaultOutlineParams, DefaultPostProcessParam, DefaultRenderParam, DefaultSSRParam, DefaultToneMappingParams, Delegate, DirectionalLightActor, DirectionalLightComponent, FirstPerson, LYAssetManager, LYLoadTask, LabelComponent, MeshComponent, Orbital, PlaneActor, PlaneComponent, SceneComponent, SphereComponent, ThreeJsApp, ToneMappingOptions, TransformGizmo, Viewport, WebGPUPostProcessFactory, World };