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
|
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,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiScene
|
2016-11-17 18:31:41 +01:00
|
|
|
) {
|
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiScene)) {
|
|
|
|
apiScene = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiScene instanceof GameLib.D3.Scene) {
|
|
|
|
return apiScene;
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:28:00 +01:00
|
|
|
GameLib.D3.API.Scene.call(
|
|
|
|
this,
|
|
|
|
apiScene.id,
|
|
|
|
apiScene.name,
|
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,
|
2017-06-09 16:03:05 +02:00
|
|
|
apiScene.images,
|
2017-06-01 16:58:22 +02:00
|
|
|
apiScene.activeCamera,
|
2017-01-10 17:04:30 +01:00
|
|
|
apiScene.parentEntity
|
2017-01-05 19:34:28 +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,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiMesh
|
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(
|
2017-01-17 13:24:45 +01:00
|
|
|
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(
|
2017-01-17 13:24:45 +01:00
|
|
|
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(
|
2017-01-17 13:24:45 +01:00
|
|
|
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
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (apiLight instanceof GameLib.D3.API.Light) {
|
2017-01-19 17:50:11 +01:00
|
|
|
|
|
|
|
return new GameLib.D3.Light(
|
2017-01-17 13:24:45 +01:00
|
|
|
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-17 13:24:45 +01:00
|
|
|
}
|
2017-01-09 15:20:48 +01:00
|
|
|
|
2017-01-10 17:04:30 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-06-09 16:03:05 +02:00
|
|
|
this.textures = this.textures.map(
|
|
|
|
function(apiTexture) {
|
|
|
|
|
|
|
|
if (apiTexture instanceof GameLib.D3.API.Texture) {
|
|
|
|
var texture = new GameLib.D3.Texture(
|
|
|
|
this.graphics,
|
|
|
|
apiTexture
|
|
|
|
);
|
|
|
|
|
|
|
|
this.idToObject[texture.id] = texture;
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
} 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) {
|
|
|
|
|
|
|
|
var material = new GameLib.D3.Material(
|
|
|
|
this.graphics,
|
|
|
|
apiMaterial
|
|
|
|
);
|
|
|
|
|
|
|
|
this.idToObject[material.id] = material;
|
|
|
|
|
|
|
|
return material;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.warn('apiMaterial not an instance of API.Material');
|
|
|
|
throw new Error('apiMaterial not an instance of API.Material');
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.images = this.images.map(
|
|
|
|
function(apiImage) {
|
|
|
|
|
|
|
|
if (apiImage instanceof GameLib.D3.API.Image) {
|
|
|
|
|
|
|
|
var image = new GameLib.D3.Image(
|
|
|
|
this.graphics,
|
|
|
|
apiImage
|
|
|
|
);
|
|
|
|
|
|
|
|
this.idToObject[image.id] = image;
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.warn('apiImage not an instance of API.Image');
|
|
|
|
throw new Error('apiImage not an instance of API.Image');
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2017-01-31 11:37:55 +01:00
|
|
|
|
2017-06-01 16:58:22 +02:00
|
|
|
if (this.activeCamera instanceof GameLib.D3.API.Camera) {
|
|
|
|
this.activeCamera = new GameLib.D3.Camera(
|
|
|
|
this.graphics,
|
|
|
|
this.activeCamera
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:09:34 +01:00
|
|
|
this.idToObject[this.id] = this;
|
2016-11-15 17:12:27 +01:00
|
|
|
|
2017-02-01 16:09:34 +01:00
|
|
|
this.linkObjects(this.idToObject);
|
2016-12-15 15:28:00 +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
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.D3.Scene.prototype.toApiObject = function() {
|
2016-11-17 18:31:41 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
var apiMeshes = this.meshes.map(
|
|
|
|
function(mesh) {
|
2017-05-16 14:51:57 +02:00
|
|
|
return mesh.toApiObject();
|
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) {
|
2017-05-16 14:51:57 +02:00
|
|
|
return light.toApiObject();
|
2016-12-09 20:32:09 +01:00
|
|
|
}
|
|
|
|
);
|
2016-11-03 14:33:14 +01:00
|
|
|
|
2017-01-31 11:37:55 +01:00
|
|
|
var apiTextures = this.textures.map(
|
|
|
|
function(texture) {
|
2017-05-16 14:51:57 +02:00
|
|
|
return texture.toApiObject();
|
2017-01-31 11:37:55 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
var apiMaterials = this.materials.map(
|
|
|
|
function(material) {
|
2017-05-16 14:51:57 +02:00
|
|
|
return material.toApiObject();
|
2017-01-31 11:37:55 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
return new GameLib.D3.API.Scene(
|
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
apiMeshes,
|
2017-05-16 14:51:57 +02:00
|
|
|
this.position.toApiObject(),
|
|
|
|
this.quaternion.toApiObject(),
|
|
|
|
this.scale.toApiObject(),
|
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-06-01 16:58:22 +02:00
|
|
|
GameLib.Utils.IdOrNull(this.activeCamera),
|
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
|
|
|
|
* @returns {GameLib.D3.Scene}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Scene.FromObjectScene = function(
|
|
|
|
graphics,
|
2017-06-08 18:17:03 +02:00
|
|
|
objectScene
|
2016-12-09 20:32:09 +01:00
|
|
|
) {
|
2017-06-08 18:17:03 +02: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,
|
2017-06-08 18:17:03 +02:00
|
|
|
apiScene
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
};
|
2017-06-02 13:52:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a mesh to the scene
|
|
|
|
* @param mesh GameLib.D3.Mesh
|
|
|
|
*/
|
|
|
|
GameLib.D3.Scene.prototype.addMesh = function(mesh) {
|
|
|
|
|
|
|
|
if (mesh instanceof GameLib.D3.Mesh) {
|
|
|
|
this.meshes.push(mesh);
|
|
|
|
} else if (mesh instanceof GameLib.D3.API.Mesh) {
|
|
|
|
mesh = new GameLib.D3.Mesh(
|
|
|
|
this.graphics,
|
2017-06-08 18:17:03 +02:00
|
|
|
mesh
|
2017-06-09 16:03:05 +02:00
|
|
|
);
|
|
|
|
this.meshes.push(mesh);
|
2017-06-02 13:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mesh.parentScene = this;
|
|
|
|
|
|
|
|
this.instance.add(mesh.instance);
|
|
|
|
|
|
|
|
this.buildIdToObject();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param mesh
|
|
|
|
*/
|
|
|
|
GameLib.D3.Scene.prototype.removeMesh = function(mesh) {
|
|
|
|
|
|
|
|
var index = -1;
|
|
|
|
|
|
|
|
if (mesh instanceof GameLib.D3.Mesh) {
|
|
|
|
index = this.meshes.indexOf(mesh);
|
|
|
|
} else {
|
|
|
|
console.warn('Cannot remove this mesh - what is this ?' + mesh.toString());
|
|
|
|
throw new Error('Cannot remove this mesh - what is this ?' + mesh.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
this.instance.remove(mesh.instance);
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
this.meshes.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh.parentScene === this) {
|
|
|
|
mesh.parentScene = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buildIdToObject();
|
|
|
|
};
|