import { LoaderEvent , Vector3D , AssetLibrary , LoaderContext , URLRequest , RequestAnimationFrame , PerspectiveProjection } from "@awayjs/core" ; import { BitmapImageCube , ImageSampler } from "@awayjs/stage" ; import { ElementsType } from "@awayjs/graphics" ; import { Sprite , Skybox , PrimitiveTorusPrefab , Scene } from "@awayjs/scene" ; import { MethodMaterial , ImageTextureCube , EffectEnvMapMethod } from "@awayjs/materials" ; import { View } from "@awayjs/view" ; class Basic_SkyBox { private _scene : Scene ; private _cubeTexture : ImageTextureCube ; private _torusMaterial : MethodMaterial ; private _skyBox : Skybox ; private _torus : Sprite ; private _timer : RequestAnimationFrame ; private _time : number = 0 ; private _mouseX : number ; private _mouseY : number ; /**
     * Constructor
     */ constructor ( ) { this . init ( ) ; } /**
     * Global initialise function
     */ private init ( ) { this . initEngine ( ) ; this . initMaterials ( ) ; this . initObjects ( ) ; this . initListeners ( ) ; } /**
     * Initialise the engine
     */ private initEngine ( ) { this . _scene = new Scene ( ) ; this . _scene . camera . z = - 600 ; this . _scene . camera . y = 0 ; this . _scene . camera . lookAt ( new Vector3D ( ) ) ; this . _scene . camera . projection = new PerspectiveProjection ( 90 ) ; this . _scene . view . backgroundColor = 0xFFFF00 ; this . _mouseX = window . innerWidth / 2 ; } /**
     * Initialise the materials
     */ private initMaterials ( ) { this . _torusMaterial = new MethodMaterial ( 0xFFFFFF , 1 ) ; this . _torusMaterial . style . color = 0x111199 ; this . _torusMaterial . style . sampler = new ImageSampler ( false , true , true ) ; this . _torusMaterial . specularMethod . strength = 0.5 ; this . _torusMaterial . ambientMethod . strength = 1 ; } /**
     * Initialise the scene objects
     */ private initObjects ( ) { this . _torus = < Sprite > new PrimitiveTorusPrefab ( this . _torusMaterial , ElementsType . TRIANGLE , 150 , 60 , 40 , 20 ) . getNewObject ( ) ; this . _torus . boundsVisible = true ; this . _scene . root . addChild ( this . _torus ) ; } /**
     * Initialise the listeners
     */ private initListeners ( ) { document . onmousemove = ( event ) => this . onMouseMove ( event ) ; window . onresize = ( event ) => this . onResize ( event ) ; this . onResize ( ) ; this . _timer = new RequestAnimationFrame ( this . onEnterFrame , this ) ; this . _timer . start ( ) ; AssetLibrary . addEventListener ( LoaderEvent . LOAD_COMPLETE , ( event ) => this . onResourceComplete ( event ) ) ; var loaderContext = new LoaderContext ( ) ; loaderContext . dependencyBaseUrl = "assets/skybox/" ; AssetLibrary . load ( new URLRequest ( "assets/skybox/snow_texture.cube" ) , loaderContext ) ; } /**
     * Navigation and render loop
     */ private onEnterFrame ( dt ) { this . _torus . rotationX += 2 ; this . _torus . rotationY += 1 ; this . _scene . camera . transform . moveTo ( 0 , 0 , 0 ) ; this . _scene . camera . rotationY += 0.5 * ( this . _mouseX - window . innerWidth / 2 ) / 800 ; this . _scene . camera . transform . moveBackward ( 600 ) ; this . _scene . render ( ) ; } /**
     * Listener function for resource complete event on asset library
     */ private onResourceComplete ( event ) { switch ( event . url ) { case 'assets/skybox/snow_texture.cube' : this . _cubeTexture = new ImageTextureCube ( < BitmapImageCube > event . assets [ 0 ] ) ; this . _skyBox = new Skybox ( < BitmapImageCube > event . assets [ 0 ] ) ; this . _scene . root . addChild ( this . _skyBox ) ; this . _torusMaterial . addEffectMethod ( new EffectEnvMapMethod ( this . _cubeTexture , 1 ) ) ; break ; } } /**
     * Mouse move listener for navigation
     */ private onMouseMove ( event ) { this . _mouseX = event . clientX ; this . _mouseY = event . clientY ; } /**
     * window listener for resize events
     */ private onResize ( event = null ) { this . _scene . view . y = 0 ; this . _scene . view . x = 0 ; this . _scene . view . width = window . innerWidth ; this . _scene . view . height = window . innerHeight ; } } window . onload = function ( ) { new Basic_SkyBox ( ) ; } ;