|
@@ -1,22 +1,21 @@
|
|
|
import { VertexData } from "../mesh.vertexData";
|
|
|
-import { Vector2, Vector3 } from "../../Maths/math.vector";
|
|
|
+import { Vector2, Vector3, Matrix } from "../../Maths/math.vector";
|
|
|
import { Mesh, _CreationDataStorage } from "../mesh";
|
|
|
- /**
|
|
|
- * Scripts based off of https://github.com/maximeq/three-js-capsule-geometry/blob/master/src/CapsuleBufferGeometry.js
|
|
|
- * @param options the constructors options used to shape the mesh.
|
|
|
- * @returns the capsule VertexData
|
|
|
- * @see https://doc.babylonjs.com/how_to/capsule_shape
|
|
|
- */
|
|
|
+/**
|
|
|
+ * Scripts based off of https://github.com/maximeq/three-js-capsule-geometry/blob/master/src/CapsuleBufferGeometry.js
|
|
|
+ * @param options the constructors options used to shape the mesh.
|
|
|
+ * @returns the capsule VertexData
|
|
|
+ * @see https://doc.babylonjs.com/how_to/capsule_shape
|
|
|
+ */
|
|
|
VertexData.CreateCapsule = function(
|
|
|
options: ICreateCapsuleOptions = {
|
|
|
- orientation : Vector3.Up(),
|
|
|
subdivisions: 2,
|
|
|
tessellation: 16,
|
|
|
height: 1,
|
|
|
radius: 0.25,
|
|
|
capSubdivisions: 6
|
|
|
}): VertexData {
|
|
|
- //let path = options.orientation || Vector3.Up()
|
|
|
+
|
|
|
let subdivisions = Math.max(options.subdivisions ? options.subdivisions : 2, 1);
|
|
|
let tessellation = Math.max(options.tessellation ? options.tessellation : 16, 3);
|
|
|
let height = Math.max(options.height ? options.height : 2, 0.);
|
|
@@ -43,31 +42,30 @@ VertexData.CreateCapsule = function(
|
|
|
var uvs = [];
|
|
|
|
|
|
var index = 0,
|
|
|
- //indexOffset = 0,
|
|
|
- indexArray = [],
|
|
|
- halfHeight = height / 2;
|
|
|
+ indexArray = [],
|
|
|
+ halfHeight = height / 2;
|
|
|
|
|
|
var x, y;
|
|
|
var normal = Vector3.Zero();
|
|
|
var vertex = Vector3.Zero();
|
|
|
|
|
|
- var cosAlpha = Math.cos(alpha);
|
|
|
- var sinAlpha = Math.sin(alpha);
|
|
|
+ var cosAlpha = Math.cos(alpha);
|
|
|
+ var sinAlpha = Math.sin(alpha);
|
|
|
|
|
|
- var cone_length =
|
|
|
- new Vector2(
|
|
|
- radiusTop * sinAlpha,
|
|
|
- halfHeight + radiusTop * cosAlpha
|
|
|
- ).subtract(new Vector2(
|
|
|
- radiusBottom * sinAlpha,
|
|
|
- -halfHeight + radiusBottom * cosAlpha
|
|
|
- )
|
|
|
- ).length();
|
|
|
+ var cone_length =
|
|
|
+ new Vector2(
|
|
|
+ radiusTop * sinAlpha,
|
|
|
+ halfHeight + radiusTop * cosAlpha
|
|
|
+ ).subtract(new Vector2(
|
|
|
+ radiusBottom * sinAlpha,
|
|
|
+ -halfHeight + radiusBottom * cosAlpha
|
|
|
+ )
|
|
|
+ ).length();
|
|
|
|
|
|
// Total length for v texture coord
|
|
|
- var vl = radiusTop * alpha
|
|
|
- + cone_length
|
|
|
- + radiusBottom * (Math.PI / 2 - alpha);
|
|
|
+ var vl = radiusTop * alpha
|
|
|
+ + cone_length
|
|
|
+ + radiusBottom * (Math.PI / 2 - alpha);
|
|
|
|
|
|
//var groupCount = 0;
|
|
|
|
|
@@ -89,20 +87,15 @@ VertexData.CreateCapsule = function(
|
|
|
var _radius = cosA * radiusTop;
|
|
|
|
|
|
for (x = 0; x <= radialSegments; x ++) {
|
|
|
-
|
|
|
var u = x / radialSegments;
|
|
|
-
|
|
|
var theta = u * thetaLength + thetaStart;
|
|
|
-
|
|
|
var sinTheta = Math.sin(theta);
|
|
|
var cosTheta = Math.cos(theta);
|
|
|
-
|
|
|
// vertex
|
|
|
vertex.x = _radius * sinTheta;
|
|
|
vertex.y = halfHeight + sinA * radiusTop;
|
|
|
vertex.z = _radius * cosTheta;
|
|
|
vertices.push(vertex.x, vertex.y, vertex.z);
|
|
|
-
|
|
|
// normal
|
|
|
normal.set(cosA * sinTheta, sinA, cosA * cosTheta);
|
|
|
normals.push(normal.x, normal.y, normal.z);
|
|
@@ -113,56 +106,39 @@ VertexData.CreateCapsule = function(
|
|
|
// increase index
|
|
|
index ++;
|
|
|
}
|
|
|
-
|
|
|
// now save vertices of the row in our index array
|
|
|
indexArray.push(indexRow);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
var cone_height = height + cosAlpha * radiusTop - cosAlpha * radiusBottom;
|
|
|
var slope = sinAlpha * (radiusBottom - radiusTop) / cone_height;
|
|
|
for (y = 1; y <= heightSegments; y++) {
|
|
|
-
|
|
|
var indexRow = [];
|
|
|
-
|
|
|
v += cone_length / heightSegments;
|
|
|
-
|
|
|
// calculate the radius of the current row
|
|
|
var _radius = sinAlpha * (y * (radiusBottom - radiusTop) / heightSegments + radiusTop);
|
|
|
-
|
|
|
for (x = 0; x <= radialSegments; x ++) {
|
|
|
-
|
|
|
var u = x / radialSegments;
|
|
|
-
|
|
|
var theta = u * thetaLength + thetaStart;
|
|
|
-
|
|
|
var sinTheta = Math.sin(theta);
|
|
|
var cosTheta = Math.cos(theta);
|
|
|
-
|
|
|
// vertex
|
|
|
vertex.x = _radius * sinTheta;
|
|
|
vertex.y = halfHeight + cosAlpha * radiusTop - y * cone_height / heightSegments;
|
|
|
vertex.z = _radius * cosTheta;
|
|
|
vertices.push(vertex.x, vertex.y, vertex.z);
|
|
|
-
|
|
|
// normal
|
|
|
normal.set(sinTheta, slope, cosTheta).normalize();
|
|
|
normals.push(normal.x, normal.y, normal.z);
|
|
|
-
|
|
|
// uv
|
|
|
uvs.push(u, 1 - v / vl);
|
|
|
-
|
|
|
// save index of vertex in respective row
|
|
|
indexRow.push(index);
|
|
|
-
|
|
|
// increase index
|
|
|
index ++;
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
// now save vertices of the row in our index array
|
|
|
indexArray.push(indexRow);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
for (y = 1; y <= capsBottomSegments; y++) {
|
|
@@ -229,13 +205,26 @@ VertexData.CreateCapsule = function(
|
|
|
}
|
|
|
indices = indices.reverse();
|
|
|
|
|
|
- let vDat = new VertexData();
|
|
|
- vDat.positions = vertices;
|
|
|
- vDat.normals = normals;
|
|
|
- vDat.uvs = uvs;
|
|
|
- vDat.indices = indices;
|
|
|
+ if (options.orientation && !options.orientation.equals(Vector3.Up())) {
|
|
|
+ let m = new Matrix();
|
|
|
+ (options.orientation.clone().scale(Math.PI * 0.5).cross(Vector3.Up()).toQuaternion()).toRotationMatrix(m);
|
|
|
+ let v = Vector3.Zero();
|
|
|
+ for (let i = 0; i < vertices.length; i += 3) {
|
|
|
+ v.set(vertices[i], vertices[i + 1], vertices[i + 2]);
|
|
|
+ Vector3.TransformCoordinatesToRef(v.clone(), m, v);
|
|
|
+ vertices[i] = v.x;
|
|
|
+ vertices[i + 1] = v.y;
|
|
|
+ vertices[i + 2] = v.z;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return vDat;
|
|
|
+ let vDat = new VertexData();
|
|
|
+ vDat.positions = vertices;
|
|
|
+ vDat.normals = normals;
|
|
|
+ vDat.uvs = uvs;
|
|
|
+ vDat.indices = indices;
|
|
|
+
|
|
|
+ return vDat;
|
|
|
};
|
|
|
|
|
|
/**
|
|
@@ -243,7 +232,7 @@ VertexData.CreateCapsule = function(
|
|
|
*/
|
|
|
export interface ICreateCapsuleOptions{
|
|
|
/** The Orientation of the capsule. Default : Vector3.Up() */
|
|
|
- orientation: Vector3;
|
|
|
+ orientation?: Vector3;
|
|
|
|
|
|
/** Number of sub segments on the tube section of the capsule running parallel to orientation. */
|
|
|
subdivisions: number;
|
|
@@ -284,6 +273,7 @@ export interface ICreateCapsuleOptions{
|
|
|
Mesh.CreateCapsule = (name: string, options: ICreateCapsuleOptions, scene): Mesh => {
|
|
|
return CapsuleBuilder.CreateCapsule(name, options, scene);
|
|
|
};
|
|
|
+
|
|
|
/**
|
|
|
* Class containing static functions to help procedurally build meshes
|
|
|
*/
|