huge refactorings - need tweaking

beta.r3js.org
Theunis J. Botha 2016-11-17 18:31:41 +01:00
parent e7144a513c
commit eefd4a9603
9 changed files with 1167 additions and 1060 deletions

View File

@ -8,7 +8,7 @@
"gulp-concat": "^2.6.0", "gulp-concat": "^2.6.0",
"gulp-minify": "0.0.14", "gulp-minify": "0.0.14",
"gulp-sort": "^2.0.0", "gulp-sort": "^2.0.0",
"lodash": "^4.17.1", "lodash": "^4.17.2",
"q": "^1.4.1", "q": "^1.4.1",
"three": "^0.81.2" "three": "^0.81.2"
}, },

View File

@ -7,6 +7,7 @@ if (typeof GameLib.D3 == 'undefined') {
} }
if (typeof Q == 'undefined') { if (typeof Q == 'undefined') {
if (typeof require == 'undefined') { if (typeof require == 'undefined') {
console.warn('You need the Q promise library for the GameLib.D3'); console.warn('You need the Q promise library for the GameLib.D3');
throw new Error('You need the Q promise library for the GameLib.D3'); throw new Error('You need the Q promise library for the GameLib.D3');
@ -16,10 +17,11 @@ if (typeof Q == 'undefined') {
} }
if (typeof _ == 'undefined') { if (typeof _ == 'undefined') {
if (typeof require == 'undefined') { if (typeof require == 'undefined') {
console.warn('You need the lowdash library for the GameLib.D3'); console.warn('You need the lowdash library for the GameLib.D3');
throw new Error('You need the lowdash library for the GameLib.D3'); throw new Error('You need the lowdash library for the GameLib.D3');
} }
var _ = require('_'); var _ = require('lodash');
} }

View File

@ -1,7 +1,69 @@
/**
* The image factory takes care that we only make requests for Image URLs which we have not already started downloading
* @param graphics GameLib.D3.Graphics
* @returns {Function}
* @constructor
*/
GameLib.D3.ImageFactory = function (graphics) {
graphics.isNotThreeThrow();
var promiseList = {};
return function(url, progressCallback) {
if (!url) {
console.log('Attempted to download bad URL : ' + url);
throw new Error('Bad URL : ' + url);
}
if (promiseList[url]) {
return promiseList[url];
}
var defer = q.defer();
promiseList[url] = defer.promise;
GameLib.D3.ImageFactory.LoadImage(graphics, url, defer, progressCallback);
return promiseList[url];
}
};
/**
* Loads an image and resolves the defered promise once it succeeded (or failed)
* @param graphics
* @param url
* @param defer
* @param progressCallback
* @constructor
*/
GameLib.D3.ImageFactory.LoadImage = function (graphics, url, defer, progressCallback) {
var loader = new graphics.instance.ImageLoader();
loader.crossOrigin = '';
loader.load(
url,
function (image) {
defer.resolve(image);
},
function onProgress(xhr) {
if (progressCallback) {
progressCallback((xhr.loaded / xhr.total * 100));
}
},
function onError() {
defer.reject('Failed to download image : ' + url);
}
);
};
/** /**
* Image * Image
* @param id * @param id
* @param textureLink
* @param filename * @param filename
* @param size * @param size
* @param contentType * @param contentType
@ -9,17 +71,17 @@
*/ */
GameLib.D3.Image = function( GameLib.D3.Image = function(
id, id,
textureLink,
filename, filename,
size, size,
contentType contentType
) { ) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id; this.id = id;
this.filename = filename; this.filename = filename;
this.textureLink = textureLink;
if (typeof size == 'undefined') { if (typeof size == 'undefined') {
size = 0; size = 0;
} }
@ -42,4 +104,4 @@ GameLib.D3.Image = function(
} }
} }
this.contentType = contentType; this.contentType = contentType;
}; };

View File

@ -1,5 +1,29 @@
/** /**
* Light Superset * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created
* @param id
* @param graphics GameLib.D3.Graphics
* @param apiLight GameLib.D3.Light.API
* @constructor
*/
GameLib.D3.Light = function(
id,
graphics,
apiLight
) {
for (var property in apiLight) {
if (apiLight.hasOwnProperty(property)) {
this[property] = apiLight[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Raw Light API object - should always correspond with the Light Schema
* @param id * @param id
* @param lightType * @param lightType
* @param name * @param name
@ -17,7 +41,7 @@
* @param penumbra * @param penumbra
* @constructor * @constructor
*/ */
GameLib.D3.Light = function( GameLib.D3.Light.API = function(
id, id,
lightType, lightType,
name, name,
@ -34,9 +58,21 @@ GameLib.D3.Light = function(
angle, angle,
penumbra penumbra
) { ) {
this.id = GameLib.D3.Tools.RandomId(); if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(lightType)) {
lightType = GameLib.D3.Light.LIGHT_TYPE_AMBIENT;
}
this.lightType = lightType; this.lightType = lightType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Light (' + lightType + ')';
}
this.name = name; this.name = name;
this.color = color; this.color = color;
this.intensity = intensity; this.intensity = intensity;
@ -89,4 +125,56 @@ GameLib.D3.Light = function(
penumbra = 0; penumbra = 0;
} }
this.penumbra = penumbra; this.penumbra = penumbra;
};
/**
* Light Types
* @type {number}
*/
GameLib.D3.Light.LIGHT_TYPE_AMBIENT = 0x1;
GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL = 0x2;
GameLib.D3.Light.LIGHT_TYPE_POINT = 0x3;
GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Light.createInstance = function() {
var instance = null;
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
instance = new this.graphics.instance.AmbientLight(this.color, this.intensity);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
instance = new this.graphics.instance.DirectionalLight(this.color, this.intensity);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
instance = new this.graphics.instance.PointLight(this.color, this.intensity);
instance.distance = this.distance;
instance.decay = this.decay;
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) {
instance = new this.graphics.instance.SpotLight(this.color, this.intensity);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
}
instance.gameLibObject = this;
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.rotation.x = this.rotation.x;
instance.rotation.y = this.rotation.y;
instance.rotation.z = this.rotation.z;
return instance;
}; };

View File

@ -1,8 +1,32 @@
/** /**
* Material Superset * Material Superset - The apiMaterial properties get moved into the Material object itself, and then the instance is
* created
* @param graphics GameLib.D3.Graphics
* @param apiMaterial GameLib.D3.Material.API
* @constructor
* @returns {GameLib.D3.Material | GameLib.D3.Material.API}
*/
GameLib.D3.Material = function(
graphics,
apiMaterial
) {
for (var property in apiMaterial) {
if (apiMaterial.hasOwnProperty(property)) {
this[property] = apiMaterial[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Raw material API object - should always correspond with the Material Schema
* @param id * @param id
* @param name
* @param materialType * @param materialType
* @param name
* @param opacity * @param opacity
* @param side * @param side
* @param transparent * @param transparent
@ -59,10 +83,10 @@
* @param envMapIntensity * @param envMapIntensity
* @constructor * @constructor
*/ */
GameLib.D3.Material = function( GameLib.D3.Material.API = function(
id, id,
name,
materialType, materialType,
name,
opacity, opacity,
side, side,
transparent, transparent,
@ -118,13 +142,21 @@ GameLib.D3.Material = function(
spriteRotation, spriteRotation,
envMapIntensity envMapIntensity
) { ) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id; this.id = id;
this.name = name;
if (typeof materialType == 'undefined') { if (typeof materialType == 'undefined') {
materialType = GameLib.D3.Material.TYPE_MESH_STANDARD; materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD;
} }
this.materialType = materialType; this.materialType = materialType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Material (' + materialType + ')';
}
this.name = name;
if (typeof opacity == 'undefined') { if (typeof opacity == 'undefined') {
opacity = 1.0; opacity = 1.0;
} }
@ -141,20 +173,7 @@ GameLib.D3.Material = function(
this.transparent = transparent; this.transparent = transparent;
if (typeof maps == 'undefined') { if (typeof maps == 'undefined') {
maps = { maps = new GameLib.D3.TextureMapTemplate.API();
alpha: null,
ao: null,
bump: null,
diffuse: null,
displacement: null,
emissive: null,
environment: null,
light: null,
metalness: null,
normal: null,
roughness: null,
specular: null
};
} }
this.maps = maps; this.maps = maps;
@ -409,22 +428,6 @@ GameLib.D3.Material = function(
this.envMapIntensity = envMapIntensity; this.envMapIntensity = envMapIntensity;
}; };
/**
* Assigns a copy of the loaded texture map to the instance material
* @param instanceMaterial
* @param instanceMapId
* @returns {Function}
* @constructor
*/
GameLib.D3.Material.ImageLoadedCallback = function(
instanceMaterial,
instanceMapId
) {
return function(texture) {
instanceMaterial[instanceMapId] = texture.clone();
};
};
/** /**
* Combine Method * Combine Method
* @type {number} * @type {number}
@ -509,337 +512,140 @@ GameLib.D3.Material.TYPE_SMOOTH_SHADING = 2;
* Material Type * Material Type
* @type {string} * @type {string}
*/ */
GameLib.D3.Material.TYPE_LINE_BASIC = "LineBasicMaterial"; GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC = "LineBasicMaterial";
GameLib.D3.Material.TYPE_LINE_DASHED = "LineDashedMaterial"; GameLib.D3.Material.MATERIAL_TYPE_LINE_DASHED = "LineDashedMaterial";
GameLib.D3.Material.TYPE_MESH_BASIC = "MeshBasicMaterial"; GameLib.D3.Material.MATERIAL_TYPE_BASIC = "MeshBasicMaterial";
GameLib.D3.Material.TYPE_MESH_DEPTH = "MeshDepthMaterial"; GameLib.D3.Material.MATERIAL_TYPE_DEPTH = "MeshDepthMaterial";
GameLib.D3.Material.TYPE_MESH_LAMBERT = "MeshLambertMaterial"; GameLib.D3.Material.MATERIAL_TYPE_LAMBERT = "MeshLambertMaterial";
GameLib.D3.Material.TYPE_MESH_NORMAL = "MeshNormalMaterial"; GameLib.D3.Material.MATERIAL_TYPE_NORMAL = "MeshNormalMaterial";
GameLib.D3.Material.TYPE_MESH_PHONG = "MeshPhongMaterial"; GameLib.D3.Material.MATERIAL_TYPE_PHONG = "MeshPhongMaterial";
GameLib.D3.Material.TYPE_MESH_STANDARD = "MeshStandardMaterial"; GameLib.D3.Material.MATERIAL_TYPE_STANDARD = "MeshStandardMaterial";
GameLib.D3.Material.TYPE_POINTS = "PointsMaterial"; GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial";
GameLib.D3.Material.TYPE_SPRITE = "SpriteMaterial"; GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial";
GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial"; GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial";
/** /**
* Creates an instance Material from a GameLib.D3.Material * Material instance
* @param apiMaterial GameLib.D3.Material * @returns {*}
* @param graphics GameLib.D3.Graphics
* @param gameLibScene GameLib.D3.Scene
*/ */
GameLib.D3.Material.CreateInstanceMaterial = function( GameLib.D3.Material.prototype.createInstance = function() {
apiMaterial,
graphics,
gameLibScene
) {
var maps = apiMaterial.maps;
var gameLibTextures = []; var instance = null;
for (var map in maps) {
if (maps.hasOwnProperty(map)) {
if (maps[map]) {
var instanceMapId = null;
if (map == 'alpha') { if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
instanceMapId = 'alhpaMap';
}
if (map == 'ao') { instance = new this.graphics.instance.MeshStandardMaterial({
instanceMapId = 'aoMap'; name: this.name,
} opacity: this.opacity,
transparent: this.transparent,
if (map == 'bump') { blending: this.blending,
instanceMapId = 'bumpMap'; blendSrc: this.blendSrc,
} blendDst: this.blendDst,
blendEquation: this.blendEquation,
if (map == 'displacement') { depthTest: this.depthTest,
instanceMapId = 'displacementMap'; depthFunc: this.depthFunc,
} depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
if (map == 'emissive') { polygonOffsetFactor: this.polygonOffsetFactor,
instanceMapId = 'emissiveMap'; polygonOffsetUnits: this.polygonOffsetUnits,
} alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
if (map == 'environment') { clipShadows: this.clipShadows,
instanceMapId = 'envMap'; overdraw: this.overdraw,
} visible: this.visible,
side: this.side,
if (map == 'light') { color: new this.graphics.instance.Color(
instanceMapId = 'lightMap'; this.color.r,
} this.color.g,
this.color.b
if (map == 'specular') {
instanceMapId = 'specularMap';
}
if (map == 'diffuse') {
instanceMapId = 'map';
}
if (map == 'roughness') {
instanceMapId = 'roughnessMap';
}
if (map == 'metalness') {
instanceMapId = 'metalnessMap';
}
if (instanceMapId == null) {
console.warn("unsupported map type : " + map);
}
var apiTexture = maps[map];
var apiImage = apiTexture.image;
var gameLibTexture = new GameLib.D3.Texture(
apiTexture.id,
apiTexture.name,
new GameLib.D3.Image(
apiImage.id,
apiImage.textureLink,
apiImage.filename,
apiImage.size,
apiImage.contentType
),
apiTexture.wrapS,
apiTexture.wrapT,
new GameLib.D3.Vector2(
apiTexture.repeat.x,
apiTexture.repeat.y
),
apiTexture.data,
apiTexture.format,
apiTexture.mapping,
apiTexture.magFilter,
apiTexture.minFilter,
apiTexture.textureType,
apiTexture.anisotropy,
new GameLib.D3.Vector2(
apiTexture.offset.x,
apiTexture.offset.y
),
apiTexture.generateMipmaps,
apiTexture.flipY,
apiTexture.mipmaps,
apiTexture.unpackAlignment,
apiTexture.premultiplyAlpha,
apiTexture.encoding,
instanceMapId
);
gameLibTextures.push(gameLibTexture);
}
}
}
var gameLibMaterial = new GameLib.D3.Material(
apiMaterial.id,
apiMaterial.name,
apiMaterial.materialType,
apiMaterial.opacity,
apiMaterial.side,
apiMaterial.transparent,
gameLibTextures,
new GameLib.D3.Color(
apiMaterial.specular.r,
apiMaterial.specular.g,
apiMaterial.specular.b,
apiMaterial.specular.a
),
apiMaterial.lightMapIntensity,
apiMaterial.aoMapIntensity,
new GameLib.D3.Color(
apiMaterial.color.r,
apiMaterial.color.g,
apiMaterial.color.b,
apiMaterial.color.a
),
new GameLib.D3.Color(
apiMaterial.emissive.r,
apiMaterial.emissive.g,
apiMaterial.emissive.b,
apiMaterial.emissive.a
),
apiMaterial.emissiveIntensity,
apiMaterial.combine,
apiMaterial.shininess,
apiMaterial.reflectivity,
apiMaterial.refractionRatio,
apiMaterial.fog,
apiMaterial.wireframe,
apiMaterial.wireframeLineWidth,
apiMaterial.wireframeLineCap,
apiMaterial.wireframeLineJoin,
apiMaterial.vertexColors,
apiMaterial.skinning,
apiMaterial.morphTargets,
apiMaterial.morphNormals,
apiMaterial.lineWidth,
apiMaterial.lineCap,
apiMaterial.lineJoin,
apiMaterial.dashSize,
apiMaterial.gapWidth,
apiMaterial.blending,
apiMaterial.blendSrc,
apiMaterial.blendDst,
apiMaterial.blendEquation,
apiMaterial.depthTest,
apiMaterial.depthFunc,
apiMaterial.depthWrite,
apiMaterial.polygonOffset,
apiMaterial.polygonOffsetFactor,
apiMaterial.polygonOffsetUnits,
apiMaterial.alphaTest,
apiMaterial.clippingPlanes,
apiMaterial.clipShadows,
apiMaterial.visible,
apiMaterial.overdraw,
apiMaterial.shading,
apiMaterial.bumpScale,
apiMaterial.normalScale,
apiMaterial.displacementScale,
apiMaterial.displacementBias,
apiMaterial.roughness,
apiMaterial.metalness,
apiMaterial.pointSize,
apiMaterial.pointSizeAttenuation,
apiMaterial.spriteRotation,
apiMaterial.envMapIntensity
);
var instanceMaterial = null;
if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) {
instanceMaterial = new graphics.instance.MeshStandardMaterial({
name: gameLibMaterial.name,
opacity: gameLibMaterial.opacity,
transparent: gameLibMaterial.transparent,
blending: gameLibMaterial.blending,
blendSrc: gameLibMaterial.blendSrc,
blendDst: gameLibMaterial.blendDst,
blendEquation: gameLibMaterial.blendEquation,
depthTest: gameLibMaterial.depthTest,
depthFunc: gameLibMaterial.depthFunc,
depthWrite: gameLibMaterial.depthWrite,
polygonOffset: gameLibMaterial.polygonOffset,
polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor,
polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits,
alphaTest: gameLibMaterial.alphaTest,
clippingPlanes: gameLibMaterial.clippingPlanes,
clipShadows: gameLibMaterial.clipShadows,
overdraw: gameLibMaterial.overdraw,
visible: gameLibMaterial.visible,
side: gameLibMaterial.side,
color: new graphics.instance.Color(
gameLibMaterial.color.r,
gameLibMaterial.color.g,
gameLibMaterial.color.b
), ),
roughness: gameLibMaterial.roughness, roughness: this.roughness,
metalness: gameLibMaterial.metalness, metalness: this.metalness,
lightMapIntensity: gameLibMaterial.lightMapIntensity, lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: gameLibMaterial.aoMapIntensity, aoMapIntensity: this.aoMapIntensity,
emissive: new graphics.instance.Color( emissive: new this.graphics.instance.Color(
gameLibMaterial.emissive.r, this.emissive.r,
gameLibMaterial.emissive.g, this.emissive.g,
gameLibMaterial.emissive.b this.emissive.b
), ),
emissiveIntensity: gameLibMaterial.emissiveIntensity, emissiveIntensity: this.emissiveIntensity,
bumpScale: gameLibMaterial.bumpScale, bumpScale: this.bumpScale,
normalScale: gameLibMaterial.normalScale, normalScale: this.normalScale,
displacementScale: gameLibMaterial.displacementScale, displacementScale: this.displacementScale,
refractionRatio: gameLibMaterial.refractionRatio, refractionRatio: this.refractionRatio,
fog: gameLibMaterial.fog, fog: this.fog,
shading: gameLibMaterial.shading, shading: this.shading,
wireframe: gameLibMaterial.wireframe, wireframe: this.wireframe,
wireframeLinewidth: gameLibMaterial.wireframeLineWidth, wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: gameLibMaterial.wireframeLineCap, wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: gameLibMaterial.wireframeLineJoin, wireframeLinejoin: this.wireframeLineJoin,
vertexColors: gameLibMaterial.vertexColors, vertexColors: this.vertexColors,
skinning: gameLibMaterial.skinning, skinning: this.skinning,
morphTargets: gameLibMaterial.morphTargets, morphTargets: this.morphTargets,
morphNormals: gameLibMaterial.morphNormals morphNormals: this.morphNormals
}); });
} else if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_PHONG) { } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
instanceMaterial = new graphics.instance.MeshPhongMaterial({ instance = new this.graphics.instance.MeshPhongMaterial({
name: gameLibMaterial.name, name: this.name,
opacity: gameLibMaterial.opacity, opacity: this.opacity,
transparent: gameLibMaterial.transparent, transparent: this.transparent,
blending: gameLibMaterial.blending, blending: this.blending,
blendSrc: gameLibMaterial.blendSrc, blendSrc: this.blendSrc,
blendDst: gameLibMaterial.blendDst, blendDst: this.blendDst,
blendEquation: gameLibMaterial.blendEquation, blendEquation: this.blendEquation,
depthTest: gameLibMaterial.depthTest, depthTest: this.depthTest,
depthFunc: gameLibMaterial.depthFunc, depthFunc: this.depthFunc,
depthWrite: gameLibMaterial.depthWrite, depthWrite: this.depthWrite,
polygonOffset: gameLibMaterial.polygonOffset, polygonOffset: this.polygonOffset,
polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor, polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits, polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: gameLibMaterial.alphaTest, alphaTest: this.alphaTest,
clippingPlanes: gameLibMaterial.clippingPlanes, clippingPlanes: this.clippingPlanes,
clipShadows: gameLibMaterial.clipShadows, clipShadows: this.clipShadows,
overdraw: gameLibMaterial.overdraw, overdraw: this.overdraw,
visible: gameLibMaterial.visible, visible: this.visible,
side: gameLibMaterial.side, side: this.side,
color: new graphics.instance.Color( color: new this.graphics.instance.Color(
gameLibMaterial.color.r, this.color.r,
gameLibMaterial.color.g, this.color.g,
gameLibMaterial.color.b this.color.b
), ),
specular: new graphics.instance.Color( specular: new this.graphics.instance.Color(
gameLibMaterial.specular.r, this.specular.r,
gameLibMaterial.specular.g, this.specular.g,
gameLibMaterial.specular.b this.specular.b
), ),
shininess: gameLibMaterial.shininess, shininess: this.shininess,
lightMapIntensity: gameLibMaterial.lightMapIntensity, lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: gameLibMaterial.aoMapIntensity, aoMapIntensity: this.aoMapIntensity,
emissive: new graphics.instance.Color( emissive: new this.graphics.instance.Color(
gameLibMaterial.emissive.r, this.emissive.r,
gameLibMaterial.emissive.g, this.emissive.g,
gameLibMaterial.emissive.b this.emissive.b
), ),
emissiveIntensity: gameLibMaterial.emissiveIntensity, emissiveIntensity: this.emissiveIntensity,
bumpScale: gameLibMaterial.bumpScale, bumpScale: this.bumpScale,
normalScale: gameLibMaterial.normalScale, normalScale: this.normalScale,
displacementScale: gameLibMaterial.displacementScale, displacementScale: this.displacementScale,
combine: gameLibMaterial.combine, combine: this.combine,
refractionRatio: gameLibMaterial.refractionRatio, refractionRatio: this.refractionRatio,
fog: gameLibMaterial.fog, fog: this.fog,
shading: gameLibMaterial.shading, shading: this.shading,
wireframe: gameLibMaterial.wireframe, wireframe: this.wireframe,
wireframeLinewidth: gameLibMaterial.wireframeLineWidth, wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: gameLibMaterial.wireframeLineCap, wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: gameLibMaterial.wireframeLineJoin, wireframeLinejoin: this.wireframeLineJoin,
vertexColors: gameLibMaterial.vertexColors, vertexColors: this.vertexColors,
skinning: gameLibMaterial.skinning, skinning: this.skinning,
morphTargets: gameLibMaterial.morphTargets, morphTargets: this.morphTargets,
morphNormals: gameLibMaterial.morphNormals morphNormals: this.morphNormals
}); });
} else { } else {
console.log("material type is not implemented yet: " + apiMaterial.materialType + " - material indexes could be screwed up"); console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up");
} }
/** return instance;
* Here we need to notify the scene of all the textures which need to be created once image loading for the
* textures are complete. (this download process will be started at a later stage)
* The Notification objects take care of applying the loaded instance texture(s) to the instance material
* once it is loaded
*/
for (var t = 0; t < gameLibTextures.length; t++) {
gameLibScene.notify(
new GameLib.D3.Texture.Notification(
gameLibTextures[t],
instanceMaterial
)
)
}
return instanceMaterial;
}; };

View File

@ -1,54 +1,110 @@
/** /**
* Mesh Superset * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created
* @param id * @param id String
* @param path * @param name String
* @param name * @param graphics GameLib.D3.Graphics
* @param meshType * @param computeNormals Boolean
* @param vertices * @param apiMesh GameLib.D3.Mesh.API
* @param faces
* @param skeleton
* @param faceVertexUvs
* @param skinIndices
* @param skinWeights
* @param materials
* @param position
* @param quaternion
* @param rotation
* @param scale
* @param up
* @param physics
* @param parentMeshId
* @param parentSceneId
* @constructor * @constructor
*/ */
GameLib.D3.Mesh = function( GameLib.D3.Mesh = function(
id, id,
path,
name, name,
graphics,
computeNormals,
apiMesh
) {
for (var property in apiMesh) {
if (apiMesh.hasOwnProperty(property)) {
this[property] = apiMesh[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.computeNormals = computeNormals;
this.instance = this.createInstance();
};
/**
* Raw Mesh API object - should always correspond with the Mesh Schema
* @param id
* @param meshType
* @param name
* @param vertices GameLib.D3.Vertex[]
* @param faces GameLib.D3.TriangleFace[]
* @param faceVertexUvs
* @param materials GameLib.D3.Material.API[]
* @param parentMeshId
* @param parentSceneId
* @param skeleton
* @param skinIndices
* @param skinWeights
* @param position GameLib.D3.Vector3
* @param quaternion GameLib.D3.Vector4
* @param rotation GameLib.D3.Vector3
* @param scale GameLib.D3.Vector3
* @param up
* @constructor
*/
GameLib.D3.Mesh.API = function(
id,
meshType, meshType,
name,
vertices, vertices,
faces, faces,
skeleton,
faceVertexUvs, faceVertexUvs,
materials,
parentMeshId,
parentSceneId,
skeleton,
skinIndices, skinIndices,
skinWeights, skinWeights,
materials,
position, position,
quaternion, quaternion,
rotation, rotation,
scale, scale,
up, up
physics,
parentMeshId,
parentSceneId
) { ) {
this.id = GameLib.D3.Tools.RandomId(); if (GameLib.D3.Utils.UndefinedOrNull(id)) {
this.path = path; id = GameLib.D3.Tools.RandomId();
this.name = name; }
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(meshType)) {
meshType = GameLib.D3.Mesh.TYPE_NORMAL;
}
this.meshType = meshType; this.meshType = meshType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Mesh (' + meshType + ')';
}
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(vertices)) {
throw new Error('Cannot create a mesh with no vertices');
}
this.vertices = vertices; this.vertices = vertices;
if (GameLib.D3.Utils.UndefinedOrNull(faces)) {
throw new Error('Cannot create a mesh with no faces');
}
this.faces = faces; this.faces = faces;
if (typeof parentMeshId == 'undefined') {
parentMeshId = null;
}
this.parentMeshId = parentMeshId;
if (typeof parentSceneId == 'undefined') {
parentSceneId = null;
}
this.parentSceneId = parentSceneId;
if (typeof skeleton == 'undefined') { if (typeof skeleton == 'undefined') {
skeleton = null; skeleton = null;
} }
@ -98,15 +154,8 @@ GameLib.D3.Mesh = function(
up = new GameLib.D3.Vector3(0,1,0); up = new GameLib.D3.Vector3(0,1,0);
} }
this.up = up; this.up = up;
this.physics = physics;
this.parentMeshId = parentMeshId;
this.parentSceneId = parentSceneId;
}; };
/** /**
* Mesh Type * Mesh Type
* @type {number} * @type {number}
@ -114,36 +163,149 @@ GameLib.D3.Mesh = function(
GameLib.D3.Mesh.TYPE_NORMAL = 0; GameLib.D3.Mesh.TYPE_NORMAL = 0;
GameLib.D3.Mesh.TYPE_SKINNED = 1; GameLib.D3.Mesh.TYPE_SKINNED = 1;
/** /**
* Creates a THREE Mesh from GameLib.D3.Mesh * Creates a mesh instance
* @param gameLibMesh GameLib.D3.Mesh
* @param instanceGeometry
* @param instanceMaterial
* @param graphics
* @returns {*}
*/ */
GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) { GameLib.D3.Mesh.prototype.createInstance = function() {
var threeMesh = null; var instance = null;
if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { var instanceGeometry = new this.graphics.instance.Geometry();
threeMesh = new graphics.instance.Mesh(instanceGeometry, instanceMaterial);
/**
* Setup vertices
*/
for (var v = 0; v < this.vertices.length; v++) {
instanceGeometry.vertices.push(
new this.graphics.instance.Vector3(
this.vertices[v].position.x,
this.vertices[v].position.y,
this.vertices[v].position.z
)
)
} }
if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { /**
* Setup faces
*/
for (var f = 0; f < this.faces.length; f++) {
var bones = gameLibMesh.skeleton.bones; var face = new this.graphics.instance.Face3(
this.faces[f].v0,
this.faces[f].v1,
this.faces[f].v2,
new this.graphics.instance.Vector3(
this.faces[f].normal.x,
this.faces[f].normal.y,
this.faces[f].normal.z
),
new this.graphics.instance.Color(
this.faces[f].color.r,
this.faces[f].color.g,
this.faces[f].color.b
),
this.faces[f].materialIndex
);
var skinIndices = gameLibMesh.skinIndices; face.vertexColors = [
new this.graphics.instance.Color(
this.faces[f].vertexColors[0].r,
this.faces[f].vertexColors[0].g,
this.faces[f].vertexColors[0].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[1].r,
this.faces[f].vertexColors[1].g,
this.faces[f].vertexColors[1].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[2].r,
this.faces[f].vertexColors[2].g,
this.faces[f].vertexColors[2].b
)
];
var skinWeights = gameLibMesh.skinWeights; face.normal = new this.graphics.instance.Vector3(
this.faces[f].normal.x,
this.faces[f].normal.y,
this.faces[f].normal.z
);
var threeBones = []; face.vertexNormals = [
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[0].x,
this.faces[f].vertexNormals[0].y,
this.faces[f].vertexNormals[0].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[1].x,
this.faces[f].vertexNormals[1].y,
this.faces[f].vertexNormals[1].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[2].x,
this.faces[f].vertexNormals[2].y,
this.faces[f].vertexNormals[2].z
)
];
instanceGeometry.faces.push(face);
}
instanceGeometry.faceVertexUvs = [];
/**
* Setup face UVs
*/
for (var fm = 0; fm < this.faceVertexUvs.length; fm++) {
var faceMaterialVertexUvs = this.faceVertexUvs[fm];
instanceGeometry.faceVertexUvs[fm] = [];
for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
instanceGeometry.faceVertexUvs[fm][fuv] = [];
instanceGeometry.faceVertexUvs[fm][fuv].push(
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][0].x,
faceMaterialVertexUvs[fuv][0].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][1].x,
faceMaterialVertexUvs[fuv][1].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][2].x,
faceMaterialVertexUvs[fuv][2].y
)
);
}
}
/**
* Re-calculate normals (if we have to)
* @type {Array}
*/
if (this.computeNormals) {
instanceGeometry.computeFaceNormals();
instanceGeometry.computeVertexNormals();
}
var instanceMaterial = this.materials[0].instance;
if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) {
instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial);
}
if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) {
var bones = this.skeleton.bones;
var instanceBones = [];
for (var bi = 0; bi < bones.length; bi++) { for (var bi = 0; bi < bones.length; bi++) {
var bone = new graphics.instance.Bone(); var bone = new this.graphics.instance.Bone();
bone.name = bones[bi].name; bone.name = bones[bi].name;
@ -168,7 +330,7 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins
bone.up.y = bones[bi].up.y; bone.up.y = bones[bi].up.y;
bone.up.z = bones[bi].up.z; bone.up.z = bones[bi].up.z;
threeBones.push(bone); instanceBones.push(bone);
} }
/** /**
@ -176,20 +338,20 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins
*/ */
for (var br = 0; br < bones.length; br++) { for (var br = 0; br < bones.length; br++) {
for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) { for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) {
threeBones[br].add(threeBones[bones[br].childBoneIds[cbi]]); instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]);
} }
} }
/** /**
* Setup bones (indexes) * Setup bones (indexes)
*/ */
for (var si = 0; si < skinIndices.length; si++) { for (var si = 0; si < this.skinIndices.length; si++) {
instanceGeometry.skinIndices.push( instanceGeometry.skinIndices.push(
new graphics.instance.Vector4( new this.graphics.instance.Vector4(
skinIndices[si].x, this.skinIndices[si].x,
skinIndices[si].y, this.skinIndices[si].y,
skinIndices[si].z, this.skinIndices[si].z,
skinIndices[si].w this.skinIndices[si].w
) )
); );
} }
@ -197,45 +359,66 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins
/** /**
* Setup bones (weights) * Setup bones (weights)
*/ */
for (var sw = 0; sw < skinWeights.length; sw++) { for (var sw = 0; sw < this.skinWeights.length; sw++) {
instanceGeometry.skinWeights.push( instanceGeometry.skinWeights.push(
new graphics.instance.Vector4( new this.graphics.instance.Vector4(
skinWeights[sw].x, this.skinWeights[sw].x,
skinWeights[sw].y, this.skinWeights[sw].y,
skinWeights[sw].z, this.skinWeights[sw].z,
skinWeights[sw].w this.skinWeights[sw].w
) )
); );
} }
threeMesh = new graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial);
var skeleton = new graphics.instance.Skeleton(threeBones); var skeleton = new this.graphics.instance.Skeleton(instanceBones);
skeleton.useVertexTexture = gameLibMesh.skeleton.useVertexTexture; skeleton.useVertexTexture = this.skeleton.useVertexTexture;
for (var i = 0; i < bones.length; i++) { for (var i = 0; i < bones.length; i++) {
if (bones[i].parentBoneId === null) { if (bones[i].parentBoneId === null) {
threeMesh.add(threeBones[i]); instance.add(instanceBones[i]);
break; break;
} }
} }
threeMesh.bind(skeleton); instance.bind(skeleton);
threeMesh.pose(); instance.pose();
threeMesh.skeleton.skeletonHelper = new graphics.instance.SkeletonHelper(threeMesh); instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance);
threeMesh.skeleton.skeletonHelper.material.linewidth = 5; instance.skeleton.skeletonHelper.material.linewidth = 5;
} }
if (threeMesh == null) { if (instance == null) {
console.log('cannot handle meshes of type ' + gameLibMesh.meshType + ' yet.'); console.log('cannot handle meshes of type ' + this.meshType + ' yet.');
} }
gameLibMesh.threeMeshId = threeMesh.id; this.threeMeshId = instance.id;
return threeMesh; instance.name = this.name;
instance.gameLibObject = this;
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.rotation.x = this.rotation.x;
instance.rotation.y = this.rotation.y;
instance.rotation.z = this.rotation.z;
instance.scale.x = this.scale.x;
instance.scale.y = this.scale.y;
instance.scale.z = this.scale.z;
instance.quaternion.x = this.quaternion.x;
instance.quaternion.y = this.quaternion.y;
instance.quaternion.z = this.quaternion.z;
instance.quaternion.w = this.quaternion.w;
return instance;
}; };
GameLib.D3.prototype.invertWindingOrder = function(triangles) { GameLib.D3.prototype.invertWindingOrder = function(triangles) {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,111 @@
/**
* We have a mapping between GameLib.D3.Texture and the Instance material map.
* Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap
* @param graphics
* @returns {{alpha: {texture: null, instanceMapId: string}, ao: {texture: null, instanceMapId: string}, bump: {texture: null, instanceMapId: string}, diffuse: {texture: null, instanceMapId: string}, displacement: {texture: null, instanceMapId: string}, emissive: {texture: null, instanceMapId: string}, environment: {texture: null, instanceMapId: string}, light: {texture: null, instanceMapId: string}, metalness: {texture: null, instanceMapId: string}, normal: {texture: null, instanceMapId: string}, roughness: {texture: null, instanceMapId: string}, specular: {texture: null, instanceMapId: string}}}
* @constructor
*/
GameLib.D3.TextureMapTemplate = function(
graphics
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
return {
alpha: {
texture : null,
instanceMapId: 'alhpaMap'
},
ao: {
texture : null,
instanceMapId: 'aoMap'
},
bump: {
texture : null,
instanceMapId: 'bumpMap'
},
diffuse: {
texture : null,
instanceMapId: 'map'
},
displacement: {
texture : null,
instanceMapId: 'displacementMap'
},
emissive: {
texture : null,
instanceMapId: 'emissiveMap'
},
environment: {
texture : null,
instanceMapId: 'envMap'
},
light: {
texture : null,
instanceMapId: 'lightMap'
},
metalness: {
texture : null,
instanceMapId: 'metalnessMap'
},
normal: {
texture : null,
instanceMapId: 'normalMap'
},
roughness: {
texture : null,
instanceMapId: 'roughnessMap'
},
specular: {
texture : null,
instanceMapId: 'specularMap'
}
};
};
/**
* Raw API Texture Map Template - should match the contents of the Material Schema (maps property)
* @returns {{alpha: {texture: null}, ao: {texture: null}, bump: {texture: null}, diffuse: {texture: null}, displacement: {texture: null}, emissive: {texture: null}, environment: {texture: null}, light: {texture: null}, metalness: {texture: null}, normal: {texture: null}, roughness: {texture: null}, specular: {texture: null}}}
* @constructor
*/
GameLib.D3.TextureMapTemplate.API = function() {
return {
alpha: {
texture : null
},
ao: {
texture : null
},
bump: {
texture : null
},
diffuse: {
texture : null
},
displacement: {
texture : null
},
emissive: {
texture : null
},
environment: {
texture : null
},
light: {
texture : null
},
metalness: {
texture : null
},
normal: {
texture : null
},
roughness: {
texture : null
},
specular: {
texture : null
}
};
};

View File

@ -1,8 +1,56 @@
/** /**
* Texture Superset * 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 parentMaterial GameLib.D3.Material
* @param parentMaterialInstanceMapId String
* @constructor
*/
GameLib.D3.Texture = function(
apiTexture,
graphics,
parentMaterial,
parentMaterialInstanceMapId
) {
for (var property in apiTexture) {
if (apiTexture.hasOwnProperty(property)) {
this[property] = apiTexture[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentMaterial = parentMaterial;
this.parentMaterialInstanceMapId = parentMaterialInstanceMapId;
this.imageFactory = GameLib.D3.ImageFactory(graphics);
this.imageData = this.imageFactory(apiTexture.imagePath);
this.imageInstance = null;
this.instance = null;
this.imageData.then(
function (imageInstance){
this.imageInstance = imageInstance;
this.instance = this.createInstance();
this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance;
this.parentMaterial.needsUpdate = true;
}.bind(this),
function onRejected() {
}
);
};
/**
* Raw Texture API object - should always correspond with the Texture Schema
* @param id * @param id
* @param name * @param name
* @param image * @param imagePath
* @param wrapS * @param wrapS
* @param wrapT * @param wrapT
* @param repeat * @param repeat
@ -20,13 +68,12 @@
* @param unpackAlignment * @param unpackAlignment
* @param premultiplyAlpha * @param premultiplyAlpha
* @param encoding * @param encoding
* @param instanceMapId
* @constructor * @constructor
*/ */
GameLib.D3.Texture = function( GameLib.D3.Texture.API = function(
id, id,
name, name,
image, imagePath,
wrapS, wrapS,
wrapT, wrapT,
repeat, repeat,
@ -43,12 +90,22 @@ GameLib.D3.Texture = function(
mipmaps, mipmaps,
unpackAlignment, unpackAlignment,
premultiplyAlpha, premultiplyAlpha,
encoding, encoding
instanceMapId
) { ) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id; this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Texture (' + image.name + ')';
}
this.name = name; this.name = name;
this.image = image;
if (GameLib.D3.Utils.UndefinedOrNull(imagePath)) {
throw new Error('Cannot create textures with no image path set');
}
this.imagePath = imagePath;
if (typeof wrapS == 'undefined') { if (typeof wrapS == 'undefined') {
wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING; wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING;
@ -134,10 +191,9 @@ GameLib.D3.Texture = function(
encoding = GameLib.D3.Texture.TYPE_LINEAR_ENCODING; encoding = GameLib.D3.Texture.TYPE_LINEAR_ENCODING;
} }
this.encoding = encoding; this.encoding = encoding;
this.instanceMapId = instanceMapId;
}; };
/** /**
* Texture Formats * Texture Formats
* @type {number} * @type {number}
@ -208,18 +264,33 @@ GameLib.D3.Texture.TYPE_RGBM16_ENCODING = 3005;
GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256. GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
/** /**
* A Notification object stores an association between a gameLibTexture and a instance material map. * Creates an instance of our texture object
* @param instanceMaterial * @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture}
* @param gameLibTexture
* @constructor
*/ */
GameLib.D3.Texture.Notification = function( GameLib.D3.Texture.prototype.createInstance = function() {
gameLibTexture,
instanceMaterial var instance = new this.graphics.instance.Texture(
) { this.imageInstance,
this.gameLibTexture = gameLibTexture; this.mapping,
this.instanceMaterial = instanceMaterial; 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.flipY = this.flipY;
instance.offset.x = this.offset.x;
instance.offset.y = this.offset.y;
instance.mipmaps = this.mipmaps;
instance.unpackAlignment = this.unpackAlignment;
instance.premultiplyAlpha = this.premultiplyAlpha;
instance.needsUpdate = true;
return instance;
}; };