3
3
////////////////////////////////////////////////////////////////////////////////
4
4
var OrbitCamera = pc . createScript ( 'orbitCamera' ) ;
5
5
6
- OrbitCamera . attributes . add ( 'distanceMax' , { type : 'number' , default : 0 , title : 'Distance Max' , description : 'Setting this at 0 will give an infinite distance limit' } ) ;
7
- OrbitCamera . attributes . add ( 'distanceMin' , { type : 'number' , default : 0 , title : 'Distance Min' } ) ;
8
- OrbitCamera . attributes . add ( 'pitchAngleMax' , { type : 'number' , default : 90 , title : 'Pitch Angle Max (degrees)' } ) ;
9
- OrbitCamera . attributes . add ( 'pitchAngleMin' , { type : 'number' , default : - 90 , title : 'Pitch Angle Min (degrees)' } ) ;
6
+ OrbitCamera . attributes . add ( 'distanceMax' , { type : 'number' , default : 0 , title : 'Distance Max' , description : 'Setting this at 0 will give an infinite distance limit' } ) ;
7
+ OrbitCamera . attributes . add ( 'distanceMin' , { type : 'number' , default : 0 , title : 'Distance Min' } ) ;
8
+ OrbitCamera . attributes . add ( 'pitchAngleMax' , { type : 'number' , default : 90 , title : 'Pitch Angle Max (degrees)' } ) ;
9
+ OrbitCamera . attributes . add ( 'pitchAngleMin' , { type : 'number' , default : - 90 , title : 'Pitch Angle Min (degrees)' } ) ;
10
10
11
11
OrbitCamera . attributes . add ( 'inertiaFactor' , {
12
12
type : 'number' ,
@@ -32,11 +32,11 @@ OrbitCamera.attributes.add('frameOnStart', {
32
32
// Property to get and set the distance between the pivot point and camera
33
33
// Clamped between this.distanceMin and this.distanceMax
34
34
Object . defineProperty ( OrbitCamera . prototype , "distance" , {
35
- get : function ( ) {
35
+ get : function ( ) {
36
36
return this . _targetDistance ;
37
37
} ,
38
38
39
- set : function ( value ) {
39
+ set : function ( value ) {
40
40
this . _targetDistance = this . _clampDistance ( value ) ;
41
41
}
42
42
} ) ;
@@ -46,26 +46,26 @@ Object.defineProperty(OrbitCamera.prototype, "distance", {
46
46
// Clamped between this.pitchAngleMin and this.pitchAngleMax
47
47
// When set at 0, the camera angle is flat, looking along the horizon
48
48
Object . defineProperty ( OrbitCamera . prototype , "pitch" , {
49
- get : function ( ) {
49
+ get : function ( ) {
50
50
return this . _targetPitch ;
51
51
} ,
52
52
53
- set : function ( value ) {
53
+ set : function ( value ) {
54
54
this . _targetPitch = this . _clampPitchAngle ( value ) ;
55
55
}
56
56
} ) ;
57
57
58
58
59
59
// Property to get and set the yaw of the camera around the pivot point (degrees)
60
60
Object . defineProperty ( OrbitCamera . prototype , "yaw" , {
61
- get : function ( ) {
61
+ get : function ( ) {
62
62
return this . _targetYaw ;
63
63
} ,
64
64
65
- set : function ( value ) {
65
+ set : function ( value ) {
66
66
this . _targetYaw = value ;
67
67
68
- // Ensure that the yaw takes the shortest route by making sure that
68
+ // Ensure that the yaw takes the shortest route by making sure that
69
69
// the difference between the targetYaw and the actual is 180 degrees
70
70
// in either direction
71
71
var diff = this . _targetYaw - this . _yaw ;
@@ -83,11 +83,11 @@ Object.defineProperty(OrbitCamera.prototype, "yaw", {
83
83
84
84
// Property to get and set the world position of the pivot point that the camera orbits around
85
85
Object . defineProperty ( OrbitCamera . prototype , "pivotPoint" , {
86
- get : function ( ) {
86
+ get : function ( ) {
87
87
return this . _pivotPoint ;
88
88
} ,
89
89
90
- set : function ( value ) {
90
+ set : function ( value ) {
91
91
this . _pivotPoint . copy ( value ) ;
92
92
}
93
93
} ) ;
@@ -99,9 +99,12 @@ OrbitCamera.prototype.focus = function (focusEntity) {
99
99
this . _buildAabb ( focusEntity , 0 ) ;
100
100
101
101
var halfExtents = this . _modelsAabb . halfExtents ;
102
- var radius = Math . max ( halfExtents . x , Math . max ( halfExtents . y , halfExtents . z ) ) ;
103
102
104
- this . distance = ( radius * 1.5 ) / Math . sin ( 0.5 * this . entity . camera . fov * pc . math . DEG_TO_RAD ) ;
103
+ var distance = Math . max ( halfExtents . x , Math . max ( halfExtents . y , halfExtents . z ) ) ;
104
+ distance = ( distance / Math . tan ( 0.5 * this . entity . camera . fov * pc . math . DEG_TO_RAD ) ) ;
105
+ distance = ( distance * 2 ) ;
106
+
107
+ this . distance = distance ;
105
108
106
109
this . _removeInertia ( ) ;
107
110
@@ -202,19 +205,19 @@ OrbitCamera.prototype.initialize = function () {
202
205
203
206
// Reapply the clamps if they are changed in the editor
204
207
this . on ( 'attr:distanceMin' , function ( value , prev ) {
205
- this . _distance = this . _clampDistance ( this . _distance ) ;
208
+ this . _targetDistance = this . _clampDistance ( this . _distance ) ;
206
209
} ) ;
207
210
208
211
this . on ( 'attr:distanceMax' , function ( value , prev ) {
209
- this . _distance = this . _clampDistance ( this . _distance ) ;
212
+ this . _targetDistance = this . _clampDistance ( this . _distance ) ;
210
213
} ) ;
211
214
212
215
this . on ( 'attr:pitchAngleMin' , function ( value , prev ) {
213
- this . _pitch = this . _clampPitchAngle ( this . _pitch ) ;
216
+ this . _targetPitch = this . _clampPitchAngle ( this . _pitch ) ;
214
217
} ) ;
215
218
216
219
this . on ( 'attr:pitchAngleMax' , function ( value , prev ) {
217
- this . _pitch = this . _clampPitchAngle ( this . _pitch ) ;
220
+ this . _targetPitch = this . _clampPitchAngle ( this . _pitch ) ;
218
221
} ) ;
219
222
220
223
// Focus on the entity if we change the focus entity
@@ -232,13 +235,13 @@ OrbitCamera.prototype.initialize = function () {
232
235
}
233
236
} ) ;
234
237
235
- this . on ( 'destroy' , function ( ) {
238
+ this . on ( 'destroy' , function ( ) {
236
239
window . removeEventListener ( 'resize' , onWindowResize , false ) ;
237
240
} ) ;
238
241
} ;
239
242
240
243
241
- OrbitCamera . prototype . update = function ( dt ) {
244
+ OrbitCamera . prototype . update = function ( dt ) {
242
245
// Add inertia, if any
243
246
var t = this . inertiaFactor === 0 ? 1 : Math . min ( dt / this . inertiaFactor , 1 ) ;
244
247
this . _distance = pc . math . lerp ( this . _distance , this . _targetDistance , t ) ;
@@ -251,7 +254,7 @@ OrbitCamera.prototype.update = function (dt) {
251
254
252
255
OrbitCamera . prototype . _updatePosition = function ( ) {
253
256
// Work out the camera position based on the pivot point, pitch, yaw and distance
254
- this . entity . setLocalPosition ( 0 , 0 , 0 ) ;
257
+ this . entity . setLocalPosition ( 0 , 0 , 0 ) ;
255
258
this . entity . setLocalEulerAngles ( this . _pitch , this . _yaw , 0 ) ;
256
259
257
260
var position = this . entity . getPosition ( ) ;
@@ -280,15 +283,32 @@ OrbitCamera.prototype._checkAspectRatio = function () {
280
283
281
284
282
285
OrbitCamera . prototype . _buildAabb = function ( entity , modelsAdded ) {
283
- var i = 0 ;
286
+ var i = 0 , j = 0 , meshInstances ;
287
+
288
+ if ( entity instanceof pc . Entity ) {
289
+ var allMeshInstances = [ ] ;
290
+ var renders = entity . findComponents ( 'render' ) ;
291
+
292
+ for ( i = 0 ; i < renders . length ; ++ i ) {
293
+ meshInstances = renders [ i ] . meshInstances ;
294
+ for ( j = 0 ; j < meshInstances . length ; j ++ ) {
295
+ allMeshInstances . push ( meshInstances [ j ] ) ;
296
+ }
297
+ }
284
298
285
- if ( entity . model ) {
286
- var mi = entity . model . meshInstances ;
287
- for ( i = 0 ; i < mi . length ; i ++ ) {
299
+ var models = entity . findComponents ( 'model' ) ;
300
+ for ( i = 0 ; i < models . length ; ++ i ) {
301
+ meshInstances = models [ i ] . meshInstances ;
302
+ for ( j = 0 ; j < meshInstances . length ; j ++ ) {
303
+ allMeshInstances . push ( meshInstances [ j ] ) ;
304
+ }
305
+ }
306
+
307
+ for ( i = 0 ; i < allMeshInstances . length ; i ++ ) {
288
308
if ( modelsAdded === 0 ) {
289
- this . _modelsAabb . copy ( mi [ i ] . aabb ) ;
309
+ this . _modelsAabb . copy ( allMeshInstances [ i ] . aabb ) ;
290
310
} else {
291
- this . _modelsAabb . add ( mi [ i ] . aabb ) ;
311
+ this . _modelsAabb . add ( allMeshInstances [ i ] . aabb ) ;
292
312
}
293
313
294
314
modelsAdded += 1 ;
@@ -314,9 +334,9 @@ OrbitCamera.prototype._calcYaw = function (quat) {
314
334
OrbitCamera . prototype . _clampDistance = function ( distance ) {
315
335
if ( this . distanceMax > 0 ) {
316
336
return pc . math . clamp ( distance , this . distanceMin , this . distanceMax ) ;
337
+ } else {
338
+ return Math . max ( distance , this . distanceMin ) ;
317
339
}
318
- return Math . max ( distance , this . distanceMin ) ;
319
-
320
340
} ;
321
341
322
342
@@ -329,7 +349,7 @@ OrbitCamera.prototype._clampPitchAngle = function (pitch) {
329
349
OrbitCamera . quatWithoutYaw = new pc . Quat ( ) ;
330
350
OrbitCamera . yawOffset = new pc . Quat ( ) ;
331
351
332
- OrbitCamera . prototype . _calcPitch = function ( quat , yaw ) {
352
+ OrbitCamera . prototype . _calcPitch = function ( quat , yaw ) {
333
353
var quatWithoutYaw = OrbitCamera . quatWithoutYaw ;
334
354
var yawOffset = OrbitCamera . yawOffset ;
335
355
0 commit comments