HTML and CSS Reference
In-Depth Information
this.ext = gl.getExtension(vendorPrefixes[i] + "WEBGL_compressed_texture_s3tc");
if (this.ext) { break; }
// When using a worker process we must keep track of the pending texture
// loads so that we can correctly correlate the DXT data to the desired
// texture when the worker completes.
this.pendingTextures = {};
// Reload this file as a worker.
this.worker = new Worker("crunch-worker.js");
var self = this;
// The worker's message handler.
this.worker.onmessage = function(msg) {
// Find the pending texture associated with the data we just received
// from the worker.
var id =;
var pt = self.pendingTextures[id];
if (!pt) { return; }
// Remove the pending texture from the waiting list.
delete self.pendingTextures[id];
// If the worker indicated an error has occured handle it now.
if ( {
self._clearOnError(, pt.texture, pt.callback);
// Upload the DXT data returned by the worker.
new Uint8Array(,,,,,,
The first few line have stayed the same, but the remainder is the other half of the worker communication. First,
you define a dictionary to store pending textures in, then you create the worker object, giving it the name of the
script to load. Then you define the worker's onmessage handler, which is what gets called any time the worker calls
postMessage . The code it contains is pretty simple: look up which texture was loaded with the ID that was passed back
and then either call _clearOnError if the worker gave you an error message or _uploadDXT if you got back valid data.
Finally, you need to replace the logic of loadCRN . This function actually gets significantly simpler now that the
AJAX request and decode is being handled in the worker. Instead all the new loadCRN needs to do is track a new
CrunchPendingTexture and send the message to the worker to begin loading the Crunch file (see Listing 21-17).
Search WWH ::

Custom Search