start messing around with texture loading

beta.r3js.org
Theunis J. Botha 2016-11-01 14:11:10 +01:00
parent 0ff071bb6b
commit eb38cbe27c
6 changed files with 245 additions and 223 deletions

File diff suppressed because one or more lines are too long

View File

@ -1419,7 +1419,7 @@ GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial";
* @param uploadUrl String * @param uploadUrl String
* @param progressCallback * @param progressCallback
*/ */
GameLib.D3.Material.createInstanceMaterial = function( GameLib.D3.Material.CreateInstanceMaterial = function(
gameLibMaterial, gameLibMaterial,
graphics, graphics,
uploadUrl, uploadUrl,
@ -1571,10 +1571,13 @@ GameLib.D3.Material.createInstanceMaterial = function(
} else { } else {
console.log("material type is not implemented yet: " + gameLibMaterial.materialType + " - material indexes could be screwed up"); console.log("material type is not implemented yet: " + gameLibMaterial.materialType + " - material indexes could be screwed up");
defer.reject(null);
return defer.promise;
} }
if (blenderMaps.length > 0) { if (blenderMaps.length > 0) {
var textureMaps = GameLib.D3.Texture.loadMaps(
var textureMaps = GameLib.D3.Texture.LoadMaps(
gameLibMaterial, gameLibMaterial,
blenderMaps, blenderMaps,
instanceMaterial, instanceMaterial,
@ -1582,16 +1585,22 @@ GameLib.D3.Material.createInstanceMaterial = function(
uploadUrl, uploadUrl,
progressCallback progressCallback
); );
if (textureMaps.length > 0) {
Q.all(textureMaps).then( Q.all(textureMaps).then(
function(){ function onFulfilled(map){
defer.resolve(instanceMaterial); defer.resolve(instanceMaterial);
} },
).catch( function onRejected(message) {
function(error){ console.log(message);
console.log(error); defer.reject(message);
defer.reject(error); },
function onProgress(progress) {
console.log('progress');
} }
) )
}
} else { } else {
defer.resolve(instanceMaterial); defer.resolve(instanceMaterial);
} }
@ -1935,7 +1944,7 @@ GameLib.D3.Mesh.TYPE_SKINNED = 1;
* @param graphics * @param graphics
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Mesh.createInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) { GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) {
var threeMesh = null; var threeMesh = null;
@ -3390,24 +3399,16 @@ GameLib.D3.Scene.loadScene = function(
geometry.computeVertexNormals(); geometry.computeVertexNormals();
} }
var instanceMaterialLoaders = []; var instanceMaterial = GameLib.D3.Material.CreateInstanceMaterial(
materials[0],
/**
* Setup materials
*/
for (var mi = 0; mi < materials.length; mi++) {
instanceMaterialLoaders.push(
GameLib.D3.Material.createInstanceMaterial(
materials[mi],
graphics, graphics,
uploadUrl, uploadUrl,
progressCallback progressCallback
)
); );
}
var result = Q.all(instanceMaterialLoaders).then( var result = instanceMaterial.then(
function(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);
@ -3417,41 +3418,49 @@ GameLib.D3.Scene.loadScene = function(
*/ */
var material = materials[0]; var material = materials[0];
var threeMesh = GameLib.D3.Mesh.createInstanceMesh( var instanceMesh = GameLib.D3.Mesh.CreateInstanceMesh(
mesh, __mesh,
geometry, __geometry,
material, material,
graphics graphics
); );
threeMesh.name = mesh.name; instanceMesh.name = __mesh.name;
threeMesh.position.x = mesh.position.x; instanceMesh.position.x = __mesh.position.x;
threeMesh.position.y = mesh.position.y; instanceMesh.position.y = __mesh.position.y;
threeMesh.position.z = mesh.position.z; instanceMesh.position.z = __mesh.position.z;
threeMesh.rotation.x = mesh.rotation.x; instanceMesh.rotation.x = __mesh.rotation.x;
threeMesh.rotation.y = mesh.rotation.y; instanceMesh.rotation.y = __mesh.rotation.y;
threeMesh.rotation.z = mesh.rotation.z; instanceMesh.rotation.z = __mesh.rotation.z;
threeMesh.scale.x = mesh.scale.x; instanceMesh.scale.x = __mesh.scale.x;
threeMesh.scale.y = mesh.scale.y; instanceMesh.scale.y = __mesh.scale.y;
threeMesh.scale.z = mesh.scale.z; instanceMesh.scale.z = __mesh.scale.z;
threeMesh.quaternion.x = mesh.quaternion.x; instanceMesh.quaternion.x = __mesh.quaternion.x;
threeMesh.quaternion.y = mesh.quaternion.y; instanceMesh.quaternion.y = __mesh.quaternion.y;
threeMesh.quaternion.z = mesh.quaternion.z; instanceMesh.quaternion.z = __mesh.quaternion.z;
threeMesh.quaternion.w = mesh.quaternion.w; instanceMesh.quaternion.w = __mesh.quaternion.w;
return threeMesh; return instanceMesh;
}; };
}(mesh, geometry) })(mesh, geometry),
).catch(function(error){ function onRejected(message) {
console.log(error); console.log(message);
}); return null;
},
function onProgress(progress) {
console.log('material progress');
}
);
meshQ.push(result); meshQ.push(result);
} }
debugger;
console.log('hi');
Q.all(meshQ).then( Q.all(meshQ).then(
function(instanceMeshes){ function(instanceMeshes){
console.log("all meshes have loaded"); console.log("all meshes have loaded");
@ -3541,7 +3550,11 @@ GameLib.D3.Scene.loadScene = function(
} }
); );
} }
}).catch( },
function(error) {
console.log(error);
}
).catch(
function(error){ function(error){
console.log(error); console.log(error);
} }
@ -4162,7 +4175,7 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
* @param progressCallback * @param progressCallback
* @returns {Promise} * @returns {Promise}
*/ */
GameLib.D3.Texture.loadMap = function( GameLib.D3.Texture.LoadMap = function(
gameLibTexture, gameLibTexture,
instanceMaterial, instanceMaterial,
instanceMaterialMapType, instanceMaterialMapType,
@ -4184,7 +4197,10 @@ GameLib.D3.Texture.loadMap = function(
imagePath = uploadUrl + '/' + gameLibTexture.image.filename; imagePath = uploadUrl + '/' + gameLibTexture.image.filename;
} }
if (imagePath) { if (!imagePath) {
defer.reject("No image path");
return defer.promise;
}
textureLoader.crossOrigin = ''; textureLoader.crossOrigin = '';
@ -4231,15 +4247,11 @@ GameLib.D3.Texture.loadMap = function(
/** /**
* onError * onError
*/ */
console.log("an error occurred while trying to load the image : " + imagePath); console.log('Image could not be loaded: ' + imagePath, error);
defer.resolve(null); defer.reject('Image could not be loaded: ' + imagePath);
} }
); );
} else {
defer.resolve(null);
}
return defer.promise; return defer.promise;
}; };
@ -4254,7 +4266,7 @@ GameLib.D3.Texture.loadMap = function(
* @param progressCallback * @param progressCallback
* @returns Q[] * @returns Q[]
*/ */
GameLib.D3.Texture.loadMaps = function( GameLib.D3.Texture.LoadMaps = function(
gameLibMaterial, gameLibMaterial,
blenderMaps, blenderMaps,
instanceMaterial, instanceMaterial,
@ -4262,7 +4274,6 @@ GameLib.D3.Texture.loadMaps = function(
uploadUrl, uploadUrl,
progressCallback progressCallback
) { ) {
var textureMaps = []; var textureMaps = [];
for (var ti = 0; ti < blenderMaps.length; ti++) { for (var ti = 0; ti < blenderMaps.length; ti++) {
@ -4330,7 +4341,7 @@ GameLib.D3.Texture.loadMaps = function(
} }
textureMaps.push( textureMaps.push(
GameLib.D3.Texture.loadMap( GameLib.D3.Texture.LoadMap(
gameLibMaterial.maps[map], gameLibMaterial.maps[map],
instanceMaterial, instanceMaterial,
instanceMap, instanceMap,

View File

@ -513,7 +513,7 @@ GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial";
* @param uploadUrl String * @param uploadUrl String
* @param progressCallback * @param progressCallback
*/ */
GameLib.D3.Material.createInstanceMaterial = function( GameLib.D3.Material.CreateInstanceMaterial = function(
gameLibMaterial, gameLibMaterial,
graphics, graphics,
uploadUrl, uploadUrl,
@ -665,10 +665,13 @@ GameLib.D3.Material.createInstanceMaterial = function(
} else { } else {
console.log("material type is not implemented yet: " + gameLibMaterial.materialType + " - material indexes could be screwed up"); console.log("material type is not implemented yet: " + gameLibMaterial.materialType + " - material indexes could be screwed up");
defer.reject(null);
return defer.promise;
} }
if (blenderMaps.length > 0) { if (blenderMaps.length > 0) {
var textureMaps = GameLib.D3.Texture.loadMaps(
var textureMaps = GameLib.D3.Texture.LoadMaps(
gameLibMaterial, gameLibMaterial,
blenderMaps, blenderMaps,
instanceMaterial, instanceMaterial,
@ -676,16 +679,22 @@ GameLib.D3.Material.createInstanceMaterial = function(
uploadUrl, uploadUrl,
progressCallback progressCallback
); );
if (textureMaps.length > 0) {
Q.all(textureMaps).then( Q.all(textureMaps).then(
function(){ function onFulfilled(map){
defer.resolve(instanceMaterial); defer.resolve(instanceMaterial);
} },
).catch( function onRejected(message) {
function(error){ console.log(message);
console.log(error); defer.reject(message);
defer.reject(error); },
function onProgress(progress) {
console.log('progress');
} }
) )
}
} else { } else {
defer.resolve(instanceMaterial); defer.resolve(instanceMaterial);
} }

View File

@ -124,7 +124,7 @@ GameLib.D3.Mesh.TYPE_SKINNED = 1;
* @param graphics * @param graphics
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Mesh.createInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) { GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) {
var threeMesh = null; var threeMesh = null;

View File

@ -470,24 +470,16 @@ GameLib.D3.Scene.loadScene = function(
geometry.computeVertexNormals(); geometry.computeVertexNormals();
} }
var instanceMaterialLoaders = []; var instanceMaterial = GameLib.D3.Material.CreateInstanceMaterial(
materials[0],
/**
* Setup materials
*/
for (var mi = 0; mi < materials.length; mi++) {
instanceMaterialLoaders.push(
GameLib.D3.Material.createInstanceMaterial(
materials[mi],
graphics, graphics,
uploadUrl, uploadUrl,
progressCallback progressCallback
)
); );
}
var result = Q.all(instanceMaterialLoaders).then( var result = instanceMaterial.then(
function(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);
@ -497,41 +489,49 @@ GameLib.D3.Scene.loadScene = function(
*/ */
var material = materials[0]; var material = materials[0];
var threeMesh = GameLib.D3.Mesh.createInstanceMesh( var instanceMesh = GameLib.D3.Mesh.CreateInstanceMesh(
mesh, __mesh,
geometry, __geometry,
material, material,
graphics graphics
); );
threeMesh.name = mesh.name; instanceMesh.name = __mesh.name;
threeMesh.position.x = mesh.position.x; instanceMesh.position.x = __mesh.position.x;
threeMesh.position.y = mesh.position.y; instanceMesh.position.y = __mesh.position.y;
threeMesh.position.z = mesh.position.z; instanceMesh.position.z = __mesh.position.z;
threeMesh.rotation.x = mesh.rotation.x; instanceMesh.rotation.x = __mesh.rotation.x;
threeMesh.rotation.y = mesh.rotation.y; instanceMesh.rotation.y = __mesh.rotation.y;
threeMesh.rotation.z = mesh.rotation.z; instanceMesh.rotation.z = __mesh.rotation.z;
threeMesh.scale.x = mesh.scale.x; instanceMesh.scale.x = __mesh.scale.x;
threeMesh.scale.y = mesh.scale.y; instanceMesh.scale.y = __mesh.scale.y;
threeMesh.scale.z = mesh.scale.z; instanceMesh.scale.z = __mesh.scale.z;
threeMesh.quaternion.x = mesh.quaternion.x; instanceMesh.quaternion.x = __mesh.quaternion.x;
threeMesh.quaternion.y = mesh.quaternion.y; instanceMesh.quaternion.y = __mesh.quaternion.y;
threeMesh.quaternion.z = mesh.quaternion.z; instanceMesh.quaternion.z = __mesh.quaternion.z;
threeMesh.quaternion.w = mesh.quaternion.w; instanceMesh.quaternion.w = __mesh.quaternion.w;
return threeMesh; return instanceMesh;
}; };
}(mesh, geometry) })(mesh, geometry),
).catch(function(error){ function onRejected(message) {
console.log(error); console.log(message);
}); return null;
},
function onProgress(progress) {
console.log('material progress');
}
);
meshQ.push(result); meshQ.push(result);
} }
debugger;
console.log('hi');
Q.all(meshQ).then( Q.all(meshQ).then(
function(instanceMeshes){ function(instanceMeshes){
console.log("all meshes have loaded"); console.log("all meshes have loaded");
@ -621,7 +621,11 @@ GameLib.D3.Scene.loadScene = function(
} }
); );
} }
}).catch( },
function(error) {
console.log(error);
}
).catch(
function(error){ function(error){
console.log(error); console.log(error);
} }

View File

@ -213,7 +213,7 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
* @param progressCallback * @param progressCallback
* @returns {Promise} * @returns {Promise}
*/ */
GameLib.D3.Texture.loadMap = function( GameLib.D3.Texture.LoadMap = function(
gameLibTexture, gameLibTexture,
instanceMaterial, instanceMaterial,
instanceMaterialMapType, instanceMaterialMapType,
@ -235,7 +235,10 @@ GameLib.D3.Texture.loadMap = function(
imagePath = uploadUrl + '/' + gameLibTexture.image.filename; imagePath = uploadUrl + '/' + gameLibTexture.image.filename;
} }
if (imagePath) { if (!imagePath) {
defer.reject("No image path");
return defer.promise;
}
textureLoader.crossOrigin = ''; textureLoader.crossOrigin = '';
@ -282,15 +285,11 @@ GameLib.D3.Texture.loadMap = function(
/** /**
* onError * onError
*/ */
console.log("an error occurred while trying to load the image : " + imagePath); console.log('Image could not be loaded: ' + imagePath, error);
defer.resolve(null); defer.reject('Image could not be loaded: ' + imagePath);
} }
); );
} else {
defer.resolve(null);
}
return defer.promise; return defer.promise;
}; };
@ -305,7 +304,7 @@ GameLib.D3.Texture.loadMap = function(
* @param progressCallback * @param progressCallback
* @returns Q[] * @returns Q[]
*/ */
GameLib.D3.Texture.loadMaps = function( GameLib.D3.Texture.LoadMaps = function(
gameLibMaterial, gameLibMaterial,
blenderMaps, blenderMaps,
instanceMaterial, instanceMaterial,
@ -313,7 +312,6 @@ GameLib.D3.Texture.loadMaps = function(
uploadUrl, uploadUrl,
progressCallback progressCallback
) { ) {
var textureMaps = []; var textureMaps = [];
for (var ti = 0; ti < blenderMaps.length; ti++) { for (var ti = 0; ti < blenderMaps.length; ti++) {
@ -381,7 +379,7 @@ GameLib.D3.Texture.loadMaps = function(
} }
textureMaps.push( textureMaps.push(
GameLib.D3.Texture.loadMap( GameLib.D3.Texture.LoadMap(
gameLibMaterial.maps[map], gameLibMaterial.maps[map],
instanceMaterial, instanceMaterial,
instanceMap, instanceMap,