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

195 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-10-14 12:32:53 +02:00
/**
* Physics Shape Superset
2018-04-09 10:05:13 +02:00
* @param engine R3.D3.Engine
2016-10-28 11:47:50 +02:00
* @param shapeType
2018-04-09 10:05:13 +02:00
* @param scale R3.API.Vector3
2016-10-28 11:47:50 +02:00
* @param vertices Number[]
* @param indices Number[]
* @param radius Number
2018-04-09 10:05:13 +02:00
* @param halfExtensions R3.API.Vector3
2016-10-28 11:47:50 +02:00
* @param radiusTop Number
* @param radiusBottom Number
* @param height Number
* @param numSegments Number
2018-04-09 10:05:13 +02:00
* @param heightmap R3.D3.Heightmap
2016-10-28 11:47:50 +02:00
* @param elementSize
2016-10-14 12:32:53 +02:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Shape = function(
2016-10-28 11:47:50 +02:00
engine,
shapeType,
scale,
vertices,
indices,
radius,
halfExtensions,
radiusTop,
radiusBottom,
height,
numSegments,
heightmap
2016-10-14 12:32:53 +02:00
) {
2016-11-01 16:08:22 +01:00
2016-10-28 11:47:50 +02:00
this.engine = engine;
this.engine.isNotCannonThrow();
2016-10-14 12:32:53 +02:00
this.shapeType = shapeType;
2016-10-28 11:47:50 +02:00
if (typeof scale == 'undefined') {
2018-04-09 10:05:13 +02:00
scale = new R3.API.Vector3(1, 1, 1)
2016-10-28 11:47:50 +02:00
}
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') {
2018-04-09 10:05:13 +02:00
halfExtensions = new R3.API.Vector3(1,1,1);
2016-10-28 11:47:50 +02:00
}
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') {
2018-04-09 10:05:13 +02:00
heightmap = new R3.D3.Heightmap();
2016-10-28 11:47:50 +02:00
}
this.heightmap = heightmap;
2016-11-01 16:08:22 +01:00
this.instance = this.createInstance();
2016-10-28 11:47:50 +02:00
};
/**
* Shape constants
* @type {number}
*/
2018-04-09 10:05:13 +02:00
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;
2016-10-28 11:47:50 +02:00
/**
*
*/
2018-04-09 10:05:13 +02:00
R3.D3.Shape.prototype.createInstance = function() {
2016-10-28 11:47:50 +02:00
var instance = null;
2018-04-09 10:05:13 +02:00
if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_TRIMESH) {
2017-06-24 02:42:28 +02:00
2018-04-09 10:05:13 +02:00
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_SPHERE) {;
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_BOX) {
2017-06-24 02:42:28 +02:00
2018-04-09 10:05:13 +02:00
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_CYLINDER) {
2017-06-24 02:42:28 +02:00
2018-04-09 10:05:13 +02:00
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_HEIGHT_MAP) {
2017-06-24 02:42:28 +02:00
2018-04-09 10:05:13 +02:00
} else if (this.shapeType == R3.D3.Shape.SHAPE_TYPE_CONVEX_HULL) {
2016-11-08 11:13:55 +01:00
instance = new this.engine.instance.ConvexPolyhedron(
this.vertices, this.indices
);
2018-04-09 10:05:13 +02:00
} else if(this.shapeType == R3.D3.Shape.SHAPE_TYPE_PLANE) {
instance = new this.engine.instance.Plane();
2016-10-28 11:47:50 +02:00
} else {
console.warn('Shape type not implemented: ' + this.shapeType);
throw new Error('Shape type not implemented: ' + this.shapeType);
}
this.instance = instance;
return instance;
2016-10-14 12:32:53 +02:00
};
2016-10-28 11:47:50 +02:00
/**
* update
*/
2018-04-09 10:05:13 +02:00
R3.D3.Shape.prototype.update = function(
2016-10-28 11:47:50 +02:00
engine
) {
engine.isNotCannonThrow();
2018-04-09 10:05:13 +02:00
if(this.shapeType === R3.D3.Shape.SHAPE_TYPE_TRIMESH) {
2016-10-28 11:47:50 +02:00
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();
2016-10-14 12:32:53 +02:00
}
};
2016-12-09 20:32:09 +01:00
/**
2018-04-09 10:05:13 +02:00
* Converts a R3.D3.Shape to a R3.D3.API.Shape
* @returns {R3.D3.API.Shape}
2016-12-09 20:32:09 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Shape.prototype.toApiShape = function() {
2016-12-09 20:32:09 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.API.Shape(
2016-12-09 20:32:09 +01:00
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
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
2016-12-09 20:32:09 +01:00
* @param objectShape Object
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Shape.FromObjectShape = function(graphics, objectShape) {
2016-12-09 20:32:09 +01:00
//todo: implement this still
return null;
};