import { AssetEvent , LoaderEvent , Vector3D , AssetLibrary , IAsset , LoaderContext , URLRequest , RequestAnimationFrame } from "@awayjs/core" ; import { BitmapImage2D , ImageSampler } from "@awayjs/stage" ; import { ElementsType } from "@awayjs/graphics" ; import { HoverController , Sprite , LoaderContainer , PrimitivePlanePrefab , PrimitiveSpherePrefab , Scene } from "@awayjs/scene" ; import { MethodMaterial , ShadowSoftMethod , ShadowHardMethod , ImageTexture2D , DirectionalLight , StaticLightPicker , ShadowFilteredMethod } from "@awayjs/materials" ; import { Max3DSParser } from "@awayjs/parsers" ; import { View } from "@awayjs/view" ; class Basic_Load3DS { private _scene : Scene ; private _cameraController : HoverController ; private _groundMaterial : MethodMaterial ; private _light : DirectionalLight ; private _lightPicker : StaticLightPicker ; private _direction : Vector3D ; private _loader : LoaderContainer ; private _plane : PrimitivePlanePrefab ; private _ground : Sprite ; private _timer : RequestAnimationFrame ; private _time : number = 0 ; private _move : boolean = false ; private _lastPanAngle : number ; private _lastTiltAngle : number ; private _lastMouseX : number ; private _lastMouseY : number ; /**
     * Constructor
     */ constructor ( ) { this . init ( ) ; } /**
     * Global initialise function
     */ private init ( ) { this . initEngine ( ) ; this . initLights ( ) ; this . initMaterials ( ) ; this . initObjects ( ) ; this . initListeners ( ) ; } /**
     * Initialise the engine
     */ private initEngine ( ) { this . _scene = new Scene ( ) ; this . _scene . camera . projection . far = 2100 ; this . _cameraController = new HoverController ( this . _scene . camera , null , 45 , 20 , 1000 , 10 ) ; } /**
     * Initialise the lights
     */ private initLights ( ) { this . _light = new DirectionalLight ( new Vector3D ( - 1 , - 1 , 1 ) ) ; this . _light . shadowMapper . epsilon = 0.2 ; this . _direction = new Vector3D ( - 1 , - 1 , 1 ) ; this . _lightPicker = new StaticLightPicker ( [ this . _light ] ) ; } /**
     * Initialise the materials
     */ private initMaterials ( ) { this . _groundMaterial = new MethodMaterial ( ) ; this . _groundMaterial . ambientMethod . texture = new ImageTexture2D ( ) ; this . _groundMaterial . shadowMethod = new ShadowSoftMethod ( this . _light , 10 , 5 ) ; this . _groundMaterial . style . sampler = new ImageSampler ( true , true , true ) ; this . _groundMaterial . style . addSamplerAt ( new ImageSampler ( true , true ) , this . _light . shadowMapper . textureMap ) ; this . _groundMaterial . lightPicker = this . _lightPicker ; this . _groundMaterial . specularMethod . strength = 0 ; } /**
     * Initialise the scene objects
     */ private initObjects ( ) { this . _loader = new LoaderContainer ( ) ; this . _loader . transform . scaleTo ( 300 , 300 , 300 ) ; this . _loader . z = - 200 ; this . _scene . root . addChild ( this . _loader ) ; this . _plane = new PrimitivePlanePrefab ( this . _groundMaterial , ElementsType . TRIANGLE , 1000 , 1000 ) ; this . _ground = < Sprite > this . _plane . getNewObject ( ) ; this . _ground . castsShadows = false ; this . _scene . root . addChild ( this . _ground ) ; } /**
     * Initialise the listeners
     */ private initListeners ( ) { window . onresize = ( event ) => this . onResize ( event ) ; document . onmousedown = ( event ) => this . onMouseDown ( event ) ; document . onmouseup = ( event ) => this . onMouseUp ( event ) ; document . onmousemove = ( event ) => this . onMouseMove ( event ) ; this . onResize ( ) ; this . _timer = new RequestAnimationFrame ( this . onEnterFrame , this ) ; this . _timer . start ( ) ; var loaderContext = new LoaderContext ( ) ; loaderContext . mapUrl ( "texture.jpg" , "assets/soldier_ant.jpg" ) ; this . _loader . addEventListener ( AssetEvent . ASSET_COMPLETE , ( event ) => this . onAssetComplete ( event ) ) ; this . _loader . load ( new URLRequest ( "assets/soldier_ant.3ds" ) , loaderContext , null , new Max3DSParser ( false ) ) ; AssetLibrary . addEventListener ( LoaderEvent . LOAD_COMPLETE , ( event ) => this . onResourceComplete ( event ) ) ; AssetLibrary . load ( new URLRequest ( "assets/CoarseRedSand.jpg" ) ) ; } /**
     * Navigation and render loop
     */ private onEnterFrame ( dt ) { this . _time += dt ; this . _direction . x = - Math . sin ( this . _time / 4000 ) ; this . _direction . z = - Math . cos ( this . _time / 4000 ) ; this . _light . direction = this . _direction ; this . _scene . render ( ) ; } /**
     * Listener function for asset complete event on loader
     */ private onAssetComplete ( event ) { var asset = event . asset ; switch ( asset . assetType ) { case Sprite . assetType : var sprite = < Sprite > event . asset ; sprite . castsShadows = true ; break ; case MethodMaterial . assetType : var material = < MethodMaterial > event . asset ; material . shadowMethod = new ShadowSoftMethod ( this . _light , 10 , 5 ) ; material . lightPicker = this . _lightPicker ; material . specularMethod . gloss = 30 ; material . specularMethod . strength = 1 ; material . style . color = 0x303040 ; material . diffuseMethod . multiply = false ; material . ambientMethod . strength = 1 ; break ; } } /**
     * 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/CoarseRedSand.jpg" : this . _groundMaterial . style . image = < BitmapImage2D > asset ; 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 ; } } /**
     * 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 Basic_Load3DS ( ) ; } ;