three.js 源碼分析 之 LoadingManager

LoadingManager是一個下載過程中記錄、下載完成情況類,當three.js中的一些文件被下載時通過設置LoadingManager可以及時獲取當前文件列表中的下載進度

//正在加載的文件管理
    function LoadingManager( onLoad, onProgress, onError ) {
        //設置域
        var scope = this;
        //當前狀態爲未加載
        var isLoading = false;
        
        var itemsLoaded = 0;
        var itemsTotal = 0;
        var urlModifier = undefined;

        // Refer to #5689 for the reason why we don't set .onStart
        // in the constructor
        //設置回調狀態
        this.onStart = undefined;
        this.onLoad = onLoad;
        this.onProgress = onProgress;
        this.onError = onError;

        //單項開始了
        this.itemStart = function ( url ) {
            //下載項總數增加
            itemsTotal ++;
            //如果當前沒有下載
            if ( isLoading === false ) {
                //設置當前開始下載
                if ( scope.onStart !== undefined ) {
                    //開始下載回調,當前下載的url,當前下載的完的數量,當前下載的總數
                    scope.onStart( url, itemsLoaded, itemsTotal );

                }

            }
            //設置正在下載狀態
            isLoading = true;

        };

        //單項結束了
        this.itemEnd = function ( url ) {
            //下載完成,完成數增加
            itemsLoaded ++;
            //設置當前處理完成回調
            if ( scope.onProgress !== undefined ) {
                //開始下載回調,當前下載的url,當前下載的完的數量,當前下載的總數
                scope.onProgress( url, itemsLoaded, itemsTotal );

            }

            //總數和下載完成數相同 
            if ( itemsLoaded === itemsTotal ) {
                //重置下載狀態
                isLoading = false;
                //回調下載完成回調
                if ( scope.onLoad !== undefined ) {
                    
                    scope.onLoad();

                }

            }

        };

        //單項出現錯誤了
        this.itemError = function ( url ) {
            //錯誤回調
            if ( scope.onError !== undefined ) {

                scope.onError( url );

            }

        };

        //設置url解析中間過程
        this.resolveURL = function ( url ) {

            if ( urlModifier ) {

                return urlModifier( url );

            }

            return url;

        };

        //設置url中間轉換過程
        this.setURLModifier = function ( transform ) {

            urlModifier = transform;
            return this;

        };

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章