r3-legacy/bak/r3-d3-shape.js

195 lines
4.4 KiB
JavaScript

/**
* Physics Shape Superset
* @param engine R3.D3.Engine
* @param shapeType
* @param scale R3.API.Vector3
* @param vertices Number[]
* @param indices Number[]
* @param radius Number
* @param halfExtensions R3.API.Vector3
* @param radiusTop Number
* @param radiusBottom Number
* @param height Number
* @param numSegments Number
* @param heightmap R3.D3.Heightmap
* @param elementSize
* @constructor
*/
R3.D3.Shape = function(
engine,
shapeType,
scale,
vertices,
indices,
radius,
halfExtensions,
radiusTop,
radiusBottom,
height,
numSegments,
heightmap
) {
this.engine = engine;
this.engine.isNotCannonThrow();
this.shapeType = shapeType;
if (typeof scale == 'undefined') {
scale = new R3.API.Vector3(1, 1, 1)
}
this.scale = scale;
if (typeof vertices == 'undefined') {
vertices = [];
}
this.vertices = vertices;
if (typeof indices == 'undefined') {
indices = [];
}
this.indices = indices;
if (typeof radius == 'undefined') {
radius = 1;
}
this.radius = radius;
if (typeof halfExtensions == 'undefined') {
halfExtensions = new R3.API.Vector3(1,1,1);
}
this.halfExtensions = halfExtensions;
if (typeof radiusTop == 'undefined') {
radiusTop = 1;
}
this.radiusTop = radiusTop;
if (typeof radiusBottom == 'undefined') {
radiusBottom = 1;
}
this.radiusBottom = radiusBottom;
if (typeof height == 'undefined') {
height = 1;
}
this.height = height;
if (typeof numSegments == 'undefined') {
numSegments = 1;
}
this.numSegments = numSegments;
if (typeof heightmap == 'undefined') {
heightmap = new R3.D3.Heightmap();
}
this.heightmap = heightmap;
this.instance = this.createInstance();
};
/**
* Shape constants
* @type {number}
*/
R3.D3.Shape.SHAPE_TYPE_SPHERE = 1;
R3.D3.Shape.SHAPE_TYPE_BOX = 2;
R3.D3.Shape.SHAPE_TYPE_TRIMESH = 3;
R3.D3.Shape.SHAPE_TYPE_CYLINDER = 4;
R3.D3.Shape.SHAPE_TYPE_HEIGHT_MAP = 5;
R3.D3.Shape.SHAPE_TYPE_CONVEX_HULL = 6;
R3.D3.Shape.SHAPE_TYPE_PLANE = 7;
/**
*
*/
R3.D3.Shape.prototype.createInstance = function() {
var instance = null;
if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_TRIMESH) {
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_SPHERE) {;
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_BOX) {
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_CYLINDER) {
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_HEIGHT_MAP) {
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_CONVEX_HULL) {
instance = new this.engine.instance.ConvexPolyhedron(
this.vertices, this.indices
);
} else if(this.shapeType == R3.D3.Shape.SHAPE_TYPE_PLANE) {
instance = new this.engine.instance.Plane();
} else {
console.warn('Shape type not implemented: ' + this.shapeType);
throw new Error('Shape type not implemented: ' + this.shapeType);
}
this.instance = instance;
return instance;
};
/**
* update
*/
R3.D3.Shape.prototype.update = function(
engine
) {
engine.isNotCannonThrow();
if(this.shapeType === R3.D3.Shape.SHAPE_TYPE_TRIMESH) {
this.instance.setScale(
new engine.instance.Vec3(
this.scale.x,
this.scale.y,
this.scale.z
)
);
this.instance.updateAABB();
this.instance.updateNormals();
this.instance.updateEdges();
this.instance.updateBoundingSphereRadius();
this.instance.updateTree();
}
};
/**
* Converts a R3.D3.Shape to a R3.D3.API.Shape
* @returns {R3.D3.API.Shape}
*/
R3.D3.Shape.prototype.toApiShape = function() {
return new R3.D3.API.Shape(
this.engine.toApiEngine(),
this.shapeType,
this.scale.toApiVector(),
this.vertices,
this.indices,
this.radius,
this.halfExtensions,
this.radiusTop,
this.radiusBottom,
this.height,
this.numSegments,
this.heightmap.toApiHeightMap()
)
};
/**
* Converts from an Object to a Shape
* @param graphics R3.D3.Graphics
* @param objectShape Object
* @constructor
*/
R3.D3.Shape.FromObjectShape = function(graphics, objectShape) {
//todo: implement this still
return null;
};