1- import { Component } from '@wonderlandengine/api' ;
1+ import { Component , Object3D } from '@wonderlandengine/api' ;
22import { property } from '@wonderlandengine/api/decorators.js' ;
33import { deg2rad , rad2deg } from './utils/utils.js' ;
44import { quat , vec3 } from 'gl-matrix' ;
@@ -52,8 +52,12 @@ export class OrbitalCamera extends Component {
5252 @property . float ( 0.9 )
5353 damping = 0.9 ;
5454
55+ @property . object ( )
56+ target : Object3D | null = null ;
57+
5558 private _mouseDown : boolean = false ;
56- private _origin = [ 0 , 0 , 0 ] ;
59+ private _origin = vec3 . create ( ) ;
60+ private _originCache = vec3 . create ( ) ;
5761 private _azimuth = 0 ;
5862 private _polar = 45 ;
5963
@@ -65,7 +69,11 @@ export class OrbitalCamera extends Component {
6569 private _polarSpeed : number = 0 ;
6670
6771 init ( ) {
68- this . object . getPositionWorld ( this . _origin ) ;
72+ if ( this . target ) {
73+ this . target . getPositionWorld ( this . _origin ) ;
74+ } else {
75+ this . object . getPositionWorld ( this . _origin ) ;
76+ }
6977 }
7078
7179 start ( ) : void {
@@ -113,6 +121,11 @@ export class OrbitalCamera extends Component {
113121 }
114122
115123 update ( ) : void {
124+ // Update the orbit center to the target's position if one is assigned.
125+ if ( this . target ) {
126+ this . target . getPositionWorld ( this . _origin ) ;
127+ }
128+
116129 /* Always apply damping, because there's no event for stop moving */
117130 this . _azimuthSpeed *= this . damping ;
118131 this . _polarSpeed *= this . damping ;
@@ -129,8 +142,13 @@ export class OrbitalCamera extends Component {
129142 this . _polar = Math . min ( this . maxElevation , Math . max ( this . minElevation , this . _polar ) ) ;
130143
131144 /* Update the camera if there's any speed */
132- if ( this . _azimuthSpeed !== 0 || this . _polarSpeed !== 0 ) {
145+ if (
146+ this . _azimuthSpeed !== 0 ||
147+ this . _polarSpeed !== 0 ||
148+ vec3 . equals ( this . _origin , this . _originCache ) === false
149+ ) {
133150 this . _updateCamera ( ) ;
151+ vec3 . copy ( this . _originCache , this . _origin ) ;
134152 }
135153 }
136154
0 commit comments