r3-legacy/src/game-lib-d3-scene.js

353 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-10-14 12:32:53 +02:00
/**
2016-11-17 18:31:41 +01:00
* Scene Superset - The apiScene properties get moved into the Scene object itself, and then the instance is
* created
* @param graphics
2016-11-29 12:54:25 +01:00
* @param apiScene GameLib.D3.API.Scene
2017-01-18 16:03:44 +01:00
* @param imageFactory
2017-01-05 19:34:28 +01:00
* @param computeNormals
2016-11-17 18:31:41 +01:00
* @constructor
*/
2017-01-17 17:16:10 +01:00
GameLib.D3.Scene = function (
2016-11-17 18:31:41 +01:00
graphics,
2016-11-21 16:08:39 +01:00
apiScene,
2017-01-05 19:34:28 +01:00
computeNormals
2016-11-17 18:31:41 +01:00
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiScene)) {
apiScene = {};
}
if (GameLib.Utils.UndefinedOrNull(computeNormals)) {
computeNormals = true;
}
this.computeNormals = computeNormals;
2016-12-15 15:28:00 +01:00
GameLib.D3.API.Scene.call(
this,
apiScene.id,
apiScene.name,
2017-02-21 18:55:18 +01:00
apiScene.imageFactory,
2017-01-10 17:04:30 +01:00
apiScene.meshes,
apiScene.position,
apiScene.quaternion,
apiScene.scale,
apiScene.parentGameId,
apiScene.lights,
2017-01-31 11:37:55 +01:00
apiScene.textures,
2017-01-10 17:04:30 +01:00
apiScene.materials,
apiScene.parentEntity
2017-01-05 19:34:28 +01:00
);
2017-02-21 18:55:18 +01:00
if (this.imageFactory instanceof GameLib.D3.API.ImageFactory) {
this.imageFactory = new GameLib.D3.ImageFactory(
this.graphics,
this.imageFactory
);
2017-01-17 17:16:10 +01:00
}
2017-01-10 17:04:30 +01:00
this.meshes = this.meshes.map(
function(apiMesh) {
2017-01-06 16:53:53 +01:00
2017-01-19 17:50:11 +01:00
if (apiMesh instanceof GameLib.D3.API.Mesh) {
2017-01-09 15:20:48 +01:00
2017-01-19 17:50:11 +01:00
return new GameLib.D3.Mesh(
this.graphics,
apiMesh,
2017-02-01 16:09:34 +01:00
this.imageFactory,
this.computeNormals
2017-01-19 17:50:11 +01:00
);
2017-01-09 15:20:48 +01:00
2017-01-19 17:50:11 +01:00
} else {
console.warn('apiMesh not an instance of API.Mesh');
throw new Error('apiMesh not an instance of API.Mesh');
2017-01-09 15:20:48 +01:00
}
}.bind(this)
);
2016-12-15 14:53:39 +01:00
this.position = new GameLib.Vector3(
this.graphics,
this.position,
this
2016-12-02 16:03:03 +01:00
);
2016-12-15 15:28:00 +01:00
this.quaternion = new GameLib.Quaternion(
this.graphics,
this.quaternion,
this
2016-12-02 16:03:03 +01:00
);
2016-12-15 15:28:00 +01:00
this.scale = new GameLib.Vector3(
this.graphics,
this.scale,
this
2016-12-02 16:03:03 +01:00
);
2017-01-10 17:04:30 +01:00
this.lights = this.lights.map(
function(apiLight) {
2017-01-09 15:20:48 +01:00
if (apiLight instanceof GameLib.D3.API.Light) {
2017-01-19 17:50:11 +01:00
return new GameLib.D3.Light(
this.graphics,
apiLight
);
} else {
2017-01-19 17:50:11 +01:00
console.warn('apiLight not an instance of API.Light');
throw new Error('apiLight not an instance of API.Light');
}
2017-01-09 15:20:48 +01:00
2017-01-10 17:04:30 +01:00
}.bind(this)
);
2017-01-31 11:37:55 +01:00
this.textures = this.textures.map(
function(apiTexture) {
if (apiTexture instanceof GameLib.D3.API.Texture) {
2017-02-01 16:09:34 +01:00
var texture = new GameLib.D3.Texture(
2017-01-31 11:37:55 +01:00
this.graphics,
apiTexture,
this.imageFactory
);
2017-02-01 16:09:34 +01:00
this.idToObject[texture.id] = texture;
return texture;
2017-01-31 11:37:55 +01:00
} else {
console.warn('apiTexture not an instance of API.Texture');
throw new Error('apiTexture not an instance of API.Texture');
}
}.bind(this)
);
this.materials = this.materials.map(
function(apiMaterial) {
if (apiMaterial instanceof GameLib.D3.API.Material) {
2017-02-01 16:09:34 +01:00
var material = new GameLib.D3.Material(
2017-01-31 11:37:55 +01:00
this.graphics,
apiMaterial,
this.imageFactory
);
2017-02-01 16:09:34 +01:00
this.idToObject[material.id] = material;
return material;
2017-01-31 11:37:55 +01:00
} else {
console.warn('apiMaterial not an instance of API.Material');
throw new Error('apiMaterial not an instance of API.Material');
}
2017-02-01 16:09:34 +01:00
2017-01-31 11:37:55 +01:00
}.bind(this)
);
2017-02-01 16:09:34 +01:00
this.idToObject[this.id] = this;
2017-02-01 16:09:34 +01:00
this.linkObjects(this.idToObject);
2016-12-15 15:28:00 +01:00
2017-01-31 15:23:38 +01:00
this.meshes.map(
function(mesh) {
2017-02-01 16:09:34 +01:00
mesh.updateInstance();
2017-01-31 15:23:38 +01:00
mesh.materials.map(
function(material) {
2017-02-01 16:09:34 +01:00
material.updateInstance();
2017-01-31 15:23:38 +01:00
}
)
}
2017-02-01 16:09:34 +01:00
);
this.buildIdToObject();
this.instance = this.createInstance();
2017-01-31 15:23:38 +01:00
};
2017-02-01 16:09:34 +01:00
GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype);
GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene;
2016-11-17 18:31:41 +01:00
/**
* Creates an instance scene
2017-01-10 17:04:30 +01:00
* @returns {THREE.Scene}
2016-11-17 18:31:41 +01:00
*/
GameLib.D3.Scene.prototype.createInstance = function() {
2016-11-01 09:43:36 +01:00
2016-12-09 20:32:09 +01:00
var instance = new THREE.Scene();
2016-11-03 16:15:19 +01:00
2016-11-17 18:31:41 +01:00
instance.name = this.name;
2016-11-03 14:33:14 +01:00
2016-12-02 16:03:03 +01:00
instance.position = this.position.instance;
2016-11-03 14:33:14 +01:00
2016-12-02 16:03:03 +01:00
instance.scale = this.scale.instance;
2016-11-10 14:12:41 +01:00
2016-12-02 16:03:03 +01:00
instance.quaternion = this.quaternion.instance;
2016-11-10 14:12:41 +01:00
2016-11-17 18:31:41 +01:00
for (var im = 0; im < this.meshes.length; im++) {
instance.add(this.meshes[im].instance);
}
for (var l = 0; l < this.lights.length; l++) {
instance.add(this.lights[l].instance);
}
2016-11-10 14:12:41 +01:00
return instance;
};
2016-11-03 14:33:14 +01:00
/**
2016-12-09 20:32:09 +01:00
* Converts a GameLib.D3.Scene to a GameLib.D3.API.Scene
* @returns {GameLib.D3.API.Scene}
2016-11-03 14:33:14 +01:00
*/
2016-12-09 20:32:09 +01:00
GameLib.D3.Scene.prototype.toApiScene = function() {
2016-11-17 18:31:41 +01:00
2017-02-21 18:55:18 +01:00
var apiImageFactory = null;
if (this.imageFactory instanceof GameLib.D3.ImageFactory) {
apiImageFactory = this.imageFactory.toApiImageFactory();
}
2016-12-09 20:32:09 +01:00
var apiMeshes = this.meshes.map(
function(mesh) {
return mesh.toApiMesh();
2016-11-03 14:33:14 +01:00
}
2016-12-09 20:32:09 +01:00
);
2016-11-03 14:33:14 +01:00
2016-12-09 20:32:09 +01:00
var apiLights = this.lights.map(
function(light) {
return light.toApiLight();
}
);
2016-11-03 14:33:14 +01:00
2017-01-31 11:37:55 +01:00
var apiTextures = this.textures.map(
function(texture) {
return texture.toApiTexture();
}
);
var apiMaterials = this.materials.map(
function(material) {
return material.toApiMaterial();
}
);
2016-12-09 20:32:09 +01:00
return new GameLib.D3.API.Scene(
this.id,
this.name,
2017-02-21 18:55:18 +01:00
apiImageFactory,
2016-12-09 20:32:09 +01:00
apiMeshes,
this.position.toApiVector(),
this.quaternion.toApiQuaternion(),
2016-12-09 20:32:09 +01:00
this.scale.toApiVector(),
2017-01-10 17:04:30 +01:00
this.parentGameId,
2016-12-09 20:32:09 +01:00
apiLights,
2017-01-31 11:37:55 +01:00
apiTextures,
apiMaterials,
2017-01-10 17:04:30 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
2016-12-09 20:32:09 +01:00
);
};
2016-11-21 16:08:39 +01:00
2016-12-09 20:32:09 +01:00
/**
* Converts a scene Object to a GameLib.D3.Scene object
* @param graphics GameLib.D3.Graphics
* @param objectScene Object
2017-01-20 13:40:27 +01:00
* @param computeNormals boolean to indicate whether or not to recalculate normals
2016-12-09 20:32:09 +01:00
* @returns {GameLib.D3.Scene}
* @constructor
*/
GameLib.D3.Scene.FromObjectScene = function(
graphics,
objectScene,
2017-01-20 13:40:27 +01:00
computeNormals
2016-12-09 20:32:09 +01:00
) {
2017-01-05 19:34:28 +01:00
var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene);
2016-11-29 12:54:25 +01:00
2016-12-09 20:32:09 +01:00
return new GameLib.D3.Scene(
graphics,
apiScene,
2017-01-06 16:53:53 +01:00
computeNormals
2016-12-09 20:32:09 +01:00
);
};
2016-11-29 12:54:25 +01:00
2016-12-09 20:32:09 +01:00
/**
* Transforms raw scene data into a GameLib.D3.Scene
* @param graphics
2017-01-10 17:04:30 +01:00
* @param objectScene Object (as it comes from the API)
2016-12-09 20:32:09 +01:00
* @param computeNormals
2017-01-10 17:04:30 +01:00
* @param onLoaded
2016-12-09 20:32:09 +01:00
* @constructor
*/
GameLib.D3.Scene.LoadScene = function(
graphics,
2017-01-10 17:04:30 +01:00
objectScene,
computeNormals,
2017-02-21 18:55:18 +01:00
onLoaded
2016-12-09 20:32:09 +01:00
) {
2017-01-10 17:04:30 +01:00
var scene = GameLib.D3.Scene.FromObjectScene(
graphics,
objectScene,
2017-01-20 13:40:27 +01:00
computeNormals
2016-11-03 14:33:14 +01:00
);
2017-01-10 17:04:30 +01:00
onLoaded(scene);
2016-10-14 12:32:53 +02:00
};
/**
* Loads a scene directly from the API
2016-10-25 17:57:32 +02:00
* @param graphics GameLib.D3.Graphics
2017-01-10 17:04:30 +01:00
* @param partialSceneObject Object {path: '', name: ''}
2016-10-25 17:57:32 +02:00
* @param apiUrl
2017-01-10 17:04:30 +01:00
* @param onLoaded
2016-10-14 12:32:53 +02:00
*/
GameLib.D3.Scene.LoadSceneFromApi = function(
2016-10-25 17:57:32 +02:00
graphics,
2017-01-10 17:04:30 +01:00
partialSceneObject,
apiUrl,
2017-02-21 18:55:18 +01:00
onLoaded
2016-10-25 17:57:32 +02:00
) {
2016-10-14 12:32:53 +02:00
/**
* First check if this is a client or server side request
*/
if (typeof XMLHttpRequest == 'undefined') {
console.warn('implement server side loading from API here');
return onLoaded(null, new Error('not implemented'));
}
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
2016-11-17 18:31:41 +01:00
apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name
2016-10-14 12:32:53 +02:00
);
2016-10-25 17:57:32 +02:00
xhr.onreadystatechange = function(xhr) {
2016-11-17 18:31:41 +01:00
2016-10-14 12:32:53 +02:00
return function() {
2016-11-17 18:31:41 +01:00
2016-10-14 12:32:53 +02:00
if (xhr.readyState == 4) {
2017-01-10 17:04:30 +01:00
try {
var response = JSON.parse(xhr.responseText);
} catch (e) {
return onLoaded(null, new Error('Could not load scene : ' + e.message));
}
2016-10-14 12:32:53 +02:00
if (!response.scene || response.scene.length == 0) {
2017-01-10 17:04:30 +01:00
return onLoaded(null, new Error('Could not load scene'));
2016-10-14 12:32:53 +02:00
}
2017-01-10 17:04:30 +01:00
var objectScene = response.scene[0];
2016-10-14 12:32:53 +02:00
2017-01-10 17:04:30 +01:00
GameLib.D3.Scene.LoadScene(
graphics,
objectScene,
true,
2017-02-21 18:55:18 +01:00
onLoaded
2017-01-10 17:04:30 +01:00
);
2016-10-14 12:32:53 +02:00
}
}
2016-10-25 17:57:32 +02:00
}(xhr);
2016-10-14 12:32:53 +02:00
xhr.send();
2017-01-10 17:04:30 +01:00
};