Skip to content

Commit 159dec0

Browse files
committed
Added render component support
1 parent f09c474 commit 159dec0

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

scripts/camera/orbit-camera.js

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
////////////////////////////////////////////////////////////////////////////////
44
var OrbitCamera = pc.createScript('orbitCamera');
55

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)'});
1010

1111
OrbitCamera.attributes.add('inertiaFactor', {
1212
type: 'number',
@@ -32,11 +32,11 @@ OrbitCamera.attributes.add('frameOnStart', {
3232
// Property to get and set the distance between the pivot point and camera
3333
// Clamped between this.distanceMin and this.distanceMax
3434
Object.defineProperty(OrbitCamera.prototype, "distance", {
35-
get: function () {
35+
get: function() {
3636
return this._targetDistance;
3737
},
3838

39-
set: function (value) {
39+
set: function(value) {
4040
this._targetDistance = this._clampDistance(value);
4141
}
4242
});
@@ -46,26 +46,26 @@ Object.defineProperty(OrbitCamera.prototype, "distance", {
4646
// Clamped between this.pitchAngleMin and this.pitchAngleMax
4747
// When set at 0, the camera angle is flat, looking along the horizon
4848
Object.defineProperty(OrbitCamera.prototype, "pitch", {
49-
get: function () {
49+
get: function() {
5050
return this._targetPitch;
5151
},
5252

53-
set: function (value) {
53+
set: function(value) {
5454
this._targetPitch = this._clampPitchAngle(value);
5555
}
5656
});
5757

5858

5959
// Property to get and set the yaw of the camera around the pivot point (degrees)
6060
Object.defineProperty(OrbitCamera.prototype, "yaw", {
61-
get: function () {
61+
get: function() {
6262
return this._targetYaw;
6363
},
6464

65-
set: function (value) {
65+
set: function(value) {
6666
this._targetYaw = value;
6767

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
6969
// the difference between the targetYaw and the actual is 180 degrees
7070
// in either direction
7171
var diff = this._targetYaw - this._yaw;
@@ -83,11 +83,11 @@ Object.defineProperty(OrbitCamera.prototype, "yaw", {
8383

8484
// Property to get and set the world position of the pivot point that the camera orbits around
8585
Object.defineProperty(OrbitCamera.prototype, "pivotPoint", {
86-
get: function () {
86+
get: function() {
8787
return this._pivotPoint;
8888
},
8989

90-
set: function (value) {
90+
set: function(value) {
9191
this._pivotPoint.copy(value);
9292
}
9393
});
@@ -99,9 +99,12 @@ OrbitCamera.prototype.focus = function (focusEntity) {
9999
this._buildAabb(focusEntity, 0);
100100

101101
var halfExtents = this._modelsAabb.halfExtents;
102-
var radius = Math.max(halfExtents.x, Math.max(halfExtents.y, halfExtents.z));
103102

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;
105108

106109
this._removeInertia();
107110

@@ -202,19 +205,19 @@ OrbitCamera.prototype.initialize = function () {
202205

203206
// Reapply the clamps if they are changed in the editor
204207
this.on('attr:distanceMin', function (value, prev) {
205-
this._distance = this._clampDistance(this._distance);
208+
this._targetDistance = this._clampDistance(this._distance);
206209
});
207210

208211
this.on('attr:distanceMax', function (value, prev) {
209-
this._distance = this._clampDistance(this._distance);
212+
this._targetDistance = this._clampDistance(this._distance);
210213
});
211214

212215
this.on('attr:pitchAngleMin', function (value, prev) {
213-
this._pitch = this._clampPitchAngle(this._pitch);
216+
this._targetPitch = this._clampPitchAngle(this._pitch);
214217
});
215218

216219
this.on('attr:pitchAngleMax', function (value, prev) {
217-
this._pitch = this._clampPitchAngle(this._pitch);
220+
this._targetPitch = this._clampPitchAngle(this._pitch);
218221
});
219222

220223
// Focus on the entity if we change the focus entity
@@ -232,13 +235,13 @@ OrbitCamera.prototype.initialize = function () {
232235
}
233236
});
234237

235-
this.on('destroy', function () {
238+
this.on('destroy', function() {
236239
window.removeEventListener('resize', onWindowResize, false);
237240
});
238241
};
239242

240243

241-
OrbitCamera.prototype.update = function (dt) {
244+
OrbitCamera.prototype.update = function(dt) {
242245
// Add inertia, if any
243246
var t = this.inertiaFactor === 0 ? 1 : Math.min(dt / this.inertiaFactor, 1);
244247
this._distance = pc.math.lerp(this._distance, this._targetDistance, t);
@@ -251,7 +254,7 @@ OrbitCamera.prototype.update = function (dt) {
251254

252255
OrbitCamera.prototype._updatePosition = function () {
253256
// 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);
255258
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
256259

257260
var position = this.entity.getPosition();
@@ -280,15 +283,32 @@ OrbitCamera.prototype._checkAspectRatio = function () {
280283

281284

282285
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+
}
284298

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++) {
288308
if (modelsAdded === 0) {
289-
this._modelsAabb.copy(mi[i].aabb);
309+
this._modelsAabb.copy(allMeshInstances[i].aabb);
290310
} else {
291-
this._modelsAabb.add(mi[i].aabb);
311+
this._modelsAabb.add(allMeshInstances[i].aabb);
292312
}
293313

294314
modelsAdded += 1;
@@ -314,9 +334,9 @@ OrbitCamera.prototype._calcYaw = function (quat) {
314334
OrbitCamera.prototype._clampDistance = function (distance) {
315335
if (this.distanceMax > 0) {
316336
return pc.math.clamp(distance, this.distanceMin, this.distanceMax);
337+
} else {
338+
return Math.max(distance, this.distanceMin);
317339
}
318-
return Math.max(distance, this.distanceMin);
319-
320340
};
321341

322342

@@ -329,7 +349,7 @@ OrbitCamera.prototype._clampPitchAngle = function (pitch) {
329349
OrbitCamera.quatWithoutYaw = new pc.Quat();
330350
OrbitCamera.yawOffset = new pc.Quat();
331351

332-
OrbitCamera.prototype._calcPitch = function (quat, yaw) {
352+
OrbitCamera.prototype._calcPitch = function(quat, yaw) {
333353
var quatWithoutYaw = OrbitCamera.quatWithoutYaw;
334354
var yawOffset = OrbitCamera.yawOffset;
335355

0 commit comments

Comments
 (0)