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

127 lines
3.6 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() {
GameLib.System.prototype.start.call(this);
2017-09-05 05:22:52 +02:00
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) {
2017-09-05 13:18:10 +02:00
var shape = data.shape;
var parentMesh = shape.parentMesh;
shape.setFromMesh();
var apiMesh = new GameLib.D3.API.Mesh();
apiMesh.name = 'Visualization Mesh for Shape ' + shape.name;
if (shape instanceof GameLib.D3.Shape.HeightMap) {
var v0 = new CANNON.Vec3();
var v1 = new CANNON.Vec3();
var v2 = new CANNON.Vec3();
for (var xi = 0; xi < shape.heightData.length - 1; xi++) {
for (var yi = 0; yi < shape.heightData[xi].length - 1; yi++) {
for (var k = 0; k < 2; k++) {
shape.instance.getConvexTrianglePillar(xi, yi, k===0);
v0.copy(shape.instance.pillarConvex.vertices[0]);
v1.copy(shape.instance.pillarConvex.vertices[1]);
v2.copy(shape.instance.pillarConvex.vertices[2]);
v0.vadd(shape.instance.pillarOffset, v0);
v1.vadd(shape.instance.pillarOffset, v1);
v2.vadd(shape.instance.pillarOffset, v2);
apiMesh.vertices.push(
new GameLib.D3.API.Vertex(
new GameLib.API.Vector3(v0.x, v0.y, v0.z)
),
new GameLib.D3.API.Vertex(
new GameLib.API.Vector3(v1.x, v1.y, v1.z)
),
new GameLib.D3.API.Vertex(
new GameLib.API.Vector3(v2.x, v2.y, v2.z)
)
);
var i = apiMesh.vertices.length - 3;
apiMesh.faces.push(
new GameLib.D3.API.Face(
null,
null,
i,
i+1,
i+2
)
);
}
2017-09-05 05:22:52 +02:00
}
}
}
2017-09-05 13:18:10 +02:00
new GameLib.D3.Mesh(
this.graphics,
apiMesh
);
2017-09-05 05:22:52 +02:00
};
GameLib.System.Visualization.prototype.stopVisualize = function(data) {
};
GameLib.System.Visualization.prototype.stop = function() {
GameLib.System.prototype.stop.call(this);
2017-09-05 05:22:52 +02:00
if (this.visualizationSubscription) {
this.visualizationSubscription.remove();
}
if (this.stopVisualizationSubscription) {
this.stopVisualizationSubscription.remove();
}
};