lythreeframe 1.0.13 → 1.0.15

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 OutlineNode_js = require('three/examples/jsm/tsl/display/OutlineNode.js');
14
14
  var MotionBlur_js = require('three/examples/jsm/tsl/display/MotionBlur.js');
15
15
  var FXAANode_js = require('three/examples/jsm/tsl/display/FXAANode.js');
16
16
  var SMAANode_js = require('three/examples/jsm/tsl/display/SMAANode.js');
17
+ var gsap = require('gsap');
17
18
  var CSS2DRenderer_js = require('three/examples/jsm/renderers/CSS2DRenderer.js');
18
19
  var PointerLockControls = require('three/examples/jsm/controls/PointerLockControls');
19
20
  var TransformControls_js = require('three/examples/jsm/controls/TransformControls.js');
@@ -1353,6 +1354,9 @@ class Viewport {
1353
1354
  }
1354
1355
  });
1355
1356
  this.resizeObserver.observe(this._canvas);
1357
+ this.app.onCameraChangedDelegate.add(() => {
1358
+ this.setupPostProcess();
1359
+ });
1356
1360
  this.setupPostProcess();
1357
1361
  }
1358
1362
  init() {
@@ -1684,6 +1688,10 @@ class Pawn {
1684
1688
  }
1685
1689
  unpossess() {
1686
1690
  }
1691
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1692
+ }
1693
+ stopFocusing() {
1694
+ }
1687
1695
  destroy() {
1688
1696
  }
1689
1697
  }
@@ -1695,8 +1703,12 @@ class Orbital extends Pawn {
1695
1703
  }
1696
1704
  return this._control;
1697
1705
  }
1706
+ get camera() {
1707
+ return this.controller.camera;
1708
+ }
1698
1709
  constructor(controller) {
1699
1710
  super(controller);
1711
+ this.anim = null;
1700
1712
  this.changeEvent = () => { this.onChange(); };
1701
1713
  this._control = new Addons_js.OrbitControls(controller.camera, controller.viewPort.canvas);
1702
1714
  this.control.target.set(0, 0, 0);
@@ -1714,6 +1726,73 @@ class Orbital extends Pawn {
1714
1726
  onChange() {
1715
1727
  this.controller.viewPort.markRenderStateDirty();
1716
1728
  }
1729
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1730
+ this.stopFocusing();
1731
+ let finalQuat = null;
1732
+ if (targetQuat instanceof three.Euler) {
1733
+ finalQuat = new three.Quaternion().setFromEuler(targetQuat);
1734
+ }
1735
+ else {
1736
+ finalQuat = targetQuat;
1737
+ }
1738
+ if (!targetQuat) {
1739
+ finalQuat = this.controller.camera.quaternion;
1740
+ }
1741
+ finalQuat.normalize();
1742
+ let dir = new three.Vector3(0, 0, 1);
1743
+ dir.applyQuaternion(finalQuat).multiplyScalar(-1);
1744
+ dir.normalize();
1745
+ // quat
1746
+ // let startQuat = this.camera.quaternion.clone();
1747
+ // position
1748
+ let startPos = this.camera.position.clone();
1749
+ let endPos = new three.Vector3(targetPos.x + dir.x * distance * -1, targetPos.y + dir.y * distance * -1, targetPos.z + dir.z * distance * -1);
1750
+ let posDir = new three.Vector3().subVectors(endPos, startPos);
1751
+ let movingLength = posDir.length();
1752
+ posDir.normalize();
1753
+ // target
1754
+ let startTarget = this.control.target.clone();
1755
+ let targetDir = new three.Vector3().subVectors(targetPos, startTarget);
1756
+ let targetDistance = targetDir.length();
1757
+ targetDir.normalize();
1758
+ // start animation
1759
+ if (movingLength + targetDistance < 0.1) {
1760
+ return;
1761
+ }
1762
+ //this.enabled = false;
1763
+ let obj = { alpha: 0 };
1764
+ this.anim = gsap.gsap.to(obj, {
1765
+ duration: time,
1766
+ alpha: 1,
1767
+ onUpdate: () => {
1768
+ let alpha = obj.alpha;
1769
+ let camPos = new three.Vector3(startPos.x + posDir.x * alpha * movingLength, startPos.y + posDir.y * alpha * movingLength, startPos.z + posDir.z * alpha * movingLength);
1770
+ let targetPos = new three.Vector3(startTarget.x + targetDir.x * alpha * targetDistance, startTarget.y + targetDir.y * alpha * targetDistance, startTarget.z + targetDir.z * alpha * targetDistance);
1771
+ this.control.object.position.copy(camPos);
1772
+ this.control.target.copy(targetPos);
1773
+ this.control.update();
1774
+ if (onGoing) {
1775
+ onGoing();
1776
+ }
1777
+ if (this.controller.viewPort)
1778
+ this.controller.viewPort.markRenderStateDirty();
1779
+ },
1780
+ onComplete: function () {
1781
+ //_this.enabled = true;
1782
+ if (onFinished) {
1783
+ onFinished();
1784
+ }
1785
+ if (this.controller.viewPort)
1786
+ this.controller.viewPort.markRenderStateDirty();
1787
+ }
1788
+ });
1789
+ }
1790
+ stopFocusing() {
1791
+ if (this.anim) {
1792
+ this.anim.kill();
1793
+ }
1794
+ this.anim = null;
1795
+ }
1717
1796
  }
1718
1797
 
1719
1798
  class Controller {
@@ -1892,6 +1971,9 @@ class Controller {
1892
1971
  }
1893
1972
  return null;
1894
1973
  }
1974
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1975
+ this.pawn.focusTo(targetPos, targetQuat, distance, time, onGoing, onFinished);
1976
+ }
1895
1977
  }
1896
1978
 
1897
1979
  class CameraFactory {
@@ -1937,6 +2019,7 @@ class CameraFactory {
1937
2019
  return new three.OrthographicCamera(param.param.left, param.param.right, param.param.top, param.param.bottom, param.near, param.far);
1938
2020
  }
1939
2021
  }
2022
+ return camera;
1940
2023
  }
1941
2024
  }
1942
2025
 
@@ -1981,7 +2064,11 @@ class ThreeJsApp {
1981
2064
  get controllerClass() {
1982
2065
  return Controller;
1983
2066
  }
2067
+ get onCameraChangedDelegate() {
2068
+ return this._onCameraChangedDelegate;
2069
+ }
1984
2070
  constructor(elementId, appParam = DefaultAppParam) {
2071
+ this._onCameraChangedDelegate = new Delegate();
1985
2072
  this._clock = new three.Clock();
1986
2073
  this._camera = CameraFactory.createCamera(appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam);
1987
2074
  this._world = new this.worldClass(this);
@@ -2003,6 +2090,14 @@ class ThreeJsApp {
2003
2090
  this.world.tick(delta);
2004
2091
  this.viewport.render();
2005
2092
  }
2093
+ updateCamera(param) {
2094
+ const previousCam = this.camera;
2095
+ this._camera = CameraFactory.updataCamera(param, this.camera);
2096
+ if (previousCam !== this.camera) {
2097
+ this._onCameraChangedDelegate.broadcast();
2098
+ }
2099
+ this.viewport.markRenderStateDirty();
2100
+ }
2006
2101
  onWindowResize(width, height) {
2007
2102
  if (this.camera instanceof three.PerspectiveCamera) {
2008
2103
  this.camera.aspect = width / height;
@@ -2014,6 +2109,7 @@ class ThreeJsApp {
2014
2109
  this.camera.updateProjectionMatrix();
2015
2110
  }
2016
2111
  destroy() {
2112
+ this.onCameraChangedDelegate.clear();
2017
2113
  this.world.destroy();
2018
2114
  this.controller.destroy();
2019
2115
  this.viewport.destroy();
@@ -12,6 +12,7 @@ import { outline } from 'three/examples/jsm/tsl/display/OutlineNode.js';
12
12
  import { motionBlur } from 'three/examples/jsm/tsl/display/MotionBlur.js';
13
13
  import { fxaa } from 'three/examples/jsm/tsl/display/FXAANode.js';
14
14
  import { smaa } from 'three/examples/jsm/tsl/display/SMAANode.js';
15
+ import { gsap } from 'gsap';
15
16
  import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
16
17
  import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls';
17
18
  import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js';
@@ -1351,6 +1352,9 @@ class Viewport {
1351
1352
  }
1352
1353
  });
1353
1354
  this.resizeObserver.observe(this._canvas);
1355
+ this.app.onCameraChangedDelegate.add(() => {
1356
+ this.setupPostProcess();
1357
+ });
1354
1358
  this.setupPostProcess();
1355
1359
  }
1356
1360
  init() {
@@ -1682,6 +1686,10 @@ class Pawn {
1682
1686
  }
1683
1687
  unpossess() {
1684
1688
  }
1689
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1690
+ }
1691
+ stopFocusing() {
1692
+ }
1685
1693
  destroy() {
1686
1694
  }
1687
1695
  }
@@ -1693,8 +1701,12 @@ class Orbital extends Pawn {
1693
1701
  }
1694
1702
  return this._control;
1695
1703
  }
1704
+ get camera() {
1705
+ return this.controller.camera;
1706
+ }
1696
1707
  constructor(controller) {
1697
1708
  super(controller);
1709
+ this.anim = null;
1698
1710
  this.changeEvent = () => { this.onChange(); };
1699
1711
  this._control = new OrbitControls(controller.camera, controller.viewPort.canvas);
1700
1712
  this.control.target.set(0, 0, 0);
@@ -1712,6 +1724,73 @@ class Orbital extends Pawn {
1712
1724
  onChange() {
1713
1725
  this.controller.viewPort.markRenderStateDirty();
1714
1726
  }
1727
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1728
+ this.stopFocusing();
1729
+ let finalQuat = null;
1730
+ if (targetQuat instanceof Euler) {
1731
+ finalQuat = new Quaternion().setFromEuler(targetQuat);
1732
+ }
1733
+ else {
1734
+ finalQuat = targetQuat;
1735
+ }
1736
+ if (!targetQuat) {
1737
+ finalQuat = this.controller.camera.quaternion;
1738
+ }
1739
+ finalQuat.normalize();
1740
+ let dir = new Vector3(0, 0, 1);
1741
+ dir.applyQuaternion(finalQuat).multiplyScalar(-1);
1742
+ dir.normalize();
1743
+ // quat
1744
+ // let startQuat = this.camera.quaternion.clone();
1745
+ // position
1746
+ let startPos = this.camera.position.clone();
1747
+ let endPos = new Vector3(targetPos.x + dir.x * distance * -1, targetPos.y + dir.y * distance * -1, targetPos.z + dir.z * distance * -1);
1748
+ let posDir = new Vector3().subVectors(endPos, startPos);
1749
+ let movingLength = posDir.length();
1750
+ posDir.normalize();
1751
+ // target
1752
+ let startTarget = this.control.target.clone();
1753
+ let targetDir = new Vector3().subVectors(targetPos, startTarget);
1754
+ let targetDistance = targetDir.length();
1755
+ targetDir.normalize();
1756
+ // start animation
1757
+ if (movingLength + targetDistance < 0.1) {
1758
+ return;
1759
+ }
1760
+ //this.enabled = false;
1761
+ let obj = { alpha: 0 };
1762
+ this.anim = gsap.to(obj, {
1763
+ duration: time,
1764
+ alpha: 1,
1765
+ onUpdate: () => {
1766
+ let alpha = obj.alpha;
1767
+ let camPos = new Vector3(startPos.x + posDir.x * alpha * movingLength, startPos.y + posDir.y * alpha * movingLength, startPos.z + posDir.z * alpha * movingLength);
1768
+ let targetPos = new Vector3(startTarget.x + targetDir.x * alpha * targetDistance, startTarget.y + targetDir.y * alpha * targetDistance, startTarget.z + targetDir.z * alpha * targetDistance);
1769
+ this.control.object.position.copy(camPos);
1770
+ this.control.target.copy(targetPos);
1771
+ this.control.update();
1772
+ if (onGoing) {
1773
+ onGoing();
1774
+ }
1775
+ if (this.controller.viewPort)
1776
+ this.controller.viewPort.markRenderStateDirty();
1777
+ },
1778
+ onComplete: function () {
1779
+ //_this.enabled = true;
1780
+ if (onFinished) {
1781
+ onFinished();
1782
+ }
1783
+ if (this.controller.viewPort)
1784
+ this.controller.viewPort.markRenderStateDirty();
1785
+ }
1786
+ });
1787
+ }
1788
+ stopFocusing() {
1789
+ if (this.anim) {
1790
+ this.anim.kill();
1791
+ }
1792
+ this.anim = null;
1793
+ }
1715
1794
  }
1716
1795
 
1717
1796
  class Controller {
@@ -1890,6 +1969,9 @@ class Controller {
1890
1969
  }
1891
1970
  return null;
1892
1971
  }
1972
+ focusTo(targetPos, targetQuat, distance, time, onGoing = null, onFinished = null) {
1973
+ this.pawn.focusTo(targetPos, targetQuat, distance, time, onGoing, onFinished);
1974
+ }
1893
1975
  }
1894
1976
 
1895
1977
  class CameraFactory {
@@ -1935,6 +2017,7 @@ class CameraFactory {
1935
2017
  return new OrthographicCamera(param.param.left, param.param.right, param.param.top, param.param.bottom, param.near, param.far);
1936
2018
  }
1937
2019
  }
2020
+ return camera;
1938
2021
  }
1939
2022
  }
1940
2023
 
@@ -1979,7 +2062,11 @@ class ThreeJsApp {
1979
2062
  get controllerClass() {
1980
2063
  return Controller;
1981
2064
  }
2065
+ get onCameraChangedDelegate() {
2066
+ return this._onCameraChangedDelegate;
2067
+ }
1982
2068
  constructor(elementId, appParam = DefaultAppParam) {
2069
+ this._onCameraChangedDelegate = new Delegate();
1983
2070
  this._clock = new Clock();
1984
2071
  this._camera = CameraFactory.createCamera(appParam.cameraParam ? appParam.cameraParam : DefaultCameraParam);
1985
2072
  this._world = new this.worldClass(this);
@@ -2001,6 +2088,14 @@ class ThreeJsApp {
2001
2088
  this.world.tick(delta);
2002
2089
  this.viewport.render();
2003
2090
  }
2091
+ updateCamera(param) {
2092
+ const previousCam = this.camera;
2093
+ this._camera = CameraFactory.updataCamera(param, this.camera);
2094
+ if (previousCam !== this.camera) {
2095
+ this._onCameraChangedDelegate.broadcast();
2096
+ }
2097
+ this.viewport.markRenderStateDirty();
2098
+ }
2004
2099
  onWindowResize(width, height) {
2005
2100
  if (this.camera instanceof PerspectiveCamera) {
2006
2101
  this.camera.aspect = width / height;
@@ -2012,6 +2107,7 @@ class ThreeJsApp {
2012
2107
  this.camera.updateProjectionMatrix();
2013
2108
  }
2014
2109
  destroy() {
2110
+ this.onCameraChangedDelegate.clear();
2015
2111
  this.world.destroy();
2016
2112
  this.controller.destroy();
2017
2113
  this.viewport.destroy();
@@ -16,5 +16,5 @@ export interface OrthographicCameraParam {
16
16
  }
17
17
  export declare class CameraFactory {
18
18
  static createCamera(param: CameraParam): PerspectiveCamera | OrthographicCamera;
19
- static updataCamera(param: CameraParam, camera: PerspectiveCamera | OrthographicCamera): PerspectiveCamera | OrthographicCamera | undefined;
19
+ static updataCamera(param: CameraParam, camera: PerspectiveCamera | OrthographicCamera): PerspectiveCamera | OrthographicCamera;
20
20
  }
@@ -1,5 +1,5 @@
1
1
  import { ThreeJsApp } from "../ThreeJsApp";
2
- import { Intersection, OrthographicCamera, PerspectiveCamera } from "three";
2
+ import { Euler, Intersection, OrthographicCamera, PerspectiveCamera, Quaternion, Vector3 } from "three";
3
3
  import { World } from "./World";
4
4
  import { Viewport } from "./Viewport";
5
5
  import { Pawn } from "../Object/PawnV2/Pawn";
@@ -37,4 +37,5 @@ export declare class Controller {
37
37
  onPointerLeaveEvent(event: MouseEvent): void;
38
38
  getHitResultUnderCursor(): Intersection | null;
39
39
  getHitResultFromScreenPoint(x: number, y: number): Intersection | null;
40
+ focusTo(targetPos: Vector3, targetQuat: Quaternion | Euler, distance: number, time: number, onGoing?: (() => void) | null, onFinished?: (() => void) | null): void;
40
41
  }
@@ -1,12 +1,17 @@
1
1
  import { Pawn } from "./Pawn";
2
2
  import { Controller } from "../../Frame/Controller";
3
3
  import { OrbitControls } from "three/examples/jsm/Addons.js";
4
+ import { Euler, Quaternion, Vector3 } from "three";
4
5
  export declare class Orbital extends Pawn {
6
+ private anim;
5
7
  get control(): OrbitControls;
6
8
  protected _control: OrbitControls;
7
9
  protected changeEvent: () => void;
10
+ get camera(): import("three").PerspectiveCamera | import("three").OrthographicCamera;
8
11
  constructor(controller: Controller);
9
12
  possess(): void;
10
13
  unpossess(): void;
11
14
  onChange(): void;
15
+ focusTo(targetPos: Vector3, targetQuat: Quaternion | Euler, distance: number, time: number, onGoing?: (() => void) | null, onFinished?: (() => void) | null): void;
16
+ stopFocusing(): void;
12
17
  }
@@ -1,3 +1,4 @@
1
+ import { Euler, Quaternion, Vector3 } from "three";
1
2
  import { Controller } from "../../Frame/Controller";
2
3
  export declare abstract class Pawn {
3
4
  get camera(): import("three").PerspectiveCamera | import("three").OrthographicCamera;
@@ -10,5 +11,7 @@ export declare abstract class Pawn {
10
11
  tick(deltaTime: number): void;
11
12
  possess(): void;
12
13
  unpossess(): void;
14
+ focusTo(targetPos: Vector3, targetQuat: Quaternion | Euler, distance: number, time: number, onGoing?: (() => void) | null, onFinished?: (() => void) | null): void;
15
+ stopFocusing(): void;
13
16
  destroy(): void;
14
17
  }
@@ -5,6 +5,7 @@ import { Controller } from "./Frame/Controller";
5
5
  import { Clock, OrthographicCamera, PerspectiveCamera } from "three";
6
6
  import { CameraParam } from "./Factory/CameraFactory";
7
7
  import { WebGPURendererParameters } from 'three/src/renderers/webgpu/WebGPURenderer.js';
8
+ import { Delegate } from './Delegate';
8
9
  export interface AppParam {
9
10
  renderParam?: WebGPURendererParameters;
10
11
  cameraParam?: CameraParam;
@@ -27,9 +28,12 @@ export declare class ThreeJsApp {
27
28
  protected _world: World;
28
29
  protected _viewport: Viewport;
29
30
  protected _controller: Controller;
31
+ get onCameraChangedDelegate(): Delegate<[void]>;
32
+ protected _onCameraChangedDelegate: Delegate<[void]>;
30
33
  constructor(elementId: string, appParam?: AppParam);
31
34
  init(): void;
32
35
  tick(): void;
36
+ updateCamera(param: CameraParam): void;
33
37
  onWindowResize(width: number, height: number): void;
34
38
  destroy(): void;
35
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lythreeframe",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Three.js 封装",
5
5
  "main": "dist/bundle.cjs.js",
6
6
  "module": "dist/bundle.esm.js",