import { AssetEvent , LoaderEvent , Vector3D , AssetLibrary , IAsset , URLRequest , Keyboard , RequestAnimationFrame } from "@awayjs/core" ; import { BitmapImage2D , ImageSampler } from "@awayjs/stage" ; import { ElementsType , VertexAnimationSet , VertexAnimator , AnimationSetBase } from "@awayjs/graphics" ; import { HoverController , Sprite , PrimitivePlanePrefab , Scene } from "@awayjs/scene" ; import { MethodMaterial , ShadowFilteredMethod , ImageTexture2D , DirectionalLight , StaticLightPicker } from "@awayjs/materials" ; import { MD2Parser } from "@awayjs/parsers" ; import { View } from "@awayjs/view" ; class Intermediate_PerelithKnight { private _spriteInitialised : boolean = false ; private _animationSetInitialised : boolean = false ; private _sceneInitialised : boolean = false ; private _pKnightTextures : Array < string > = new Array < string > ( "assets/pknight1.png" , "assets/pknight2.png" , "assets/pknight3.png" , "assets/pknight4.png" ) ; private _pKnightMaterials : Array < MethodMaterial > = new Array < MethodMaterial > ( ) ; private _scene : Scene ; private _cameraController : HoverController ; private _light : DirectionalLight ; private _lightPicker : StaticLightPicker ; private _floorMaterial : MethodMaterial ; private _shadowMapMethod : ShadowFilteredMethod ; private _floor : Sprite ; private _sprite : Sprite ; private _timer : RequestAnimationFrame ; private _time : number = 0 ; private _move : boolean = false ; private _lastPanAngle : number ; private _lastTiltAngle : number ; private _lastMouseX : number ; private _lastMouseY : number ; private _keyUp : boolean ; private _keyDown : boolean ; private _keyLeft : boolean ; private _keyRight : boolean ; private _lookAtPosition : Vector3D = new Vector3D ( ) ; private _animationSet : VertexAnimationSet ; /**
     * Constructor
     */ constructor ( ) { this . _scene = new Scene ( ) ; this . _scene . camera . projection . far = 5000 ; this . _cameraController = new HoverController ( this . _scene . camera , null , 45 , 20 , 2000 , 5 ) ; this . _light = new DirectionalLight ( new Vector3D ( - 0.5 , - 1 , - 1 ) ) ; this . _light . ambient = 0.4 ; this . _light . shadowMapper . epsilon = 0.2 ; this . _lightPicker = new StaticLightPicker ( [ this . _light ] ) ; AssetLibrary . addEventListener ( AssetEvent . ASSET_COMPLETE , ( event ) => this . onAssetComplete ( event ) ) ; AssetLibrary . addEventListener ( LoaderEvent . LOAD_COMPLETE , ( event ) => this . onResourceComplete ( event ) ) ; AssetLibrary . load ( new URLRequest ( "assets/pknight1.png" ) ) ; AssetLibrary . load ( new URLRequest ( "assets/pknight2.png" ) ) ; AssetLibrary . load ( new URLRequest ( "assets/pknight3.png" ) ) ; AssetLibrary . load ( new URLRequest ( "assets/pknight4.png" ) ) ; AssetLibrary . load ( new URLRequest ( "assets/floor_diffuse.jpg" ) ) ; AssetLibrary . load ( new URLRequest ( "assets/pknight.md2" ) , null , null , new MD2Parser ( ) ) ; this . _shadowMapMethod = new ShadowFilteredMethod ( this . _light ) ; this . _floorMaterial = new MethodMaterial ( ) ; this . _floorMaterial . lightPicker = this . _lightPicker ; this . _floorMaterial . specularMethod . strength = 0 ; this . _floorMaterial . ambientMethod . strength = 1 ; this . _floorMaterial . shadowMethod = this . _shadowMapMethod ; this . _floorMaterial . style . sampler = new ImageSampler ( true ) ; for ( var i = 0 ; i < this . _pKnightTextures . length ; i ++ ) { var knightMaterial = new MethodMaterial ( ) ; knightMaterial . lightPicker = this . _lightPicker ; knightMaterial . specularMethod . gloss = 30 ; knightMaterial . specularMethod . strength = 1 ; knightMaterial . ambientMethod . strength = 1 ; knightMaterial . shadowMethod = this . _shadowMapMethod ; this . _pKnightMaterials . push ( knightMaterial ) ; } this . _floor = < Sprite > new PrimitivePlanePrefab ( this . _floorMaterial , ElementsType . TRIANGLE , 5000 , 5000 ) . getNewObject ( ) ; this . _floor . graphics . scaleUV ( 5 , 5 ) ; this . _scene . root . addChild ( this . _floor ) ; window . onresize = ( event ) => this . onResize ( event ) ; document . onmousedown = ( event ) => this . onMouseDown ( event ) ; document . onmouseup = ( event ) => this . onMouseUp ( event ) ; document . onmousemove = ( event ) => this . onMouseMove ( event ) ; document . onwheel = ( event ) => this . onWheel ( event ) ; document . onkeydown = ( event ) => this . onKeyDown ( event ) ; document . onkeyup = ( event ) => this . onKeyUp ( event ) ; this . onResize ( ) ; this . _timer = new RequestAnimationFrame ( this . onEnterFrame , this ) ; this . _timer . start ( ) ; } /**
     * Navigation and render loop
     */ private onEnterFrame ( dt ) { this . _time += dt ; if ( this . _keyUp ) this . _lookAtPosition . x -= 10 ; if ( this . _keyDown ) this . _lookAtPosition . x += 10 ; if ( this . _keyLeft ) this . _lookAtPosition . z -= 10 ; if ( this . _keyRight ) this . _lookAtPosition . z += 10 ; this . _cameraController . lookAtPosition = this . _lookAtPosition ; this . _scene . render ( ) ; } /**
     * Listener for asset complete event on loader
     */ private onAssetComplete ( event ) { var asset = event . asset ; switch ( asset . assetType ) { case Sprite . assetType : this . _sprite = < Sprite > event . asset ; this . _sprite . y = 120 ; this . _sprite . transform . scaleTo ( 5 , 5 , 5 ) ; this . _spriteInitialised = true ; break ; case AnimationSetBase . assetType : this . _animationSet = < VertexAnimationSet > event . asset ; this . _animationSetInitialised = true ; break ; } if ( this . _animationSetInitialised && this . _spriteInitialised && ! this . _sceneInitialised ) { this . _sceneInitialised = true ; var numWide = 20 ; var numDeep = 20 ; var k = 0 ; for ( var i = 0 ; i < numWide ; i ++ ) { for ( var j = 0 ; j < numDeep ; j ++ ) { var clone = < Sprite > this . _sprite . clone ( ) ; clone . x = ( i - ( numWide - 1 ) / 2 ) * 5000 / numWide ; clone . z = ( j - ( numDeep - 1 ) / 2 ) * 5000 / numDeep ; clone . castsShadows = true ; clone . material = this . _pKnightMaterials [ Math . floor ( Math . random ( ) * this . _pKnightMaterials . length ) ] ; this . _scene . root . addChild ( clone ) ; var vertexAnimator = new VertexAnimator ( this . _animationSet ) ; vertexAnimator . play ( this . _animationSet . animationNames [ Math . floor ( Math . random ( ) * this . _animationSet . animationNames . length ) ] , null , Math . random ( ) * 1000 ) ; clone . animator = vertexAnimator ; k ++ ; } } } } /**
     * Listener function for resource complete event on asset library
     */ private onResourceComplete ( event ) { var assets = event . assets ; var length = assets . length ; for ( var c = 0 ; c < length ; c ++ ) { var asset = assets [ c ] ; console . log ( asset . name , event . url ) ; switch ( event . url ) { case "assets/floor_diffuse.jpg" : this . _floorMaterial . ambientMethod . texture = new ImageTexture2D ( < BitmapImage2D > asset ) ; break ; case "assets/pknight1.png" : case "assets/pknight2.png" : case "assets/pknight3.png" : case "assets/pknight4.png" : this . _pKnightMaterials [ this . _pKnightTextures . indexOf ( event . url ) ] . ambientMethod . texture = new ImageTexture2D ( < BitmapImage2D > asset ) ; break ; case "assets/pknight.md2" : break ; } } } /**
     * Key down listener for animation
     */ private onKeyDown ( event ) { switch ( event . keyCode ) { case Keyboard . UP : case Keyboard . W : case Keyboard . Z : this . _keyUp = true ; break ; case Keyboard . DOWN : case Keyboard . S : this . _keyDown = true ; break ; case Keyboard . LEFT : case Keyboard . A : case Keyboard . Q : this . _keyLeft = true ; break ; case Keyboard . RIGHT : case Keyboard . D : this . _keyRight = true ; break ; } } /**
     * Key up listener
     */ private onKeyUp ( event ) { switch ( event . keyCode ) { case Keyboard . UP : case Keyboard . W : case Keyboard . Z : this . _keyUp = false ; break ; case Keyboard . DOWN : case Keyboard . S : this . _keyDown = false ; break ; case Keyboard . LEFT : case Keyboard . A : case Keyboard . Q : this . _keyLeft = false ; break ; case Keyboard . RIGHT : case Keyboard . D : this . _keyRight = false ; break ; } } /**
     * Mouse down listener for navigation
     */ private onMouseDown ( event ) { this . _lastPanAngle = this . _cameraController . panAngle ; this . _lastTiltAngle = this . _cameraController . tiltAngle ; this . _lastMouseX = event . clientX ; this . _lastMouseY = event . clientY ; this . _move = true ; } /**
     * Mouse up listener for navigation
     */ private onMouseUp ( event ) { this . _move = false ; } private onMouseMove ( event ) { if ( this . _move ) { this . _cameraController . panAngle = 0.3 * ( event . clientX - this . _lastMouseX ) + this . _lastPanAngle ; this . _cameraController . tiltAngle = 0.3 * ( event . clientY - this . _lastMouseY ) + this . _lastTiltAngle ; } } /**
     * Wheel listener for navigation
     */ private onWheel ( event ) { this . _cameraController . distance -= event . deltaY / 2 ; if ( this . _cameraController . distance < 100 ) this . _cameraController . distance = 100 ; else if ( this . _cameraController . distance > 2000 ) this . _cameraController . distance = 2000 ; } /**
     * Stage 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 Intermediate_PerelithKnight ( ) ; } ;