2016-10-14 12:32:53 +02:00
|
|
|
/**
|
2016-11-17 18:31:41 +01:00
|
|
|
* Texture Superset - The apiTexture properties get moved into the Texture object itself, and then the instance is
|
|
|
|
* created
|
|
|
|
* @param apiTexture
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-05-11 04:48:02 +02:00
|
|
|
GameLib.D3.Texture = function(
|
2016-11-17 18:31:41 +01:00
|
|
|
graphics,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiTexture
|
2016-11-17 18:31:41 +01:00
|
|
|
) {
|
2016-12-02 16:03:03 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
2017-01-17 13:24:45 +01:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiTexture)) {
|
|
|
|
apiTexture = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiTexture instanceof GameLib.D3.Texture) {
|
|
|
|
return apiTexture;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
GameLib.D3.API.Texture.call(
|
2017-01-05 19:34:28 +01:00
|
|
|
this,
|
|
|
|
apiTexture.id,
|
|
|
|
apiTexture.typeId,
|
|
|
|
apiTexture.name,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiTexture.image,
|
2017-01-05 19:34:28 +01:00
|
|
|
apiTexture.wrapS,
|
|
|
|
apiTexture.wrapT,
|
|
|
|
apiTexture.repeat,
|
|
|
|
apiTexture.data,
|
|
|
|
apiTexture.format,
|
|
|
|
apiTexture.mapping,
|
|
|
|
apiTexture.magFilter,
|
|
|
|
apiTexture.minFilter,
|
|
|
|
apiTexture.textureType,
|
|
|
|
apiTexture.anisotropy,
|
|
|
|
apiTexture.offset,
|
|
|
|
apiTexture.generateMipmaps,
|
|
|
|
apiTexture.flipY,
|
|
|
|
apiTexture.mipmaps,
|
|
|
|
apiTexture.unpackAlignment,
|
|
|
|
apiTexture.premultiplyAlpha,
|
2017-01-19 17:50:11 +01:00
|
|
|
apiTexture.encoding,
|
|
|
|
apiTexture.parentEntity
|
2017-01-05 19:34:28 +01:00
|
|
|
);
|
2016-12-02 16:03:03 +01:00
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.offset = new GameLib.Vector2(
|
2017-06-08 18:17:03 +02:00
|
|
|
this.graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.offset,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.repeat = new GameLib.Vector2(
|
2017-06-08 18:17:03 +02:00
|
|
|
this.graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.repeat,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2017-06-09 16:03:05 +02:00
|
|
|
if (this.image instanceof GameLib.D3.API.Image) {
|
|
|
|
this.image = new GameLib.D3.Image(
|
|
|
|
this.graphics,
|
|
|
|
this.image
|
|
|
|
);
|
|
|
|
}
|
2016-11-17 18:31:41 +01:00
|
|
|
|
2017-06-08 18:17:03 +02:00
|
|
|
this.instance = this.createInstance();
|
2016-11-21 16:08:39 +01:00
|
|
|
};
|
|
|
|
|
2017-01-05 19:34:28 +01:00
|
|
|
GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype);
|
|
|
|
GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture;
|
|
|
|
|
2016-10-14 12:32:53 +02:00
|
|
|
/**
|
|
|
|
* Texture Formats
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_ALPHA_FORMAT = 1019;
|
|
|
|
GameLib.D3.Texture.TYPE_RGB_FORMAT = 1020;
|
|
|
|
GameLib.D3.Texture.TYPE_RGBA_FORMAT = 1021;
|
|
|
|
GameLib.D3.Texture.TYPE_LUMINANCE_FORMAT = 1022;
|
|
|
|
GameLib.D3.Texture.TYPE_LUMINANCE_ALPHA_FORMAT = 1023;
|
|
|
|
GameLib.D3.Texture.TYPE_DEPTH_FORMAT = 1026;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping modes
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_UV_MAPPING = 300;
|
|
|
|
GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING = 301;
|
|
|
|
GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING = 302;
|
|
|
|
GameLib.D3.Texture.TYPE_EQUI_RECTANGULAR_REFLECTION_MAPPING = 303;
|
|
|
|
GameLib.D3.Texture.TYPE_EQUI_RECTANGULAR_REFRACTION_MAPPING = 304;
|
|
|
|
GameLib.D3.Texture.TYPE_SPHERICAL_REFLECTION_MAPPING = 305;
|
|
|
|
GameLib.D3.Texture.TYPE_CUBE_UV_REFLECTION_MAPPING = 306;
|
|
|
|
GameLib.D3.Texture.TYPE_CUBE_UV_REFRACTION_MAPPING = 307;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapping Modes
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_REPEAT_WRAPPING = 1000;
|
|
|
|
GameLib.D3.Texture.TYPE_CLAMP_TO_EDGE_WRAPPING = 1001;
|
|
|
|
GameLib.D3.Texture.TYPE_MIRRORED_REPEAT_WRAPPING = 1002;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mipmap Filters
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_NEAREST_FILTER = 1003;
|
|
|
|
GameLib.D3.Texture.TYPE_NEAREST_MIPMAP_NEAREST_FILTER = 1004;
|
|
|
|
GameLib.D3.Texture.TYPE_NEAREST_MIPMAP_LINEAR_FILTER = 1005;
|
|
|
|
GameLib.D3.Texture.TYPE_LINEAR_FILTER = 1006;
|
|
|
|
GameLib.D3.Texture.TYPE_LINEAR_MIPMAP_NEAREST_FILTER = 1007;
|
|
|
|
GameLib.D3.Texture.TYPE_LINEAR_MIPMAP_LINEAR_FILTER = 1008;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Texture Data Types
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_UNSIGNED_BYTE = 1009;
|
|
|
|
GameLib.D3.Texture.TYPE_BYTE = 1010;
|
|
|
|
GameLib.D3.Texture.TYPE_SHORT = 1011;
|
|
|
|
GameLib.D3.Texture.TYPE_UNSIGNED_SHORT = 1012;
|
|
|
|
GameLib.D3.Texture.TYPE_INT = 1013;
|
|
|
|
GameLib.D3.Texture.TYPE_UNSIGNED_INT = 1014;
|
|
|
|
GameLib.D3.Texture.TYPE_FLOAT = 1015;
|
|
|
|
GameLib.D3.Texture.TYPE_HALF_FLOAT = 1025;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encoding Modes
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.TYPE_LINEAR_ENCODING = 3000; // NO ENCODING AT ALL.
|
|
|
|
GameLib.D3.Texture.TYPE_SRGB_ENCODING = 3001;
|
|
|
|
GameLib.D3.Texture.TYPE_GAMMA_ENCODING = 3007; // USES GAMMA_FACTOR, FOR BACKWARDS COMPATIBILITY WITH WEBGLRENDERER.GAMMAINPUT/GAMMAOUTPUT
|
|
|
|
GameLib.D3.Texture.TYPE_RGBE_ENCODING = 3002; // AKA RADIANCE.
|
|
|
|
GameLib.D3.Texture.TYPE_LOG_LUV_ENCODING = 3003;
|
|
|
|
GameLib.D3.Texture.TYPE_RGBM7_ENCODING = 3004;
|
|
|
|
GameLib.D3.Texture.TYPE_RGBM16_ENCODING = 3005;
|
|
|
|
GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
|
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
GameLib.D3.Texture.TEXTURE_TYPE_NORMAL = 0x1;
|
|
|
|
GameLib.D3.Texture.TEXTURE_TYPE_CUBE = 0x2;
|
|
|
|
|
|
|
|
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_ALPHA = 'alpha';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_AO = 'ao';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_BUMP = 'bump';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_DIFFUSE = 'diffuse';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_DISPLACEMENT = 'displacement';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_EMISSIVE = 'emissive';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_ENVIRONMENT = 'environment';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_LIGHT = 'light';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_METALNESS = 'metalness';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_NORMAL = 'normal';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_ROUGHNESS = 'roughness';
|
|
|
|
// GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular';
|
2016-11-22 19:20:25 +01:00
|
|
|
|
2016-10-14 12:32:53 +02:00
|
|
|
/**
|
2016-11-17 18:31:41 +01:00
|
|
|
* Creates an instance of our texture object
|
2017-02-01 16:09:34 +01:00
|
|
|
* @param update
|
|
|
|
* @returns {*}
|
2016-10-14 12:32:53 +02:00
|
|
|
*/
|
2016-11-18 16:00:13 +01:00
|
|
|
GameLib.D3.Texture.prototype.createInstance = function(update) {
|
2017-06-09 16:03:05 +02:00
|
|
|
|
2017-01-10 17:04:30 +01:00
|
|
|
if (update) {
|
2017-06-09 16:03:05 +02:00
|
|
|
|
|
|
|
if (!this.instance) {
|
|
|
|
console.warn('trying to update a non-existent texture instance');
|
|
|
|
return;
|
|
|
|
}
|
2017-06-12 22:40:00 +02:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(this.image.instance)) {
|
|
|
|
console.warn('trying to update while no image instance');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:17:03 +02:00
|
|
|
this.instance.name = this.name;
|
|
|
|
this.instance.flipY = this.flipY;
|
|
|
|
this.instance.encoding = this.encoding;
|
|
|
|
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;
|
2017-06-12 15:35:13 +02:00
|
|
|
this.instance.mapping = this.mapping;
|
|
|
|
this.instance.format = this.format;
|
2017-06-12 22:40:00 +02:00
|
|
|
this.instance.wrapS = this.wrapS;
|
|
|
|
this.instance.wrapT = this.wrapT;
|
2017-06-12 15:35:13 +02:00
|
|
|
|
|
|
|
this.publish(
|
|
|
|
GameLib.Event.TEXTURE_LOADED,
|
|
|
|
{
|
|
|
|
texture : this
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.instance.needsUpdate = true;
|
2017-06-09 16:03:05 +02:00
|
|
|
|
2017-01-10 17:04:30 +01:00
|
|
|
} else {
|
2017-06-09 16:03:05 +02:00
|
|
|
|
|
|
|
if (this.image instanceof GameLib.D3.Image) {
|
|
|
|
|
|
|
|
if (this.image.instance) {
|
|
|
|
|
|
|
|
var instance = new THREE.Texture(
|
|
|
|
this.image.instance
|
|
|
|
);
|
|
|
|
instance.name = this.name;
|
|
|
|
instance.flipY = this.flipY;
|
|
|
|
instance.encoding = this.encoding;
|
|
|
|
instance.offset.x = this.offset.x;
|
|
|
|
instance.offset.y = this.offset.y;
|
|
|
|
instance.repeat.x = this.repeat.x;
|
|
|
|
instance.repeat.y = this.repeat.y;
|
2017-06-12 15:35:13 +02:00
|
|
|
instance.mapping = this.mapping;
|
|
|
|
instance.format = this.format;
|
2017-06-12 22:40:00 +02:00
|
|
|
instance.wrapS = this.wrapS;
|
|
|
|
instance.wrapT = this.wrapT;
|
2017-06-12 15:35:13 +02:00
|
|
|
|
2017-06-12 22:40:00 +02:00
|
|
|
this.publish(
|
2017-06-12 15:35:13 +02:00
|
|
|
GameLib.Event.TEXTURE_LOADED,
|
|
|
|
{
|
|
|
|
texture : this
|
|
|
|
}
|
|
|
|
);
|
2017-06-09 16:03:05 +02:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.subscribe(
|
|
|
|
GameLib.Event.IMAGE_LOADED,
|
|
|
|
function (data) {
|
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
/**
|
|
|
|
* Only work with images that belong to this texture
|
|
|
|
*/
|
|
|
|
if (data.imagePath === this.image.path) {
|
2017-06-09 16:03:05 +02:00
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
if (!(this.image instanceof GameLib.D3.Image)) {
|
|
|
|
if (!data.cubeTexture) {
|
|
|
|
console.warn('The image associated with this texture has not been setup properly - objects linked?');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:40:00 +02:00
|
|
|
var instance = null;
|
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
if (data.cubeTexture) {
|
|
|
|
|
2017-06-12 22:40:00 +02:00
|
|
|
instance = data.cubeTexture;
|
|
|
|
|
|
|
|
if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING &&
|
|
|
|
this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING
|
|
|
|
) {
|
|
|
|
this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING;
|
|
|
|
}
|
2017-06-12 15:35:13 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-06-12 22:40:00 +02:00
|
|
|
instance = new THREE.Texture(
|
|
|
|
data.imageInstance
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) {
|
|
|
|
this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.name = this.name;
|
|
|
|
instance.flipY = this.flipY;
|
|
|
|
instance.encoding = this.encoding;
|
|
|
|
instance.offset.x = this.offset.x;
|
|
|
|
instance.offset.y = this.offset.y;
|
|
|
|
instance.repeat.x = this.repeat.x;
|
|
|
|
instance.repeat.y = this.repeat.y;
|
|
|
|
instance.mapping = this.mapping;
|
|
|
|
instance.format = this.format;
|
|
|
|
instance.wrapS = this.wrapS;
|
|
|
|
instance.wrapT = this.wrapT;
|
|
|
|
|
|
|
|
this.instance = instance;
|
2017-06-09 16:03:05 +02:00
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
this.publish(
|
|
|
|
GameLib.Event.TEXTURE_LOADED,
|
|
|
|
{
|
|
|
|
texture : this
|
|
|
|
}
|
2017-06-09 16:03:05 +02:00
|
|
|
);
|
2017-06-12 15:35:13 +02:00
|
|
|
|
|
|
|
this.instance.needsUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.subscribe(
|
|
|
|
GameLib.Event.IMAGE_CHANGE,
|
|
|
|
function (data) {
|
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
if (this.image === null) {
|
|
|
|
instance = new THREE.Texture();
|
|
|
|
} else {
|
|
|
|
instance = new THREE.Texture(this.image.instance);
|
2017-06-12 22:40:00 +02:00
|
|
|
// instance.needsUpdate = true;
|
|
|
|
// instance.name = this.name;
|
|
|
|
// instance.flipY = this.flipY;
|
|
|
|
// instance.encoding = this.encoding;
|
|
|
|
// instance.offset.x = this.offset.x;
|
|
|
|
// instance.offset.y = this.offset.y;
|
|
|
|
// instance.repeat.x = this.repeat.x;
|
|
|
|
// instance.repeat.y = this.repeat.y;
|
|
|
|
// this.format = instance.format;
|
2017-06-12 15:35:13 +02:00
|
|
|
}
|
2017-06-09 16:03:05 +02:00
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
this.instance = instance;
|
|
|
|
|
|
|
|
this.publish(
|
|
|
|
GameLib.Event.TEXTURE_LOADED,
|
|
|
|
{
|
|
|
|
texture : this
|
|
|
|
}
|
|
|
|
);
|
2017-06-09 21:52:18 +02:00
|
|
|
|
2017-06-09 16:03:05 +02:00
|
|
|
}
|
2017-01-10 17:04:30 +01:00
|
|
|
);
|
2017-06-09 16:03:05 +02:00
|
|
|
|
|
|
|
return null;
|
2017-01-10 17:04:30 +01:00
|
|
|
}
|
2016-11-17 18:31:41 +01:00
|
|
|
};
|
2016-11-18 16:00:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the instance with the current state
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.prototype.updateInstance = function() {
|
2017-06-08 18:17:03 +02:00
|
|
|
this.createInstance(true);
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a GameLib.D3.Texture to a GameLib.D3.API.Texture
|
|
|
|
* @returns {GameLib.D3.API.Texture}
|
|
|
|
*/
|
2017-06-13 14:09:18 +02:00
|
|
|
GameLib.D3.Texture.prototype.toApiObject = function(save) {
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2017-06-12 15:35:13 +02:00
|
|
|
var apiImage = null;
|
|
|
|
if (this.image) {
|
2017-06-13 14:09:18 +02:00
|
|
|
if (save) {
|
|
|
|
this.image.save();
|
|
|
|
}
|
2017-06-12 15:35:13 +02:00
|
|
|
apiImage = this.image.id
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
return new GameLib.D3.API.Texture(
|
|
|
|
this.id,
|
|
|
|
this.typeId,
|
|
|
|
this.name,
|
2017-06-12 15:35:13 +02:00
|
|
|
apiImage,
|
2016-12-09 20:32:09 +01:00
|
|
|
this.wrapS,
|
|
|
|
this.wrapT,
|
2017-05-16 14:51:57 +02:00
|
|
|
this.repeat.toApiObject(),
|
2016-12-09 20:32:09 +01:00
|
|
|
this.data,
|
|
|
|
this.format,
|
|
|
|
this.mapping,
|
|
|
|
this.magFilter,
|
|
|
|
this.minFilter,
|
|
|
|
this.textureType,
|
|
|
|
this.anisotropy,
|
2017-05-16 14:51:57 +02:00
|
|
|
this.offset.toApiObject(),
|
2016-12-09 20:32:09 +01:00
|
|
|
this.generateMipmaps,
|
|
|
|
this.flipY,
|
|
|
|
this.mipmaps,
|
|
|
|
this.unpackAlignment,
|
|
|
|
this.premultiplyAlpha,
|
2017-01-19 17:50:11 +01:00
|
|
|
this.encoding,
|
2017-06-13 14:09:18 +02:00
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
2016-12-09 20:32:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts from an Object texture to a GameLib.D3.Texture
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @param objectTexture Object
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Texture.FromObjectTexture = function(
|
|
|
|
graphics,
|
2017-06-08 18:17:03 +02:00
|
|
|
objectTexture
|
2016-12-09 20:32:09 +01:00
|
|
|
) {
|
2017-01-10 17:04:30 +01:00
|
|
|
var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
return new GameLib.D3.Texture(
|
|
|
|
graphics,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiTexture
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
2017-01-05 19:34:28 +01:00
|
|
|
};
|