This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ray Chen
committed
May 30, 2019
1 parent
2fd47d6
commit dc5d6f3
Showing
20 changed files
with
1,653 additions
and
1,532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ImageLoader } from './ImageLoader.js'; | ||
import 'three'; | ||
|
||
/** | ||
* Cube Texture Loader based on {@link https://github.com/mrdoob/three.js/blob/master/src/loaders/CubeTextureLoader.js} | ||
* @memberOf PANOLENS | ||
* @namespace | ||
*/ | ||
const CubeTextureLoader = { | ||
|
||
/** | ||
* Load 6 images as a cube texture | ||
* @param {array} urls - Array with 6 image urls | ||
* @param {function} onLoad - On load callback | ||
* @param {function} onProgress - In progress callback | ||
* @param {function} onError - On error callback | ||
* @return {THREE.CubeTexture} - Cube texture | ||
*/ | ||
load: function ( urls, onLoad, onProgress, onError ) { | ||
|
||
var texture, loaded, progress, all, loadings; | ||
|
||
texture = new THREE.CubeTexture( [] ); | ||
|
||
loaded = 0; | ||
progress = {}; | ||
all = {}; | ||
|
||
urls.map( function ( url, index ) { | ||
|
||
ImageLoader.load( url, function ( image ) { | ||
|
||
texture.images[ index ] = image; | ||
|
||
loaded++; | ||
|
||
if ( loaded === 6 ) { | ||
|
||
texture.needsUpdate = true; | ||
|
||
onLoad && onLoad( texture ); | ||
|
||
} | ||
|
||
}, function ( event ) { | ||
|
||
progress[ index ] = { loaded: event.loaded, total: event.total }; | ||
|
||
all.loaded = 0; | ||
all.total = 0; | ||
loadings = 0; | ||
|
||
for ( var i in progress ) { | ||
|
||
loadings++; | ||
all.loaded += progress[ i ].loaded; | ||
all.total += progress[ i ].total; | ||
|
||
} | ||
|
||
if ( loadings < 6 ) { | ||
|
||
all.total = all.total / loadings * 6; | ||
|
||
} | ||
|
||
onProgress && onProgress( all ); | ||
|
||
}, onError ); | ||
|
||
} ); | ||
|
||
return texture; | ||
|
||
} | ||
|
||
}; | ||
|
||
export { CubeTextureLoader }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { DataImage } from '../DataImage.js'; | ||
import 'three'; | ||
|
||
/** | ||
* Image loader with progress based on {@link https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js} | ||
* @memberOf PANOLENS | ||
* @namespace | ||
*/ | ||
const ImageLoader = { | ||
|
||
load: function ( url, onLoad, onProgress, onError ) { | ||
|
||
// Enable cache | ||
THREE.Cache.enabled = true; | ||
|
||
let cached, request, arrayBufferView, blob, urlCreator, image, reference; | ||
|
||
// Reference key | ||
for ( let iconName in DataImage ) { | ||
|
||
if ( DataImage.hasOwnProperty( iconName ) && url === DataImage[ iconName ] ) { | ||
|
||
reference = iconName; | ||
|
||
} | ||
|
||
} | ||
|
||
// Cached | ||
cached = THREE.Cache.get( reference ? reference : url ); | ||
|
||
if ( cached !== undefined ) { | ||
|
||
if ( onLoad ) { | ||
|
||
setTimeout( function () { | ||
|
||
if ( onProgress ) { | ||
|
||
onProgress( { loaded: 1, total: 1 } ); | ||
|
||
} | ||
|
||
onLoad( cached ); | ||
|
||
}, 0 ); | ||
|
||
} | ||
|
||
return cached; | ||
|
||
} | ||
|
||
// Construct a new XMLHttpRequest | ||
urlCreator = window.URL || window.webkitURL; | ||
image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' ); | ||
|
||
// Add to cache | ||
THREE.Cache.add( reference ? reference : url, image ); | ||
|
||
const onImageLoaded = () => { | ||
|
||
urlCreator.revokeObjectURL( image.src ); | ||
onLoad && onLoad( image ); | ||
|
||
} | ||
|
||
if ( url.indexOf( 'data:' ) === 0 ) { | ||
|
||
image.addEventListener( 'load', onImageLoaded, false ); | ||
image.src = url; | ||
return image; | ||
} | ||
|
||
image.crossOrigin = this.crossOrigin !== undefined ? this.crossOrigin : ''; | ||
|
||
request = new XMLHttpRequest(); | ||
request.open( 'GET', url, true ); | ||
request.responseType = 'arraybuffer'; | ||
request.onprogress = function ( event ) { | ||
|
||
if ( event.lengthComputable ) { | ||
|
||
onProgress && onProgress( { loaded: event.loaded, total: event.total } ); | ||
|
||
} | ||
|
||
}; | ||
request.onloadend = function( event ) { | ||
|
||
arrayBufferView = new Uint8Array( this.response ); | ||
blob = new Blob( [ arrayBufferView ] ); | ||
|
||
image.addEventListener( 'load', onImageLoaded, false ); | ||
image.src = urlCreator.createObjectURL( blob ); | ||
|
||
}; | ||
|
||
request.send(null); | ||
|
||
} | ||
|
||
}; | ||
|
||
export { ImageLoader }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.