lythreeframe 1.1.11 → 1.1.13

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.
@@ -14,6 +14,7 @@ var MotionBlur_js = require('three/examples/jsm/tsl/display/MotionBlur.js');
14
14
  var FXAANode_js = require('three/examples/jsm/tsl/display/FXAANode.js');
15
15
  var SMAANode_js = require('three/examples/jsm/tsl/display/SMAANode.js');
16
16
  var gsap = require('gsap');
17
+ var three = require('three');
17
18
  var SkyMesh_js = require('three/examples/jsm/objects/SkyMesh.js');
18
19
  var CSS2DRenderer_js = require('three/examples/jsm/renderers/CSS2DRenderer.js');
19
20
  var PointerLockControls = require('three/examples/jsm/controls/PointerLockControls');
@@ -23,10 +24,10 @@ var TransformControls_js = require('three/examples/jsm/controls/TransformControl
23
24
  * virtual class of 3D Object, should not be use directly.
24
25
  */
25
26
  class BaseObject {
26
- get IsTickable() {
27
+ get isTickEnabled() {
27
28
  return this.bCanTick;
28
29
  }
29
- set IsTickable(bCanTick) {
30
+ set isTickEnabled(bCanTick) {
30
31
  this.bCanTick = bCanTick;
31
32
  }
32
33
  get uuid() {
@@ -34,6 +35,7 @@ class BaseObject {
34
35
  }
35
36
  constructor(uuid = webgpu.MathUtils.generateUUID()) {
36
37
  this.bCanTick = false;
38
+ this.tickEvents = [];
37
39
  this.bCanTick = false;
38
40
  this._uuid = uuid;
39
41
  }
@@ -41,6 +43,21 @@ class BaseObject {
41
43
  if (!this.bCanTick) {
42
44
  return;
43
45
  }
46
+ this.tickEvents.forEach((elem) => {
47
+ elem(deltaTime);
48
+ });
49
+ }
50
+ addTickEvent(event) {
51
+ this.tickEvents.push(event);
52
+ }
53
+ removeTickEvent(event) {
54
+ let index = this.tickEvents.indexOf(event);
55
+ if (index != -1) {
56
+ this.tickEvents.splice(index, 1);
57
+ }
58
+ }
59
+ clearTickEvents() {
60
+ this.tickEvents = [];
44
61
  }
45
62
  destroy() {
46
63
  }
@@ -106,7 +123,6 @@ class SceneComponent extends Component {
106
123
  this._parentActor = value;
107
124
  }
108
125
  get parentActor() {
109
- // 通过 _parentActor 私有字段访问父级 Actor
110
126
  return this._parentActor;
111
127
  }
112
128
  get world() {
@@ -122,6 +138,12 @@ class SceneComponent extends Component {
122
138
  createDefaultThreeObject() {
123
139
  this.threeObject = new webgpu.Group();
124
140
  }
141
+ tick(deltaTime) {
142
+ super.tick(deltaTime);
143
+ this.childrenComponents.forEach((elem) => {
144
+ elem.tick(deltaTime);
145
+ });
146
+ }
125
147
  get isVisible() {
126
148
  if (!this.threeObject) {
127
149
  return false;
@@ -1226,25 +1248,13 @@ const DefaultPostProcessParam = {
1226
1248
  }
1227
1249
  };
1228
1250
 
1229
- class ThreeObjectLibrary {
1230
- static disposeMeshMaterial(mesh) {
1231
- let mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
1232
- mats.forEach((elem) => {
1233
- elem.dispose();
1234
- });
1235
- }
1236
- static disposeMeshGeometry(mesh) {
1237
- mesh.geometry.dispose();
1238
- }
1239
- static disposeMeshResource(mesh) {
1240
- ThreeObjectLibrary.disposeMeshMaterial(mesh);
1241
- ThreeObjectLibrary.disposeMeshGeometry(mesh);
1242
- }
1243
- }
1244
-
1245
1251
  class World {
1246
1252
  get scene() {
1247
- return this._scene;
1253
+ let scene = this.rootActor.scene;
1254
+ if (!scene) {
1255
+ throw new Error("scene is null");
1256
+ }
1257
+ return scene;
1248
1258
  }
1249
1259
  get viewport() {
1250
1260
  return this.app.viewport;
@@ -1252,36 +1262,41 @@ class World {
1252
1262
  get controller() {
1253
1263
  return this.app.controller;
1254
1264
  }
1255
- constructor(app) {
1265
+ constructor(app, worldParam) {
1266
+ this.rootActor = null;
1256
1267
  this.actors = new Set();
1268
+ this.tickableActors = new Set();
1257
1269
  this.app = app;
1258
- this._scene = new webgpu.Scene();
1270
+ this.worldParam = worldParam;
1259
1271
  }
1260
1272
  init() {
1273
+ this.rootActor = new this.worldParam.levelActorClass(this.app);
1274
+ }
1275
+ addTickableActor(actor) {
1276
+ this.tickableActors.add(actor);
1277
+ }
1278
+ removeTickableActor(actor) {
1279
+ this.tickableActors.delete(actor);
1261
1280
  }
1262
1281
  tick(deltaTime) {
1263
- this.actors.forEach((elem) => {
1282
+ this.tickableActors.forEach((elem) => {
1264
1283
  elem.tick(deltaTime);
1265
1284
  });
1266
1285
  }
1267
1286
  destroy() {
1287
+ this.tickableActors.clear();
1268
1288
  this.actors.forEach((elem) => {
1269
1289
  elem.destroy();
1270
1290
  });
1271
1291
  this.actors.clear();
1272
- this.scene.traverse((child) => {
1273
- if (child instanceof webgpu.Mesh) {
1274
- ThreeObjectLibrary.disposeMeshResource(child);
1275
- }
1276
- });
1277
- this.scene.clear();
1292
+ this.rootActor.destroy();
1278
1293
  }
1279
1294
  addActor(actor) {
1280
1295
  if (!actor.rootComponent.threeObject) {
1281
- throw Error("actor.threeObject is null");
1296
+ throw new Error("actor.threeObject is null");
1282
1297
  }
1283
1298
  actor.removeFromParent();
1284
- this.scene.add(actor.rootComponent.threeObject);
1299
+ this.rootActor.addChildActor(actor);
1285
1300
  this.actors.add(actor);
1286
1301
  actor.onAddedToWorld(this);
1287
1302
  this.viewport.markRenderStateDirty();
@@ -2274,97 +2289,6 @@ const DefaultAppParam = {
2274
2289
  }
2275
2290
  };
2276
2291
 
2277
- class ThreeJsApp {
2278
- get camera() {
2279
- return this._camera;
2280
- }
2281
- get clock() {
2282
- return this._clock;
2283
- }
2284
- get world() {
2285
- return this._world;
2286
- }
2287
- get viewport() {
2288
- return this._viewport;
2289
- }
2290
- get controller() {
2291
- return this._controller;
2292
- }
2293
- get assetManager() {
2294
- return this._assetManager;
2295
- }
2296
- get appParam() {
2297
- return this._appParam;
2298
- }
2299
- get onCameraChangedDelegate() {
2300
- return this._onCameraChangedDelegate;
2301
- }
2302
- constructor(appParam = DefaultAppParam) {
2303
- this._appParam = { viewportParam: DefaultViewportParam };
2304
- this._onCameraChangedDelegate = new Delegate();
2305
- this._appParam.cameraParam = appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam;
2306
- this._appParam.renderParam = appParam.renderParam ? appParam.renderParam : DefaultRenderParam;
2307
- this._appParam.postProcessParam = appParam.postProcessParam ? appParam.postProcessParam : DefaultPostProcessParam;
2308
- this._appParam.viewportParam = appParam.viewportParam ? appParam.viewportParam : DefaultViewportParam;
2309
- this._appParam.classes = appParam.classes ? appParam.classes : {
2310
- assetManagerClass: AssetManager,
2311
- controllerClass: Controller,
2312
- worldClass: World,
2313
- viewportClass: Viewport,
2314
- };
2315
- this._clock = new webgpu.Clock();
2316
- this._camera = CameraFactory.createCamera(this._appParam.cameraParam);
2317
- this._world = new this._appParam.classes.worldClass(this);
2318
- this._viewport = new this._appParam.classes.viewportClass(this, this._appParam.viewportParam, this._appParam.renderParam, this._appParam.postProcessParam);
2319
- this._controller = new this._appParam.classes.controllerClass(this);
2320
- this._assetManager = new this._appParam.classes.assetManagerClass(this);
2321
- this.viewport.renderer.setAnimationLoop(() => {
2322
- this.tick();
2323
- });
2324
- this.init();
2325
- }
2326
- init() {
2327
- this.controller.init();
2328
- this.world.init();
2329
- this.viewport.init();
2330
- }
2331
- tick() {
2332
- const delta = this._clock.getDelta();
2333
- this._controller.tick(delta);
2334
- this.world.tick(delta);
2335
- this.viewport.render();
2336
- }
2337
- destroy() {
2338
- this.onCameraChangedDelegate.clear();
2339
- this.world.destroy();
2340
- this.controller.destroy();
2341
- this.viewport.destroy();
2342
- this._assetManager.clearAssets();
2343
- }
2344
- updateCamera(param) {
2345
- const previousCam = this.camera;
2346
- this._camera = CameraFactory.updataCamera(param, this.camera);
2347
- if (previousCam !== this.camera) {
2348
- this._onCameraChangedDelegate.broadcast();
2349
- }
2350
- this._camera.updateProjectionMatrix();
2351
- this.viewport.markRenderStateDirty();
2352
- }
2353
- onWindowResize(width, height) {
2354
- if (this.camera instanceof webgpu.PerspectiveCamera) {
2355
- this.camera.aspect = width / height;
2356
- }
2357
- // if(this.camera instanceof OrthographicCamera)
2358
- // {
2359
- // // to do
2360
- // }
2361
- this.camera.updateProjectionMatrix();
2362
- }
2363
- async renderAsImage(width = 1024, height = 1024) {
2364
- return await this.viewport.renderAsImage(width, height);
2365
- }
2366
- }
2367
-
2368
2292
  class Actor extends BaseObject {
2369
2293
  get world() {
2370
2294
  return this._world;
@@ -2417,6 +2341,15 @@ class Actor extends BaseObject {
2417
2341
  get isVisible() {
2418
2342
  return this.rootComponent ? this.rootComponent.isVisible : false;
2419
2343
  }
2344
+ set isTickEnabled(bCanTick) {
2345
+ super.isTickEnabled = bCanTick;
2346
+ if (this.isTickEnabled) {
2347
+ this.app.world.addTickableActor(this);
2348
+ }
2349
+ else {
2350
+ this.app.world.removeTickableActor(this);
2351
+ }
2352
+ }
2420
2353
  constructor(app, myThreeObject = null) {
2421
2354
  super();
2422
2355
  this._name = "Actor";
@@ -2497,6 +2430,9 @@ class Actor extends BaseObject {
2497
2430
  }
2498
2431
  }
2499
2432
  tick(deltaTime) {
2433
+ if (!this.isTickEnabled) {
2434
+ return;
2435
+ }
2500
2436
  super.tick(deltaTime);
2501
2437
  this.rootComponent.tick(deltaTime);
2502
2438
  }
@@ -2742,6 +2678,146 @@ class Actor extends BaseObject {
2742
2678
  }
2743
2679
  }
2744
2680
 
2681
+ class ThreeObjectLibrary {
2682
+ static disposeMeshMaterial(mesh) {
2683
+ let mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
2684
+ mats.forEach((elem) => {
2685
+ elem.dispose();
2686
+ });
2687
+ }
2688
+ static disposeMeshGeometry(mesh) {
2689
+ mesh.geometry.dispose();
2690
+ }
2691
+ static disposeMeshResource(mesh) {
2692
+ ThreeObjectLibrary.disposeMeshMaterial(mesh);
2693
+ ThreeObjectLibrary.disposeMeshGeometry(mesh);
2694
+ }
2695
+ }
2696
+
2697
+ class LevelActor extends Actor {
2698
+ get scene() {
2699
+ if (!this._scene) {
2700
+ throw new Error("scene is null");
2701
+ }
2702
+ return this._scene;
2703
+ }
2704
+ constructor(app) {
2705
+ let scene = new webgpu.Scene();
2706
+ super(app, new webgpu.Scene());
2707
+ this._scene = null;
2708
+ this._scene = scene;
2709
+ this.isTickEnabled = false;
2710
+ }
2711
+ tick(_deltaTime) {
2712
+ return;
2713
+ }
2714
+ destroy() {
2715
+ this.childActors.forEach((elem) => {
2716
+ elem.destroy();
2717
+ });
2718
+ this.scene.traverse((child) => {
2719
+ if (child instanceof three.Mesh) {
2720
+ ThreeObjectLibrary.disposeMeshResource(child);
2721
+ }
2722
+ });
2723
+ }
2724
+ }
2725
+
2726
+ const DefaultWorldParam = {
2727
+ levelActorClass: LevelActor,
2728
+ };
2729
+
2730
+ class ThreeJsApp {
2731
+ get camera() {
2732
+ return this._camera;
2733
+ }
2734
+ get clock() {
2735
+ return this._clock;
2736
+ }
2737
+ get world() {
2738
+ return this._world;
2739
+ }
2740
+ get viewport() {
2741
+ return this._viewport;
2742
+ }
2743
+ get controller() {
2744
+ return this._controller;
2745
+ }
2746
+ get assetManager() {
2747
+ return this._assetManager;
2748
+ }
2749
+ get appParam() {
2750
+ return this._appParam;
2751
+ }
2752
+ get onCameraChangedDelegate() {
2753
+ return this._onCameraChangedDelegate;
2754
+ }
2755
+ constructor(appParam = DefaultAppParam) {
2756
+ this._appParam = { viewportParam: DefaultViewportParam };
2757
+ this._onCameraChangedDelegate = new Delegate();
2758
+ this._appParam.cameraParam = appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam;
2759
+ this._appParam.renderParam = appParam.renderParam ? appParam.renderParam : DefaultRenderParam;
2760
+ this._appParam.postProcessParam = appParam.postProcessParam ? appParam.postProcessParam : DefaultPostProcessParam;
2761
+ this._appParam.viewportParam = appParam.viewportParam ? appParam.viewportParam : DefaultViewportParam;
2762
+ this._appParam.classes = appParam.classes ? appParam.classes : {
2763
+ assetManagerClass: AssetManager,
2764
+ controllerClass: Controller,
2765
+ worldClass: World,
2766
+ viewportClass: Viewport,
2767
+ };
2768
+ this._clock = new webgpu.Clock();
2769
+ this._camera = CameraFactory.createCamera(this._appParam.cameraParam);
2770
+ this._world = new this._appParam.classes.worldClass(this, this._appParam.worldParam ? this._appParam.worldParam : DefaultWorldParam);
2771
+ this._viewport = new this._appParam.classes.viewportClass(this, this._appParam.viewportParam, this._appParam.renderParam, this._appParam.postProcessParam);
2772
+ this._controller = new this._appParam.classes.controllerClass(this);
2773
+ this._assetManager = new this._appParam.classes.assetManagerClass(this);
2774
+ this.viewport.renderer.setAnimationLoop(() => {
2775
+ this.tick();
2776
+ });
2777
+ this.init();
2778
+ }
2779
+ init() {
2780
+ this.controller.init();
2781
+ this.world.init();
2782
+ this.viewport.init();
2783
+ }
2784
+ tick() {
2785
+ const delta = this._clock.getDelta();
2786
+ this._controller.tick(delta);
2787
+ this.world.tick(delta);
2788
+ this.viewport.render();
2789
+ }
2790
+ destroy() {
2791
+ this.onCameraChangedDelegate.clear();
2792
+ this.world.destroy();
2793
+ this.controller.destroy();
2794
+ this.viewport.destroy();
2795
+ this._assetManager.clearAssets();
2796
+ }
2797
+ updateCamera(param) {
2798
+ const previousCam = this.camera;
2799
+ this._camera = CameraFactory.updataCamera(param, this.camera);
2800
+ if (previousCam !== this.camera) {
2801
+ this._onCameraChangedDelegate.broadcast();
2802
+ }
2803
+ this._camera.updateProjectionMatrix();
2804
+ this.viewport.markRenderStateDirty();
2805
+ }
2806
+ onWindowResize(width, height) {
2807
+ if (this.camera instanceof webgpu.PerspectiveCamera) {
2808
+ this.camera.aspect = width / height;
2809
+ }
2810
+ // if(this.camera instanceof OrthographicCamera)
2811
+ // {
2812
+ // // to do
2813
+ // }
2814
+ this.camera.updateProjectionMatrix();
2815
+ }
2816
+ async renderAsImage(width = 1024, height = 1024) {
2817
+ return await this.viewport.renderAsImage(width, height);
2818
+ }
2819
+ }
2820
+
2745
2821
  /*
2746
2822
  * virtual class of light, should not be use directly.
2747
2823
  */
@@ -3231,12 +3307,14 @@ exports.DefaultSSRParam = DefaultSSRParam;
3231
3307
  exports.DefaultSkyParam = DefaultSkyParam;
3232
3308
  exports.DefaultToneMappingParams = DefaultToneMappingParams;
3233
3309
  exports.DefaultViewportParam = DefaultViewportParam;
3310
+ exports.DefaultWorldParam = DefaultWorldParam;
3234
3311
  exports.Delegate = Delegate;
3235
3312
  exports.DirectionalLightActor = DirectionalLightActor;
3236
3313
  exports.DirectionalLightComponent = DirectionalLightComponent;
3237
3314
  exports.FirstPerson = FirstPerson;
3238
3315
  exports.GeometryAssetPointer = GeometryAssetPointer;
3239
3316
  exports.LabelComponent = LabelComponent;
3317
+ exports.LevelActor = LevelActor;
3240
3318
  exports.MaterialAssetPointer = MaterialAssetPointer;
3241
3319
  exports.MeshComponent = MeshComponent;
3242
3320
  exports.Orbital = Orbital;
@@ -1,4 +1,4 @@
1
- import { MathUtils, Group, Vector3, Box3, Quaternion, Euler, Matrix4, Mesh, LoadingManager, BufferGeometry, Texture, FileLoader, Material, Scene, NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping, NeutralToneMapping, WebGPURenderer, PostProcessing, Color, Vector2, Raycaster, PerspectiveCamera, OrthographicCamera, Clock, DirectionalLight, MeshStandardMaterial, BoxGeometry, MeshBasicMaterial, PlaneGeometry, SphereGeometry, Object3D } from 'three/webgpu';
1
+ import { MathUtils, Group, Vector3, Box3, Quaternion, Euler, Matrix4, Mesh, LoadingManager, BufferGeometry, Texture, FileLoader, Material, NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping, NeutralToneMapping, WebGPURenderer, PostProcessing, Color, Vector2, Raycaster, PerspectiveCamera, OrthographicCamera, Scene, Clock, DirectionalLight, MeshStandardMaterial, BoxGeometry, MeshBasicMaterial, PlaneGeometry, SphereGeometry, Object3D } from 'three/webgpu';
2
2
  import { GLTFLoader, DRACOLoader, CSS2DRenderer, OrbitControls } from 'three/examples/jsm/Addons.js';
3
3
  import { pass, mrt, output, uniform, velocity, metalness, transformedNormalView, blendColor, time, oscSine } from 'three/tsl';
4
4
  import { bloom } from 'three/examples/jsm/tsl/display/BloomNode.js';
@@ -12,6 +12,7 @@ import { motionBlur } from 'three/examples/jsm/tsl/display/MotionBlur.js';
12
12
  import { fxaa } from 'three/examples/jsm/tsl/display/FXAANode.js';
13
13
  import { smaa } from 'three/examples/jsm/tsl/display/SMAANode.js';
14
14
  import { gsap } from 'gsap';
15
+ import { Mesh as Mesh$1 } from 'three';
15
16
  import { SkyMesh } from 'three/examples/jsm/objects/SkyMesh.js';
16
17
  import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
17
18
  import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls';
@@ -21,10 +22,10 @@ import { TransformControls } from 'three/examples/jsm/controls/TransformControls
21
22
  * virtual class of 3D Object, should not be use directly.
22
23
  */
23
24
  class BaseObject {
24
- get IsTickable() {
25
+ get isTickEnabled() {
25
26
  return this.bCanTick;
26
27
  }
27
- set IsTickable(bCanTick) {
28
+ set isTickEnabled(bCanTick) {
28
29
  this.bCanTick = bCanTick;
29
30
  }
30
31
  get uuid() {
@@ -32,6 +33,7 @@ class BaseObject {
32
33
  }
33
34
  constructor(uuid = MathUtils.generateUUID()) {
34
35
  this.bCanTick = false;
36
+ this.tickEvents = [];
35
37
  this.bCanTick = false;
36
38
  this._uuid = uuid;
37
39
  }
@@ -39,6 +41,21 @@ class BaseObject {
39
41
  if (!this.bCanTick) {
40
42
  return;
41
43
  }
44
+ this.tickEvents.forEach((elem) => {
45
+ elem(deltaTime);
46
+ });
47
+ }
48
+ addTickEvent(event) {
49
+ this.tickEvents.push(event);
50
+ }
51
+ removeTickEvent(event) {
52
+ let index = this.tickEvents.indexOf(event);
53
+ if (index != -1) {
54
+ this.tickEvents.splice(index, 1);
55
+ }
56
+ }
57
+ clearTickEvents() {
58
+ this.tickEvents = [];
42
59
  }
43
60
  destroy() {
44
61
  }
@@ -104,7 +121,6 @@ class SceneComponent extends Component {
104
121
  this._parentActor = value;
105
122
  }
106
123
  get parentActor() {
107
- // 通过 _parentActor 私有字段访问父级 Actor
108
124
  return this._parentActor;
109
125
  }
110
126
  get world() {
@@ -120,6 +136,12 @@ class SceneComponent extends Component {
120
136
  createDefaultThreeObject() {
121
137
  this.threeObject = new Group();
122
138
  }
139
+ tick(deltaTime) {
140
+ super.tick(deltaTime);
141
+ this.childrenComponents.forEach((elem) => {
142
+ elem.tick(deltaTime);
143
+ });
144
+ }
123
145
  get isVisible() {
124
146
  if (!this.threeObject) {
125
147
  return false;
@@ -1224,25 +1246,13 @@ const DefaultPostProcessParam = {
1224
1246
  }
1225
1247
  };
1226
1248
 
1227
- class ThreeObjectLibrary {
1228
- static disposeMeshMaterial(mesh) {
1229
- let mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
1230
- mats.forEach((elem) => {
1231
- elem.dispose();
1232
- });
1233
- }
1234
- static disposeMeshGeometry(mesh) {
1235
- mesh.geometry.dispose();
1236
- }
1237
- static disposeMeshResource(mesh) {
1238
- ThreeObjectLibrary.disposeMeshMaterial(mesh);
1239
- ThreeObjectLibrary.disposeMeshGeometry(mesh);
1240
- }
1241
- }
1242
-
1243
1249
  class World {
1244
1250
  get scene() {
1245
- return this._scene;
1251
+ let scene = this.rootActor.scene;
1252
+ if (!scene) {
1253
+ throw new Error("scene is null");
1254
+ }
1255
+ return scene;
1246
1256
  }
1247
1257
  get viewport() {
1248
1258
  return this.app.viewport;
@@ -1250,36 +1260,41 @@ class World {
1250
1260
  get controller() {
1251
1261
  return this.app.controller;
1252
1262
  }
1253
- constructor(app) {
1263
+ constructor(app, worldParam) {
1264
+ this.rootActor = null;
1254
1265
  this.actors = new Set();
1266
+ this.tickableActors = new Set();
1255
1267
  this.app = app;
1256
- this._scene = new Scene();
1268
+ this.worldParam = worldParam;
1257
1269
  }
1258
1270
  init() {
1271
+ this.rootActor = new this.worldParam.levelActorClass(this.app);
1272
+ }
1273
+ addTickableActor(actor) {
1274
+ this.tickableActors.add(actor);
1275
+ }
1276
+ removeTickableActor(actor) {
1277
+ this.tickableActors.delete(actor);
1259
1278
  }
1260
1279
  tick(deltaTime) {
1261
- this.actors.forEach((elem) => {
1280
+ this.tickableActors.forEach((elem) => {
1262
1281
  elem.tick(deltaTime);
1263
1282
  });
1264
1283
  }
1265
1284
  destroy() {
1285
+ this.tickableActors.clear();
1266
1286
  this.actors.forEach((elem) => {
1267
1287
  elem.destroy();
1268
1288
  });
1269
1289
  this.actors.clear();
1270
- this.scene.traverse((child) => {
1271
- if (child instanceof Mesh) {
1272
- ThreeObjectLibrary.disposeMeshResource(child);
1273
- }
1274
- });
1275
- this.scene.clear();
1290
+ this.rootActor.destroy();
1276
1291
  }
1277
1292
  addActor(actor) {
1278
1293
  if (!actor.rootComponent.threeObject) {
1279
- throw Error("actor.threeObject is null");
1294
+ throw new Error("actor.threeObject is null");
1280
1295
  }
1281
1296
  actor.removeFromParent();
1282
- this.scene.add(actor.rootComponent.threeObject);
1297
+ this.rootActor.addChildActor(actor);
1283
1298
  this.actors.add(actor);
1284
1299
  actor.onAddedToWorld(this);
1285
1300
  this.viewport.markRenderStateDirty();
@@ -2272,97 +2287,6 @@ const DefaultAppParam = {
2272
2287
  }
2273
2288
  };
2274
2289
 
2275
- class ThreeJsApp {
2276
- get camera() {
2277
- return this._camera;
2278
- }
2279
- get clock() {
2280
- return this._clock;
2281
- }
2282
- get world() {
2283
- return this._world;
2284
- }
2285
- get viewport() {
2286
- return this._viewport;
2287
- }
2288
- get controller() {
2289
- return this._controller;
2290
- }
2291
- get assetManager() {
2292
- return this._assetManager;
2293
- }
2294
- get appParam() {
2295
- return this._appParam;
2296
- }
2297
- get onCameraChangedDelegate() {
2298
- return this._onCameraChangedDelegate;
2299
- }
2300
- constructor(appParam = DefaultAppParam) {
2301
- this._appParam = { viewportParam: DefaultViewportParam };
2302
- this._onCameraChangedDelegate = new Delegate();
2303
- this._appParam.cameraParam = appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam;
2304
- this._appParam.renderParam = appParam.renderParam ? appParam.renderParam : DefaultRenderParam;
2305
- this._appParam.postProcessParam = appParam.postProcessParam ? appParam.postProcessParam : DefaultPostProcessParam;
2306
- this._appParam.viewportParam = appParam.viewportParam ? appParam.viewportParam : DefaultViewportParam;
2307
- this._appParam.classes = appParam.classes ? appParam.classes : {
2308
- assetManagerClass: AssetManager,
2309
- controllerClass: Controller,
2310
- worldClass: World,
2311
- viewportClass: Viewport,
2312
- };
2313
- this._clock = new Clock();
2314
- this._camera = CameraFactory.createCamera(this._appParam.cameraParam);
2315
- this._world = new this._appParam.classes.worldClass(this);
2316
- this._viewport = new this._appParam.classes.viewportClass(this, this._appParam.viewportParam, this._appParam.renderParam, this._appParam.postProcessParam);
2317
- this._controller = new this._appParam.classes.controllerClass(this);
2318
- this._assetManager = new this._appParam.classes.assetManagerClass(this);
2319
- this.viewport.renderer.setAnimationLoop(() => {
2320
- this.tick();
2321
- });
2322
- this.init();
2323
- }
2324
- init() {
2325
- this.controller.init();
2326
- this.world.init();
2327
- this.viewport.init();
2328
- }
2329
- tick() {
2330
- const delta = this._clock.getDelta();
2331
- this._controller.tick(delta);
2332
- this.world.tick(delta);
2333
- this.viewport.render();
2334
- }
2335
- destroy() {
2336
- this.onCameraChangedDelegate.clear();
2337
- this.world.destroy();
2338
- this.controller.destroy();
2339
- this.viewport.destroy();
2340
- this._assetManager.clearAssets();
2341
- }
2342
- updateCamera(param) {
2343
- const previousCam = this.camera;
2344
- this._camera = CameraFactory.updataCamera(param, this.camera);
2345
- if (previousCam !== this.camera) {
2346
- this._onCameraChangedDelegate.broadcast();
2347
- }
2348
- this._camera.updateProjectionMatrix();
2349
- this.viewport.markRenderStateDirty();
2350
- }
2351
- onWindowResize(width, height) {
2352
- if (this.camera instanceof PerspectiveCamera) {
2353
- this.camera.aspect = width / height;
2354
- }
2355
- // if(this.camera instanceof OrthographicCamera)
2356
- // {
2357
- // // to do
2358
- // }
2359
- this.camera.updateProjectionMatrix();
2360
- }
2361
- async renderAsImage(width = 1024, height = 1024) {
2362
- return await this.viewport.renderAsImage(width, height);
2363
- }
2364
- }
2365
-
2366
2290
  class Actor extends BaseObject {
2367
2291
  get world() {
2368
2292
  return this._world;
@@ -2415,6 +2339,15 @@ class Actor extends BaseObject {
2415
2339
  get isVisible() {
2416
2340
  return this.rootComponent ? this.rootComponent.isVisible : false;
2417
2341
  }
2342
+ set isTickEnabled(bCanTick) {
2343
+ super.isTickEnabled = bCanTick;
2344
+ if (this.isTickEnabled) {
2345
+ this.app.world.addTickableActor(this);
2346
+ }
2347
+ else {
2348
+ this.app.world.removeTickableActor(this);
2349
+ }
2350
+ }
2418
2351
  constructor(app, myThreeObject = null) {
2419
2352
  super();
2420
2353
  this._name = "Actor";
@@ -2495,6 +2428,9 @@ class Actor extends BaseObject {
2495
2428
  }
2496
2429
  }
2497
2430
  tick(deltaTime) {
2431
+ if (!this.isTickEnabled) {
2432
+ return;
2433
+ }
2498
2434
  super.tick(deltaTime);
2499
2435
  this.rootComponent.tick(deltaTime);
2500
2436
  }
@@ -2740,6 +2676,146 @@ class Actor extends BaseObject {
2740
2676
  }
2741
2677
  }
2742
2678
 
2679
+ class ThreeObjectLibrary {
2680
+ static disposeMeshMaterial(mesh) {
2681
+ let mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
2682
+ mats.forEach((elem) => {
2683
+ elem.dispose();
2684
+ });
2685
+ }
2686
+ static disposeMeshGeometry(mesh) {
2687
+ mesh.geometry.dispose();
2688
+ }
2689
+ static disposeMeshResource(mesh) {
2690
+ ThreeObjectLibrary.disposeMeshMaterial(mesh);
2691
+ ThreeObjectLibrary.disposeMeshGeometry(mesh);
2692
+ }
2693
+ }
2694
+
2695
+ class LevelActor extends Actor {
2696
+ get scene() {
2697
+ if (!this._scene) {
2698
+ throw new Error("scene is null");
2699
+ }
2700
+ return this._scene;
2701
+ }
2702
+ constructor(app) {
2703
+ let scene = new Scene();
2704
+ super(app, new Scene());
2705
+ this._scene = null;
2706
+ this._scene = scene;
2707
+ this.isTickEnabled = false;
2708
+ }
2709
+ tick(_deltaTime) {
2710
+ return;
2711
+ }
2712
+ destroy() {
2713
+ this.childActors.forEach((elem) => {
2714
+ elem.destroy();
2715
+ });
2716
+ this.scene.traverse((child) => {
2717
+ if (child instanceof Mesh$1) {
2718
+ ThreeObjectLibrary.disposeMeshResource(child);
2719
+ }
2720
+ });
2721
+ }
2722
+ }
2723
+
2724
+ const DefaultWorldParam = {
2725
+ levelActorClass: LevelActor,
2726
+ };
2727
+
2728
+ class ThreeJsApp {
2729
+ get camera() {
2730
+ return this._camera;
2731
+ }
2732
+ get clock() {
2733
+ return this._clock;
2734
+ }
2735
+ get world() {
2736
+ return this._world;
2737
+ }
2738
+ get viewport() {
2739
+ return this._viewport;
2740
+ }
2741
+ get controller() {
2742
+ return this._controller;
2743
+ }
2744
+ get assetManager() {
2745
+ return this._assetManager;
2746
+ }
2747
+ get appParam() {
2748
+ return this._appParam;
2749
+ }
2750
+ get onCameraChangedDelegate() {
2751
+ return this._onCameraChangedDelegate;
2752
+ }
2753
+ constructor(appParam = DefaultAppParam) {
2754
+ this._appParam = { viewportParam: DefaultViewportParam };
2755
+ this._onCameraChangedDelegate = new Delegate();
2756
+ this._appParam.cameraParam = appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam;
2757
+ this._appParam.renderParam = appParam.renderParam ? appParam.renderParam : DefaultRenderParam;
2758
+ this._appParam.postProcessParam = appParam.postProcessParam ? appParam.postProcessParam : DefaultPostProcessParam;
2759
+ this._appParam.viewportParam = appParam.viewportParam ? appParam.viewportParam : DefaultViewportParam;
2760
+ this._appParam.classes = appParam.classes ? appParam.classes : {
2761
+ assetManagerClass: AssetManager,
2762
+ controllerClass: Controller,
2763
+ worldClass: World,
2764
+ viewportClass: Viewport,
2765
+ };
2766
+ this._clock = new Clock();
2767
+ this._camera = CameraFactory.createCamera(this._appParam.cameraParam);
2768
+ this._world = new this._appParam.classes.worldClass(this, this._appParam.worldParam ? this._appParam.worldParam : DefaultWorldParam);
2769
+ this._viewport = new this._appParam.classes.viewportClass(this, this._appParam.viewportParam, this._appParam.renderParam, this._appParam.postProcessParam);
2770
+ this._controller = new this._appParam.classes.controllerClass(this);
2771
+ this._assetManager = new this._appParam.classes.assetManagerClass(this);
2772
+ this.viewport.renderer.setAnimationLoop(() => {
2773
+ this.tick();
2774
+ });
2775
+ this.init();
2776
+ }
2777
+ init() {
2778
+ this.controller.init();
2779
+ this.world.init();
2780
+ this.viewport.init();
2781
+ }
2782
+ tick() {
2783
+ const delta = this._clock.getDelta();
2784
+ this._controller.tick(delta);
2785
+ this.world.tick(delta);
2786
+ this.viewport.render();
2787
+ }
2788
+ destroy() {
2789
+ this.onCameraChangedDelegate.clear();
2790
+ this.world.destroy();
2791
+ this.controller.destroy();
2792
+ this.viewport.destroy();
2793
+ this._assetManager.clearAssets();
2794
+ }
2795
+ updateCamera(param) {
2796
+ const previousCam = this.camera;
2797
+ this._camera = CameraFactory.updataCamera(param, this.camera);
2798
+ if (previousCam !== this.camera) {
2799
+ this._onCameraChangedDelegate.broadcast();
2800
+ }
2801
+ this._camera.updateProjectionMatrix();
2802
+ this.viewport.markRenderStateDirty();
2803
+ }
2804
+ onWindowResize(width, height) {
2805
+ if (this.camera instanceof PerspectiveCamera) {
2806
+ this.camera.aspect = width / height;
2807
+ }
2808
+ // if(this.camera instanceof OrthographicCamera)
2809
+ // {
2810
+ // // to do
2811
+ // }
2812
+ this.camera.updateProjectionMatrix();
2813
+ }
2814
+ async renderAsImage(width = 1024, height = 1024) {
2815
+ return await this.viewport.renderAsImage(width, height);
2816
+ }
2817
+ }
2818
+
2743
2819
  /*
2744
2820
  * virtual class of light, should not be use directly.
2745
2821
  */
@@ -3211,4 +3287,4 @@ class TransformGizmo extends Pawn {
3211
3287
  }
3212
3288
  }
3213
3289
 
3214
- export { Actor, AssetManager, AttachmentRules, BoxActor, BoxComponent, Controller, DefaultAppParam, DefaultBloomParam, DefaultCameraParam, DefaultDOFParam, DefaultDenoiseParam, DefaultGTAOParam, DefaultOutlineParams, DefaultPostProcessParam, DefaultRenderParam, DefaultSSRParam, DefaultSkyParam, DefaultToneMappingParams, DefaultViewportParam, Delegate, DirectionalLightActor, DirectionalLightComponent, FirstPerson, GeometryAssetPointer, LabelComponent, MaterialAssetPointer, MeshComponent, Orbital, PlaneActor, PlaneComponent, SceneComponent, SkyActor, SkyComponent, SphereComponent, TAssetPointer, TSmartPointer, TextureAssetPointer, ThreeJsApp, ThreeObjectLibrary, ToneMappingOptions, TransformGizmo, Viewport, WebGPUPostProcessFactory, World };
3290
+ export { Actor, AssetManager, AttachmentRules, BoxActor, BoxComponent, Controller, DefaultAppParam, DefaultBloomParam, DefaultCameraParam, DefaultDOFParam, DefaultDenoiseParam, DefaultGTAOParam, DefaultOutlineParams, DefaultPostProcessParam, DefaultRenderParam, DefaultSSRParam, DefaultSkyParam, DefaultToneMappingParams, DefaultViewportParam, DefaultWorldParam, Delegate, DirectionalLightActor, DirectionalLightComponent, FirstPerson, GeometryAssetPointer, LabelComponent, LevelActor, MaterialAssetPointer, MeshComponent, Orbital, PlaneActor, PlaneComponent, SceneComponent, SkyActor, SkyComponent, SphereComponent, TAssetPointer, TSmartPointer, TextureAssetPointer, ThreeJsApp, ThreeObjectLibrary, ToneMappingOptions, TransformGizmo, Viewport, WebGPUPostProcessFactory, World };
package/dist/index.d.ts CHANGED
@@ -16,11 +16,14 @@ export type { AppParam, AppClass } from "./lythreeframe/Frame/Parameters/AppPara
16
16
  export { DefaultRenderParam, DefaultAppParam } from "./lythreeframe/Frame/Parameters/AppParameter";
17
17
  export type { ViewportParam } from './lythreeframe/Frame/Parameters/ViewportParameters';
18
18
  export { DefaultViewportParam } from './lythreeframe/Frame/Parameters/ViewportParameters';
19
+ export type { WorldParam } from "./lythreeframe/Frame/Parameters/WorldParameter";
20
+ export { DefaultWorldParam } from "./lythreeframe/Frame/Parameters/WorldParameter";
19
21
  export { Actor } from "./lythreeframe/Object/Actor";
20
22
  export { DirectionalLightActor } from "./lythreeframe/Object/Actors/Light/DirectionalLightActor";
21
23
  export { BoxActor } from "./lythreeframe/Object/Actors/Shape/BoxActor";
22
24
  export { SkyActor } from "./lythreeframe/Object/Actors/Sky/SkyActor";
23
25
  export { PlaneActor } from "./lythreeframe/Object/Actors/Shape/PlaneActor";
26
+ export { LevelActor } from './lythreeframe/Object/Actors/Level/LevelActor';
24
27
  export { SceneComponent } from "./lythreeframe/Object/Components/SceneComponent";
25
28
  export { MeshComponent } from "./lythreeframe/Object/Components/Mesh/MeshComponent";
26
29
  export { BoxComponent } from "./lythreeframe/Object/Components/Mesh/Shape/BoxComponent";
@@ -1,2 +1,5 @@
1
+ import { LevelActor } from "../../Object/Actors/Level/LevelActor";
1
2
  export interface WorldParam {
3
+ levelActorClass: typeof LevelActor;
2
4
  }
5
+ export declare const DefaultWorldParam: WorldParam;
@@ -3,15 +3,21 @@ import { ThreeJsApp } from "../ThreeJsApp";
3
3
  import { Viewport } from "./Viewport";
4
4
  import { Controller } from "./Controller";
5
5
  import { Actor } from "../Object/Actor";
6
+ import { LevelActor } from "../Object/Actors/Level/LevelActor";
7
+ import { WorldParam } from "./Parameters/WorldParameter";
6
8
  export declare class World {
7
9
  get scene(): Scene;
8
10
  get viewport(): Viewport;
9
11
  get controller(): Controller;
10
- protected _scene: Scene;
12
+ protected rootActor: LevelActor | null;
11
13
  protected app: ThreeJsApp;
12
14
  protected actors: Set<Actor>;
13
- constructor(app: ThreeJsApp);
15
+ protected tickableActors: Set<Actor>;
16
+ protected worldParam: WorldParam;
17
+ constructor(app: ThreeJsApp, worldParam: WorldParam);
14
18
  init(): void;
19
+ addTickableActor(actor: Actor): void;
20
+ removeTickableActor(actor: Actor): void;
15
21
  tick(deltaTime: number): void;
16
22
  destroy(): void;
17
23
  addActor(actor: Actor): void;
@@ -14,6 +14,7 @@ export declare class Actor extends BaseObject {
14
14
  get childActors(): Actor[];
15
15
  get parentActor(): Actor | null;
16
16
  get isVisible(): boolean;
17
+ set isTickEnabled(bCanTick: boolean);
17
18
  protected _name: string;
18
19
  protected _rootComponent: SceneComponent | null;
19
20
  protected _world: World | null;
@@ -0,0 +1,10 @@
1
+ import { Scene } from "three/webgpu";
2
+ import { ThreeJsApp } from "../../../ThreeJsApp";
3
+ import { Actor } from "../../Actor";
4
+ export declare class LevelActor extends Actor {
5
+ get scene(): Scene;
6
+ protected _scene: Scene | null;
7
+ constructor(app: ThreeJsApp);
8
+ tick(_deltaTime: number): void;
9
+ destroy(): void;
10
+ }
@@ -1,10 +1,14 @@
1
1
  export declare abstract class BaseObject {
2
- get IsTickable(): boolean;
3
- set IsTickable(bCanTick: boolean);
2
+ get isTickEnabled(): boolean;
3
+ set isTickEnabled(bCanTick: boolean);
4
4
  get uuid(): string;
5
5
  protected bCanTick: boolean;
6
+ protected tickEvents: ((deltaTime: number) => void)[];
6
7
  protected _uuid: string;
7
8
  protected constructor(uuid?: string);
8
9
  tick(deltaTime: number): void;
10
+ addTickEvent(event: (deltaTime: number) => void): void;
11
+ removeTickEvent(event: (deltaTime: number) => void): void;
12
+ clearTickEvents(): void;
9
13
  destroy(): void;
10
14
  }
@@ -13,6 +13,7 @@ export declare class SceneComponent extends Component {
13
13
  protected app: ThreeJsApp;
14
14
  constructor(app: ThreeJsApp, newThreeObject: Object3D);
15
15
  createDefaultThreeObject(): void;
16
+ tick(deltaTime: number): void;
16
17
  get isVisible(): boolean;
17
18
  setVisible(bVisible: boolean): void;
18
19
  setLayers(layer: number): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lythreeframe",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "Three.js 封装",
5
5
  "main": "dist/bundle.cjs.js",
6
6
  "module": "dist/bundle.esm.js",