import { Laya } from "Laya" ; import { Script3D } from "laya/d3/component/Script3D" ; import { Camera } from "laya/d3/core/Camera" ; import { DirectionLight } from "laya/d3/core/light/DirectionLight" ; import { PBRSpecularMaterial } from "laya/d3/core/material/PBRSpecularMaterial" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Texture2D } from "laya/resource/Texture2D" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Laya3D } from "Laya3D" ; export class ScriptDemo { private _translate : Vector3 = new Vector3 ( 0 , 3 , 3 ) ; private _rotation : Vector3 = new Vector3 ( - 30 , 0 , 0 ) ; private _rotation2 : Vector3 = new Vector3 ( 0 , 45 , 0 ) ; private _forward : Vector3 = new Vector3 ( 1 , - 1 , 0 ) ; constructor ( ) { Laya3D . init ( 0 , 0 ) ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; var scene = ( < Scene3D > Laya . stage . addChild ( new Scene3D ( ) ) ) ; var camera = ( < Camera > ( scene . addChild ( new Camera ( 0 , 0.1 , 100 ) ) ) ) ; camera . transform . translate ( this . _translate ) ; camera . transform . rotate ( this . _rotation , true , false ) ; camera . clearColor = null ; var directionLight = ( < DirectionLight > scene . addChild ( new DirectionLight ( ) ) ) ; var lightColor = directionLight . color ; lightColor . setValue ( 0.6 , 0.6 , 0.6 ) ; var mat = directionLight . transform . worldMatrix ; mat . setForward ( this . _forward ) ; directionLight . transform . worldMatrix = mat ; var box = ( < MeshSprite3D > scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( 1 , 1 , 1 ) , "MOs" ) ) ) ; box . transform . rotate ( this . _rotation2 , false , false ) ; var material = new PBRSpecularMaterial ( ) ; Texture2D . load ( "res/threeDimen/layabox.png" , Handler . create ( this , function ( text ) : void { material . albedoTexture = text ; box . meshRenderer . material = material ; box . addComponent ( BoxControlScript ) ; } ) ) ; Laya . timer . once ( 4000 , this , this . onLoop , [ box ] ) ; } private onLoop ( box ) { console . log ( "移除组件" ) ; var boxContro = box . getComponent ( BoxControlScript ) ; boxContro . destroy ( ) ; } } class BoxControlScript extends Script3D { private box : MeshSprite3D ; private _albedoColor : Vector4 = new Vector4 ( 1 , 0 , 0 , 1 ) ; private _rotation : Vector3 = new Vector3 ( 0 , 0.5 , 0 ) ; constructor ( ) { super ( ) ; } /**
     * 覆写3D对象组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
     */ onAwake ( ) { this . box = ( < MeshSprite3D > this . owner ) ; } onStart ( ) { var material = ( < PBRSpecularMaterial > this . box . meshRenderer . material ) ; material . albedoColor = this . _albedoColor ; } /**
     * 覆写组件更新方法（相当于帧循环）
     */ onUpdate ( ) { this . box . transform . rotate ( this . _rotation , false , false ) ; } onDisable ( ) { console . log ( "组件设置为不可用" ) ; } }