-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4814 from AnalyticalGraphicsInc/crunch
Add crunch compressed texture support
- Loading branch information
Showing
27 changed files
with
1,471 additions
and
75 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
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,93 @@ | ||
/*global define*/ | ||
define([ | ||
'./defined', | ||
'./defineProperties' | ||
], function( | ||
defined, | ||
defineProperties | ||
) { | ||
'use strict'; | ||
|
||
/** | ||
* Describes a compressed texture and contains a compressed texture buffer. | ||
* | ||
* @param {PixelFormat} internalFormat The pixel format of the compressed texture. | ||
* @param {Number} width The width of the texture. | ||
* @param {Number} height The height of the texture. | ||
* @param {Uint8Array} buffer The compressed texture buffer. | ||
*/ | ||
function CompressedTextureBuffer(internalFormat, width, height, buffer) { | ||
this._format = internalFormat; | ||
this._width = width; | ||
this._height = height; | ||
this._buffer = buffer; | ||
} | ||
|
||
defineProperties(CompressedTextureBuffer.prototype, { | ||
/** | ||
* The format of the compressed texture. | ||
* @type PixelFormat | ||
* @readonly | ||
*/ | ||
internalFormat : { | ||
get : function() { | ||
return this._format; | ||
} | ||
}, | ||
/** | ||
* The width of the texture. | ||
* @type Number | ||
* @readonly | ||
*/ | ||
width : { | ||
get : function() { | ||
return this._width; | ||
} | ||
}, | ||
/** | ||
* The height of the texture. | ||
* @type Number | ||
* @readonly | ||
*/ | ||
height : { | ||
get : function() { | ||
return this._height; | ||
} | ||
}, | ||
/** | ||
* The compressed texture buffer. | ||
* @type Uint8Array | ||
* @readonly | ||
*/ | ||
bufferView : { | ||
get : function() { | ||
return this._buffer; | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Creates a shallow clone of a compressed texture buffer. | ||
* | ||
* @param {CompressedTextureBuffer} object The compressed texture buffer to be cloned. | ||
* @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer. | ||
*/ | ||
CompressedTextureBuffer.clone = function(object) { | ||
if (!defined(object)) { | ||
return undefined; | ||
} | ||
|
||
return new CompressedTextureBuffer(object._format, object._width, object._height, object._buffer); | ||
}; | ||
|
||
/** | ||
* Creates a shallow clone of this compressed texture buffer. | ||
* | ||
* @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer. | ||
*/ | ||
CompressedTextureBuffer.prototype.clone = function() { | ||
return CompressedTextureBuffer.clone(this); | ||
}; | ||
|
||
return CompressedTextureBuffer; | ||
}); |
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,84 @@ | ||
/*global define*/ | ||
define([ | ||
'./CompressedTextureBuffer', | ||
'./defined', | ||
'./DeveloperError', | ||
'./loadArrayBuffer', | ||
'./TaskProcessor', | ||
'../ThirdParty/when' | ||
], function( | ||
CompressedTextureBuffer, | ||
defined, | ||
DeveloperError, | ||
loadArrayBuffer, | ||
TaskProcessor, | ||
when) { | ||
'use strict'; | ||
|
||
var transcodeTaskProcessor = new TaskProcessor('transcodeCRNToDXT', Number.POSITIVE_INFINITY); | ||
|
||
/** | ||
* Asynchronously loads and parses the given URL to a CRN file or parses the raw binary data of a CRN file. | ||
* Returns a promise that will resolve to an object containing the image buffer, width, height and format once loaded, | ||
* or reject if the URL failed to load or failed to parse the data. The data is loaded | ||
* using XMLHttpRequest, which means that in order to make requests to another origin, | ||
* the server must have Cross-Origin Resource Sharing (CORS) headers enabled. | ||
* | ||
* @exports loadCRN | ||
* | ||
* @param {String|Promise.<String>|ArrayBuffer} urlOrBuffer The URL of the binary data, a promise for the URL, or an ArrayBuffer. | ||
* @param {Object} [headers] HTTP headers to send with the requests. | ||
* @returns {Promise.<CompressedTextureBuffer>} A promise that will resolve to the requested data when loaded. | ||
* | ||
* @exception {RuntimeError} Unsupported compressed format. | ||
* | ||
* @example | ||
* // load a single URL asynchronously | ||
* Cesium.loadCRN('some/url').then(function(textureData) { | ||
* var width = textureData.width; | ||
* var height = textureData.height; | ||
* var format = textureData.internalFormat; | ||
* var arrayBufferView = textureData.bufferView; | ||
* // use the data to create a texture | ||
* }).otherwise(function(error) { | ||
* // an error occurred | ||
* }); | ||
* | ||
* @see {@link https://github.com/BinomialLLC/crunch|crunch DXTc texture compression and transcoding library} | ||
* @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing} | ||
* @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A} | ||
*/ | ||
function loadCRN(urlOrBuffer, headers) { | ||
//>>includeStart('debug', pragmas.debug); | ||
if (!defined(urlOrBuffer)) { | ||
throw new DeveloperError('urlOrBuffer is required.'); | ||
} | ||
//>>includeEnd('debug'); | ||
|
||
var loadPromise; | ||
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) { | ||
loadPromise = when.resolve(urlOrBuffer); | ||
} else { | ||
loadPromise = loadArrayBuffer(urlOrBuffer, headers); | ||
} | ||
|
||
return loadPromise.then(function(data) { | ||
var transferrableObjects = []; | ||
if (data instanceof ArrayBuffer) { | ||
transferrableObjects.push(data); | ||
} else if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) { | ||
transferrableObjects.push(data.buffer); | ||
} else { | ||
// data is a view of an array buffer. need to copy so it is transferrable to web worker | ||
data = data.slice(0, data.length); | ||
transferrableObjects.push(data.buffer); | ||
} | ||
|
||
return transcodeTaskProcessor.scheduleTask(data, transferrableObjects); | ||
}).then(function(compressedTextureBuffer) { | ||
return CompressedTextureBuffer.clone(compressedTextureBuffer); | ||
}); | ||
} | ||
|
||
return loadCRN; | ||
}); |
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
Oops, something went wrong.