Spaces:
Running
Running
Update orbit-camera.js
Browse files- orbit-camera.js +53 -46
orbit-camera.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
| 1 |
// orbit-camera.js
|
| 2 |
-
//
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
OrbitCamera.attributes.add('distanceMax', { type: 'number', default: 20, title: 'Distance Max' });
|
| 8 |
-
OrbitCamera.attributes.add('distanceMin', { type: 'number', default: 1, title: 'Distance Min' });
|
| 9 |
-
OrbitCamera.attributes.add('pitchAngleMax', { type: 'number', default: 90, title: 'Pitch Angle Max (degrees)' });
|
| 10 |
-
OrbitCamera.attributes.add('pitchAngleMin', { type: 'number', default: 0, title: 'Pitch Angle Min (degrees)' });
|
| 11 |
-
OrbitCamera.attributes.add('yawAngleMax', { type: 'number', default: 360, title: 'Yaw Angle Max (degrees)' });
|
| 12 |
-
OrbitCamera.attributes.add('yawAngleMin', { type: 'number', default: -360, title: 'Yaw Angle Min (degrees)' });
|
| 13 |
-
OrbitCamera.attributes.add('minY', { type: 'number', default: 0, title: 'Minimum Y', description: 'Minimum Y value for camera world position' });
|
| 14 |
|
| 15 |
-
OrbitCamera.attributes.add('
|
| 16 |
-
OrbitCamera.attributes.add('
|
| 17 |
-
OrbitCamera.attributes.add('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
Object.defineProperty(OrbitCamera.prototype, 'distance', {
|
| 20 |
get: function () { return this._targetDistance; },
|
| 21 |
set: function (value) { this._targetDistance = this._clampDistance(value); }
|
| 22 |
});
|
| 23 |
|
| 24 |
-
Object.defineProperty(OrbitCamera.prototype, 'orthoHeight', {
|
| 25 |
-
get: function () { return this.entity.camera.orthoHeight; },
|
| 26 |
-
set: function (value) { this.entity.camera.orthoHeight = Math.max(0, value); }
|
| 27 |
-
});
|
| 28 |
-
|
| 29 |
Object.defineProperty(OrbitCamera.prototype, 'pitch', {
|
| 30 |
get: function () { return this._targetPitch; },
|
| 31 |
set: function (value) { this._targetPitch = this._clampPitchAngle(value); }
|
|
@@ -93,7 +93,6 @@ OrbitCamera.prototype.resetToPosition = function (position, lookAtPoint) {
|
|
| 93 |
this._updatePosition();
|
| 94 |
};
|
| 95 |
|
| 96 |
-
// ==== Clamp helper for panning/orbiting so camera never under minY ====
|
| 97 |
OrbitCamera.prototype.worldCameraYForPivot = function(pivot) {
|
| 98 |
var quat = new pc.Quat();
|
| 99 |
quat.setFromEulerAngles(this._pitch, this._yaw, 0);
|
|
@@ -104,10 +103,6 @@ OrbitCamera.prototype.worldCameraYForPivot = function(pivot) {
|
|
| 104 |
return camPos.y;
|
| 105 |
};
|
| 106 |
|
| 107 |
-
////////////////////////////////////////////////////////////////////////////////
|
| 108 |
-
// Private Methods //
|
| 109 |
-
////////////////////////////////////////////////////////////////////////////////
|
| 110 |
-
|
| 111 |
OrbitCamera.prototype.initialize = function () {
|
| 112 |
var self = this;
|
| 113 |
var onWindowResize = function () { self._checkAspectRatio(); };
|
|
@@ -167,31 +162,43 @@ OrbitCamera.prototype._updatePosition = function () {
|
|
| 167 |
this.entity.setPosition(position);
|
| 168 |
};
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
OrbitCamera.prototype._buildAabb = function (entity) {
|
| 172 |
-
var meshInstances = [];
|
| 173 |
-
// Standard renders
|
| 174 |
var renders = entity.findComponents('render');
|
| 175 |
-
for (
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
-
// Old model components
|
| 179 |
var models = entity.findComponents('model');
|
| 180 |
-
for (
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
-
// GSplat SOGS support (PlayCanvas 2.8+)
|
| 184 |
var gsplats = entity.findComponents('gsplat');
|
| 185 |
-
for (
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
for (let mi of gsplat.instance.meshInstances) meshInstances.push(mi);
|
| 191 |
-
}
|
| 192 |
}
|
| 193 |
}
|
| 194 |
-
for (
|
| 195 |
if (i === 0) {
|
| 196 |
this._modelsAabb.copy(meshInstances[i].aabb);
|
| 197 |
} else {
|
|
@@ -203,7 +210,7 @@ OrbitCamera.prototype._buildAabb = function (entity) {
|
|
| 203 |
OrbitCamera.prototype._calcYaw = function (quat) {
|
| 204 |
var transformedForward = new pc.Vec3();
|
| 205 |
quat.transformVector(pc.Vec3.FORWARD, transformedForward);
|
| 206 |
-
return Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.
|
| 207 |
};
|
| 208 |
|
| 209 |
OrbitCamera.prototype._clampDistance = function (distance) {
|
|
@@ -218,7 +225,7 @@ OrbitCamera.prototype._clampPitchAngle = function (pitch) {
|
|
| 218 |
};
|
| 219 |
|
| 220 |
OrbitCamera.prototype._clampYawAngle = function (yaw) {
|
| 221 |
-
return pc.math.clamp(yaw,
|
| 222 |
};
|
| 223 |
|
| 224 |
OrbitCamera.quatWithoutYaw = new pc.Quat();
|
|
@@ -234,11 +241,11 @@ OrbitCamera.prototype._calcPitch = function (quat, yaw) {
|
|
| 234 |
return Math.atan2(-transformedForward.y, -transformedForward.z) * pc.math.RAD_TO_DEG;
|
| 235 |
};
|
| 236 |
|
|
|
|
| 237 |
|
| 238 |
-
// ===================== Orbit Camera Input Mouse Script ========================
|
| 239 |
var OrbitCameraInputMouse = pc.createScript('orbitCameraInputMouse');
|
| 240 |
-
OrbitCameraInputMouse.attributes.add('orbitSensitivity',
|
| 241 |
-
OrbitCameraInputMouse.attributes.add('distanceSensitivity',
|
| 242 |
|
| 243 |
OrbitCameraInputMouse.prototype.initialize = function () {
|
| 244 |
this.orbitCamera = this.entity.script.orbitCamera;
|
|
@@ -363,7 +370,7 @@ OrbitCameraInputMouse.prototype.onMouseOut = function (event) {
|
|
| 363 |
|
| 364 |
// =================== Orbit Camera Input Touch Script ========================
|
| 365 |
var OrbitCameraInputTouch = pc.createScript('orbitCameraInputTouch');
|
| 366 |
-
OrbitCameraInputTouch.attributes.add('orbitSensitivity',
|
| 367 |
OrbitCameraInputTouch.attributes.add('distanceSensitivity', { type: 'number', default: 0.5, title: 'Distance Sensitivity' });
|
| 368 |
OrbitCameraInputTouch.prototype.initialize = function () {
|
| 369 |
this.orbitCamera = this.entity.script.orbitCamera;
|
|
|
|
| 1 |
// orbit-camera.js
|
| 2 |
+
// ==============================
|
| 3 |
|
| 4 |
+
/*
|
| 5 |
+
* PlayCanvas Orbit Camera for GSplat/SOGS/GLB viewers.
|
| 6 |
+
* - Handles min/max zoom, pitch, yaw, minY (don't go below ground)
|
| 7 |
+
* - Mouse & touch controls (orbit, pan, zoom)
|
| 8 |
+
* - Attributes are auto-injected by viewer.js
|
| 9 |
+
*/
|
| 10 |
|
| 11 |
+
var OrbitCamera = pc.createScript('orbitCamera');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
OrbitCamera.attributes.add('distanceMax', { type: 'number', default: 20, title: 'Distance Max' });
|
| 14 |
+
OrbitCamera.attributes.add('distanceMin', { type: 'number', default: 1, title: 'Distance Min' });
|
| 15 |
+
OrbitCamera.attributes.add('pitchAngleMax', { type: 'number', default: 90, title: 'Pitch Angle Max (degrees)' });
|
| 16 |
+
OrbitCamera.attributes.add('pitchAngleMin', { type: 'number', default: 0, title: 'Pitch Angle Min (degrees)' });
|
| 17 |
+
OrbitCamera.attributes.add('yawAngleMax', { type: 'number', default: 360, title: 'Yaw Angle Max (degrees)' });
|
| 18 |
+
OrbitCamera.attributes.add('yawAngleMin', { type: 'number', default: -360,title: 'Yaw Angle Min (degrees)' });
|
| 19 |
+
OrbitCamera.attributes.add('minY', { type: 'number', default: 0, title: 'Minimum Y' });
|
| 20 |
+
OrbitCamera.attributes.add('inertiaFactor', { type: 'number', default: 0.2, title: 'Inertia Factor' });
|
| 21 |
+
OrbitCamera.attributes.add('focusEntity', { type: 'entity', title: 'Focus Entity' });
|
| 22 |
+
OrbitCamera.attributes.add('frameOnStart', { type: 'boolean', default: true, title: 'Frame on Start' });
|
| 23 |
|
| 24 |
Object.defineProperty(OrbitCamera.prototype, 'distance', {
|
| 25 |
get: function () { return this._targetDistance; },
|
| 26 |
set: function (value) { this._targetDistance = this._clampDistance(value); }
|
| 27 |
});
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
Object.defineProperty(OrbitCamera.prototype, 'pitch', {
|
| 30 |
get: function () { return this._targetPitch; },
|
| 31 |
set: function (value) { this._targetPitch = this._clampPitchAngle(value); }
|
|
|
|
| 93 |
this._updatePosition();
|
| 94 |
};
|
| 95 |
|
|
|
|
| 96 |
OrbitCamera.prototype.worldCameraYForPivot = function(pivot) {
|
| 97 |
var quat = new pc.Quat();
|
| 98 |
quat.setFromEulerAngles(this._pitch, this._yaw, 0);
|
|
|
|
| 103 |
return camPos.y;
|
| 104 |
};
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
OrbitCamera.prototype.initialize = function () {
|
| 107 |
var self = this;
|
| 108 |
var onWindowResize = function () { self._checkAspectRatio(); };
|
|
|
|
| 162 |
this.entity.setPosition(position);
|
| 163 |
};
|
| 164 |
|
| 165 |
+
OrbitCamera.prototype._removeInertia = function () {
|
| 166 |
+
this._yaw = this._targetYaw;
|
| 167 |
+
this._pitch = this._targetPitch;
|
| 168 |
+
this._distance = this._targetDistance;
|
| 169 |
+
};
|
| 170 |
+
|
| 171 |
+
OrbitCamera.prototype._checkAspectRatio = function () {
|
| 172 |
+
var height = this.app.graphicsDevice.height;
|
| 173 |
+
var width = this.app.graphicsDevice.width;
|
| 174 |
+
this.entity.camera.horizontalFov = (height > width);
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
OrbitCamera.prototype._buildAabb = function (entity) {
|
| 178 |
+
var i, m, meshInstances = [];
|
|
|
|
| 179 |
var renders = entity.findComponents('render');
|
| 180 |
+
for (i = 0; i < renders.length; i++) {
|
| 181 |
+
var render = renders[i];
|
| 182 |
+
for (m = 0; m < render.meshInstances.length; m++) {
|
| 183 |
+
meshInstances.push(render.meshInstances[m]);
|
| 184 |
+
}
|
| 185 |
}
|
|
|
|
| 186 |
var models = entity.findComponents('model');
|
| 187 |
+
for (i = 0; i < models.length; i++) {
|
| 188 |
+
var model = models[i];
|
| 189 |
+
for (m = 0; m < model.meshInstances.length; m++) {
|
| 190 |
+
meshInstances.push(model.meshInstances[m]);
|
| 191 |
+
}
|
| 192 |
}
|
|
|
|
| 193 |
var gsplats = entity.findComponents('gsplat');
|
| 194 |
+
for (i = 0; i < gsplats.length; i++) {
|
| 195 |
+
var gsplat = gsplats[i];
|
| 196 |
+
var instance = gsplat.instance;
|
| 197 |
+
if (instance?.meshInstance) {
|
| 198 |
+
meshInstances.push(instance.meshInstance);
|
|
|
|
|
|
|
| 199 |
}
|
| 200 |
}
|
| 201 |
+
for (i = 0; i < meshInstances.length; i++) {
|
| 202 |
if (i === 0) {
|
| 203 |
this._modelsAabb.copy(meshInstances[i].aabb);
|
| 204 |
} else {
|
|
|
|
| 210 |
OrbitCamera.prototype._calcYaw = function (quat) {
|
| 211 |
var transformedForward = new pc.Vec3();
|
| 212 |
quat.transformVector(pc.Vec3.FORWARD, transformedForward);
|
| 213 |
+
return Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.DEG_TO_RAD * 180 / Math.PI;
|
| 214 |
};
|
| 215 |
|
| 216 |
OrbitCamera.prototype._clampDistance = function (distance) {
|
|
|
|
| 225 |
};
|
| 226 |
|
| 227 |
OrbitCamera.prototype._clampYawAngle = function (yaw) {
|
| 228 |
+
return pc.math.clamp(yaw, this.yawAngleMin, this.yawAngleMax);
|
| 229 |
};
|
| 230 |
|
| 231 |
OrbitCamera.quatWithoutYaw = new pc.Quat();
|
|
|
|
| 241 |
return Math.atan2(-transformedForward.y, -transformedForward.z) * pc.math.RAD_TO_DEG;
|
| 242 |
};
|
| 243 |
|
| 244 |
+
// =================== Orbit Camera Input Mouse Script ========================
|
| 245 |
|
|
|
|
| 246 |
var OrbitCameraInputMouse = pc.createScript('orbitCameraInputMouse');
|
| 247 |
+
OrbitCameraInputMouse.attributes.add('orbitSensitivity', { type: 'number', default: 0.3, title: 'Orbit Sensitivity' });
|
| 248 |
+
OrbitCameraInputMouse.attributes.add('distanceSensitivity',{ type: 'number', default: 0.4, title: 'Distance Sensitivity' });
|
| 249 |
|
| 250 |
OrbitCameraInputMouse.prototype.initialize = function () {
|
| 251 |
this.orbitCamera = this.entity.script.orbitCamera;
|
|
|
|
| 370 |
|
| 371 |
// =================== Orbit Camera Input Touch Script ========================
|
| 372 |
var OrbitCameraInputTouch = pc.createScript('orbitCameraInputTouch');
|
| 373 |
+
OrbitCameraInputTouch.attributes.add('orbitSensitivity', { type: 'number', default: 0.6, title: 'Orbit Sensitivity' });
|
| 374 |
OrbitCameraInputTouch.attributes.add('distanceSensitivity', { type: 'number', default: 0.5, title: 'Distance Sensitivity' });
|
| 375 |
OrbitCameraInputTouch.prototype.initialize = function () {
|
| 376 |
this.orbitCamera = this.entity.script.orbitCamera;
|