Browse Source

Added ArcFollowCamera

Fredrik Bondza 10 năm trước cách đây
mục cha
commit
a0274e7d31

+ 3 - 2
Tools/Gulp/config.json

@@ -142,7 +142,8 @@
       "../../src/PostProcess/babylon.lensRenderingPipeline.js",
       "../../src/PostProcess/babylon.colorCorrectionPostProcess.js",
       "../../src/Cameras/babylon.stereoscopicCameras.js",
-      "../../src/PostProcess/babylon.hdrRenderingPipeline.js"
+      "../../src/PostProcess/babylon.hdrRenderingPipeline.js",
+      "../../src/Cameras/babylon.arcFollowCamera.js"
     ]
   },
   "shadersDirectories": [
@@ -166,4 +167,4 @@
     "files": [
     ]
   }
-}
+}

+ 38 - 0
src/Cameras/babylon.arcFollowCamera.js

@@ -0,0 +1,38 @@
+var __extends = this.__extends || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    __.prototype = b.prototype;
+    d.prototype = new __();
+};
+var BABYLON;
+(function (BABYLON) {
+    var ArcFollowCamera = (function (_super) {
+        __extends(ArcFollowCamera, _super);
+        function ArcFollowCamera(name, alpha, beta, radius, target, scene) {
+            _super.call(this, name, BABYLON.Vector3.Zero(), scene);
+            this.alpha = alpha;
+            this.beta = beta;
+            this.radius = radius;
+            this.target = target;
+            this._radPerDeg = Math.PI / 180;
+            this._cartesianCoordinates = BABYLON.Vector3.Zero();
+            this.follow();
+        }
+        ArcFollowCamera.prototype._degToRad = function (deg) {
+            return deg * this._radPerDeg;
+        };
+        ArcFollowCamera.prototype.follow = function () {
+            this._cartesianCoordinates.x = this.radius * Math.cos(this._degToRad(this.alpha)) * Math.cos(this._degToRad(this.beta));
+            this._cartesianCoordinates.y = this.radius * Math.sin(this._degToRad(this.beta));
+            this._cartesianCoordinates.z = this.radius * Math.sin(this._degToRad(this.alpha)) * Math.cos(this._degToRad(this.beta));
+            this.position = this.target.position.add(this._cartesianCoordinates);
+            this.setTarget(this.target.position);
+        };
+        ArcFollowCamera.prototype._checkInputs = function () {
+            _super.prototype._checkInputs.call(this);
+            this.follow();
+        };
+        return ArcFollowCamera;
+    })(BABYLON.TargetCamera);
+    BABYLON.ArcFollowCamera = ArcFollowCamera;
+})(BABYLON || (BABYLON = {}));

+ 30 - 0
src/Cameras/babylon.arcFollowCamera.ts

@@ -0,0 +1,30 @@
+module BABYLON {
+    export class ArcFollowCamera extends TargetCamera {
+
+        private _radPerDeg:number = Math.PI / 180;
+        private _cartesianCoordinates:Vector3 = Vector3.Zero();
+
+        constructor(name:string, public alpha:number, public beta:number, public radius:number, public target:AbstractMesh, scene:Scene) {
+            super(name, Vector3.Zero(), scene);
+            this.follow();
+        }
+
+        private _degToRad(deg) {
+            return deg * this._radPerDeg;
+        }
+
+        private follow():void {
+            this._cartesianCoordinates.x = this.radius * Math.cos(this._degToRad(this.alpha)) * Math.cos(this._degToRad(this.beta));
+            this._cartesianCoordinates.y = this.radius * Math.sin(this._degToRad(this.beta));
+            this._cartesianCoordinates.z = this.radius * Math.sin(this._degToRad(this.alpha)) * Math.cos(this._degToRad(this.beta));
+
+            this.position = this.target.position.add(this._cartesianCoordinates);
+            this.setTarget(this.target.position);
+        }
+
+        public _checkInputs():void {
+            super._checkInputs();
+            this.follow();
+        }
+    }
+}