浏览代码

created babylon.followCamera. A camera that smoothly follows a mesh using easing.

Alex Bogartz 11 年之前
父节点
当前提交
041d2351e9
共有 4 个文件被更改,包括 111 次插入6 次删除
  1. 56 0
      Babylon/Cameras/babylon.followCamera.js
  2. 49 0
      Babylon/Cameras/babylon.followCamera.ts
  3. 5 6
      Babylon/babylon.engine.js
  4. 1 0
      Tools/Gulp/gulpfile.js

+ 56 - 0
Babylon/Cameras/babylon.followCamera.js

@@ -0,0 +1,56 @@
+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 FollowCamera = (function (_super) {
+        __extends(FollowCamera, _super);
+        function FollowCamera(name, position, scene) {
+            _super.call(this, name, position, scene);
+            this.radius = 12;
+            this.rotationOffset = 0;
+            this.heightOffset = 4;
+            this.cameraAcceleration = 0.05;
+            this.maxCameraSpeed = 20;
+        }
+        FollowCamera.prototype.getRadians = function (degrees) {
+            return degrees * Math.PI / 180;
+        };
+
+        FollowCamera.prototype.follow = function (cameraTarget) {
+            if (!cameraTarget)
+                return;
+
+            var radians = this.getRadians(cameraTarget.rotation.y - this.rotationOffset);
+            var targetX = cameraTarget.position.x + Math.sin(radians) * this.radius;
+
+            var targetZ = cameraTarget.position.z + Math.cos(radians) * this.radius;
+            var dx = targetX - this.position.x;
+            var dy = (cameraTarget.position.y + this.heightOffset) - this.position.y;
+            var dz = (targetZ) - this.position.z;
+            var vx = dx * this.cameraAcceleration * 2;
+            var vy = dy * this.cameraAcceleration;
+            var vz = dz * this.cameraAcceleration * 2;
+
+            if (vx > this.maxCameraSpeed || vx < -this.maxCameraSpeed) {
+                vx = vx < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed; //max speed is 40
+            }
+
+            if (vy > this.maxCameraSpeed || vy < -this.maxCameraSpeed) {
+                vy = vy < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
+            }
+
+            if (vz > this.maxCameraSpeed || vz < -this.maxCameraSpeed) {
+                vz = vz < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
+            }
+
+            this.position = new BABYLON.Vector3(this.position.x + vx, this.position.y + vy, this.position.z + vz);
+            this.setTarget(cameraTarget.position);
+        };
+        return FollowCamera;
+    })(FreeCamera);
+    BABYLON.FollowCamera = FollowCamera;
+})(BABYLON || (BABYLON = {}));

+ 49 - 0
Babylon/Cameras/babylon.followCamera.ts

@@ -0,0 +1,49 @@
+module BABYLON {
+    export class FollowCamera extends FreeCamera {
+
+        public radius:number=12;
+        public rotationOffset:number=0;
+        public heightOffset:number=4;
+        public cameraAcceleration:number=0.05;
+        public maxCameraSpeed:number=20;
+
+        constructor(name: string, position: Vector3, scene: Scene) {
+            super(name, position, scene);
+        }
+
+        private getRadians (degrees):number {
+            return degrees * Math.PI / 180;
+        }
+
+        public follow( cameraTarget:BABYLON.AbstractMesh) {
+            if (!cameraTarget)
+                return;
+
+            var radians:number = this.getRadians(cameraTarget.rotation.y - this.rotationOffset);
+            var targetX:number = cameraTarget.position.x + Math.sin(radians) * this.radius;
+
+            var targetZ:number = cameraTarget.position.z + Math.cos(radians) * this.radius;
+            var dx:number = targetX - this.position.x;
+            var dy:number = (cameraTarget.position.y + this.heightOffset) - this.position.y;
+            var dz:number = (targetZ) - this.position.z;
+            var vx:number = dx * this.cameraAcceleration * 2;//this is set to .05
+            var vy:number = dy * this.cameraAcceleration;
+            var vz:number = dz * this.cameraAcceleration * 2;
+
+            if (vx > this.maxCameraSpeed || vx < -this.maxCameraSpeed) {
+                vx = vx < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed; //max speed is 40
+            }
+
+            if (vy > this.maxCameraSpeed || vy < -this.maxCameraSpeed) {
+                vy = vy < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
+            }
+
+            if (vz > this.maxCameraSpeed || vz < -this.maxCameraSpeed) {
+                vz = vz < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
+            }
+
+            this.position = new BABYLON.Vector3(this.position.x + vx, this.position.y + vy, this.position.z + vz);
+            this.setTarget(cameraTarget.position);
+        }
+    }
+} 

+ 5 - 6
Babylon/babylon.engine.js

@@ -58,7 +58,7 @@
     };
 
     var prepareWebGLTexture = function (texture, gl, scene, width, height, invertY, noMipmap, isCompressed, processFunction, samplingMode) {
-        if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
+        if (typeof samplingMode === "undefined") { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
         var engine = scene.getEngine();
         var potWidth = getExponantOfTwo(width, engine.getCaps().maxTextureSize);
         var potHeight = getExponantOfTwo(height, engine.getCaps().maxTextureSize);
@@ -920,7 +920,7 @@
 
         Engine.prototype.createTexture = function (url, noMipmap, invertY, scene, samplingMode) {
             var _this = this;
-            if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
+            if (typeof samplingMode === "undefined") { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
             var texture = this._gl.createTexture();
 
             var extension = url.substr(url.length - 4, 4).toLowerCase();
@@ -940,7 +940,7 @@
                     var header = BABYLON.Internals.TGATools.GetTGAHeader(data);
 
                     prepareWebGLTexture(texture, _this._gl, scene, header.width, header.height, invertY, noMipmap, false, function () {
-                        BABYLON.Internals.TGATools.UploadContent(_this._gl, data);
+                        Internals.TGATools.UploadContent(_this._gl, data);
                     }, samplingMode);
                 }, null, scene.database, true);
             } else if (isDDS) {
@@ -950,7 +950,7 @@
                     var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) == 1);
                     prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, function () {
                         console.log("loading " + url);
-                        BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
+                        Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
                     }, samplingMode);
                 }, null, scene.database, true);
             } else {
@@ -1137,7 +1137,7 @@
                     gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
                     gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
 
-                    BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
+                    Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
 
                     if (!noMipmap && !info.isFourCC && info.mipmapCount == 1) {
                         gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
@@ -1405,4 +1405,3 @@
     })();
     BABYLON.Engine = Engine;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.engine.js.map

+ 1 - 0
Tools/Gulp/gulpfile.js

@@ -70,6 +70,7 @@ gulp.task('scripts', ['shaders'] ,function() {
       '../../Babylon/Collisions/babylon.collider.js',
       '../../Babylon/Cameras/babylon.camera.js',
       '../../Babylon/Cameras/babylon.freeCamera.js',
+      '../../Babylon/Cameras/babylon.followCamera.js',
       '../../Babylon/Cameras/babylon.touchCamera.js',
       '../../Babylon/Cameras/babylon.arcRotateCamera.js',
       '../../Babylon/Cameras/babylon.deviceOrientationCamera.js',