modular refactoring

beta.r3js.org
Theunis J. Botha 2016-10-25 17:57:32 +02:00
parent 92ff078277
commit 39258d2afe
9 changed files with 813 additions and 534 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ var minify = require('gulp-minify');
var plumber = require('gulp-plumber');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha');
var watch = require('gulp-watch');
gulp.task(
'build',
@ -59,6 +60,17 @@ gulp.task(
gulp.task(
'default',
['build'],
function() {}
[
'build'
],
function() {
return watch([
'src/*.js'
],
function() {
gulp.start([
'build'
]);
})
}
);

View File

@ -17,6 +17,7 @@
"gulp-istanbul": "^1.1.1",
"gulp-mocha": "^3.0.1",
"gulp-plumber": "^1.1.0",
"gulp-watch": "^4.3.10",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0"
}

37
src/game-lib-graphics.js Normal file
View File

@ -0,0 +1,37 @@
/**
* Graphics Superset
* @param graphicsType
* @param instance {THREE}
* @constructor
*/
GameLib.D3.Graphics = function(
graphicsType,
instance
) {
this.graphicsType = graphicsType;
this.instance = instance;
};
/**
* True if THREE physics
* @returns {boolean}
*/
GameLib.D3.Graphics.prototype.isThree = function() {
return (this.graphicsType == GameLib.D3.Graphics.GRAPHICS_TYPE_THREE)
};
/**
* Logs a warning and throws an error if not cannon
*/
GameLib.D3.Graphics.prototype.isNotThreeThrow = function() {
if (this.graphicsType != GameLib.D3.Graphics.GRAPHICS_TYPE_THREE) {
console.warn('Only THREE supported for this function');
throw new Error('Only THREE supported for this function');
}
};
/**
* Physics GameLib.D3.Graphics Types
* @type {number}
*/
GameLib.D3.Graphics.GRAPHICS_TYPE_THREE = 0x1;

View File

@ -507,69 +507,76 @@ GameLib.D3.Material.TYPE_SPRITE = "SpriteMaterial";
GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial";
/**
* Creates a THREE.Material from a GameLib.D3.Material
* @param blenderMaterial GameLib.D3.Material
* @param THREE THREE.js
* Creates an instance Material from a GameLib.D3.Material
* @param gameLibMaterial GameLib.D3.Material
* @param graphics GameLib.D3.Graphics
* @param uploadPath String
* @param progressCallback
*/
GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
GameLib.D3.Material.createInstanceMaterial = function(
gameLibMaterial,
graphics,
uploadPath,
progressCallback
) {
var defer = this.Q.defer();
var threeMaterial = null;
var instanceMaterial = null;
var blenderMaps = [];
if (blenderMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) {
if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) {
threeMaterial = new THREE.MeshStandardMaterial({
name: blenderMaterial.name,
opacity: blenderMaterial.opacity,
transparent: blenderMaterial.transparent,
blending: blenderMaterial.blending,
blendSrc: blenderMaterial.blendSrc,
blendDst: blenderMaterial.blendDst,
blendEquation: blenderMaterial.blendEquation,
depthTest: blenderMaterial.depthTest,
depthFunc: blenderMaterial.depthFunc,
depthWrite: blenderMaterial.depthWrite,
polygonOffset: blenderMaterial.polygonOffset,
polygonOffsetFactor: blenderMaterial.polygonOffsetFactor,
polygonOffsetUnits: blenderMaterial.polygonOffsetUnits,
alphaTest: blenderMaterial.alphaTest,
clippingPlanes: blenderMaterial.clippingPlanes,
clipShadows: blenderMaterial.clipShadows,
overdraw: blenderMaterial.overdraw,
visible: blenderMaterial.visible,
side: blenderMaterial.side,
color: new THREE.Color(
blenderMaterial.color.r,
blenderMaterial.color.g,
blenderMaterial.color.b
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: blenderMaterial.roughness,
metalness: blenderMaterial.metalness,
lightMapIntensity: blenderMaterial.lightMapIntensity,
aoMapIntensity: blenderMaterial.aoMapIntensity,
emissive: new THREE.Color(
blenderMaterial.emissive.r,
blenderMaterial.emissive.g,
blenderMaterial.emissive.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
),
emissiveIntensity: blenderMaterial.emissiveIntensity,
bumpScale: blenderMaterial.bumpScale,
normalScale: blenderMaterial.normalScale,
displacementScale: blenderMaterial.displacementScale,
refractionRatio: blenderMaterial.refractionRatio,
fog: blenderMaterial.fog,
shading: blenderMaterial.shading,
wireframe: blenderMaterial.wireframe,
wireframeLinewidth: blenderMaterial.wireframeLineWidth,
wireframeLinecap: blenderMaterial.wireframeLineCap,
wireframeLinejoin: blenderMaterial.wireframeLineJoin,
vertexColors: blenderMaterial.vertexColors,
skinning: blenderMaterial.skinning,
morphTargets: blenderMaterial.morphTargets,
morphNormals: blenderMaterial.morphNormals
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
});
blenderMaps.push(
@ -585,62 +592,62 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
'alpha',
'environment'
);
} else if (blenderMaterial.materialType == GameLib.D3.Material.TYPE_MESH_PHONG) {
} else if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_PHONG) {
threeMaterial = new THREE.MeshPhongMaterial({
name: blenderMaterial.name,
opacity: blenderMaterial.opacity,
transparent: blenderMaterial.transparent,
blending: blenderMaterial.blending,
blendSrc: blenderMaterial.blendSrc,
blendDst: blenderMaterial.blendDst,
blendEquation: blenderMaterial.blendEquation,
depthTest: blenderMaterial.depthTest,
depthFunc: blenderMaterial.depthFunc,
depthWrite: blenderMaterial.depthWrite,
polygonOffset: blenderMaterial.polygonOffset,
polygonOffsetFactor: blenderMaterial.polygonOffsetFactor,
polygonOffsetUnits: blenderMaterial.polygonOffsetUnits,
alphaTest: blenderMaterial.alphaTest,
clippingPlanes: blenderMaterial.clippingPlanes,
clipShadows: blenderMaterial.clipShadows,
overdraw: blenderMaterial.overdraw,
visible: blenderMaterial.visible,
side: blenderMaterial.side,
color: new THREE.Color(
blenderMaterial.color.r,
blenderMaterial.color.g,
blenderMaterial.color.b
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
),
specular: new THREE.Color(
blenderMaterial.specular.r,
blenderMaterial.specular.g,
blenderMaterial.specular.b
specular: new graphics.instance.Color(
gameLibMaterial.specular.r,
gameLibMaterial.specular.g,
gameLibMaterial.specular.b
),
shininess: blenderMaterial.shininess,
lightMapIntensity: blenderMaterial.lightMapIntensity,
aoMapIntensity: blenderMaterial.aoMapIntensity,
emissive: new THREE.Color(
blenderMaterial.emissive.r,
blenderMaterial.emissive.g,
blenderMaterial.emissive.b
shininess: gameLibMaterial.shininess,
lightMapIntensity: gameLibMaterial.lightMapIntensity,
aoMapIntensity: gameLibMaterial.aoMapIntensity,
emissive: new graphics.instance.Color(
gameLibMaterial.emissive.r,
gameLibMaterial.emissive.g,
gameLibMaterial.emissive.b
),
emissiveIntensity: blenderMaterial.emissiveIntensity,
bumpScale: blenderMaterial.bumpScale,
normalScale: blenderMaterial.normalScale,
displacementScale: blenderMaterial.displacementScale,
combine: blenderMaterial.combine,
refractionRatio: blenderMaterial.refractionRatio,
fog: blenderMaterial.fog,
shading: blenderMaterial.shading,
wireframe: blenderMaterial.wireframe,
wireframeLinewidth: blenderMaterial.wireframeLineWidth,
wireframeLinecap: blenderMaterial.wireframeLineCap,
wireframeLinejoin: blenderMaterial.wireframeLineJoin,
vertexColors: blenderMaterial.vertexColors,
skinning: blenderMaterial.skinning,
morphTargets: blenderMaterial.morphTargets,
morphNormals: blenderMaterial.morphNormals
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
});
blenderMaps.push(
@ -657,14 +664,21 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
);
} else {
console.log("material type is not implemented yet: " + blenderMaterial.materialType + " - material indexes could be screwed up");
console.log("material type is not implemented yet: " + gameLibMaterial.materialType + " - material indexes could be screwed up");
}
if (blenderMaps.length > 0) {
var textureMaps = this.loadMaps(blenderMaterial, blenderMaps, threeMaterial);
var textureMaps = GameLib.D3.Texture.loadMaps(
gameLibMaterial,
blenderMaps,
instanceMaterial,
graphics,
uploadPath,
progressCallback
);
Q.all(textureMaps).then(
function(){
defer.resolve(threeMaterial);
defer.resolve(instanceMaterial);
}
).catch(
function(error){
@ -673,7 +687,7 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
}
)
} else {
defer.resolve(threeMaterial);
defer.resolve(instanceMaterial);
}
return defer.promise;

View File

@ -121,17 +121,18 @@ GameLib.D3.Mesh.TYPE_SKINNED = 1;
/**
* Creates a THREE Mesh from GameLib.D3.Mesh
* @param gameLibMesh
* @param threeGeometry
* @param threeMaterial
* @param gameLibMesh GameLib.D3.Mesh
* @param instanceGeometry
* @param instanceMaterial
* @param graphics
* @returns {*}
*/
GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, threeMaterial) {
GameLib.D3.Mesh.createInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) {
var threeMesh = null;
if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_NORMAL) {
threeMesh = new this.THREE.Mesh(threeGeometry, threeMaterial);
threeMesh = new graphics.instance.Mesh(instanceGeometry, instanceMaterial);
}
if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_SKINNED) {
@ -146,7 +147,7 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
for (var bi = 0; bi < bones.length; bi++) {
var bone = new this.THREE.Bone();
var bone = new graphics.instance.Bone();
bone.name = bones[bi].name;
@ -187,8 +188,8 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
* Setup bones (indexes)
*/
for (var si = 0; si < skinIndices.length; si++) {
threeGeometry.skinIndices.push(
new this.THREE.Vector4(
instanceGeometry.skinIndices.push(
new graphics.instance.Vector4(
skinIndices[si].x,
skinIndices[si].y,
skinIndices[si].z,
@ -201,8 +202,8 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
* Setup bones (weights)
*/
for (var sw = 0; sw < skinWeights.length; sw++) {
threeGeometry.skinWeights.push(
new this.THREE.Vector4(
instanceGeometry.skinWeights.push(
new graphics.instance.Vector4(
skinWeights[sw].x,
skinWeights[sw].y,
skinWeights[sw].z,
@ -211,9 +212,9 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
);
}
threeMesh = new this.THREE.SkinnedMesh(threeGeometry, threeMaterial);
threeMesh = new graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial);
var skeleton = new this.THREE.Skeleton(threeBones);
var skeleton = new graphics.instance.Skeleton(threeBones);
skeleton.useVertexTexture = gameLibMesh.skeleton.useVertexTexture;
@ -228,7 +229,7 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
threeMesh.pose();
threeMesh.skeleton.skeletonHelper = new this.THREE.SkeletonHelper(threeMesh);
threeMesh.skeleton.skeletonHelper = new graphics.instance.SkeletonHelper(threeMesh);
threeMesh.skeleton.skeletonHelper.material.linewidth = 5;
}

View File

@ -1,3 +1,7 @@
if (typeof require != 'undefined') {
var Q = require('q');
}
/**
* Scenes are objects putting meshes into 'world space'
* @param id
@ -10,6 +14,7 @@
* @param scale
* @param parentSceneId
* @param lights
* @param physics GameLib.D3.Physics
* @constructor
*/
GameLib.D3.Scene = function(
@ -71,10 +76,21 @@ GameLib.D3.Scene = function(
/**
* Loads a scene directly from the API
* @param sceneName
* @param gameLibScene GameLib.D3.Scene
* @param onLoaded callback
* @param graphics GameLib.D3.Graphics
* @param uploadPath String
* @param progressCallback callback
* @param apiUrl
*/
GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
GameLib.D3.Scene.loadSceneFromApi = function(
gameLibScene,
onLoaded,
graphics,
uploadPath,
progressCallback,
apiUrl
) {
/**
* First check if this is a client or server side request
@ -87,10 +103,10 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
this.apiUrl + '/scene/load' + scene.path + '/' + scene.name
apiUrl + '/scene/load' + gameLibScene.path + '/' + gameLibScene.name
);
xhr.onreadystatechange = function(xhr, gameLibD3) {
xhr.onreadystatechange = function(xhr) {
return function() {
if (xhr.readyState == 4) {
@ -110,12 +126,19 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
var physics = scene.physics[p];
var engine = null;
if (physics.engine.engineType == GameLib.D3.Engine.ENGINE_TYPE_CANNON) {
engine = new GameLib.D3.Engine(
GameLib.D3.Engine.ENGINE_TYPE_CANNON,
CANNON
);
}
var physics3d = new GameLib.D3.Physics(
physics.id,
physics.name,
physics.engineType,
gameLibD3.CANNON,
null,
engine,
null
);
@ -232,7 +255,7 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
);
lights3d.push(light3d);
};
}
var scene3d = new GameLib.D3.Scene(
scene._id || scene.id,
@ -265,27 +288,41 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
physics3ds
);
gameLibD3.loadScene(scene3d, onLoaded, false);
GameLib.D3.Scene.loadScene(
scene3d,
onLoaded,
false,
graphics,
uploadPath,
progressCallback
);
}
}
}(xhr, this);
}(xhr);
xhr.send();
};
/**
* Loads a GameLib.D3.Scene object into a ThreeJS Scene object
* Loads a GameLib.D3.Scene object into a Graphics Instance Scene object
* @param gameLibScene GameLib.D3.Scene
* @param onLoaded callback when all meshes have loaded
* @param computeNormals set to true to compute new face and vertex normals during load
* @param graphics GameLib.D3.Graphics
* @param uploadPath
* @param progressCallback
*/
GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals) {
GameLib.D3.Scene.loadScene = function(
gameLibScene,
onLoaded,
computeNormals,
graphics,
uploadPath,
progressCallback
) {
console.log("loading scene " + gameLibScene.name);
this.path = gameLibScene.path;
var meshQ = [];
for (var m = 0; m < gameLibScene.meshes.length; m++) {
@ -294,7 +331,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
console.log("loading mesh " + mesh.name);
var geometry = new this.THREE.Geometry();
var geometry = new graphics.instance.Geometry();
var vertices = mesh.vertices;
@ -309,7 +346,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/
for (var v = 0; v < vertices.length; v++) {
geometry.vertices.push(
new this.THREE.Vector3(
new graphics.instance.Vector3(
vertices[v].position.x,
vertices[v].position.y,
vertices[v].position.z
@ -322,16 +359,16 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/
for (var f = 0; f < faces.length; f++) {
var face = new this.THREE.Face3(
var face = new graphics.instance.Face3(
faces[f].v0,
faces[f].v1,
faces[f].v2,
new this.THREE.Vector3(
new graphics.instance.Vector3(
faces[f].normal.x,
faces[f].normal.y,
faces[f].normal.z
),
new this.THREE.Color(
new graphics.instance.Color(
faces[f].color.r,
faces[f].color.g,
faces[f].color.b
@ -340,41 +377,41 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
);
face.vertexColors = [
new this.THREE.Color(
new graphics.instance.Color(
faces[f].vertexColors[0].r,
faces[f].vertexColors[0].g,
faces[f].vertexColors[0].b
),
new this.THREE.Color(
new graphics.instance.Color(
faces[f].vertexColors[1].r,
faces[f].vertexColors[1].g,
faces[f].vertexColors[1].b
),
new this.THREE.Color(
new graphics.instance.Color(
faces[f].vertexColors[2].r,
faces[f].vertexColors[2].g,
faces[f].vertexColors[2].b
)
];
face.normal = new this.THREE.Vector3(
face.normal = new graphics.instance.Vector3(
faces[f].normal.x,
faces[f].normal.y,
faces[f].normal.z
);
face.vertexNormals = [
new this.THREE.Vector3(
new graphics.instance.Vector3(
faces[f].vertexNormals[0].x,
faces[f].vertexNormals[0].y,
faces[f].vertexNormals[0].z
),
new this.THREE.Vector3(
new graphics.instance.Vector3(
faces[f].vertexNormals[1].x,
faces[f].vertexNormals[1].y,
faces[f].vertexNormals[1].z
),
new this.THREE.Vector3(
new graphics.instance.Vector3(
faces[f].vertexNormals[2].x,
faces[f].vertexNormals[2].y,
faces[f].vertexNormals[2].z
@ -398,15 +435,15 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
geometry.faceVertexUvs[fm][fuv] = [];
geometry.faceVertexUvs[fm][fuv].push(
new this.THREE.Vector2(
new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][0].x,
faceMaterialVertexUvs[fuv][0].y
),
new this.THREE.Vector2(
new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][1].x,
faceMaterialVertexUvs[fuv][1].y
),
new this.THREE.Vector2(
new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][2].x,
faceMaterialVertexUvs[fuv][2].y
)
@ -423,17 +460,24 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
geometry.computeVertexNormals();
}
var threeMaterialLoaders = [];
var instanceMaterialLoaders = [];
/**
* Setup materials
*/
for (var mi = 0; mi < materials.length; mi++) {
threeMaterialLoaders.push(this.createThreeMaterial(materials[mi]));
instanceMaterialLoaders.push(
GameLib.D3.Material.createInstanceMaterial(
materials[mi],
graphics,
uploadPath,
progressCallback
)
);
}
var result = this.Q.all(threeMaterialLoaders).then(
function(gl3d, mesh, geometry) {
var result = Q.all(instanceMaterialLoaders).then(
function(mesh, geometry) {
return function(materials) {
console.log("loaded material : " + materials[0].name);
@ -443,7 +487,12 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/
var material = materials[0];
var threeMesh = gl3d.createThreeMesh(mesh, geometry, material);
var threeMesh = GameLib.D3.Mesh.createInstanceMesh(
mesh,
geometry,
material,
graphics
);
threeMesh.name = mesh.name;
threeMesh.position.x = mesh.position.x;
@ -465,7 +514,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
return threeMesh;
};
}(this, mesh, geometry)
}(mesh, geometry)
).catch(function(error){
console.log(error);
});
@ -473,99 +522,98 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
meshQ.push(result);
}
this.Q.all(meshQ).then(function(threeMeshes){
Q.all(meshQ).then(
function(instanceMeshes){
console.log("all meshes have loaded");
if (typeof onLoaded != 'undefined') {
var threeLights = [];
var instanceLights = [];
for (var sli = 0; sli < gameLibScene.lights.length; sli++) {
var blenderLight = gameLibScene.lights[sli];
var gameLibLight = gameLibScene.lights[sli];
var light = null;
if (blenderLight.lightType == 'AmbientLight') {
light = new this.THREE.AmbientLight(blenderLight.color, blenderLight.intensity);
if (gameLibLight.lightType == 'AmbientLight') {
light = new graphics.instance.AmbientLight(gameLibLight.color, gameLibLight.intensity);
}
if (blenderLight.lightType == 'DirectionalLight') {
light = new this.THREE.DirectionalLight(blenderLight.color, blenderLight.intensity);
if (gameLibLight.lightType == 'DirectionalLight') {
light = new graphics.instance.DirectionalLight(gameLibLight.color, gameLibLight.intensity);
}
if (blenderLight.lightType == 'PointLight') {
light = new this.THREE.PointLight(blenderLight.color, blenderLight.intensity);
light.distance = blenderLight.distance;
light.decay = blenderLight.decay;
if (gameLibLight.lightType == 'PointLight') {
light = new graphics.instance.PointLight(gameLibLight.color, gameLibLight.intensity);
light.distance = gameLibLight.distance;
light.decay = gameLibLight.decay;
}
if (blenderLight.lightType == 'SpotLight') {
light = new this.THREE.SpotLight(blenderLight.color, blenderLight.intensity);
light.distance = blenderLight.distance;
light.angle = blenderLight.angle;
light.penumbra = blenderLight.penumbra;
light.decay = blenderLight.decay;
if (gameLibLight.lightType == 'SpotLight') {
light = new graphics.instance.SpotLight(gameLibLight.color, gameLibLight.intensity);
light.distance = gameLibLight.distance;
light.angle = gameLibLight.angle;
light.penumbra = gameLibLight.penumbra;
light.decay = gameLibLight.decay;
}
light.position.x = blenderLight.position.x;
light.position.y = blenderLight.position.y;
light.position.z = blenderLight.position.z;
light.position.x = gameLibLight.position.x;
light.position.y = gameLibLight.position.y;
light.position.z = gameLibLight.position.z;
light.rotation.x = blenderLight.rotation.x;
light.rotation.y = blenderLight.rotation.y;
light.rotation.z = blenderLight.rotation.z;
// light.scale.x = blenderLight.scale.x;
// light.scale.y = blenderLight.scale.y;
// light.scale.z = blenderLight.scale.z;
light.rotation.x = gameLibLight.rotation.x;
light.rotation.y = gameLibLight.rotation.y;
light.rotation.z = gameLibLight.rotation.z;
if (light == null) {
console.warn('Does not support lights of type :' + blenderLight.lightType + ', not imported');
console.warn('Does not support lights of type :' + gameLibLight.lightType + ', not imported');
} else {
light.name = blenderLight.name;
threeLights.push(light);
light.name = gameLibLight.name;
instanceLights.push(light);
}
}
var threeScene = new this.THREE.Scene();
var instanceScene = new graphics.instance.Scene();
threeScene.name = gameLibScene.name;
instanceScene.name = gameLibScene.name;
threeScene.position.x = gameLibScene.position.x;
threeScene.position.y = gameLibScene.position.y;
threeScene.position.z = gameLibScene.position.z;
instanceScene.position.x = gameLibScene.position.x;
instanceScene.position.y = gameLibScene.position.y;
instanceScene.position.z = gameLibScene.position.z;
threeScene.rotation.x = gameLibScene.rotation.x;
threeScene.rotation.y = gameLibScene.rotation.y;
threeScene.rotation.z = gameLibScene.rotation.z;
instanceScene.rotation.x = gameLibScene.rotation.x;
instanceScene.rotation.y = gameLibScene.rotation.y;
instanceScene.rotation.z = gameLibScene.rotation.z;
threeScene.scale.x = gameLibScene.scale.x;
threeScene.scale.y = gameLibScene.scale.y;
threeScene.scale.z = gameLibScene.scale.z;
instanceScene.scale.x = gameLibScene.scale.x;
instanceScene.scale.y = gameLibScene.scale.y;
instanceScene.scale.z = gameLibScene.scale.z;
threeScene.quaternion.x = gameLibScene.quaternion.x;
threeScene.quaternion.y = gameLibScene.quaternion.y;
threeScene.quaternion.z = gameLibScene.quaternion.z;
threeScene.quaternion.w = gameLibScene.quaternion.w;
instanceScene.quaternion.x = gameLibScene.quaternion.x;
instanceScene.quaternion.y = gameLibScene.quaternion.y;
instanceScene.quaternion.z = gameLibScene.quaternion.z;
instanceScene.quaternion.w = gameLibScene.quaternion.w;
for (var m = 0; m < threeMeshes.length; m++) {
threeScene.add(threeMeshes[m]);
for (var m = 0; m < instanceMeshes.length; m++) {
instanceScene.add(instanceMeshes[m]);
}
for (var l = 0; l < threeLights.length; l++) {
threeScene.add(threeLights[l]);
for (var l = 0; l < instanceLights.length; l++) {
instanceScene.add(instanceLights[l]);
}
onLoaded(
gameLibScene,
{
scene: threeScene,
lights: threeLights,
meshes: threeMeshes
scene: instanceScene,
lights: instanceLights,
meshes: instanceMeshes
}
);
}
}.bind(this)).catch(function(error){
console.log(error);
});
}.catch(
function(error){
console.log(error);
}
));
};

View File

@ -1,3 +1,7 @@
if (typeof require != 'undefined') {
var Q = require('q');
}
/**
* Texture Superset
* @param id
@ -203,69 +207,79 @@ GameLib.D3.Texture.TYPE_RGBM7_ENCODING = 3004;
GameLib.D3.Texture.TYPE_RGBM16_ENCODING = 3005;
GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
/**
* Defers loading of an image and resolves once image is loaded
* @param gameLibTexture
* @param threeMaterial
* @param threeMaterialMapType
* @param gameLibTexture GameLib.D3.Texture
* @param instanceMaterial
* @param instanceMaterialMapType
* @param graphics GameLib.D3.Graphics
* @param uploadPath String
* @param progressCallback
* @returns {Promise}
*/
GameLib.D3.prototype.loadMap = function(gameLibTexture, threeMaterial, threeMaterialMapType) {
GameLib.D3.Texture.loadMap = function(
gameLibTexture,
instanceMaterial,
instanceMaterialMapType,
graphics,
uploadPath,
progressCallback
) {
var q = this.Q.defer();
var defer = Q.defer();
var imagePath = null;
var textureLoader = new graphics.instance.TextureLoader();
if (gameLibTexture && gameLibTexture.image && gameLibTexture.image.filename) {
/**
* Else, load from upload source
*/
imagePath = this.editorUrl + '/uploads' + this.path + '/' + gameLibTexture.image.filename;
imagePath = uploadPath + '/' + gameLibTexture.image.filename;
}
if (imagePath) {
this.textureLoader.crossOrigin = '';
textureLoader.crossOrigin = '';
this.textureLoader.load(
textureLoader.load(
imagePath,
function(texture) {
/**
* onLoad
*/
threeMaterial[threeMaterialMapType] = texture;
threeMaterial[threeMaterialMapType].name = gameLibTexture.name;
threeMaterial[threeMaterialMapType].anisotropy = gameLibTexture.anisotropy;
threeMaterial[threeMaterialMapType].encoding = gameLibTexture.encoding;
threeMaterial[threeMaterialMapType].flipY = gameLibTexture.flipY;
instanceMaterial[instanceMaterialMapType] = texture;
instanceMaterial[instanceMaterialMapType].name = gameLibTexture.name;
instanceMaterial[instanceMaterialMapType].anisotropy = gameLibTexture.anisotropy;
instanceMaterial[instanceMaterialMapType].encoding = gameLibTexture.encoding;
instanceMaterial[instanceMaterialMapType].flipY = gameLibTexture.flipY;
/**
* We don't restore the format since this changing from OS to OS and breaks the implementation sometimes
*/
threeMaterial[threeMaterialMapType].generateMipmaps = gameLibTexture.generateMipmaps;
threeMaterial[threeMaterialMapType].magFilter = gameLibTexture.magFilter;
threeMaterial[threeMaterialMapType].minFilter = gameLibTexture.minFilter;
threeMaterial[threeMaterialMapType].mapping = gameLibTexture.mapping;
threeMaterial[threeMaterialMapType].mipmaps = gameLibTexture.mipmaps;
threeMaterial[threeMaterialMapType].offset = new this.THREE.Vector2(
instanceMaterial[instanceMaterialMapType].generateMipmaps = gameLibTexture.generateMipmaps;
instanceMaterial[instanceMaterialMapType].magFilter = gameLibTexture.magFilter;
instanceMaterial[instanceMaterialMapType].minFilter = gameLibTexture.minFilter;
instanceMaterial[instanceMaterialMapType].mapping = gameLibTexture.mapping;
instanceMaterial[instanceMaterialMapType].mipmaps = gameLibTexture.mipmaps;
instanceMaterial[instanceMaterialMapType].offset = new graphics.instance.Vector2(
gameLibTexture.offset.x,
gameLibTexture.offset.y
);
threeMaterial[threeMaterialMapType].premultiplyAlpha = gameLibTexture.premultiplyAlpha;
threeMaterial[threeMaterialMapType].textureType = gameLibTexture.textureType;
threeMaterial[threeMaterialMapType].wrapS = gameLibTexture.wrapS;
threeMaterial[threeMaterialMapType].wrapT = gameLibTexture.wrapT;
threeMaterial[threeMaterialMapType].unpackAlignment = gameLibTexture.unpackAlignment;
threeMaterial.needsUpdate = true;
q.resolve(true);
instanceMaterial[instanceMaterialMapType].premultiplyAlpha = gameLibTexture.premultiplyAlpha;
instanceMaterial[instanceMaterialMapType].textureType = gameLibTexture.textureType;
instanceMaterial[instanceMaterialMapType].wrapS = gameLibTexture.wrapS;
instanceMaterial[instanceMaterialMapType].wrapT = gameLibTexture.wrapT;
instanceMaterial[instanceMaterialMapType].unpackAlignment = gameLibTexture.unpackAlignment;
instanceMaterial.needsUpdate = true;
defer.resolve(true);
},
function(xhr) {
/**
* onProgress
*/
if (this.editor) {
this.editor.setServerStatus(Math.round(xhr.loaded / xhr.total * 100) + '% complete', 'success');
if (progressCallback) {
progressCallback(Math.round(xhr.loaded / xhr.total * 100));
}
},
function(error) {
@ -273,26 +287,36 @@ GameLib.D3.prototype.loadMap = function(gameLibTexture, threeMaterial, threeMate
* onError
*/
console.log("an error occurred while trying to load the image : " + imagePath);
q.resolve(null);
defer.resolve(null);
}
);
} else {
q.resolve(null);
defer.resolve(null);
}
return q.promise;
return defer.promise;
};
/**
* Returns an array of image loading Promises
* @param blenderMaterial
* @param gameLibMaterial
* @param blenderMaps
* @param threeMaterial
* @param instanceMaterial
* @param graphics GameLib.D3.Graphics
* @param uploadPath String
* @param progressCallback
* @returns Q[]
*/
GameLib.D3.prototype.loadMaps = function(blenderMaterial, blenderMaps, threeMaterial) {
GameLib.D3.Texture.loadMaps = function(
gameLibMaterial,
blenderMaps,
instanceMaterial,
graphics,
uploadPath,
progressCallback
) {
var textureMaps = [];
@ -300,9 +324,9 @@ GameLib.D3.prototype.loadMaps = function(blenderMaterial, blenderMaps, threeMate
var map = blenderMaps[ti];
if (blenderMaterial.maps.hasOwnProperty(map)) {
if (gameLibMaterial.maps.hasOwnProperty(map)) {
var blenderTexture = blenderMaterial.maps[map];
var blenderTexture = gameLibMaterial.maps[map];
if (
blenderTexture &&
@ -310,57 +334,66 @@ GameLib.D3.prototype.loadMaps = function(blenderMaterial, blenderMaps, threeMate
blenderTexture.image.filename
) {
var threeMap = null;
var instanceMap = null;
if (map == 'alpha') {
threeMap = 'alhpaMap';
instanceMap = 'alhpaMap';
}
if (map == 'ao') {
threeMap = 'aoMap';
instanceMap = 'aoMap';
}
if (map == 'bump') {
threeMap = 'bumpMap';
instanceMap = 'bumpMap';
}
if (map == 'displacement') {
threeMap = 'displacementMap';
instanceMap = 'displacementMap';
}
if (map == 'emissive') {
threeMap = 'emissiveMap';
instanceMap = 'emissiveMap';
}
if (map == 'environment') {
threeMap = 'envMap';
instanceMap = 'envMap';
}
if (map == 'light') {
threeMap = 'lightMap';
instanceMap = 'lightMap';
}
if (map == 'specular') {
threeMap = 'specularMap';
instanceMap = 'specularMap';
}
if (map == 'diffuse') {
threeMap = 'map';
instanceMap = 'map';
}
if (map == 'roughness') {
threeMap = 'roughnessMap';
instanceMap = 'roughnessMap';
}
if (map == 'metalness') {
threeMap = 'metalnessMap';
instanceMap = 'metalnessMap';
}
if (threeMap == null) {
if (instanceMap == null) {
console.warn("unsupported map type : " + map);
}
textureMaps.push(this.loadMap(blenderMaterial.maps[map], threeMaterial, threeMap));
textureMaps.push(
GameLib.D3.Texture.loadMap(
gameLibMaterial.maps[map],
instanceMaterial,
instanceMap,
graphics,
uploadPath,
progressCallback
)
);
}
}
}