import { Laya } from "Laya" ; import { BaseCamera } from "laya/d3/core/BaseCamera" ; import { Camera } from "laya/d3/core/Camera" ; import { DirectionLight } from "laya/d3/core/light/DirectionLight" ; import { BlinnPhongMaterial } from "laya/d3/core/material/BlinnPhongMaterial" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Sprite3D } from "laya/d3/core/Sprite3D" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Event } from "laya/events/Event" ; import { Loader } from "laya/net/Loader" ; import { Texture2D } from "laya/resource/Texture2D" ; import { Button } from "laya/ui/Button" ; import { Browser } from "laya/utils/Browser" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Utils } from "laya/utils/Utils" ; import { Laya3D } from "Laya3D" ; import Client from "../../Client" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; author ...
  export class CameraLookAt { private index : number = 0 ; private camera : Camera ; private box : MeshSprite3D ; private capsule : MeshSprite3D ; private cylinder : MeshSprite3D ; private _translate = new Vector3 ( 0 , 0.7 , 5 ) ; private _rotation : Vector3 = new Vector3 ( - 15 , 0 , 0 ) ; private _rotation2 : Vector3 = new Vector3 ( - 3.14 / 3 , 0 , 0 ) ; private _rotation3 : Vector3 = new Vector3 ( 0 , 45 , 0 ) ; private _position : Vector3 = new Vector3 ( 1.5 , 0.0 , 2 ) ; private _position2 : Vector3 = new Vector3 ( - 1.5 , 0.0 , 2 ) ; private _position3 : Vector3 = new Vector3 ( 0.0 , 0.0 , 2 ) ; private _up : Vector3 = new Vector3 ( 0 , 1 , 0 ) ; /**实例类型*/ private btype : any = "CameraLookAt" ; /**场景内按钮类型*/ private stype : any = 0 ; constructor ( ) { Laya3D . init ( 0 , 0 ) ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; var resource = [ "res/threeDimen/texture/layabox.png" , "res/threeDimen/skyBox/skyBox3/skyBox3.lmat" ] ; Laya . loader . create ( resource , Handler . create ( this , this . onPreLoadFinish ) ) ; } private onPreLoadFinish ( ) { var scene = new Scene3D ( ) ; Laya . stage . addChild ( scene ) ; this . camera = new Camera ( 0 , 0.1 , 100 ) ; this . camera . transform . translate ( this . _translate ) ; this . camera . transform . rotate ( this . _rotation , true , false ) ; this . camera . clearFlag = BaseCamera . CLEARFLAG_SOLIDCOLOR ; this . camera . fieldOfView = 60 ; this . camera . addComponent ( CameraMoveScript ) ; scene . addChild ( this . camera ) ; var directionLight = ( < DirectionLight > scene . addChild ( new DirectionLight ( ) ) ) ; directionLight . color . setValue ( 1 , 1 , 1 ) ; directionLight . transform . rotate ( this . _rotation2 ) ; var sprite = new Sprite3D ; scene . addChild ( sprite ) ; this . box = ( < MeshSprite3D > sprite . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( 0.5 , 0.5 , 0.5 ) ) ) ) ; this . box . transform . position = this . _position ; this . box . transform . rotate ( this . _rotation3 , false , false ) ; this . capsule = new MeshSprite3D ( PrimitiveMesh . createCapsule ( 0.25 , 1 , 10 , 20 ) ) ; this . capsule . transform . position = this . _position2 ; sprite . addChild ( this . capsule ) ; this . cylinder = new MeshSprite3D ( PrimitiveMesh . createCylinder ( 0.25 , 1 , 20 ) ) ; this . cylinder . transform . position = this . _position3 ; sprite . addChild ( this . cylinder ) ; var materialBill = new BlinnPhongMaterial ; this . box . meshRenderer . material = materialBill ; this . capsule . meshRenderer . material = materialBill ; this . cylinder . meshRenderer . material = materialBill ; var tex = ( < Texture2D > Loader . getRes ( "res/threeDimen/texture/layabox.png" ) ) ; materialBill . albedoTexture = tex ; this . loadUI ( ) ; } private loadUI ( ) { Laya . loader . load ( [ "res/threeDimen/ui/button.png" ] , Handler . create ( this , function ( ) : void { var changeActionButton = ( < Button > Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "切换注视目标" ) ) ) ; changeActionButton . size ( 200 , 40 ) ; changeActionButton . labelBold = true ; changeActionButton . labelSize = 30 ; changeActionButton . sizeGrid = "4,4,4,4" ; changeActionButton . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; changeActionButton . pos ( Laya . stage . width / 2 - changeActionButton . width * Browser . pixelRatio / 2 , Laya . stage . height - 100 * Browser . pixelRatio ) ; changeActionButton . on ( Event . CLICK , this , this . stypeFun0 ) ; } ) ) ; } stypeFun0 ( index = 0 ) { this . index ++ ; if ( this . index % 3 === 1 ) { this . camera . transform . lookAt ( this . box . transform . position , this . _up ) ; } else if ( this . index % 3 === 2 ) { this . camera . transform . lookAt ( this . cylinder . transform . position , this . _up ) ; } else { this . camera . transform . lookAt ( this . capsule . transform . position , this . _up ) ; } index = this . index ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 0 , value : index } ) ; } }