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
|
|
|
|
* @param progressCallback
|
2016-11-29 12:54:25 +01:00
|
|
|
* @param apiScene GameLib.D3.API.Scene
|
2016-11-28 15:05:02 +01:00
|
|
|
* @param imageFactory
|
2017-01-05 19:34:28 +01:00
|
|
|
* @param computeNormals
|
2016-11-17 18:31:41 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-11-21 16:08:39 +01:00
|
|
|
GameLib.D3.Scene = function Scene(
|
2016-11-17 18:31:41 +01:00
|
|
|
graphics,
|
|
|
|
progressCallback,
|
2016-11-21 16:08:39 +01:00
|
|
|
apiScene,
|
2017-01-05 19:34:28 +01:00
|
|
|
imageFactory,
|
|
|
|
computeNormals
|
2016-11-17 18:31:41 +01:00
|
|
|
) {
|
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
this.imageFactory = imageFactory;
|
|
|
|
|
2016-12-15 15:28:00 +01:00
|
|
|
GameLib.D3.API.Scene.call(
|
|
|
|
this,
|
|
|
|
apiScene.id,
|
|
|
|
apiScene.path,
|
|
|
|
apiScene.name,
|
2017-01-09 15:20:48 +01:00
|
|
|
apiScene.width,
|
|
|
|
apiScene.height,
|
2016-12-15 15:28:00 +01:00
|
|
|
apiScene.meshes,
|
|
|
|
apiScene.position,
|
|
|
|
apiScene.quaternion,
|
|
|
|
apiScene.scale,
|
|
|
|
apiScene.parentSceneId,
|
|
|
|
apiScene.lights,
|
|
|
|
apiScene.worlds,
|
|
|
|
apiScene.entityManager,
|
|
|
|
apiScene.shapes,
|
|
|
|
apiScene.cameras,
|
2017-01-06 16:53:53 +01:00
|
|
|
apiScene.activeCameraIndex,
|
2017-01-09 15:20:48 +01:00
|
|
|
apiScene.textures,
|
|
|
|
apiScene.renderers,
|
|
|
|
apiScene.activeRendererIndex,
|
|
|
|
apiScene.raycaster,
|
|
|
|
apiScene.mouse
|
2016-12-15 15:28:00 +01:00
|
|
|
);
|
|
|
|
|
2017-01-05 19:34:28 +01:00
|
|
|
this.meshes = this.meshes.map(
|
|
|
|
function(apiMesh) {
|
|
|
|
return new GameLib.D3.Mesh(
|
|
|
|
this.graphics,
|
|
|
|
apiMesh,
|
|
|
|
computeNormals,
|
|
|
|
imageFactory
|
|
|
|
)
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.lights = this.lights.map(
|
|
|
|
function(apiLight) {
|
|
|
|
return new GameLib.D3.Light(
|
|
|
|
this.graphics,
|
|
|
|
apiLight
|
|
|
|
)
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
if (this.entityManager) {
|
|
|
|
this.entityManager = new GameLib.EntityManager(
|
|
|
|
this.graphics,
|
|
|
|
this.entityManager
|
|
|
|
);
|
|
|
|
}
|
2017-01-05 19:34:28 +01:00
|
|
|
|
|
|
|
this.cameras = this.cameras.map(
|
|
|
|
function(apiCamera) {
|
2017-01-09 15:20:48 +01:00
|
|
|
if (apiCamera.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
|
|
|
|
return new GameLib.D3.StereoCamera(
|
|
|
|
this.graphics,
|
|
|
|
apiCamera
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return new GameLib.D3.Camera(
|
|
|
|
this.graphics,
|
|
|
|
apiCamera
|
|
|
|
)
|
|
|
|
}
|
2017-01-05 19:34:28 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
this.textures = this.textures.map(
|
|
|
|
function(apiTexture) {
|
|
|
|
return new GameLib.D3.Texture(
|
|
|
|
this.graphics,
|
|
|
|
apiTexture,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
this.imageFactory
|
|
|
|
)
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2017-01-09 15:20:48 +01:00
|
|
|
this.renderers = this.renderers.map(
|
|
|
|
function(apiRenderer) {
|
|
|
|
var renderer = null;
|
|
|
|
|
|
|
|
if (apiRenderer.rendererType == GameLib.Component.COMPONENT_RENDERER) {
|
|
|
|
|
|
|
|
renderer = new GameLib.D3.Renderer(
|
|
|
|
this.graphics,
|
|
|
|
apiRenderer
|
|
|
|
);
|
|
|
|
|
|
|
|
} else if (apiRenderer.rendererType == GameLib.Component.COMPONENT_STEREO_RENDERER) {
|
|
|
|
renderer = new GameLib.D3.StereoRenderer(
|
|
|
|
this.graphics,
|
|
|
|
apiRenderer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (renderer.scene.id == this.id) {
|
|
|
|
renderer.scene = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderer) {
|
|
|
|
console.log("Renderer type not supported : " + apiRenderer.rendererType);
|
|
|
|
throw new Error("Renderer type not supported : " + apiRenderer.rendererType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderer;
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.raycaster = new GameLib.D3.Raycaster(
|
|
|
|
this.graphics,
|
|
|
|
this.raycaster
|
|
|
|
);
|
|
|
|
|
|
|
|
this.mouse = new GameLib.Mouse(
|
|
|
|
this.graphics,
|
|
|
|
this,
|
|
|
|
this.mouse
|
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.position = new GameLib.Vector3(
|
2016-12-02 16:03:03 +01:00
|
|
|
graphics,
|
|
|
|
this,
|
|
|
|
this.position
|
|
|
|
);
|
|
|
|
|
2016-12-15 15:28:00 +01:00
|
|
|
this.quaternion = new GameLib.Quaternion(
|
2016-12-02 16:03:03 +01:00
|
|
|
graphics,
|
|
|
|
this,
|
2016-12-15 15:28:00 +01:00
|
|
|
this.quaternion
|
2016-12-02 16:03:03 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 15:28:00 +01:00
|
|
|
this.scale = new GameLib.Vector3(
|
2016-12-02 16:03:03 +01:00
|
|
|
graphics,
|
|
|
|
this,
|
2016-12-15 15:28:00 +01:00
|
|
|
this.scale
|
2016-12-02 16:03:03 +01:00
|
|
|
);
|
|
|
|
|
2016-11-17 18:31:41 +01:00
|
|
|
this.progressCallback = progressCallback;
|
|
|
|
|
|
|
|
this.instance = this.createInstance();
|
2016-11-18 16:00:13 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
this.interestingProperties = [
|
|
|
|
"cameras",
|
|
|
|
"meshes",
|
2017-01-06 16:53:53 +01:00
|
|
|
"lights",
|
2017-01-09 15:20:48 +01:00
|
|
|
"textures",
|
|
|
|
"renderers",
|
|
|
|
"raycaster",
|
|
|
|
"mouse"
|
2016-12-09 20:32:09 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
this.idToObject = {};
|
|
|
|
|
|
|
|
this.idToObject[this.id] = this;
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
var material = null;
|
|
|
|
|
2017-01-09 15:20:48 +01:00
|
|
|
var object = null;
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
for (var p = 0; p < this.interestingProperties.length; p++) {
|
|
|
|
property = this.interestingProperties[p];
|
|
|
|
if (this.hasOwnProperty(property)) {
|
2017-01-09 15:20:48 +01:00
|
|
|
|
|
|
|
if (_.isArray(this[property])) {
|
|
|
|
for (var i = 0; i < this[property].length; i++) {
|
|
|
|
object = this[property][i];
|
|
|
|
this.idToObject[object.id] = object;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
object = this[property];
|
2016-12-09 20:32:09 +01:00
|
|
|
this.idToObject[object.id] = object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2016-12-23 16:07:10 +01:00
|
|
|
for (var m = 0; m < this.meshes.length; m++) {
|
|
|
|
if (this.meshes[m].skeleton) {
|
|
|
|
this.meshes[m].skeleton.bones.map(
|
|
|
|
function (bone) {
|
|
|
|
this.idToObject[bone.id] = bone;
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.idToObject[this.meshes[m].skeleton.id] = this.meshes[m].skeleton;
|
|
|
|
}
|
2017-01-06 16:53:53 +01:00
|
|
|
|
|
|
|
if (this.meshes[m].materials[0]) {
|
|
|
|
|
|
|
|
material = this.meshes[m].materials[0];
|
|
|
|
|
|
|
|
for (var property in material) {
|
|
|
|
if (material.hasOwnProperty(property) && material[property] instanceof GameLib.D3.Texture) {
|
|
|
|
this.idToObject[material[property].id] = material[property];
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 15:20:48 +01:00
|
|
|
|
|
|
|
this.idToObject[material.id] = material;
|
2017-01-06 16:53:53 +01:00
|
|
|
}
|
2016-12-23 16:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
this.entityManager.entities.map(
|
|
|
|
function (entity) {
|
|
|
|
this.idToObject[entity.id] = entity;
|
|
|
|
entity.components.map(
|
|
|
|
function(component) {
|
|
|
|
if (component instanceof GameLib.Component) {
|
|
|
|
this.idToObject[component.id] = component;
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
)
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.entityManager.linkObjects(this.idToObject);
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
2016-11-15 17:12:27 +01:00
|
|
|
|
2016-12-15 15:28:00 +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
|
|
|
|
* @returns {GameLib.D3.Scene|THREE.Scene|ApiLib.Scene|*|Scene}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
instance.render = true;
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2016-12-09 20:32:09 +01:00
|
|
|
GameLib.D3.Scene.prototype.toApiScene = function() {
|
2016-11-17 18:31:41 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
var apiMeshes = this.meshes.map(
|
|
|
|
function(mesh) {
|
|
|
|
return mesh.toApiMesh();
|
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) {
|
|
|
|
return light.toApiLight();
|
|
|
|
}
|
|
|
|
);
|
2016-11-03 14:33:14 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var apiEntityManager = null;
|
|
|
|
if (this.entityManager) {
|
|
|
|
apiEntityManager = this.entityManager.toApiEntityManager();
|
|
|
|
}
|
2016-11-03 14:33:14 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
var apiCameras = this.cameras.map(
|
|
|
|
function(camera) {
|
|
|
|
return camera.toApiCamera();
|
|
|
|
}
|
|
|
|
);
|
2016-11-03 14:33:14 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
var apiWorlds = this.worlds.map(
|
|
|
|
function(world) {
|
|
|
|
return world.toApiWorld();
|
|
|
|
}
|
|
|
|
);
|
2016-11-21 16:08:39 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
var apiShapes = this.shapes.map(
|
|
|
|
function(shape) {
|
|
|
|
return shape.toApiShape();
|
|
|
|
}
|
|
|
|
);
|
2016-11-21 16:08:39 +01:00
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
var apiTextures = this.textures.map(
|
|
|
|
function(texture) {
|
|
|
|
return texture.toApiTexture();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-01-09 15:20:48 +01:00
|
|
|
var apiRenderers = this.renderers.map(
|
|
|
|
function(renderer) {
|
|
|
|
return renderer.toApiComponent();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
return new GameLib.D3.API.Scene(
|
|
|
|
this.id,
|
|
|
|
this.path,
|
|
|
|
this.name,
|
2017-01-09 15:20:48 +01:00
|
|
|
this.width,
|
|
|
|
this.height,
|
2016-12-09 20:32:09 +01:00
|
|
|
apiMeshes,
|
|
|
|
this.position.toApiVector(),
|
2016-12-16 16:03:48 +01:00
|
|
|
this.quaternion.toApiQuaternion(),
|
2016-12-09 20:32:09 +01:00
|
|
|
this.scale.toApiVector(),
|
|
|
|
this.parentSceneId,
|
|
|
|
apiLights,
|
|
|
|
apiWorlds,
|
2016-12-15 15:28:00 +01:00
|
|
|
apiEntityManager,
|
2016-12-09 20:32:09 +01:00
|
|
|
apiShapes,
|
|
|
|
apiCameras,
|
2017-01-06 16:53:53 +01:00
|
|
|
this.activeCameraIndex,
|
2017-01-09 15:20:48 +01:00
|
|
|
apiTextures,
|
|
|
|
apiRenderers,
|
|
|
|
this.activeRendererIndex,
|
|
|
|
this.raycaster.toApiRaycaster(),
|
|
|
|
this.mouse.toApiMouse()
|
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
|
|
|
|
* @param computeNormals boolean to indicate whether or not to recalculate normals
|
|
|
|
* @param imageFactory GameLib.D3.ImageFactory
|
|
|
|
* @param progressCallback callback
|
|
|
|
* @returns {GameLib.D3.Scene}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Scene.FromObjectScene = function(
|
|
|
|
graphics,
|
|
|
|
objectScene,
|
|
|
|
computeNormals,
|
|
|
|
imageFactory,
|
|
|
|
progressCallback
|
|
|
|
) {
|
2017-01-05 19:34:28 +01: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,
|
|
|
|
progressCallback,
|
|
|
|
apiScene,
|
2017-01-06 16:53:53 +01:00
|
|
|
imageFactory,
|
|
|
|
computeNormals
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
2016-11-29 12:54:25 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
2016-11-29 12:54:25 +01:00
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
/**
|
|
|
|
* Transforms raw scene data into a GameLib.D3.Scene
|
|
|
|
* @param objectScene Object (as it comes from the API)
|
|
|
|
* @param onLoaded
|
|
|
|
* @param graphics
|
|
|
|
* @param uploadUrl
|
|
|
|
* @param progressCallback
|
|
|
|
* @param computeNormals
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Scene.LoadScene = function(
|
|
|
|
objectScene,
|
|
|
|
onLoaded,
|
|
|
|
graphics,
|
|
|
|
uploadUrl,
|
|
|
|
progressCallback,
|
|
|
|
computeNormals
|
|
|
|
) {
|
|
|
|
onLoaded(
|
|
|
|
GameLib.D3.Scene.FromObjectScene(
|
2016-11-29 12:54:25 +01:00
|
|
|
graphics,
|
2016-12-09 20:32:09 +01:00
|
|
|
objectScene,
|
|
|
|
computeNormals,
|
|
|
|
GameLib.D3.ImageFactory(
|
|
|
|
graphics,
|
|
|
|
uploadUrl
|
2016-11-17 18:31:41 +01:00
|
|
|
),
|
2016-12-09 20:32:09 +01:00
|
|
|
progressCallback
|
|
|
|
)
|
2016-11-03 14:33:14 +01:00
|
|
|
);
|
2016-10-14 12:32:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a scene directly from the API
|
2016-11-17 18:31:41 +01:00
|
|
|
* @param partialSceneObject Object {path: '', name: ''}
|
2016-10-14 12:32:53 +02:00
|
|
|
* @param onLoaded callback
|
2016-10-25 17:57:32 +02:00
|
|
|
* @param graphics GameLib.D3.Graphics
|
2016-10-26 16:13:18 +02:00
|
|
|
* @param uploadUrl String
|
2016-10-25 17:57:32 +02:00
|
|
|
* @param progressCallback callback
|
|
|
|
* @param apiUrl
|
2016-10-14 12:32:53 +02:00
|
|
|
*/
|
2016-10-28 16:02:45 +02:00
|
|
|
GameLib.D3.Scene.LoadSceneFromApi = function(
|
2016-11-17 18:31:41 +01:00
|
|
|
partialSceneObject,
|
2016-10-25 17:57:32 +02:00
|
|
|
onLoaded,
|
|
|
|
graphics,
|
2016-10-26 16:13:18 +02:00
|
|
|
uploadUrl,
|
2016-10-25 17:57:32 +02:00
|
|
|
progressCallback,
|
|
|
|
apiUrl
|
|
|
|
) {
|
2016-10-14 12:32:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* First check if this is a client or server side request
|
|
|
|
*/
|
|
|
|
if (typeof XMLHttpRequest == 'undefined') {
|
|
|
|
console.warn('implement server side loading from API here');
|
|
|
|
return onLoaded(null, new Error('not implemented'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(
|
|
|
|
'GET',
|
2016-11-17 18:31:41 +01:00
|
|
|
apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name
|
2016-10-14 12:32:53 +02:00
|
|
|
);
|
|
|
|
|
2016-10-25 17:57:32 +02:00
|
|
|
xhr.onreadystatechange = function(xhr) {
|
2016-11-17 18:31:41 +01:00
|
|
|
|
2016-10-14 12:32:53 +02:00
|
|
|
return function() {
|
2016-11-17 18:31:41 +01:00
|
|
|
|
2016-10-14 12:32:53 +02:00
|
|
|
if (xhr.readyState == 4) {
|
|
|
|
|
|
|
|
var response = JSON.parse(xhr.responseText);
|
|
|
|
|
|
|
|
if (!response.scene || response.scene.length == 0) {
|
|
|
|
return onLoaded(null, null, new Error('Could not load scene'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var scene = response.scene[0];
|
|
|
|
|
2016-11-24 12:56:39 +01:00
|
|
|
GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback, true);
|
2016-10-14 12:32:53 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-25 17:57:32 +02:00
|
|
|
}(xhr);
|
2016-10-14 12:32:53 +02:00
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
};
|