function CamCtrl(canvas, scene) {
    var _inst = this;

    this.isManYou = false;
    this.manyouIndex = 1;
    this.awake = function (){
        this.freeCamera = new BABYLON.FreeCamera("freeCamera", new BABYLON.Vector3(ModelConfig.cameraInitPostion[0], ModelConfig.cameraInitPostion[1],  ModelConfig.cameraInitPostion[2]), scene);

        this.freeCamera.minZ = 0.01;
        this.freeCamera.invertRotation = true;

        if (ModelConfig.type == 2) { // 古代
            this.freeCamera.setTarget(new BABYLON.Vector3(this.freeCamera.position.x, this.freeCamera.position.y, this.freeCamera.position.z+0.01));
        } else if (ModelConfig.type == 1) { // 现代
            this.freeCamera.rotation.y = -0.3113103829599399;
        }
        this.freeCamera.attachControl(canvas, true);

        if(ToolMgr.isMobel()){
            this.freeCamera.speed = OptConfig.cam_moveSpeed;
            this.freeCamera.inverseRotationSpeed = OptConfig.cam_rotateSpeed_mobile;
        }
        else {
            this.freeCamera.speed = OptConfig.cam_moveSpeed;
            this.freeCamera.inverseRotationSpeed = OptConfig.cam_rotateSpeed;

            this.freeCamera.keysLeft.push(65);
            this.freeCamera.keysRight.push(68);
            this.freeCamera.keysUp.push(87);
            this.freeCamera.keysDown.push(83);
        }

        this.freeCamera.checkCollisions = true;
        this.freeCamera.applyGravity = true;
        this.freeCamera.ellipsoid = new BABYLON.Vector3(0.5, 0.8, 0.5);
        scene.activeCamera = this.freeCamera;
        document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e && (e.key=='a' || e.key == 's' || e.key == 'w' || e.key == 'd' || e.key == 'ArrowLeft' || e.key == 'ArrowRight' || e.key == 'ArrowUp' || e.key == 'ArrowDown')){
                if(_inst.moveTween){
                    TWEEN.remove(_inst.moveTween);
                }
                _inst.stopManYou();
            }
            else if(e && e.key == "Escape"){
                //manyouCtrl.printPoint();
            }
        };
    }
    this.moveTarget = function (posX, posZ){
        _inst.stopManYou();
        let dis = BABYLON.Vector3.Distance(new BABYLON.Vector3(posX, this.freeCamera.position.y, posZ), this.freeCamera.position);
        if(this.moveTween){
            TWEEN.remove(this.moveTween);
        }
        this.moveTween = new TWEEN.Tween({
            px: this.freeCamera.position.x,
            pz: this.freeCamera.position.z,
        })
            .to({
                px:posX,
                pz:posZ
            }, dis*200)
            .onUpdate(function(object) {
                _inst.freeCamera.position.x = object.px;
                _inst.freeCamera.position.z = object.pz;
            })
            .onComplete(function() {
            })
            .start();
    }
    this.playManYou = function (index){
        if(!index){
            this.manyouIndex = index;
        }
        this.isManYou = true;
        if(this.manyouIndex == 1){
            let name = "PIC_";
            if(this.manyouIndex < 10){
                name += "0"+this.manyouIndex;
            }
            else{
                name += this.manyouIndex;
            }
            let obj = scene.getMeshByName(name);
            if(obj){
                var pos = obj.absolutePosition.clone();
                let ry = Math.PI - obj.rotationQuaternion.toEulerAngles().y;

                let n = Math.sin(ry);
                let n2 = Math.cos(ry);
                pos.x += n * -5;
                pos.z += n2 * -5;
                if(ry < 0){
                    ry += Math.PI*2;
                }
                ry = ry%(Math.PI*2);

                _inst.freeCamera.position.x = pos.x;
                _inst.freeCamera.position.z = pos.z;
                _inst.freeCamera.rotation.y = ry;
            }
        }

        this.moveNext(this.manyouIndex);
    }
    this.moveNext = function (index){
        let name = "PIC_";
        if(index < 10){
            name += "0"+index;
        }
        else{
            name += index;
        }
        let obj = scene.getMeshByName(name);
        if(obj){
            if(!obj.isEnabled()) {
                _inst.manyouIndex++;
                this.moveNext(_inst.manyouIndex);
                return;
            }
            var pos = obj.absolutePosition.clone();
            let ry = Math.PI - obj.rotationQuaternion.toEulerAngles().y;

            let n = Math.sin(ry);
            let n2 = Math.cos(ry);
             pos.x += n * -5;
             pos.z += n2 * -5;
            if(ry < 0){
                ry += Math.PI*2;
            }
            ry = ry%(Math.PI*2);

            this.moveTarget2(pos.x, pos.z, ry, ()=>{
                setTimeout(()=>{
                    if(_inst.isManYou){
                        _inst.manyouIndex++;
                        this.moveNext(_inst.manyouIndex);
                    }
                }, obj.stayTime)

            })
        }
        else {
            this.isManYou = false;
        }
    }
    this.moveTarget2 = function (px, py, ry, onFinish){
        let dis = BABYLON.Vector3.Distance(new BABYLON.Vector3(px, this.freeCamera.position.y, py), this.freeCamera.position);
        if(dis < 1){
            dis = 1;
        }
        if(this.moveTween){
            TWEEN.remove(this.moveTween);
        }
        let y = this.freeCamera.rotation.y;
        if(y < 0){
            y += Math.PI*2;
        }
        y = y%(Math.PI*2);
        if((y-ry) > Math.PI){
            y -= Math.PI*2;
        }
        if((ry-y) > Math.PI){
            ry -= Math.PI*2;
        }
        this.moveTween = new TWEEN.Tween({
            px: this.freeCamera.position.x,
            pz: this.freeCamera.position.z,
            ry:y,
            rx:this.freeCamera.rotation.x,
        })
            .to({
                px:px,
                pz:py,
                ry:ry,
                rx:0,
            }, dis/OptConfig.cam_autoMoveSpeed)
            .onUpdate(function(object) {
                _inst.freeCamera.position.x = object.px;
                _inst.freeCamera.position.z = object.pz;
                _inst.freeCamera.rotation.y = object.ry;
                _inst.freeCamera.rotation.x = object.rx;
            })
            .onComplete(function() {
                if(onFinish){
                    onFinish();
                }
            })
            .start();
    }
    this.stopManYou = function (){
        this.isManYou = false;
        _inst.manyouIndex = 1;
        if(this.moveTween){
            TWEEN.remove(this.moveTween);
        }
    }
    this.pauseManYou = function (){
        this.isManYou = false;
        if(this.moveTween){
            TWEEN.remove(this.moveTween);
        }
    }

    this.lookAt = function(obj) {
        var pos = obj.absolutePosition.clone();
        let ry = Math.PI - obj.rotationQuaternion.toEulerAngles().y;

        let n = Math.sin(ry);
        let n2 = Math.cos(ry);
        pos.x += n * -3;
        pos.z += n2 * -3;
        if(ry < 0){
            ry += Math.PI*2;
        }
        ry = ry%(Math.PI*2);

        _inst.freeCamera.position.x = pos.x;
        _inst.freeCamera.position.z = pos.z;
        _inst.freeCamera.rotation.y = ry;
    }
};
