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-minify": "0.0.14",
"gulp-sort": "^2.0.0",
"lodash": "^4.17.1",
"lodash": "^4.17.2",
"q": "^1.4.1",
"three": "^0.81.2"
},

View File

@ -7,6 +7,7 @@ if (typeof GameLib.D3 == 'undefined') {
}
if (typeof Q == 'undefined') {
if (typeof require == 'undefined') {
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');
@ -16,10 +17,11 @@ if (typeof Q == 'undefined') {
}
if (typeof _ == 'undefined') {
if (typeof require == 'undefined') {
console.warn('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
* @param id
* @param textureLink
* @param filename
* @param size
* @param contentType
@ -9,17 +71,17 @@
*/
GameLib.D3.Image = function(
id,
textureLink,
filename,
size,
contentType
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
this.filename = filename;
this.textureLink = textureLink;
if (typeof size == 'undefined') {
size = 0;
}
@ -42,4 +104,4 @@ GameLib.D3.Image = function(
}
}
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 lightType
* @param name
@ -17,7 +41,7 @@
* @param penumbra
* @constructor
*/
GameLib.D3.Light = function(
GameLib.D3.Light.API = function(
id,
lightType,
name,
@ -34,9 +58,21 @@ GameLib.D3.Light = function(
angle,
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;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Light (' + lightType + ')';
}
this.name = name;
this.color = color;
this.intensity = intensity;
@ -89,4 +125,56 @@ GameLib.D3.Light = function(
penumbra = 0;
}
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 name
* @param materialType
* @param name
* @param opacity
* @param side
* @param transparent
@ -59,10 +83,10 @@
* @param envMapIntensity
* @constructor
*/
GameLib.D3.Material = function(
GameLib.D3.Material.API = function(
id,
name,
materialType,
name,
opacity,
side,
transparent,
@ -118,13 +142,21 @@ GameLib.D3.Material = function(
spriteRotation,
envMapIntensity
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
this.name = name;
if (typeof materialType == 'undefined') {
materialType = GameLib.D3.Material.TYPE_MESH_STANDARD;
materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD;
}
this.materialType = materialType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Material (' + materialType + ')';
}
this.name = name;
if (typeof opacity == 'undefined') {
opacity = 1.0;
}
@ -141,20 +173,7 @@ GameLib.D3.Material = function(
this.transparent = transparent;
if (typeof maps == 'undefined') {
maps = {
alpha: null,
ao: null,
bump: null,
diffuse: null,
displacement: null,
emissive: null,
environment: null,
light: null,
metalness: null,
normal: null,
roughness: null,
specular: null
};
maps = new GameLib.D3.TextureMapTemplate.API();
}
this.maps = maps;
@ -409,22 +428,6 @@ GameLib.D3.Material = function(
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
* @type {number}
@ -509,337 +512,140 @@ GameLib.D3.Material.TYPE_SMOOTH_SHADING = 2;
* Material Type
* @type {string}
*/
GameLib.D3.Material.TYPE_LINE_BASIC = "LineBasicMaterial";
GameLib.D3.Material.TYPE_LINE_DASHED = "LineDashedMaterial";
GameLib.D3.Material.TYPE_MESH_BASIC = "MeshBasicMaterial";
GameLib.D3.Material.TYPE_MESH_DEPTH = "MeshDepthMaterial";
GameLib.D3.Material.TYPE_MESH_LAMBERT = "MeshLambertMaterial";
GameLib.D3.Material.TYPE_MESH_NORMAL = "MeshNormalMaterial";
GameLib.D3.Material.TYPE_MESH_PHONG = "MeshPhongMaterial";
GameLib.D3.Material.TYPE_MESH_STANDARD = "MeshStandardMaterial";
GameLib.D3.Material.TYPE_POINTS = "PointsMaterial";
GameLib.D3.Material.TYPE_SPRITE = "SpriteMaterial";
GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial";
GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC = "LineBasicMaterial";
GameLib.D3.Material.MATERIAL_TYPE_LINE_DASHED = "LineDashedMaterial";
GameLib.D3.Material.MATERIAL_TYPE_BASIC = "MeshBasicMaterial";
GameLib.D3.Material.MATERIAL_TYPE_DEPTH = "MeshDepthMaterial";
GameLib.D3.Material.MATERIAL_TYPE_LAMBERT = "MeshLambertMaterial";
GameLib.D3.Material.MATERIAL_TYPE_NORMAL = "MeshNormalMaterial";
GameLib.D3.Material.MATERIAL_TYPE_PHONG = "MeshPhongMaterial";
GameLib.D3.Material.MATERIAL_TYPE_STANDARD = "MeshStandardMaterial";
GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial";
GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial";
GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial";
/**
* Creates an instance Material from a GameLib.D3.Material
* @param apiMaterial GameLib.D3.Material
* @param graphics GameLib.D3.Graphics
* @param gameLibScene GameLib.D3.Scene
* Material instance
* @returns {*}
*/
GameLib.D3.Material.CreateInstanceMaterial = function(
apiMaterial,
graphics,
gameLibScene
) {
var maps = apiMaterial.maps;
GameLib.D3.Material.prototype.createInstance = function() {
var gameLibTextures = [];
for (var map in maps) {
if (maps.hasOwnProperty(map)) {
if (maps[map]) {
var instanceMapId = null;
var instance = null;
if (map == 'alpha') {
instanceMapId = 'alhpaMap';
}
if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
if (map == 'ao') {
instanceMapId = 'aoMap';
}
if (map == 'bump') {
instanceMapId = 'bumpMap';
}
if (map == 'displacement') {
instanceMapId = 'displacementMap';
}
if (map == 'emissive') {
instanceMapId = 'emissiveMap';
}
if (map == 'environment') {
instanceMapId = 'envMap';
}
if (map == 'light') {
instanceMapId = 'lightMap';
}
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
instance = new this.graphics.instance.MeshStandardMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
roughness: gameLibMaterial.roughness,
metalness: gameLibMaterial.metalness,
lightMapIntensity: gameLibMaterial.lightMapIntensity,
aoMapIntensity: gameLibMaterial.aoMapIntensity,
emissive: new graphics.instance.Color(
gameLibMaterial.emissive.r,
gameLibMaterial.emissive.g,
gameLibMaterial.emissive.b
roughness: this.roughness,
metalness: this.metalness,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: new this.graphics.instance.Color(
this.emissive.r,
this.emissive.g,
this.emissive.b
),
emissiveIntensity: gameLibMaterial.emissiveIntensity,
bumpScale: gameLibMaterial.bumpScale,
normalScale: gameLibMaterial.normalScale,
displacementScale: gameLibMaterial.displacementScale,
refractionRatio: gameLibMaterial.refractionRatio,
fog: gameLibMaterial.fog,
shading: gameLibMaterial.shading,
wireframe: gameLibMaterial.wireframe,
wireframeLinewidth: gameLibMaterial.wireframeLineWidth,
wireframeLinecap: gameLibMaterial.wireframeLineCap,
wireframeLinejoin: gameLibMaterial.wireframeLineJoin,
vertexColors: gameLibMaterial.vertexColors,
skinning: gameLibMaterial.skinning,
morphTargets: gameLibMaterial.morphTargets,
morphNormals: gameLibMaterial.morphNormals
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
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({
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
instance = new this.graphics.instance.MeshPhongMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
specular: new graphics.instance.Color(
gameLibMaterial.specular.r,
gameLibMaterial.specular.g,
gameLibMaterial.specular.b
specular: new this.graphics.instance.Color(
this.specular.r,
this.specular.g,
this.specular.b
),
shininess: gameLibMaterial.shininess,
lightMapIntensity: gameLibMaterial.lightMapIntensity,
aoMapIntensity: gameLibMaterial.aoMapIntensity,
emissive: new graphics.instance.Color(
gameLibMaterial.emissive.r,
gameLibMaterial.emissive.g,
gameLibMaterial.emissive.b
shininess: this.shininess,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: new this.graphics.instance.Color(
this.emissive.r,
this.emissive.g,
this.emissive.b
),
emissiveIntensity: gameLibMaterial.emissiveIntensity,
bumpScale: gameLibMaterial.bumpScale,
normalScale: gameLibMaterial.normalScale,
displacementScale: gameLibMaterial.displacementScale,
combine: gameLibMaterial.combine,
refractionRatio: gameLibMaterial.refractionRatio,
fog: gameLibMaterial.fog,
shading: gameLibMaterial.shading,
wireframe: gameLibMaterial.wireframe,
wireframeLinewidth: gameLibMaterial.wireframeLineWidth,
wireframeLinecap: gameLibMaterial.wireframeLineCap,
wireframeLinejoin: gameLibMaterial.wireframeLineJoin,
vertexColors: gameLibMaterial.vertexColors,
skinning: gameLibMaterial.skinning,
morphTargets: gameLibMaterial.morphTargets,
morphNormals: gameLibMaterial.morphNormals
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
combine: this.combine,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} 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");
}
/**
* 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;
return instance;
};

View File

@ -1,54 +1,110 @@
/**
* Mesh Superset
* @param id
* @param path
* @param name
* @param meshType
* @param vertices
* @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
* Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created
* @param id String
* @param name String
* @param graphics GameLib.D3.Graphics
* @param computeNormals Boolean
* @param apiMesh GameLib.D3.Mesh.API
* @constructor
*/
GameLib.D3.Mesh = function(
id,
path,
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,
name,
vertices,
faces,
skeleton,
faceVertexUvs,
materials,
parentMeshId,
parentSceneId,
skeleton,
skinIndices,
skinWeights,
materials,
position,
quaternion,
rotation,
scale,
up,
physics,
parentMeshId,
parentSceneId
up
) {
this.id = GameLib.D3.Tools.RandomId();
this.path = path;
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(meshType)) {
meshType = GameLib.D3.Mesh.TYPE_NORMAL;
}
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;
if (GameLib.D3.Utils.UndefinedOrNull(faces)) {
throw new Error('Cannot create a mesh with no 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') {
skeleton = null;
}
@ -98,15 +154,8 @@ GameLib.D3.Mesh = function(
up = new GameLib.D3.Vector3(0,1,0);
}
this.up = up;
this.physics = physics;
this.parentMeshId = parentMeshId;
this.parentSceneId = parentSceneId;
};
/**
* Mesh Type
* @type {number}
@ -114,36 +163,149 @@ GameLib.D3.Mesh = function(
GameLib.D3.Mesh.TYPE_NORMAL = 0;
GameLib.D3.Mesh.TYPE_SKINNED = 1;
/**
* Creates a THREE Mesh from GameLib.D3.Mesh
* @param gameLibMesh GameLib.D3.Mesh
* @param instanceGeometry
* @param instanceMaterial
* @param graphics
* @returns {*}
* Creates a mesh instance
*/
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) {
threeMesh = new graphics.instance.Mesh(instanceGeometry, instanceMaterial);
var instanceGeometry = new this.graphics.instance.Geometry();
/**
* 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++) {
var bone = new graphics.instance.Bone();
var bone = new this.graphics.instance.Bone();
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.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 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)
*/
for (var si = 0; si < skinIndices.length; si++) {
for (var si = 0; si < this.skinIndices.length; si++) {
instanceGeometry.skinIndices.push(
new graphics.instance.Vector4(
skinIndices[si].x,
skinIndices[si].y,
skinIndices[si].z,
skinIndices[si].w
new this.graphics.instance.Vector4(
this.skinIndices[si].x,
this.skinIndices[si].y,
this.skinIndices[si].z,
this.skinIndices[si].w
)
);
}
@ -197,45 +359,66 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins
/**
* Setup bones (weights)
*/
for (var sw = 0; sw < skinWeights.length; sw++) {
for (var sw = 0; sw < this.skinWeights.length; sw++) {
instanceGeometry.skinWeights.push(
new graphics.instance.Vector4(
skinWeights[sw].x,
skinWeights[sw].y,
skinWeights[sw].z,
skinWeights[sw].w
new this.graphics.instance.Vector4(
this.skinWeights[sw].x,
this.skinWeights[sw].y,
this.skinWeights[sw].z,
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++) {
if (bones[i].parentBoneId === null) {
threeMesh.add(threeBones[i]);
instance.add(instanceBones[i]);
break;
}
}
threeMesh.bind(skeleton);
instance.bind(skeleton);
threeMesh.pose();
instance.pose();
threeMesh.skeleton.skeletonHelper = new graphics.instance.SkeletonHelper(threeMesh);
threeMesh.skeleton.skeletonHelper.material.linewidth = 5;
instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance);
instance.skeleton.skeletonHelper.material.linewidth = 5;
}
if (threeMesh == null) {
console.log('cannot handle meshes of type ' + gameLibMesh.meshType + ' yet.');
if (instance == null) {
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) {

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 name
* @param image
* @param imagePath
* @param wrapS
* @param wrapT
* @param repeat
@ -20,13 +68,12 @@
* @param unpackAlignment
* @param premultiplyAlpha
* @param encoding
* @param instanceMapId
* @constructor
*/
GameLib.D3.Texture = function(
GameLib.D3.Texture.API = function(
id,
name,
image,
imagePath,
wrapS,
wrapT,
repeat,
@ -43,12 +90,22 @@ GameLib.D3.Texture = function(
mipmaps,
unpackAlignment,
premultiplyAlpha,
encoding,
instanceMapId
encoding
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Texture (' + image.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') {
wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING;
@ -134,10 +191,9 @@ GameLib.D3.Texture = function(
encoding = GameLib.D3.Texture.TYPE_LINEAR_ENCODING;
}
this.encoding = encoding;
this.instanceMapId = instanceMapId;
};
/**
* Texture Formats
* @type {number}
@ -208,18 +264,33 @@ GameLib.D3.Texture.TYPE_RGBM16_ENCODING = 3005;
GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
/**
* A Notification object stores an association between a gameLibTexture and a instance material map.
* @param instanceMaterial
* @param gameLibTexture
* @constructor
* Creates an instance of our texture object
* @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture}
*/
GameLib.D3.Texture.Notification = function(
gameLibTexture,
instanceMaterial
) {
this.gameLibTexture = gameLibTexture;
this.instanceMaterial = instanceMaterial;
GameLib.D3.Texture.prototype.createInstance = function() {
var instance = new this.graphics.instance.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.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;
};