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

338 lines
7.9 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
2017-01-05 19:34:28 +01:00
* @param computeNormals
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,
2016-11-21 16:08:39 +01:00
apiScene,
2017-01-05 19:34:28 +01:00
computeNormals
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;
}
if (GameLib.Utils.UndefinedOrNull(computeNormals)) {
computeNormals = true;
}
this.computeNormals = computeNormals;
2016-12-15 15:28:00 +01:00
GameLib.D3.API.Scene.call(
this,
apiScene.id,
apiScene.name,
2017-02-21 18:55:18 +01:00
apiScene.imageFactory,
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,
apiScene.activeCamera,
2017-01-10 17:04:30 +01:00
apiScene.parentEntity
2017-01-05 19:34:28 +01:00
);
2017-02-21 18:55:18 +01:00
if (this.imageFactory instanceof GameLib.D3.API.ImageFactory) {
this.imageFactory = new GameLib.D3.ImageFactory(
this.graphics,
this.imageFactory
);
2017-01-17 17:16:10 +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,
apiMesh,
2017-02-01 16:09:34 +01:00
this.imageFactory,
this.computeNormals
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-01-31 11:37:55 +01:00
this.textures = this.textures.map(
function(apiTexture) {
if (apiTexture instanceof GameLib.D3.API.Texture) {
2017-02-01 16:09:34 +01:00
var texture = new GameLib.D3.Texture(
2017-01-31 11:37:55 +01:00
this.graphics,
apiTexture,
this.imageFactory
);
2017-02-01 16:09:34 +01:00
this.idToObject[texture.id] = texture;
return texture;
2017-01-31 11:37:55 +01:00
} 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) {
2017-02-01 16:09:34 +01:00
var material = new GameLib.D3.Material(
2017-01-31 11:37:55 +01:00
this.graphics,
apiMaterial,
this.imageFactory
);
2017-02-01 16:09:34 +01:00
this.idToObject[material.id] = material;
return material;
2017-01-31 11:37:55 +01:00
} else {
console.warn('apiMaterial not an instance of API.Material');
throw new Error('apiMaterial not an instance of API.Material');
}
2017-02-01 16:09:34 +01:00
2017-01-31 11:37:55 +01:00
}.bind(this)
);
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;
2017-02-01 16:09:34 +01:00
this.linkObjects(this.idToObject);
2016-12-15 15:28:00 +01:00
2017-01-31 15:23:38 +01:00
this.meshes.map(
function(mesh) {
2017-02-01 16:09:34 +01:00
mesh.updateInstance();
2017-01-31 15:23:38 +01:00
mesh.materials.map(
function(material) {
2017-02-01 16:09:34 +01:00
material.updateInstance();
2017-01-31 15:23:38 +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
2017-02-21 18:55:18 +01:00
var apiImageFactory = null;
if (this.imageFactory instanceof GameLib.D3.ImageFactory) {
2017-05-16 14:51:57 +02:00
apiImageFactory = this.imageFactory.toApiObject();
2017-02-21 18:55:18 +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,
2017-02-21 18:55:18 +01:00
apiImageFactory,
2016-12-09 20:32:09 +01:00
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
2017-01-20 13:40:27 +01:00
* @param computeNormals boolean to indicate whether or not to recalculate normals
* @param imageFactory GameLib.D3.ImageFactory
2016-12-09 20:32:09 +01:00
* @returns {GameLib.D3.Scene}
* @constructor
*/
GameLib.D3.Scene.FromObjectScene = function(
graphics,
objectScene,
computeNormals,
imageFactory
2016-12-09 20:32:09 +01:00
) {
var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene, imageFactory);
2016-11-29 12:54:25 +01:00
2016-12-09 20:32:09 +01:00
return new GameLib.D3.Scene(
graphics,
apiScene,
2017-01-06 16:53:53 +01:00
computeNormals
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,
mesh,
mesh.imageFactory,
mesh.computeNormals
)
}
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();
};