303 lines
8.6 KiB
JavaScript
303 lines
8.6 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.D3.Graphics
|
|
* @param imageFactory GameLib.D3.ImageFactory result
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.Texture = function Texture(
|
|
graphics,
|
|
apiTexture,
|
|
// parentMaterial,
|
|
// parentMaterialInstanceMapId,
|
|
imageFactory
|
|
) {
|
|
this.graphics = graphics;
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiTexture)) {
|
|
apiTexture = {};
|
|
}
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
|
|
console.warn('Cannot create a material without specifying an ImageFactory');
|
|
throw new Error('Cannot create a material without specifying an ImageFactory');
|
|
}
|
|
|
|
GameLib.D3.API.Texture.call(
|
|
this,
|
|
apiTexture.id,
|
|
apiTexture.typeId,
|
|
apiTexture.name,
|
|
apiTexture.imagePath,
|
|
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,
|
|
apiTexture.encoding
|
|
);
|
|
|
|
this.offset = new GameLib.Vector2(
|
|
graphics,
|
|
this.offset,
|
|
this
|
|
);
|
|
|
|
this.repeat = new GameLib.Vector2(
|
|
graphics,
|
|
this.repeat,
|
|
this
|
|
);
|
|
|
|
// this.parentMaterial = parentMaterial;
|
|
//
|
|
// this.parentMaterialInstanceMapId = parentMaterialInstanceMapId;
|
|
|
|
this.imageInstance = null;
|
|
|
|
this.instance = null;
|
|
|
|
this.loadTexture(imageFactory);
|
|
};
|
|
|
|
GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype);
|
|
GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture;
|
|
|
|
/**
|
|
* Loads a texture from the image factory, it could already have downloaded, and then it updates the instance
|
|
* @param imageFactory
|
|
*/
|
|
GameLib.D3.Texture.prototype.loadTexture = function(imageFactory) {
|
|
|
|
this.imageData = imageFactory(this.imagePath);
|
|
|
|
this.imageData.then(
|
|
function (imageInstance){
|
|
this.imageInstance = imageInstance;
|
|
this.instance = this.createInstance();
|
|
// this.parentMaterial.instance[this.parentMaterialInstanceMapId] = this.instance;
|
|
// this.parentMaterial.instance.needsUpdate = true;
|
|
}.bind(this),
|
|
function onRejected() {
|
|
}
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 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.
|
|
|
|
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';
|
|
|
|
/**
|
|
* Creates an instance of our texture object
|
|
* @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture}
|
|
*/
|
|
GameLib.D3.Texture.prototype.createInstance = function(update) {
|
|
|
|
var instance = null;
|
|
|
|
if (update) {
|
|
instance = this.instance;
|
|
instance.mapping = this.mapping;
|
|
instance.wrapS = this.wrapS;
|
|
instance.wrapT = this.wrapT;
|
|
instance.magFilter = this.magFilter;
|
|
instance.minFilter = this.minFilter;
|
|
instance.anisotropy = this.anisotropy;
|
|
} else {
|
|
instance = new THREE.Texture(
|
|
this.imageInstance,
|
|
this.mapping,
|
|
this.wrapS,
|
|
this.wrapT,
|
|
this.magFilter,
|
|
this.minFilter,
|
|
undefined, //format and textureType is different on different archs
|
|
undefined,
|
|
this.anisotropy
|
|
);
|
|
}
|
|
|
|
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.mipmaps = this.mipmaps;
|
|
instance.unpackAlignment = this.unpackAlignment;
|
|
instance.premultiplyAlpha = this.premultiplyAlpha;
|
|
instance.textureType = this.textureType;
|
|
|
|
// if (this.parentMaterial &&
|
|
// this.parentMaterial.instance &&
|
|
// this.parentMaterialInstanceMapId) {
|
|
// this.parentMaterial.instance[this.parentMaterialInstanceMapId] = instance;
|
|
// }
|
|
|
|
instance.needsUpdate = true;
|
|
|
|
return instance;
|
|
};
|
|
|
|
/**
|
|
* Updates the instance with the current state
|
|
*/
|
|
GameLib.D3.Texture.prototype.updateInstance = function() {
|
|
this.instance = this.createInstance(true);
|
|
};
|
|
|
|
/**
|
|
* Clones this texture object
|
|
* @returns {*}
|
|
*/
|
|
GameLib.D3.Texture.prototype.clone = function() {
|
|
return _.cloneDeep(this);
|
|
};
|
|
|
|
/**
|
|
* Converts a GameLib.D3.Texture to a GameLib.D3.API.Texture
|
|
* @returns {GameLib.D3.API.Texture}
|
|
*/
|
|
GameLib.D3.Texture.prototype.toApiTexture = function() {
|
|
|
|
return new GameLib.D3.API.Texture(
|
|
this.id,
|
|
this.typeId,
|
|
this.name,
|
|
this.imagePath,
|
|
this.wrapS,
|
|
this.wrapT,
|
|
this.repeat.toApiVector(),
|
|
this.data,
|
|
this.format,
|
|
this.mapping,
|
|
this.magFilter,
|
|
this.minFilter,
|
|
this.textureType,
|
|
this.anisotropy,
|
|
this.offset.toApiVector(),
|
|
this.generateMipmaps,
|
|
this.flipY,
|
|
this.mipmaps,
|
|
this.unpackAlignment,
|
|
this.premultiplyAlpha,
|
|
this.encoding
|
|
)
|
|
|
|
};
|
|
|
|
/**
|
|
* Converts from an Object texture to a GameLib.D3.Texture
|
|
* @param graphics GameLib.D3.Graphics
|
|
* @param objectTexture Object
|
|
* @param imageFactory GameLib.D3.ImageFactory
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.Texture.FromObjectTexture = function(
|
|
graphics,
|
|
objectTexture,
|
|
// gameLibMaterial,
|
|
// instanceMapId,
|
|
imageFactory
|
|
) {
|
|
var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture);
|
|
|
|
return new GameLib.D3.Texture(
|
|
graphics,
|
|
apiTexture,
|
|
imageFactory
|
|
);
|
|
};
|