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

254 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-11-29 12:54:25 +01:00
/**
* Raw Scene API object - should always correspond with the Scene Schema
* @param id String
* @param path String
* @param name String
2017-01-09 15:20:48 +01:00
* @param width
* @param height
2016-11-29 12:54:25 +01:00
* @param meshes GameLib.D3.API.Mesh []
2016-12-15 14:53:39 +01:00
* @param position GameLib.API.Vector3
2016-12-15 15:28:00 +01:00
* @param quaternion GameLib.API.Quaternion
2016-12-15 14:53:39 +01:00
* @param scale GameLib.API.Vector3
2016-11-29 12:54:25 +01:00
* @param parentSceneId
* @param lights GameLib.D3.API.Light[]
* @param worlds GameLib.D3.API.World[]
2016-12-15 15:28:00 +01:00
* @param entityManager GameLib.EntityManager
2016-11-29 12:54:25 +01:00
* @param shapes GameLib.D3.API.Shape[]
* @param cameras
* @param activeCameraIndex
2017-01-06 16:53:53 +01:00
* @param textures GameLib.D3.Texture[] - additional textures
2017-01-09 15:20:48 +01:00
* @param renderers
* @param activeRendererIndex
* @param raycaster
* @param mouse
2016-11-29 12:54:25 +01:00
* @constructor
*/
GameLib.D3.API.Scene = function(
id,
path,
name,
2017-01-09 15:20:48 +01:00
width,
height,
2016-11-29 12:54:25 +01:00
meshes,
position,
2016-12-15 15:28:00 +01:00
quaternion,
2016-11-29 12:54:25 +01:00
scale,
parentSceneId,
lights,
worlds,
2016-12-15 15:28:00 +01:00
entityManager,
2016-11-29 12:54:25 +01:00
shapes,
cameras,
2017-01-06 16:53:53 +01:00
activeCameraIndex,
2017-01-09 15:20:48 +01:00
textures,
renderers,
activeRendererIndex,
raycaster,
mouse
2016-11-29 12:54:25 +01:00
) {
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
2016-11-29 12:54:25 +01:00
}
this.id = id;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(path)) {
2016-11-29 12:54:25 +01:00
path = null;
}
this.path = path;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(name)) {
2016-11-29 12:54:25 +01:00
name = 'unnamed';
}
this.name = name;
2017-01-09 15:20:48 +01:00
if (GameLib.Utils.UndefinedOrNull(width)) {
width = 800;
}
this.width = width;
if (GameLib.Utils.UndefinedOrNull(height)) {
height = 600;
}
this.height = height;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(meshes)) {
2016-11-29 12:54:25 +01:00
meshes = [];
}
this.meshes = meshes;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(position)) {
position = new GameLib.API.Vector3();
2016-11-29 12:54:25 +01:00
}
this.position = position;
2016-12-15 15:28:00 +01:00
if (GameLib.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.API.Quaternion();
}
this.quaternion = quaternion;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1,1,1);
2016-11-29 12:54:25 +01:00
}
this.scale = scale;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(parentSceneId)) {
2016-11-29 12:54:25 +01:00
parentSceneId = null;
}
this.parentSceneId = parentSceneId;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(lights)) {
2016-11-29 12:54:25 +01:00
lights = [];
}
this.lights = lights;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(worlds)) {
2016-11-29 12:54:25 +01:00
worlds = [];
}
this.worlds = worlds;
2016-12-15 15:28:00 +01:00
if (GameLib.Utils.UndefinedOrNull(entityManager)) {
entityManager = new GameLib.API.EntityManager();
2016-11-29 12:54:25 +01:00
}
2016-12-15 15:28:00 +01:00
this.entityManager = entityManager;
2016-11-29 12:54:25 +01:00
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(shapes)) {
2016-11-29 12:54:25 +01:00
shapes = [];
}
this.shapes = shapes;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(cameras)) {
2017-01-09 15:20:48 +01:00
cameras = [
new GameLib.D3.API.Camera(
null,
GameLib.Component.COMPONENT_CAMERA,
null,
null,
this.width / this.height
)
];
2016-11-29 12:54:25 +01:00
}
this.cameras = cameras;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(activeCameraIndex)) {
2016-11-29 12:54:25 +01:00
activeCameraIndex = 0;
}
this.activeCameraIndex = activeCameraIndex;
2017-01-06 16:53:53 +01:00
if (GameLib.Utils.UndefinedOrNull(textures)) {
textures = [];
}
this.textures = textures;
2017-01-09 15:20:48 +01:00
if (GameLib.Utils.UndefinedOrNull(renderers)) {
renderers = [
new GameLib.D3.API.Renderer(
null,
null,
GameLib.Component.COMPONENT_RENDERER,
this,
this.cameras[this.activeCameraIndex],
true,
false,
this.width,
this.height
)
];
}
this.renderers = renderers;
if (GameLib.Utils.UndefinedOrNull(activeRendererIndex)) {
activeRendererIndex = 0;
}
this.activeRendererIndex = activeRendererIndex;
if (GameLib.Utils.UndefinedOrNull(raycaster)) {
raycaster = new GameLib.D3.API.Raycaster();
}
this.raycaster = raycaster;
if (GameLib.Utils.UndefinedOrNull(mouse)) {
mouse = new GameLib.API.Mouse();
}
this.mouse = mouse;
2016-11-29 12:54:25 +01:00
};
2017-01-05 19:34:28 +01:00
/**
* Returns an API scene from an Object scene
* @param objectScene
* @constructor
*/
GameLib.D3.API.Scene.FromObjectScene = function(objectScene) {
var apiEntityManager = null;
2017-01-06 16:53:53 +01:00
var apiTextures = null;
2017-01-09 15:20:48 +01:00
var apiRenderers = null;
var apiRaycaster = null;
var apiMouse = null;
2017-01-06 16:53:53 +01:00
2017-01-05 19:34:28 +01:00
if (objectScene.entityManager) {
apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager);
}
2017-01-06 16:53:53 +01:00
if (objectScene.textures) {
apiTextures = objectScene.textures.map(
function(objectTexture) {
return GameLib.D3.API.Texture.FromObjectTexture(objectTexture)
}
)
}
2017-01-09 15:20:48 +01:00
if (objectScene.renderers) {
apiRenderers = objectScene.renderers.map(
function(objectRenderer) {
return GameLib.D3.API.Renderer.FromObjectComponent(objectRenderer)
}
)
}
if (objectScene.raycaster) {
apiRaycaster = GameLib.D3.API.Raycaster.FromObjectRaycaster(objectScene.raycaster);
}
if (objectScene.mouse) {
apiMouse = GameLib.API.Mouse.FromObjectMouse(objectScene.mouse);
}
2017-01-05 19:34:28 +01:00
return new GameLib.D3.API.Scene(
objectScene.id,
objectScene.path,
objectScene.name,
2017-01-09 15:20:48 +01:00
objectScene.width,
objectScene.height,
2017-01-05 19:34:28 +01:00
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);
}
),
2017-01-06 16:53:53 +01:00
objectScene.activeCameraIndex,
2017-01-09 15:20:48 +01:00
apiTextures,
apiRenderers,
objectScene.activeRendererIndex,
apiRaycaster,
apiMouse
2017-01-05 19:34:28 +01:00
);
};