r3-legacy/src/game-lib-system-visualizati...

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-09-05 05:22:52 +02:00
/**
* System takes care of updating all the entities (based on their component data)
* Visualization System takes care of visualizing all objects which are not meshes (like physics data)
*
* @param apiSystem GameLib.API.System
* @param graphics
* @param physics
* @constructor
*/
GameLib.System.Visualization = function(
apiSystem,
graphics,
physics
) {
GameLib.System.call(
this,
apiSystem
);
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.physics = physics;
this.physics.isNotCannonThrow();
this.visualizationSubscription = null;
this.stopVisualizationSubscription = null;
};
GameLib.System.Visualization.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Visualization.prototype.constructor = GameLib.System.Visualization;
GameLib.System.Visualization.prototype.start = function() {
this.visualizationSubscription = this.subscribe(
GameLib.Event.VISUALIZE,
this.visualize
);
this.stopVisualizationSubscription = this.subscribe(
GameLib.Event.STOP_VISUALIZE,
this.stopVisualize
)
};
GameLib.System.Visualization.prototype.visualize = function(data) {
var shape = this.instance;
var geometry = new THREE.Geometry();
var v0 = new CANNON.Vec3();
var v1 = new CANNON.Vec3();
var v2 = new CANNON.Vec3();
for (var xi = 0; xi < shape.data.length - 1; xi++) {
for (var yi = 0; yi < shape.data[xi].length - 1; yi++) {
for (var k = 0; k < 2; k++) {
shape.getConvexTrianglePillar(xi, yi, k===0);
v0.copy(shape.pillarConvex.vertices[0]);
v1.copy(shape.pillarConvex.vertices[1]);
v2.copy(shape.pillarConvex.vertices[2]);
v0.vadd(shape.pillarOffset, v0);
v1.vadd(shape.pillarOffset, v1);
v2.vadd(shape.pillarOffset, v2);
geometry.vertices.push(
new THREE.Vector3(v0.x, v0.y, v0.z),
new THREE.Vector3(v1.x, v1.y, v1.z),
new THREE.Vector3(v2.x, v2.y, v2.z)
);
var i = geometry.vertices.length - 3;
geometry.faces.push(new THREE.Face3(i, i+1, i+2));
}
}
}
geometry.computeBoundingSphere();
geometry.computeFaceNormals();
mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial());
this.mesh = mesh;
};
GameLib.System.Visualization.prototype.stopVisualize = function(data) {
};
GameLib.System.Visualization.prototype.stop = function() {
if (this.visualizationSubscription) {
this.visualizationSubscription.remove();
}
if (this.stopVisualizationSubscription) {
this.stopVisualizationSubscription.remove();
}
};