var WAFFLE = {}; WAFFLE.AssetLoader = {}; var objectLoader = new THREE.ObjectLoader(); var textureLoader = new THREE.TextureLoader(); var audioLoader = new THREE.AudioLoader(); var queuedAssets = []; var asyncQueuedAssets = {}; var geometryList = [], textureList = {}, audioBufferList = [], shaderList = []; var assetsRemaining = 0; var isLoadingSync = false; var isLoadingAsync = false; textureLoader.setCrossOrigin(''); WAFFLE.AssetLoader.isTextureLoaded = function ( name ) { return name in textureList; } WAFFLE.AssetLoader.queue = function ( URL, loadIndex, properties, callback ) { if ( loadIndex in queuedAssets == false ) { queuedAssets[ loadIndex ] = {}; } if ( typeof URL == "string" ) { URL = [ URL ]; } $.each( URL, function ( index, value ) { queuedAssets[ loadIndex ][ value ] = { URL: value, properties: properties, callback: callback }; assetsRemaining ++; } ); }; WAFFLE.AssetLoader.queueAsync = function ( URL, properties, callback ) { asyncQueuedAssets[ URL ] = { URL: URL, properties: properties, callback: callback }; var type = getAssetType( URL ); if ( ( type == 'jpg' || type == 'png' ) && ( properties == null || properties.useLoadingPlaceholder ) ) { textureList[ URL ] = textureList[ "$textures/Default.png" ].clone(); textureList[ URL ].name = getAssetName( URL ); textureList[ URL ].sourceFile = URL; textureList[ URL ].needsUpdate = true; } $( "#asyncAssetCount" ).text( "Loading " + Object.keys( asyncQueuedAssets ).length + " assets" ); }; WAFFLE.AssetLoader.load = function ( loadCallback ) { isLoadingSync = true; loadAssets( queuedAssets[ 0 ], function ( d ) { loadCallback( assetsRemaining ); if ( Object.keys( queuedAssets[ 0 ] ).length == 0 ) { queuedAssets.shift(); WAFFLE.AssetLoader.load( loadCallback ); } if ( assetsRemaining == 0 ) { isLoadingSync = false; WAFFLE.AssetLoader.loadAsync(); } } ); }; WAFFLE.AssetLoader.loadAsync = function () { if ( !isLoadingAsync && !isLoadingSync ) { isLoadingAsync = true; assetsRemaining = Object.keys( asyncQueuedAssets ).length; loadAssets( asyncQueuedAssets, null, true ); $( "#asyncAssetLoadingBar" ).stop(); $( "#asyncAssetLoadingBar" ).animate( { height: "48px" }, { duration: 600, complete: function () { $( "#asyncAssetLoadingBar" ).animate( { opacity: 1 }, { duration: 600 } ); } } ); } }; function loadAssets( assets, loadCallback, async ) { $.each( assets, function ( index, asset ) { var type = getAssetType( asset.URL ); switch ( type ) { case "js": $.getScript( asset.URL, function () { if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); } } ); break; case "json": objectLoader.load( asset.URL, function ( geo ) { if ( asset.properties[ "initRotation" ] != null ) { geo.geometry.rotateX( asset.properties[ "initRotation" ]._x ); geo.geometry.rotateY( asset.properties[ "initRotation" ]._z ); geo.geometry.rotateZ( asset.properties[ "initRotation" ]._y ); } asset.properties.resourceID = getAssetName( asset.URL ); geometryList[ asset.properties.resourceID ] = geo.geometry; addSpawnTile( asset.properties ); if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); } } ); break; case "svg": case "jpg": case "png": var passedURL = asset.URL.replace(/\$textures\//, texturePath); if ( asset.properties != null && asset.properties.preloadIcon ) { var preloadImage = new Image(); preloadImage.src = passedURL; preloadImage.onload = function() { loadedAsset( asset, loadCallback ); } } else { textureLoader.load( passedURL, function ( texture ) { texture.anisotropy = getOption( "graphics_anisotropy" ); texture.sourceFile = asset.URL; textureList[ asset.URL ] = texture; if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); if ( asset.properties == null || ! asset.properties.ignoreChecks ) { if ( grassPlane.material.map.sourceFile == asset.URL ) { grassPlane.material.map = textureList[ asset.URL ]; grassPlane.material.map.wrapS = THREE.RepeatWrapping; grassPlane.material.map.wrapT = THREE.RepeatWrapping; grassPlane.material.map.repeat.set( 128, 128 ); grassPlane.material.map.offset.set( player.position.x / 32, -( player.position.z / 32 ) ); } else if ( grassPlane.material.normalMap != null && grassPlane.material.normalMap.sourceFile == asset.URL ) { grassPlane.material.normalMap = textureList[ asset.URL ]; grassPlane.material.normalMap.wrapS = THREE.RepeatWrapping; grassPlane.material.normalMap.wrapT = THREE.RepeatWrapping; grassPlane.material.normalMap.repeat.set( 128, 128 ); } else if ( grassPlane.material.displacementMap != null && grassPlane.material.displacementMap.sourceFile == asset.URL ) { grassPlane.material.displacementMap = textureList[ asset.URL ]; } $.each( Object.keys( meshList ), function ( i, val ) { if ( meshList[ val ].material.map != null && meshList[ val ].material.map.sourceFile == asset.URL ) { meshList[ val ].material.map = textureList[ asset.URL ]; } else if ( meshList[ val ].material.normalMap != null && meshList[ val ].material.normalMap.sourceFile == asset.URL ) { meshList[ val ].material.normalMap = textureList[ asset.URL ]; } else if ( meshList[ val ].material.aoMap != null && meshList[ val ].material.aoMap.sourceFile == asset.URL ) { meshList[ val ].material.aoMap = textureList[ asset.URL ]; } } ); } } }, null, function() { // Error loading, ignore it if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); } } ); } break; case "mp3": audioLoader.load( asset.URL, function ( buffer ) { audioBufferList[ getAssetName( asset.URL ) ] = buffer; if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); } } ); break; case "shader": $.ajax( { url: asset.URL, dataType: "text", success: function ( data ) { shaderList[ getAssetName( asset.URL ) ] = data; if ( ! async ) { loadedAsset( asset, loadCallback ); } else { loadedAsyncAsset( asset ); } } } ); break; default: console.warn( asset.URL + " has unknown type '" + type + "' attempted to load and will be ignored" ); break; } } ); } function loadedAsyncAsset( asset ) { delete asyncQueuedAssets[ asset.URL ]; var asyncQueuedAssetsLength = Object.keys( asyncQueuedAssets ).length; if ( --assetsRemaining != 0 ) { $( "#asyncAssetCount" ).text( "Loading " + asyncQueuedAssetsLength + ( asyncQueuedAssetsLength == 1 ? " asset" : " assets" ) ); } else { if ( asyncQueuedAssetsLength != 0 ) { assetsRemaining = asyncQueuedAssetsLength; loadAssets( asyncQueuedAssets, null, true ); } else { isLoadingAsync = false; $( "#asyncAssetLoadingBar" ).stop(); $( "#asyncAssetLoadingBar" ).animate( { opacity: 0 }, { duration: 600, complete: function () { $( "#asyncAssetLoadingBar" ).animate( { height: "0px" }, { duration: 600 } ); } } ); } } if ( asset.callback != undefined ) asset.callback(); } function loadedAsset( asset, loadCallback ) { assetsRemaining --; delete queuedAssets[ 0 ][ asset.URL ]; if ( asset.callback != undefined ) asset.callback(); loadCallback(); } function getAssetName( URL ) { return /([^\/]+)(?=\.\w+$)/.exec( URL )[ 1 ]; } function getAssetType( URL ) { return /(?:\.([^.]+))?$/.exec( URL )[ 1 ]; } // // Texture handler // function getTexture(textureURL) { if (textureURL == null) { return null; } if (textureURL in textureList == false) { WAFFLE.AssetLoader.queueAsync(textureURL); WAFFLE.AssetLoader.loadAsync(); } return textureList[textureURL]; }