r3-legacy/src/game-lib-d3-shape-convex-hu...

281 lines
7.4 KiB
JavaScript

/**
* Shape Superset - The apiShape properties get moved into the Shape object itself, and then the instance is created
* @param physics
* @param apiShape GameLib.D3.API.Shape
* @param faces
* @param uniqueAxes
* @param uniqueEdges
* @param vertices
* @constructor
*/
GameLib.D3.Shape.ConvexHull = function (
physics,
apiShape,
vertices,
faces,
uniqueAxes,
uniqueEdges
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(apiShape)) {
apiShape = {
shapeType : GameLib.D3.API.Shape.SHAPE_TYPE_CONVEX_HULL
};
}
if (GameLib.Utils.UndefinedOrNull(vertices)) {
vertices = [];
}
this.vertices = vertices;
if (GameLib.Utils.UndefinedOrNull(faces)) {
faces = [];
}
this.faces = faces;
if (GameLib.Utils.UndefinedOrNull(uniqueAxes)) {
uniqueAxes = [];
}
this.uniqueAxes = uniqueAxes;
if (GameLib.Utils.UndefinedOrNull(uniqueEdges)) {
uniqueEdges = [];
}
this.uniqueEdges = uniqueEdges;
this.vertices = this.vertices.map(function(vertex){
if (vertex instanceof GameLib.D3.API.Vertex){
return new GameLib.D3.Vertex(
this.physics,
vertex
)
}
return vertex;
}.bind(this));
this.faces = this.faces.map(function(face){
if (face instanceof GameLib.D3.API.Face){
return new GameLib.D3.Face(
this.physics,
face
)
}
return face;
}.bind(this));
this.uniqueAxes = this.uniqueAxes.map(function(axis){
if (axis instanceof GameLib.API.Vector3) {
return new GameLib.Vector3(
this.physics,
axis,
this
)
}
return axis;
}.bind(this));
this.uniqueEdges = this.uniqueEdges.map(function(edge){
if (edge instanceof GameLib.API.Vector3) {
return new GameLib.Vector3(
this.physics,
edge,
this
)
}
return edge;
}.bind(this));
GameLib.D3.Shape.call(
this,
this.physics,
apiShape
);
};
GameLib.D3.Shape.ConvexHull.prototype = Object.create(GameLib.D3.Shape.prototype);
GameLib.D3.Shape.ConvexHull.prototype.constructor = GameLib.D3.Shape.ConvexHull;
/**
* Create instance
* @returns {GameLib.D3.Shape.ConvexHull}
*/
GameLib.D3.Shape.ConvexHull.prototype.createInstance = function() {
var faceNormals = [];
this.instance = new CANNON.ConvexPolyhedron(
this.vertices.map(
function(vertex) {
if (GameLib.Utils.UndefinedOrNull(vertex)) {
throw new Error('no vertex');
}
if (GameLib.Utils.UndefinedOrNull(vertex.position)) {
throw new Error('no vertex position');
}
if (GameLib.Utils.UndefinedOrNull(vertex.position.instance)) {
throw new Error('no vertex position instance');
}
return vertex.position.instance;
}
),
this.faces.map(
function(face) {
if (GameLib.Utils.UndefinedOrNull(face)) {
throw new Error('no face');
}
if (GameLib.Utils.UndefinedOrNull(face.normal)) {
throw new Error('no face normal');
}
if (GameLib.Utils.UndefinedOrNull(face.normal.instance)) {
throw new Error('no face normal instance');
}
if (GameLib.Utils.UndefinedOrNull(face.v0index)) {
throw new Error('no face v0index');
}
if (GameLib.Utils.UndefinedOrNull(face.v1index)) {
throw new Error('no face v1index');
}
if (GameLib.Utils.UndefinedOrNull(face.v2index)) {
throw new Error('no face v2index');
}
faceNormals.push(face.normal.instance);
return [face.v0index, face.v1index, face.v2index];
}
)
);
this.instance.faceNormals = faceNormals;
GameLib.D3.Shape.prototype.createInstance.call(this);
};
/**
* Update instance
*/
GameLib.D3.Shape.ConvexHull.prototype.updateInstance = function() {
console.log('todo: update convex hull instance');
// this.instance.vertices = this.vertices;
// this.instance.indices = this.indices;
// this.instance.updateAABB();
// this.instance.updateBoundingSphereRadius();
// this.instance.updateEdges();
// this.instance.updateNormals();
// this.instance.updateTree();
};
GameLib.D3.Shape.ConvexHull.prototype.loadFromInstance = function() {
console.log('todo: eventually load the faces and vertices from the instance faces and vertices and normals');
console.log('todo: this way we can nicely visualize them with our gamelib classes :)');
};
GameLib.D3.Shape.ConvexHull.prototype.toApiObject = function() {
var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this);
apiShape.vertices = this.vertices.map(
function(vertex) {
if (vertex instanceof GameLib.D3.Vertex) {
return vertex.toApiObject();
}
return vertex;
}
);
apiShape.faces = this.faces.map(
function(face) {
if (face instanceof GameLib.D3.Face){
return face.toApiObject();
}
return face;
}
);
apiShape.uniqueAxes = this.uniqueAxes.map(
function(axis){
if (axis instanceof GameLib.Vector3) {
return axis.toApiObject();
}
return axis;
}
);
apiShape.uniqueEdges = this.uniqueEdges.map(
function(edge) {
if (edge instanceof GameLib.Vector3) {
return edge.toApiObject();
}
return edge;
}
);
return apiShape;
};
GameLib.D3.Shape.ConvexHull.prototype.setFromMesh = function() {
console.log('todo: set convex hull from mesh');
this.updateInstance();
};
GameLib.D3.Shape.ConvexHull.InheritableProperties = function(physics, objectShape) {
var vertices = objectShape.vertices.map(
function(objectVertex) {
return GameLib.D3.Vertex.FromObject(physics, objectVertex);
}
);
var faces = objectShape.faces.map(
function(objectFace) {
return GameLib.D3.Face.FromObject(physics, objectFace);
}
);
var uniqueAxes = objectShape.uniqueAxes.map(
function(axis) {
return GameLib.API.Vector3.FromObject(axis);
}
);
var uniqueEdges = objectShape.uniqueEdges.map(
function(edge) {
return GameLib.API.Vector3.FromObject(edge);
}
);
return {
vertices : vertices,
faces : faces,
uniqueAxes : uniqueAxes,
uniqueEdges : uniqueEdges
};
};
GameLib.D3.Shape.ConvexHull.FromObject = function(physics, objectShape) {
var apiShape = GameLib.D3.API.Shape.FromObject(objectShape);
var inheritableProperties = GameLib.D3.Shape.ConvexHull.InheritableProperties(physics, objectShape);
return new GameLib.D3.Shape.ConvexHull.call(
this,
physics,
apiShape,
inheritableProperties.vertices,
inheritableProperties.faces,
inheritableProperties.uniqueAxes,
inheritableProperties.uniqueEdges
);
};