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

358 lines
8.3 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
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();
if (GameLib.Utils.UndefinedOrNull(apiScene)) {
apiScene = {};
}
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,
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(
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(
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(
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
if (apiLight instanceof GameLib.D3.API.Light) {
2017-01-19 17:50:11 +01:00
return new GameLib.D3.Light(
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-09 15:20:48 +01:00
2017-01-10 17:04:30 +01:00
}.bind(this)
);
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
);
2017-06-16 15:49:53 +02:00
// this.idToObject[texture.id] = texture;
2017-06-09 16:03:05 +02:00
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
);
2017-06-16 15:49:53 +02:00
// this.idToObject[material.id] = material;
2017-06-09 16:03:05 +02:00
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
);
2017-06-16 15:49:53 +02:00
// this.idToObject[image.id] = image;
2017-06-09 16:03:05 +02:00
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
if (this.activeCamera instanceof GameLib.D3.API.Camera) {
this.activeCamera = new GameLib.D3.Camera(
this.graphics,
this.activeCamera
);
}
2017-06-16 15:49:53 +02:00
/**
* TODO : Refactor (linking ?)
* @type {GameLib.D3.Scene}
*/
// this.idToObject[this.id] = this;
//
// this.linkObjects(this.idToObject);
2017-06-16 15:49:53 +02:00
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_SCENE,
{
'meshes' : [GameLib.D3.Mesh],
'lights' : [GameLib.D3.Light],
'textures' : [GameLib.D3.Texture],
'materials' : [GameLib.D3.Material],
'images' : [GameLib.D3.Image],
'activeCamera' : GameLib.D3.Camera
}
);
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-06-13 14:09:18 +02:00
GameLib.D3.Scene.prototype.toApiObject = function(save) {
2016-11-17 18:31:41 +01:00
2016-12-09 20:32:09 +01:00
var apiMeshes = this.meshes.map(
function(mesh) {
2017-06-13 14:09:18 +02:00
if (save) {
mesh.save();
}
return mesh.id;
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-06-13 14:09:18 +02:00
if (save) {
light.save();
}
return light.id;
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-06-13 14:09:18 +02:00
if (save) {
texture.save();
}
return texture.id;
2017-01-31 11:37:55 +01:00
}
);
var apiMaterials = this.materials.map(
function(material) {
2017-06-13 14:09:18 +02:00
if (save) {
material.save();
}
return material.id;
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,
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
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.Scene.FromObject = function(
2016-12-09 20:32:09 +01:00
graphics,
2017-06-08 18:17:03 +02:00
objectScene
2016-12-09 20:32:09 +01:00
) {
2017-06-14 14:21:57 +02:00
var apiScene = GameLib.D3.API.Scene.FromObject(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 object GameLib.D3.Mesh
2017-06-02 13:52:29 +02:00
*/
GameLib.D3.Scene.prototype.addObject = function(object) {
2017-06-02 13:52:29 +02:00
if (object instanceof GameLib.D3.Mesh) {
this.meshes.push(object);
} else if (object instanceof GameLib.D3.API.Mesh) {
object = new GameLib.D3.Mesh(
2017-06-02 13:52:29 +02:00
this.graphics,
object
2017-06-09 16:03:05 +02:00
);
this.meshes.push(object);
2017-06-02 13:52:29 +02:00
}
if (object instanceof GameLib.D3.Light) {
this.lights.push(object);
} else if (object instanceof GameLib.D3.API.Light) {
object = new GameLib.D3.Light(
this.graphics,
object
);
this.lights.push(object);
}
2017-06-02 13:52:29 +02:00
object.parentScene = this;
this.instance.add(object.instance);
2017-06-02 13:52:29 +02:00
this.buildIdToObject();
};
/**
*
* @param object
2017-06-02 13:52:29 +02:00
*/
GameLib.D3.Scene.prototype.removeObject = function(object) {
2017-06-02 13:52:29 +02:00
var index = -1;
if (object instanceof GameLib.D3.Mesh) {
index = this.meshes.indexOf(object);
if (index !== -1) {
this.meshes.splice(index, 1);
}
} else if (object instanceof GameLib.D3.Light) {
index = this.lights.indexOf(object);
if (index !== -1) {
this.lights.splice(index, 1);
}
2017-06-02 13:52:29 +02:00
} else {
console.warn('Cannot remove this mesh - what is this ?' + object.toString())
return;
2017-06-02 13:52:29 +02:00
}
this.instance.remove(object.instance);
2017-06-02 13:52:29 +02:00
if (object.parentScene === this) {
object.parentScene = null;
2017-06-02 13:52:29 +02:00
}
this.buildIdToObject();
};