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

167 lines
4.2 KiB
JavaScript

/**
* Raw Scene API object - should always correspond with the Scene Schema
* @param id String
* @param path String
* @param name String
* @param meshes GameLib.D3.API.Mesh []
* @param position GameLib.API.Vector3
* @param quaternion GameLib.API.Quaternion
* @param scale GameLib.API.Vector3
* @param parentSceneId
* @param lights GameLib.D3.API.Light[]
* @param worlds GameLib.D3.API.World[]
* @param entityManager GameLib.EntityManager
* @param shapes GameLib.D3.API.Shape[]
* @param cameras
* @param activeCameraIndex
* @param textures GameLib.D3.Texture[] - additional textures
* @constructor
*/
GameLib.D3.API.Scene = function(
id,
path,
name,
meshes,
position,
quaternion,
scale,
parentSceneId,
lights,
worlds,
entityManager,
shapes,
cameras,
activeCameraIndex,
textures
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(path)) {
path = null;
}
this.path = path;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'unnamed';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(meshes)) {
meshes = [];
}
this.meshes = meshes;
if (GameLib.Utils.UndefinedOrNull(position)) {
position = new GameLib.API.Vector3();
}
this.position = position;
if (GameLib.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.API.Quaternion();
}
this.quaternion = quaternion;
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1,1,1);
}
this.scale = scale;
if (GameLib.Utils.UndefinedOrNull(parentSceneId)) {
parentSceneId = null;
}
this.parentSceneId = parentSceneId;
if (GameLib.Utils.UndefinedOrNull(lights)) {
lights = [];
}
this.lights = lights;
if (GameLib.Utils.UndefinedOrNull(worlds)) {
worlds = [];
}
this.worlds = worlds;
if (GameLib.Utils.UndefinedOrNull(entityManager)) {
entityManager = new GameLib.API.EntityManager();
}
this.entityManager = entityManager;
if (GameLib.Utils.UndefinedOrNull(shapes)) {
shapes = [];
}
this.shapes = shapes;
if (GameLib.Utils.UndefinedOrNull(cameras)) {
cameras = [];
}
this.cameras = cameras;
if (GameLib.Utils.UndefinedOrNull(activeCameraIndex)) {
activeCameraIndex = 0;
}
this.activeCameraIndex = activeCameraIndex;
if (GameLib.Utils.UndefinedOrNull(textures)) {
textures = [];
}
this.textures = textures;
};
/**
* Returns an API scene from an Object scene
* @param objectScene
* @constructor
*/
GameLib.D3.API.Scene.FromObjectScene = function(objectScene) {
var apiEntityManager = null;
var apiTextures = null;
if (objectScene.entityManager) {
apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager);
}
if (objectScene.textures) {
apiTextures = objectScene.textures.map(
function(objectTexture) {
return GameLib.D3.API.Texture.FromObjectTexture(objectTexture)
}
)
}
return new GameLib.D3.API.Scene(
objectScene.id,
objectScene.path,
objectScene.name,
objectScene.meshes.map(
function (objectMesh) {
return GameLib.D3.API.Mesh.FromObjectMesh(objectMesh)
}
),
GameLib.API.Vector3.FromObjectVector(objectScene.position),
GameLib.API.Quaternion.FromObjectQuaternion(objectScene.quaternion),
GameLib.API.Vector3.FromObjectVector(objectScene.scale),
objectScene.parentSceneId,
objectScene.lights.map(
function (objectLight) {
return GameLib.D3.API.Light.FromObjectLight(objectLight)
}
),
[], //TODO : implement worlds here
apiEntityManager,
[], //TODO : implement shapes here
objectScene.cameras.map(
function (objectCamera) {
return GameLib.D3.API.Camera.FromObjectCamera(objectCamera);
}
),
objectScene.activeCameraIndex,
apiTextures
);
};