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 plumber = require('gulp-plumber');
var istanbul = require('gulp-istanbul'); var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha'); var mocha = require('gulp-mocha');
var watch = require('gulp-watch');
gulp.task( gulp.task(
'build', 'build',
@ -59,6 +60,17 @@ gulp.task(
gulp.task( gulp.task(
'default', '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-istanbul": "^1.1.1",
"gulp-mocha": "^3.0.1", "gulp-mocha": "^3.0.1",
"gulp-plumber": "^1.1.0", "gulp-plumber": "^1.1.0",
"gulp-watch": "^4.3.10",
"sinon": "^1.17.6", "sinon": "^1.17.6",
"sinon-chai": "^2.8.0" "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"; GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial";
/** /**
* Creates a THREE.Material from a GameLib.D3.Material * Creates an instance Material from a GameLib.D3.Material
* @param blenderMaterial GameLib.D3.Material * @param gameLibMaterial GameLib.D3.Material
* @param THREE THREE.js * @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 defer = this.Q.defer();
var threeMaterial = null; var instanceMaterial = null;
var blenderMaps = []; var blenderMaps = [];
if (blenderMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) { if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) {
threeMaterial = new THREE.MeshStandardMaterial({ instanceMaterial = new graphics.instance.MeshStandardMaterial({
name: blenderMaterial.name, name: gameLibMaterial.name,
opacity: blenderMaterial.opacity, opacity: gameLibMaterial.opacity,
transparent: blenderMaterial.transparent, transparent: gameLibMaterial.transparent,
blending: blenderMaterial.blending, blending: gameLibMaterial.blending,
blendSrc: blenderMaterial.blendSrc, blendSrc: gameLibMaterial.blendSrc,
blendDst: blenderMaterial.blendDst, blendDst: gameLibMaterial.blendDst,
blendEquation: blenderMaterial.blendEquation, blendEquation: gameLibMaterial.blendEquation,
depthTest: blenderMaterial.depthTest, depthTest: gameLibMaterial.depthTest,
depthFunc: blenderMaterial.depthFunc, depthFunc: gameLibMaterial.depthFunc,
depthWrite: blenderMaterial.depthWrite, depthWrite: gameLibMaterial.depthWrite,
polygonOffset: blenderMaterial.polygonOffset, polygonOffset: gameLibMaterial.polygonOffset,
polygonOffsetFactor: blenderMaterial.polygonOffsetFactor, polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor,
polygonOffsetUnits: blenderMaterial.polygonOffsetUnits, polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits,
alphaTest: blenderMaterial.alphaTest, alphaTest: gameLibMaterial.alphaTest,
clippingPlanes: blenderMaterial.clippingPlanes, clippingPlanes: gameLibMaterial.clippingPlanes,
clipShadows: blenderMaterial.clipShadows, clipShadows: gameLibMaterial.clipShadows,
overdraw: blenderMaterial.overdraw, overdraw: gameLibMaterial.overdraw,
visible: blenderMaterial.visible, visible: gameLibMaterial.visible,
side: blenderMaterial.side, side: gameLibMaterial.side,
color: new THREE.Color( color: new graphics.instance.Color(
blenderMaterial.color.r, gameLibMaterial.color.r,
blenderMaterial.color.g, gameLibMaterial.color.g,
blenderMaterial.color.b gameLibMaterial.color.b
), ),
roughness: blenderMaterial.roughness, roughness: gameLibMaterial.roughness,
metalness: blenderMaterial.metalness, metalness: gameLibMaterial.metalness,
lightMapIntensity: blenderMaterial.lightMapIntensity, lightMapIntensity: gameLibMaterial.lightMapIntensity,
aoMapIntensity: blenderMaterial.aoMapIntensity, aoMapIntensity: gameLibMaterial.aoMapIntensity,
emissive: new THREE.Color( emissive: new graphics.instance.Color(
blenderMaterial.emissive.r, gameLibMaterial.emissive.r,
blenderMaterial.emissive.g, gameLibMaterial.emissive.g,
blenderMaterial.emissive.b gameLibMaterial.emissive.b
), ),
emissiveIntensity: blenderMaterial.emissiveIntensity, emissiveIntensity: gameLibMaterial.emissiveIntensity,
bumpScale: blenderMaterial.bumpScale, bumpScale: gameLibMaterial.bumpScale,
normalScale: blenderMaterial.normalScale, normalScale: gameLibMaterial.normalScale,
displacementScale: blenderMaterial.displacementScale, displacementScale: gameLibMaterial.displacementScale,
refractionRatio: blenderMaterial.refractionRatio, refractionRatio: gameLibMaterial.refractionRatio,
fog: blenderMaterial.fog, fog: gameLibMaterial.fog,
shading: blenderMaterial.shading, shading: gameLibMaterial.shading,
wireframe: blenderMaterial.wireframe, wireframe: gameLibMaterial.wireframe,
wireframeLinewidth: blenderMaterial.wireframeLineWidth, wireframeLinewidth: gameLibMaterial.wireframeLineWidth,
wireframeLinecap: blenderMaterial.wireframeLineCap, wireframeLinecap: gameLibMaterial.wireframeLineCap,
wireframeLinejoin: blenderMaterial.wireframeLineJoin, wireframeLinejoin: gameLibMaterial.wireframeLineJoin,
vertexColors: blenderMaterial.vertexColors, vertexColors: gameLibMaterial.vertexColors,
skinning: blenderMaterial.skinning, skinning: gameLibMaterial.skinning,
morphTargets: blenderMaterial.morphTargets, morphTargets: gameLibMaterial.morphTargets,
morphNormals: blenderMaterial.morphNormals morphNormals: gameLibMaterial.morphNormals
}); });
blenderMaps.push( blenderMaps.push(
@ -585,62 +592,62 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
'alpha', 'alpha',
'environment' '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({ instanceMaterial = new graphics.instance.MeshPhongMaterial({
name: blenderMaterial.name, name: gameLibMaterial.name,
opacity: blenderMaterial.opacity, opacity: gameLibMaterial.opacity,
transparent: blenderMaterial.transparent, transparent: gameLibMaterial.transparent,
blending: blenderMaterial.blending, blending: gameLibMaterial.blending,
blendSrc: blenderMaterial.blendSrc, blendSrc: gameLibMaterial.blendSrc,
blendDst: blenderMaterial.blendDst, blendDst: gameLibMaterial.blendDst,
blendEquation: blenderMaterial.blendEquation, blendEquation: gameLibMaterial.blendEquation,
depthTest: blenderMaterial.depthTest, depthTest: gameLibMaterial.depthTest,
depthFunc: blenderMaterial.depthFunc, depthFunc: gameLibMaterial.depthFunc,
depthWrite: blenderMaterial.depthWrite, depthWrite: gameLibMaterial.depthWrite,
polygonOffset: blenderMaterial.polygonOffset, polygonOffset: gameLibMaterial.polygonOffset,
polygonOffsetFactor: blenderMaterial.polygonOffsetFactor, polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor,
polygonOffsetUnits: blenderMaterial.polygonOffsetUnits, polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits,
alphaTest: blenderMaterial.alphaTest, alphaTest: gameLibMaterial.alphaTest,
clippingPlanes: blenderMaterial.clippingPlanes, clippingPlanes: gameLibMaterial.clippingPlanes,
clipShadows: blenderMaterial.clipShadows, clipShadows: gameLibMaterial.clipShadows,
overdraw: blenderMaterial.overdraw, overdraw: gameLibMaterial.overdraw,
visible: blenderMaterial.visible, visible: gameLibMaterial.visible,
side: blenderMaterial.side, side: gameLibMaterial.side,
color: new THREE.Color( color: new graphics.instance.Color(
blenderMaterial.color.r, gameLibMaterial.color.r,
blenderMaterial.color.g, gameLibMaterial.color.g,
blenderMaterial.color.b gameLibMaterial.color.b
), ),
specular: new THREE.Color( specular: new graphics.instance.Color(
blenderMaterial.specular.r, gameLibMaterial.specular.r,
blenderMaterial.specular.g, gameLibMaterial.specular.g,
blenderMaterial.specular.b gameLibMaterial.specular.b
), ),
shininess: blenderMaterial.shininess, shininess: gameLibMaterial.shininess,
lightMapIntensity: blenderMaterial.lightMapIntensity, lightMapIntensity: gameLibMaterial.lightMapIntensity,
aoMapIntensity: blenderMaterial.aoMapIntensity, aoMapIntensity: gameLibMaterial.aoMapIntensity,
emissive: new THREE.Color( emissive: new graphics.instance.Color(
blenderMaterial.emissive.r, gameLibMaterial.emissive.r,
blenderMaterial.emissive.g, gameLibMaterial.emissive.g,
blenderMaterial.emissive.b gameLibMaterial.emissive.b
), ),
emissiveIntensity: blenderMaterial.emissiveIntensity, emissiveIntensity: gameLibMaterial.emissiveIntensity,
bumpScale: blenderMaterial.bumpScale, bumpScale: gameLibMaterial.bumpScale,
normalScale: blenderMaterial.normalScale, normalScale: gameLibMaterial.normalScale,
displacementScale: blenderMaterial.displacementScale, displacementScale: gameLibMaterial.displacementScale,
combine: blenderMaterial.combine, combine: gameLibMaterial.combine,
refractionRatio: blenderMaterial.refractionRatio, refractionRatio: gameLibMaterial.refractionRatio,
fog: blenderMaterial.fog, fog: gameLibMaterial.fog,
shading: blenderMaterial.shading, shading: gameLibMaterial.shading,
wireframe: blenderMaterial.wireframe, wireframe: gameLibMaterial.wireframe,
wireframeLinewidth: blenderMaterial.wireframeLineWidth, wireframeLinewidth: gameLibMaterial.wireframeLineWidth,
wireframeLinecap: blenderMaterial.wireframeLineCap, wireframeLinecap: gameLibMaterial.wireframeLineCap,
wireframeLinejoin: blenderMaterial.wireframeLineJoin, wireframeLinejoin: gameLibMaterial.wireframeLineJoin,
vertexColors: blenderMaterial.vertexColors, vertexColors: gameLibMaterial.vertexColors,
skinning: blenderMaterial.skinning, skinning: gameLibMaterial.skinning,
morphTargets: blenderMaterial.morphTargets, morphTargets: gameLibMaterial.morphTargets,
morphNormals: blenderMaterial.morphNormals morphNormals: gameLibMaterial.morphNormals
}); });
blenderMaps.push( blenderMaps.push(
@ -657,14 +664,21 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
); );
} else { } 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) { 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( Q.all(textureMaps).then(
function(){ function(){
defer.resolve(threeMaterial); defer.resolve(instanceMaterial);
} }
).catch( ).catch(
function(error){ function(error){
@ -673,7 +687,7 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial, THREE) {
} }
) )
} else { } else {
defer.resolve(threeMaterial); defer.resolve(instanceMaterial);
} }
return defer.promise; return defer.promise;

View File

@ -121,17 +121,18 @@ GameLib.D3.Mesh.TYPE_SKINNED = 1;
/** /**
* Creates a THREE Mesh from GameLib.D3.Mesh * Creates a THREE Mesh from GameLib.D3.Mesh
* @param gameLibMesh * @param gameLibMesh GameLib.D3.Mesh
* @param threeGeometry * @param instanceGeometry
* @param threeMaterial * @param instanceMaterial
* @param graphics
* @returns {*} * @returns {*}
*/ */
GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, threeMaterial) { GameLib.D3.Mesh.createInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) {
var threeMesh = null; var threeMesh = null;
if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { 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) { 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++) { 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; bone.name = bones[bi].name;
@ -187,8 +188,8 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
* Setup bones (indexes) * Setup bones (indexes)
*/ */
for (var si = 0; si < skinIndices.length; si++) { for (var si = 0; si < skinIndices.length; si++) {
threeGeometry.skinIndices.push( instanceGeometry.skinIndices.push(
new this.THREE.Vector4( new graphics.instance.Vector4(
skinIndices[si].x, skinIndices[si].x,
skinIndices[si].y, skinIndices[si].y,
skinIndices[si].z, skinIndices[si].z,
@ -201,8 +202,8 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
* Setup bones (weights) * Setup bones (weights)
*/ */
for (var sw = 0; sw < skinWeights.length; sw++) { for (var sw = 0; sw < skinWeights.length; sw++) {
threeGeometry.skinWeights.push( instanceGeometry.skinWeights.push(
new this.THREE.Vector4( new graphics.instance.Vector4(
skinWeights[sw].x, skinWeights[sw].x,
skinWeights[sw].y, skinWeights[sw].y,
skinWeights[sw].z, 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; skeleton.useVertexTexture = gameLibMesh.skeleton.useVertexTexture;
@ -228,7 +229,7 @@ GameLib.D3.prototype.createThreeMesh = function(gameLibMesh, threeGeometry, thre
threeMesh.pose(); threeMesh.pose();
threeMesh.skeleton.skeletonHelper = new this.THREE.SkeletonHelper(threeMesh); threeMesh.skeleton.skeletonHelper = new graphics.instance.SkeletonHelper(threeMesh);
threeMesh.skeleton.skeletonHelper.material.linewidth = 5; 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' * Scenes are objects putting meshes into 'world space'
* @param id * @param id
@ -10,6 +14,7 @@
* @param scale * @param scale
* @param parentSceneId * @param parentSceneId
* @param lights * @param lights
* @param physics GameLib.D3.Physics
* @constructor * @constructor
*/ */
GameLib.D3.Scene = function( GameLib.D3.Scene = function(
@ -71,10 +76,21 @@ GameLib.D3.Scene = function(
/** /**
* Loads a scene directly from the API * Loads a scene directly from the API
* @param sceneName * @param gameLibScene GameLib.D3.Scene
* @param onLoaded callback * @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 * 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(); var xhr = new XMLHttpRequest();
xhr.open( xhr.open(
'GET', '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() { return function() {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
@ -110,12 +126,19 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
var physics = scene.physics[p]; 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( var physics3d = new GameLib.D3.Physics(
physics.id, physics.id,
physics.name, physics.name,
physics.engineType, engine,
gameLibD3.CANNON,
null,
null null
); );
@ -232,7 +255,7 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
); );
lights3d.push(light3d); lights3d.push(light3d);
}; }
var scene3d = new GameLib.D3.Scene( var scene3d = new GameLib.D3.Scene(
scene._id || scene.id, scene._id || scene.id,
@ -265,27 +288,41 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
physics3ds physics3ds
); );
gameLibD3.loadScene(scene3d, onLoaded, false); GameLib.D3.Scene.loadScene(
scene3d,
onLoaded,
false,
graphics,
uploadPath,
progressCallback
);
} }
} }
}(xhr, this); }(xhr);
xhr.send(); 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 gameLibScene GameLib.D3.Scene
* @param onLoaded callback when all meshes have loaded * @param onLoaded callback when all meshes have loaded
* @param computeNormals set to true to compute new face and vertex normals during load * @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); console.log("loading scene " + gameLibScene.name);
this.path = gameLibScene.path;
var meshQ = []; var meshQ = [];
for (var m = 0; m < gameLibScene.meshes.length; m++) { 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); console.log("loading mesh " + mesh.name);
var geometry = new this.THREE.Geometry(); var geometry = new graphics.instance.Geometry();
var vertices = mesh.vertices; var vertices = mesh.vertices;
@ -309,7 +346,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/ */
for (var v = 0; v < vertices.length; v++) { for (var v = 0; v < vertices.length; v++) {
geometry.vertices.push( geometry.vertices.push(
new this.THREE.Vector3( new graphics.instance.Vector3(
vertices[v].position.x, vertices[v].position.x,
vertices[v].position.y, vertices[v].position.y,
vertices[v].position.z vertices[v].position.z
@ -322,16 +359,16 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/ */
for (var f = 0; f < faces.length; f++) { 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].v0,
faces[f].v1, faces[f].v1,
faces[f].v2, faces[f].v2,
new this.THREE.Vector3( new graphics.instance.Vector3(
faces[f].normal.x, faces[f].normal.x,
faces[f].normal.y, faces[f].normal.y,
faces[f].normal.z faces[f].normal.z
), ),
new this.THREE.Color( new graphics.instance.Color(
faces[f].color.r, faces[f].color.r,
faces[f].color.g, faces[f].color.g,
faces[f].color.b faces[f].color.b
@ -340,41 +377,41 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
); );
face.vertexColors = [ face.vertexColors = [
new this.THREE.Color( new graphics.instance.Color(
faces[f].vertexColors[0].r, faces[f].vertexColors[0].r,
faces[f].vertexColors[0].g, faces[f].vertexColors[0].g,
faces[f].vertexColors[0].b faces[f].vertexColors[0].b
), ),
new this.THREE.Color( new graphics.instance.Color(
faces[f].vertexColors[1].r, faces[f].vertexColors[1].r,
faces[f].vertexColors[1].g, faces[f].vertexColors[1].g,
faces[f].vertexColors[1].b faces[f].vertexColors[1].b
), ),
new this.THREE.Color( new graphics.instance.Color(
faces[f].vertexColors[2].r, faces[f].vertexColors[2].r,
faces[f].vertexColors[2].g, faces[f].vertexColors[2].g,
faces[f].vertexColors[2].b faces[f].vertexColors[2].b
) )
]; ];
face.normal = new this.THREE.Vector3( face.normal = new graphics.instance.Vector3(
faces[f].normal.x, faces[f].normal.x,
faces[f].normal.y, faces[f].normal.y,
faces[f].normal.z faces[f].normal.z
); );
face.vertexNormals = [ face.vertexNormals = [
new this.THREE.Vector3( new graphics.instance.Vector3(
faces[f].vertexNormals[0].x, faces[f].vertexNormals[0].x,
faces[f].vertexNormals[0].y, faces[f].vertexNormals[0].y,
faces[f].vertexNormals[0].z faces[f].vertexNormals[0].z
), ),
new this.THREE.Vector3( new graphics.instance.Vector3(
faces[f].vertexNormals[1].x, faces[f].vertexNormals[1].x,
faces[f].vertexNormals[1].y, faces[f].vertexNormals[1].y,
faces[f].vertexNormals[1].z faces[f].vertexNormals[1].z
), ),
new this.THREE.Vector3( new graphics.instance.Vector3(
faces[f].vertexNormals[2].x, faces[f].vertexNormals[2].x,
faces[f].vertexNormals[2].y, faces[f].vertexNormals[2].y,
faces[f].vertexNormals[2].z 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++) { for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
geometry.faceVertexUvs[fm][fuv] = []; geometry.faceVertexUvs[fm][fuv] = [];
geometry.faceVertexUvs[fm][fuv].push( geometry.faceVertexUvs[fm][fuv].push(
new this.THREE.Vector2( new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][0].x, faceMaterialVertexUvs[fuv][0].x,
faceMaterialVertexUvs[fuv][0].y faceMaterialVertexUvs[fuv][0].y
), ),
new this.THREE.Vector2( new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][1].x, faceMaterialVertexUvs[fuv][1].x,
faceMaterialVertexUvs[fuv][1].y faceMaterialVertexUvs[fuv][1].y
), ),
new this.THREE.Vector2( new graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][2].x, faceMaterialVertexUvs[fuv][2].x,
faceMaterialVertexUvs[fuv][2].y faceMaterialVertexUvs[fuv][2].y
) )
@ -423,17 +460,24 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
geometry.computeVertexNormals(); geometry.computeVertexNormals();
} }
var threeMaterialLoaders = []; var instanceMaterialLoaders = [];
/** /**
* Setup materials * Setup materials
*/ */
for (var mi = 0; mi < materials.length; mi++) { 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( var result = Q.all(instanceMaterialLoaders).then(
function(gl3d, mesh, geometry) { function(mesh, geometry) {
return function(materials) { return function(materials) {
console.log("loaded material : " + materials[0].name); console.log("loaded material : " + materials[0].name);
@ -443,7 +487,12 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
*/ */
var material = materials[0]; 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.name = mesh.name;
threeMesh.position.x = mesh.position.x; threeMesh.position.x = mesh.position.x;
@ -465,7 +514,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
return threeMesh; return threeMesh;
}; };
}(this, mesh, geometry) }(mesh, geometry)
).catch(function(error){ ).catch(function(error){
console.log(error); console.log(error);
}); });
@ -473,99 +522,98 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
meshQ.push(result); meshQ.push(result);
} }
this.Q.all(meshQ).then(function(threeMeshes){ Q.all(meshQ).then(
function(instanceMeshes){
console.log("all meshes have loaded"); console.log("all meshes have loaded");
if (typeof onLoaded != 'undefined') { if (typeof onLoaded != 'undefined') {
var threeLights = []; var instanceLights = [];
for (var sli = 0; sli < gameLibScene.lights.length; sli++) { for (var sli = 0; sli < gameLibScene.lights.length; sli++) {
var blenderLight = gameLibScene.lights[sli]; var gameLibLight = gameLibScene.lights[sli];
var light = null; var light = null;
if (blenderLight.lightType == 'AmbientLight') { if (gameLibLight.lightType == 'AmbientLight') {
light = new this.THREE.AmbientLight(blenderLight.color, blenderLight.intensity); light = new graphics.instance.AmbientLight(gameLibLight.color, gameLibLight.intensity);
} }
if (blenderLight.lightType == 'DirectionalLight') { if (gameLibLight.lightType == 'DirectionalLight') {
light = new this.THREE.DirectionalLight(blenderLight.color, blenderLight.intensity); light = new graphics.instance.DirectionalLight(gameLibLight.color, gameLibLight.intensity);
} }
if (blenderLight.lightType == 'PointLight') { if (gameLibLight.lightType == 'PointLight') {
light = new this.THREE.PointLight(blenderLight.color, blenderLight.intensity); light = new graphics.instance.PointLight(gameLibLight.color, gameLibLight.intensity);
light.distance = blenderLight.distance; light.distance = gameLibLight.distance;
light.decay = blenderLight.decay; light.decay = gameLibLight.decay;
} }
if (blenderLight.lightType == 'SpotLight') { if (gameLibLight.lightType == 'SpotLight') {
light = new this.THREE.SpotLight(blenderLight.color, blenderLight.intensity); light = new graphics.instance.SpotLight(gameLibLight.color, gameLibLight.intensity);
light.distance = blenderLight.distance; light.distance = gameLibLight.distance;
light.angle = blenderLight.angle; light.angle = gameLibLight.angle;
light.penumbra = blenderLight.penumbra; light.penumbra = gameLibLight.penumbra;
light.decay = blenderLight.decay; light.decay = gameLibLight.decay;
} }
light.position.x = blenderLight.position.x; light.position.x = gameLibLight.position.x;
light.position.y = blenderLight.position.y; light.position.y = gameLibLight.position.y;
light.position.z = blenderLight.position.z; light.position.z = gameLibLight.position.z;
light.rotation.x = blenderLight.rotation.x; light.rotation.x = gameLibLight.rotation.x;
light.rotation.y = blenderLight.rotation.y; light.rotation.y = gameLibLight.rotation.y;
light.rotation.z = blenderLight.rotation.z; light.rotation.z = gameLibLight.rotation.z;
// light.scale.x = blenderLight.scale.x;
// light.scale.y = blenderLight.scale.y;
// light.scale.z = blenderLight.scale.z;
if (light == null) { 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 { } else {
light.name = blenderLight.name; light.name = gameLibLight.name;
threeLights.push(light); 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; instanceScene.position.x = gameLibScene.position.x;
threeScene.position.y = gameLibScene.position.y; instanceScene.position.y = gameLibScene.position.y;
threeScene.position.z = gameLibScene.position.z; instanceScene.position.z = gameLibScene.position.z;
threeScene.rotation.x = gameLibScene.rotation.x; instanceScene.rotation.x = gameLibScene.rotation.x;
threeScene.rotation.y = gameLibScene.rotation.y; instanceScene.rotation.y = gameLibScene.rotation.y;
threeScene.rotation.z = gameLibScene.rotation.z; instanceScene.rotation.z = gameLibScene.rotation.z;
threeScene.scale.x = gameLibScene.scale.x; instanceScene.scale.x = gameLibScene.scale.x;
threeScene.scale.y = gameLibScene.scale.y; instanceScene.scale.y = gameLibScene.scale.y;
threeScene.scale.z = gameLibScene.scale.z; instanceScene.scale.z = gameLibScene.scale.z;
threeScene.quaternion.x = gameLibScene.quaternion.x; instanceScene.quaternion.x = gameLibScene.quaternion.x;
threeScene.quaternion.y = gameLibScene.quaternion.y; instanceScene.quaternion.y = gameLibScene.quaternion.y;
threeScene.quaternion.z = gameLibScene.quaternion.z; instanceScene.quaternion.z = gameLibScene.quaternion.z;
threeScene.quaternion.w = gameLibScene.quaternion.w; instanceScene.quaternion.w = gameLibScene.quaternion.w;
for (var m = 0; m < threeMeshes.length; m++) { for (var m = 0; m < instanceMeshes.length; m++) {
threeScene.add(threeMeshes[m]); instanceScene.add(instanceMeshes[m]);
} }
for (var l = 0; l < threeLights.length; l++) { for (var l = 0; l < instanceLights.length; l++) {
threeScene.add(threeLights[l]); instanceScene.add(instanceLights[l]);
} }
onLoaded( onLoaded(
gameLibScene, gameLibScene,
{ {
scene: threeScene, scene: instanceScene,
lights: threeLights, lights: instanceLights,
meshes: threeMeshes meshes: instanceMeshes
} }
); );
} }
}.bind(this)).catch(function(error){ }.catch(
function(error){
console.log(error); console.log(error);
}); }
));
}; };

View File

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