r3-legacy/src/r3-d3-texture-cube.js

141 lines
3.0 KiB
JavaScript

/**
* R3.D3.Texture.Cube
* @param graphics
* @param apiTextureCube
* @constructor
*/
R3.D3.Texture.Cube = function(
graphics,
apiTextureCube
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiTextureCube)) {
apiTextureCube = {
textureType : R3.D3.API.Texture.TEXTURE_TYPE_CUBE
};
}
R3.D3.API.Texture.Cube.call(
this,
apiTextureCube,
apiTextureCube.images
);
this.images = this.images.map(
function(image) {
if (image instanceof R3.API.Image) {
return new R3.Image(image);
} else {
return image;
}
}
);
R3.D3.Texture.call(
this,
this.graphics,
this
);
};
R3.D3.Texture.Cube.prototype = Object.create(R3.D3.Texture.prototype);
R3.D3.Texture.Cube.prototype.constructor = R3.D3.Texture.Cube;
/**
* Returns all image instances, or null if one of the images are not loaded
*/
R3.D3.Texture.Cube.prototype.getImageInstances = function() {
return this.images.reduce(
function(result, image) {
/**
* If we have a null result return early
*/
if (result === null) {
return result;
}
if (R3.Utils.UndefinedOrNull(image.instance)) {
result = null;
} else {
result.push(image.instance);
}
return result;
},
[]
);
};
/**
* Creates an instance of our texture object
* @returns {*}
*/
R3.D3.Texture.Cube.prototype.createInstance = function() {
var imageInstances = this.getImageInstances();
if (!imageInstances) {
console.warn('cube texture not ready');
return;
}
this.instance = new THREE.CubeTexture(imageInstances);
R3.D3.Texture.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
R3.D3.Texture.Cube.prototype.updateInstance = function(property) {
if (property === 'images') {
var imageInstances = this.getImageInstances();
if (imageInstances) {
console.log('updating cube texture image instances');
this.image = imageInstances;
}
this.emit(
R3.Event.TEXTURE_INSTANCE_UPDATED,
{
texture : this
}
);
return;
}
R3.D3.Texture.prototype.updateInstance.call(this, property);
};
/**
* Converts a R3.D3.Texture.Cube to a R3.D3.API.Texture.Cube
* @returns {R3.D3.API.Texture.Cube}
*/
R3.D3.Texture.Cube.prototype.toApiObject = function() {
var apiTexture = R3.D3.Texture.prototype.toApiObject.call(this);
var apiTextureCube = new R3.D3.API.Texture.Cube(
apiTexture,
this.images.map(
function(image) {
return R3.Utils.IdOrNull(image);
}
)
);
return apiTextureCube;
};