Laya3D-摄像机捕捉目标

摄像机捕捉目标

  /* 摄像机捕捉目标 */
export class CameraLookAt extends Laya.Scene3D{
        private camera:Laya.Camera;
        private box:Laya.MeshSprite3D;
        private capsule:Laya.MeshSprite3D;
        private cylinder:Laya.MeshSprite3D;
        private upVector:Laya.Vector3 = new Laya.Vector3(0, 1, 0);
        private index:number = 0;
        constructor(){
            super();
            this.init();
        }
        private init(){
            //创建相机,构造函数的三个参数为相机横纵比,近距裁剪,远距裁剪
            this.camera = new Laya.Camera(0, 0.1, 100);
            this.camera.transform.translate(new Laya.Vector3(0, 0.7, 5));
            this.camera.transform.rotate(new Laya.Vector3( -15, 0, 0), true, false);
            this.camera.clearFlag = Laya.CameraClearFlags.SolidColor;
            //设置摄像机视野范围(角度)
            this.camera.fieldOfView = 60;
            this.addChild(this.camera);
            //添加平行光
            let directionLight = new Laya.DirectionLight();
            this.addChild(directionLight);
            //设置平行光颜色
            directionLight.color = new Laya.Vector3(1, 1, 1);
            directionLight.transform.rotate(new Laya.Vector3( -3.14 / 3, 0, 0));
            let sprite = new Laya.Sprite3D();
            this.addChild(sprite);
            //正方体
            this.box = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(0.5, 0.5, 0.5));
            sprite.addChild(this.box);
            this.box.transform.position = new Laya.Vector3(1.5, 0.0, 2);
            this.box.transform.rotate(new Laya.Vector3(0, 45, 0), false, false);
            //胶囊体
            this.capsule = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createCapsule(0.25, 1, 10, 20));
            this.capsule.transform.position = new Laya.Vector3(-1.5, 0.0, 2);
            sprite.addChild(this.capsule); 
            //圆柱
            this.cylinder = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createCylinder(0.25, 1, 20));
            this.cylinder.transform.position = new Laya.Vector3(0.0, 0.0, 2);
            sprite.addChild(this.cylinder);
            //创建linnPhong材质
            let mat:Laya.BlinnPhongMaterial = new Laya.BlinnPhongMaterial();
            this.box.meshRenderer.material = mat;
            this.capsule.meshRenderer.material = mat;
            this.cylinder.meshRenderer.material = mat;
            //为材质加载纹理
            Laya.Texture2D.load("res/threeDimen/layabox.png",Laya.Handler.create(this,function(tex):void{
                mat.albedoTexture = tex;
            }))
            this.loadUI();  
        }
        private loadUI() {
            Laya.loader.load(["res/threeDimen/ui/button.png"], Laya.Handler.create(this, function() {
                let changeActionButton:Laya.Button = Laya.stage.addChild(new Laya.Button("res/threeDimen/ui/button.png", "切换注视目标")) as Laya.Button;
                changeActionButton.size(200, 40);
                changeActionButton.labelBold = true;
                changeActionButton.labelSize = 30;
                changeActionButton.sizeGrid = "4,4,4,4";
                changeActionButton.scale(Laya.Browser.pixelRatio, Laya.Browser.pixelRatio);
                changeActionButton.pos(Laya.stage.width / 2 - changeActionButton.width * Laya.Browser.pixelRatio / 2 , Laya.stage.height - 100 * Laya.Browser.pixelRatio);
                changeActionButton.on(Laya.Event.CLICK, this, function(){
                    this.index++;
                    if (this.index % 3 === 1){
                        //摄像机捕捉模型正方体
                        this.camera.transform.lookAt(this.box.transform.position, this.upVector);
                    }else if (this.index % 3 === 2){
                        //摄像机捕捉模型圆柱体
                        this.camera.transform.lookAt(this.cylinder.transform.position, this.upVector);
                    }else{
                        //摄像机捕捉模型胶囊体
                        this.camera.transform.lookAt(this.capsule.transform.position, this.upVector);
                    }
                });
                        
            }));
        }       
    }

  

原文地址:https://www.cnblogs.com/kootimloe/p/14650993.html