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

388 lines
9.2 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
* @param progressCallback
2016-11-29 12:54:25 +01:00
* @param apiScene GameLib.D3.API.Scene
2016-11-28 15:05:02 +01:00
* @param imageFactory
2016-11-17 18:31:41 +01:00
* @constructor
*/
2016-11-21 16:08:39 +01:00
GameLib.D3.Scene = function Scene(
2016-11-17 18:31:41 +01:00
graphics,
progressCallback,
2016-11-21 16:08:39 +01:00
apiScene,
2016-12-09 20:32:09 +01:00
imageFactory
2016-11-17 18:31:41 +01:00
) {
2016-12-09 20:32:09 +01:00
var property;
for (property in apiScene) {
2016-11-17 18:31:41 +01:00
if (apiScene.hasOwnProperty(property)) {
this[property] = apiScene[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-12-15 14:53:39 +01:00
this.position = new GameLib.Vector3(
2016-12-02 16:03:03 +01:00
graphics,
this,
this.position
);
2016-12-15 14:53:39 +01:00
this.scale = new GameLib.Vector3(
2016-12-02 16:03:03 +01:00
graphics,
this,
this.scale
);
2016-12-15 14:53:39 +01:00
this.quaternion = new GameLib.Quaternion(
2016-12-02 16:03:03 +01:00
graphics,
this,
this.quaternion
);
2016-11-17 18:31:41 +01:00
this.progressCallback = progressCallback;
this.instance = this.createInstance();
2016-11-18 16:00:13 +01:00
2016-11-21 16:08:39 +01:00
this.imageFactory = imageFactory;
2016-12-09 20:32:09 +01:00
this.interestingProperties = [
"cameras",
"meshes",
"lights",
"components",
"entities",
"splines"
];
this.idToObject = {};
this.idToObject[this.id] = this;
for (var p = 0; p < this.interestingProperties.length; p++) {
property = this.interestingProperties[p];
if (this.hasOwnProperty(property)) {
for (var i = 0; i < this[property].length; i++) {
var object = this[property][i];
this.idToObject[object.id] = object;
}
}
}
2016-11-17 18:31:41 +01:00
2016-12-09 20:32:09 +01:00
this.linkObjects();
};
2016-11-17 18:31:41 +01:00
/**
* Creates an instance scene
* @returns {GameLib.D3.Scene|THREE.Scene|ApiLib.Scene|*|Scene}
*/
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
instance.render = true;
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
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
2016-12-09 20:32:09 +01:00
var apiComponents = this.components.map(
function(component) {
return component.toApiComponent();
}
);
2016-11-03 14:33:14 +01:00
2016-12-09 20:32:09 +01:00
var apiEntities = this.entities.map(
function(entity) {
return entity.toApiEntity();
}
);
2016-11-03 14:33:14 +01:00
2016-12-09 20:32:09 +01:00
var apiCameras = this.cameras.map(
function(camera) {
return camera.toApiCamera();
}
);
2016-11-03 14:33:14 +01:00
2016-12-09 20:32:09 +01:00
var apiSplines = this.splines.map(
function(spline) {
return spline.toApiSpline();
2016-11-03 14:33:14 +01:00
}
2016-12-09 20:32:09 +01:00
);
2016-11-17 18:31:41 +01:00
2016-12-09 20:32:09 +01:00
var apiWorlds = this.worlds.map(
function(world) {
return world.toApiWorld();
}
);
2016-11-21 16:08:39 +01:00
2016-12-09 20:32:09 +01:00
var apiShapes = this.shapes.map(
function(shape) {
return shape.toApiShape();
}
);
2016-11-21 16:08:39 +01:00
2016-12-09 20:32:09 +01:00
return new GameLib.D3.API.Scene(
this.id,
this.path,
this.name,
apiMeshes,
this.quaternion.toApiQuaternion(),
this.position.toApiVector(),
this.scale.toApiVector(),
this.parentSceneId,
apiLights,
apiWorlds,
apiEntities,
apiComponents,
apiShapes,
apiCameras,
this.activeCameraIndex,
apiSplines
);
};
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
* @param computeNormals boolean to indicate whether or not to recalculate normals
* @param imageFactory GameLib.D3.ImageFactory
* @param progressCallback callback
* @returns {GameLib.D3.Scene}
* @constructor
*/
GameLib.D3.Scene.FromObjectScene = function(
graphics,
objectScene,
computeNormals,
imageFactory,
progressCallback
) {
var apiScene = new GameLib.D3.API.Scene(
objectScene.id,
objectScene.path,
objectScene.name,
objectScene.meshes.map(
function (objectMesh) {
return GameLib.D3.Mesh.FromObjectMesh(
graphics,
objectMesh,
computeNormals,
imageFactory
2016-11-21 16:08:39 +01:00
)
}
2016-12-09 20:32:09 +01:00
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
objectScene.quaternion.x,
objectScene.quaternion.y,
objectScene.quaternion.z,
objectScene.quaternion.w,
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
objectScene.quaternion.axis.x,
objectScene.quaternion.axis.y,
objectScene.quaternion.axis.z
),
objectScene.quaternion.angle
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
objectScene.position.x,
objectScene.position.y,
objectScene.position.z
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
objectScene.scale.x,
objectScene.scale.y,
objectScene.scale.z
),
objectScene.parentSceneId,
objectScene.lights.map(
function (objectLight) {
return GameLib.D3.Light.FromObjectLight(
graphics,
objectLight
2016-11-21 16:08:39 +01:00
)
2016-12-09 20:32:09 +01:00
}
),
objectScene.worlds.map(
function (objectWorld) {
return GameLib.D3.World.FromObjectWorld(
graphics,
objectWorld
);
}
),
objectScene.entities.map(
function (objectEntity) {
2016-12-15 14:53:39 +01:00
return GameLib.Entity.FromObjectEntity(
2016-12-09 20:32:09 +01:00
graphics,
objectEntity
);
}
),
objectScene.components.map(
function (objectComponent) {
2016-12-15 14:53:39 +01:00
return GameLib.Component.FromObjectComponent(
2016-12-09 20:32:09 +01:00
graphics,
objectComponent
);
}
),
objectScene.shapes.map(
function (objectShape) {
return GameLib.D3.Shape.FromObjectShape(
graphics,
objectShape
);
}
),
objectScene.cameras.map(
function (objectCamera) {
return GameLib.D3.Camera.FromObjectCamera(
graphics,
objectCamera
);
}
),
objectScene.activeCameraIndex,
objectScene.splines.map(
function (objectSpline) {
return GameLib.D3.Spline.FromObjectSpline(
graphics,
objectSpline
2016-11-18 16:00:13 +01:00
)
2016-12-09 20:32:09 +01:00
}
)
);
2016-11-29 12:54:25 +01:00
2016-12-09 20:32:09 +01:00
return new GameLib.D3.Scene(
graphics,
progressCallback,
apiScene,
imageFactory
);
2016-11-29 12:54:25 +01:00
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 objectScene Object (as it comes from the API)
* @param onLoaded
* @param graphics
* @param uploadUrl
* @param progressCallback
* @param computeNormals
* @constructor
*/
GameLib.D3.Scene.LoadScene = function(
objectScene,
onLoaded,
graphics,
uploadUrl,
progressCallback,
computeNormals
) {
onLoaded(
GameLib.D3.Scene.FromObjectScene(
2016-11-29 12:54:25 +01:00
graphics,
2016-12-09 20:32:09 +01:00
objectScene,
computeNormals,
GameLib.D3.ImageFactory(
graphics,
uploadUrl
2016-11-17 18:31:41 +01:00
),
2016-12-09 20:32:09 +01:00
progressCallback
)
2016-11-03 14:33:14 +01:00
);
2016-10-14 12:32:53 +02:00
};
/**
* Loads a scene directly from the API
2016-11-17 18:31:41 +01:00
* @param partialSceneObject Object {path: '', name: ''}
2016-10-14 12:32:53 +02:00
* @param onLoaded callback
2016-10-25 17:57:32 +02:00
* @param graphics GameLib.D3.Graphics
2016-10-26 16:13:18 +02:00
* @param uploadUrl String
2016-10-25 17:57:32 +02:00
* @param progressCallback callback
* @param apiUrl
2016-10-14 12:32:53 +02:00
*/
GameLib.D3.Scene.LoadSceneFromApi = function(
2016-11-17 18:31:41 +01:00
partialSceneObject,
2016-10-25 17:57:32 +02:00
onLoaded,
graphics,
2016-10-26 16:13:18 +02:00
uploadUrl,
2016-10-25 17:57:32 +02:00
progressCallback,
apiUrl
) {
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) {
var response = JSON.parse(xhr.responseText);
if (!response.scene || response.scene.length == 0) {
return onLoaded(null, null, new Error('Could not load scene'));
}
var scene = response.scene[0];
GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback, true);
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();
};