254 lines
6.4 KiB
JavaScript
254 lines
6.4 KiB
JavaScript
/**
|
|
* Raw Scene API object - should always correspond with the Scene Schema
|
|
* @param id String
|
|
* @param path String
|
|
* @param name String
|
|
* @param width
|
|
* @param height
|
|
* @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
|
|
* @param renderers
|
|
* @param activeRendererIndex
|
|
* @param raycaster
|
|
* @param mouse
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.API.Scene = function(
|
|
id,
|
|
path,
|
|
name,
|
|
width,
|
|
height,
|
|
meshes,
|
|
position,
|
|
quaternion,
|
|
scale,
|
|
parentSceneId,
|
|
lights,
|
|
worlds,
|
|
entityManager,
|
|
shapes,
|
|
cameras,
|
|
activeCameraIndex,
|
|
textures,
|
|
renderers,
|
|
activeRendererIndex,
|
|
raycaster,
|
|
mouse
|
|
) {
|
|
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(width)) {
|
|
width = 800;
|
|
}
|
|
this.width = width;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(height)) {
|
|
height = 600;
|
|
}
|
|
this.height = height;
|
|
|
|
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 = [
|
|
new GameLib.D3.API.Camera(
|
|
null,
|
|
GameLib.Component.COMPONENT_CAMERA,
|
|
null,
|
|
null,
|
|
this.width / this.height
|
|
)
|
|
];
|
|
}
|
|
this.cameras = cameras;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(activeCameraIndex)) {
|
|
activeCameraIndex = 0;
|
|
}
|
|
this.activeCameraIndex = activeCameraIndex;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(textures)) {
|
|
textures = [];
|
|
}
|
|
this.textures = textures;
|
|
|
|
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;
|
|
};
|
|
|
|
/**
|
|
* Returns an API scene from an Object scene
|
|
* @param objectScene
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.API.Scene.FromObjectScene = function(objectScene) {
|
|
|
|
var apiEntityManager = null;
|
|
|
|
var apiTextures = null;
|
|
var apiRenderers = null;
|
|
var apiRaycaster = null;
|
|
var apiMouse = 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)
|
|
}
|
|
)
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return new GameLib.D3.API.Scene(
|
|
objectScene.id,
|
|
objectScene.path,
|
|
objectScene.name,
|
|
objectScene.width,
|
|
objectScene.height,
|
|
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,
|
|
apiRenderers,
|
|
objectScene.activeRendererIndex,
|
|
apiRaycaster,
|
|
apiMouse
|
|
);
|
|
|
|
};
|