r3-legacy/src/game-lib-d3-texture.js

389 lines
9.4 KiB
JavaScript

/**
* Texture Superset - The apiTexture properties get moved into the Texture object itself, and then the instance is
* created
* @param apiTexture
* @param graphics GameLib.GraphicsRuntime
* @constructor
*/
GameLib.D3.Texture = function(
graphics,
apiTexture
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiTexture)) {
apiTexture = {};
}
if (apiTexture instanceof GameLib.D3.Texture) {
return apiTexture;
}
GameLib.D3.API.Texture.call(
this,
apiTexture.id,
apiTexture.textureType,
apiTexture.name,
apiTexture.image,
apiTexture.images,
apiTexture.wrapS,
apiTexture.wrapT,
apiTexture.repeat,
apiTexture.data,
apiTexture.format,
apiTexture.mapping,
apiTexture.magFilter,
apiTexture.minFilter,
apiTexture.storageType,
apiTexture.anisotropy,
apiTexture.offset,
apiTexture.generateMipmaps,
apiTexture.flipY,
apiTexture.mipmaps,
apiTexture.unpackAlignment,
apiTexture.premultiplyAlpha,
apiTexture.encoding,
apiTexture.canvas,
apiTexture.animated,
apiTexture.reverseAnimation,
apiTexture.forward,
apiTexture.parentEntity
);
this.offset = new GameLib.Vector2(
this.graphics,
this.offset,
this
);
this.repeat = new GameLib.Vector2(
this.graphics,
this.repeat,
this
);
if (this.image instanceof GameLib.API.Image) {
this.image = new GameLib.Image(
this.graphics,
this.image
);
}
this.images = this.images.map(
function(image) {
if (image instanceof GameLib.API.Image) {
return new GameLib.Image(
this.graphics,
image
);
} else {
return image;
}
}.bind(this)
);
if (this.canvas instanceof GameLib.API.Canvas) {
this.canvas = new GameLib.Canvas(
this.graphics,
this.canvas
);
}
GameLib.Component.call(
this,
{
'image' : GameLib.Image,
'images' : [GameLib.Image],
'canvas' : GameLib.Canvas
}
);
};
GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype);
GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture;
/**
* Creates an instance of our texture object
* @returns {*}
*/
GameLib.D3.Texture.prototype.createInstance = function() {
if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_CUBE) {
if (this.images.length !== 6) {
throw new Error('not enough images for cube texture');
}
var imageInstances = this.images.map(
function(image) {
if (GameLib.Utils.UndefinedOrNull(image)) {
throw new Error('no image');
}
if (GameLib.Utils.UndefinedOrNull(image.instance)) {
throw new Error('no image instance');
}
return image.instance
}
);
this.instance = new THREE.CubeTexture(imageInstances);
} else if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_NORMAL) {
if (
GameLib.Utils.UndefinedOrNull(this.image) ||
GameLib.Utils.UndefinedOrNull(this.image.instance)
) {
this.instance = new THREE.Texture();
} else {
//if (GameLib.Utils.UndefinedOrNull(this.image.instance)) {
// throw new Error('no image instance');
//}
this.instance = new THREE.Texture(
this.image.instance
);
}
} else if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_CANVAS) {
if (GameLib.Utils.UndefinedOrNull(this.canvas)) {
this.instance = new THREE.Texture();
} else {
if (GameLib.Utils.UndefinedOrNull(this.canvas.instance)) {
throw new Error('no canvas instance');
}
this.instance = new THREE.Texture(
this.canvas.instance
);
}
}
/**
* Some settings we copy from the instance
*/
this.mapping = this.instance.mapping;
this.encoding = this.instance.encoding;
this.format = this.instance.format;
/**
* Others we apply to the instance
*/
this.instance.name = this.name;
this.instance.flipY = this.flipY;
this.instance.offset.x = this.offset.x;
this.instance.offset.y = this.offset.y;
this.instance.repeat.x = this.repeat.x;
this.instance.repeat.y = this.repeat.y;
this.instance.wrapS = this.wrapS;
this.instance.wrapT = this.wrapT;
this.instance.needsUpdate = true;
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Texture.prototype.updateInstance = function(property) {
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
try {
this.createInstance();
return;
} catch (error) {
console.error(error);
}
}
if (GameLib.Utils.UndefinedOrNull(property)) {
throw new Error('need to specify a property');
}
if (property === 'image') {
if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_NORMAL) {
if (
GameLib.Utils.UndefinedOrNull(this.image) &&
this.instance.image
) {
try {
this.createInstance();
} catch (error) {
console.error(error);
}
}
if (this.image && this.image.instance && this.instance.image !== this.image.instance) {
try {
this.createInstance();
} catch (error) {
console.error(error);
}
}
} else if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_CANVAS) {
if (
GameLib.Utils.UndefinedOrNull(this.canvas) &&
this.instance.canvas
) {
try {
this.createInstance();
} catch (error) {
console.error(error);
}
}
if (this.canvas && this.canvas.instance && this.instance.image !== this.canvas.instance) {
try {
this.createInstance();
} catch (error) {
console.error(error);
}
}
} else if (this.textureType === GameLib.D3.API.Texture.TEXTURE_TYPE_CUBE) {
console.log('todo : cube images change check here');
}
this.publish(
GameLib.Event.IMAGE_CHANGED,
{
texture : this
}
);
this.instance.needsUpdate = true;
}
if (property === 'name') {
this.instance.name = this.name;
}
if (property === 'flipY') {
this.instance.flipY = this.flipY;
}
if (property === 'encoding') {
this.instance.encoding = this.encoding;
}
if (property === 'offset') {
this.instance.offset.x = this.offset.x;
this.instance.offset.y = this.offset.y;
}
if (property === 'repeat') {
this.instance.repeat.x = this.repeat.x;
this.instance.repeat.y = this.repeat.y;
}
if (property === 'mapping') {
this.instance.mapping = this.mapping;
}
if (property === 'format') {
this.instance.format = this.format;
}
if (property === 'wrapS') {
this.instance.wrapS = this.wrapS;
this.instance.needsUpdate = true;
}
if (property === 'wrapT') {
this.instance.wrapT = this.wrapT;
this.instance.needsUpdate = true;
}
if (property === 'animated') {
GameLib.Event.Emit(
GameLib.Event.TEXTURE_ANIMATED_CHANGE,
{
texture : this
}
)
}
};
/**
* Converts a GameLib.D3.Texture to a GameLib.D3.API.Texture
* @returns {GameLib.D3.API.Texture}
*/
GameLib.D3.Texture.prototype.toApiObject = function() {
var apiTexture = new GameLib.D3.API.Texture(
this.id,
this.textureType,
this.name,
GameLib.Utils.IdOrNull(this.image),
this.images.map(
function(image) {
return GameLib.Utils.IdOrNull(image)
}
),
this.wrapS,
this.wrapT,
this.repeat.toApiObject(),
this.data,
this.format,
this.mapping,
this.magFilter,
this.minFilter,
this.storageType,
this.anisotropy,
this.offset.toApiObject(),
this.generateMipmaps,
this.flipY,
this.mipmaps,
this.unpackAlignment,
this.premultiplyAlpha,
this.encoding,
GameLib.Utils.IdOrNull(this.canvas),
this.animated,
this.reverseAnimation,
this.forward,
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiTexture;
};
/**
* Converts from an Object texture to a GameLib.D3.Texture
* @param graphics GameLib.GraphicsRuntime
* @param objectTexture Object
* @constructor
*/
GameLib.D3.Texture.FromObject = function(
graphics,
objectTexture
) {
var apiTexture = GameLib.D3.API.Texture.FromObject(objectTexture);
return new GameLib.D3.Texture(
graphics,
apiTexture
);
};