Graphics Reference
In-Depth Information
ture is loaded, we will then load the next resource and so on. In this
code fragment, you can also see that we also call a catch and a
progress function. If an error occurs during loading, the function
provided to catch() will be called. The same goes for progress() .
If one of the methods wants to provide information about its progress,
the function passed into progress() will be called.
3. However, you will then find out that this won't work with the functions
from our previous recipe. To get this to work, we have to replace the
callbacks from these functions with a special Q construct that is called
a deferred function:
function loadTexture(texture) {
var deferred = Q.defer();
var text =
THREE.ImageUtils.loadTexture
(texture, null,
function(loaded) {
console.log("Loaded texture:
", texture);
deferred.resolve(loaded);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
4. In this code snippet, we create a new JavaScript object with the name
deferred . The deferred object will make sure that the results of
the callbacks, this time defined as anonymous functions, are returned
in such a way that we can use the then function we saw at the
beginning of this chapter. If the resource is loaded successfully, we
use the deferred.resolve function to store the result; if the re-
source was loaded unsuccessfully, we store the error using the de-
ferred.reject function.
5. We use the same approach for the loadModel , loadOthers , and
loadModelWithProgress functions:
Search WWH ::




Custom Search