|
@@ -7,7 +7,7 @@ var BABYLON = BABYLON || {};
|
|
|
};
|
|
|
|
|
|
// Methods
|
|
|
- BABYLON.VertexData.prototype.applyToMesh = function(mesh, updatable) {
|
|
|
+ BABYLON.VertexData.prototype.applyToMesh = function (mesh, updatable) {
|
|
|
if (this.positions) {
|
|
|
mesh.setVerticesData(this.positions, BABYLON.VertexBuffer.PositionKind, updatable);
|
|
|
}
|
|
@@ -20,10 +20,22 @@ var BABYLON = BABYLON || {};
|
|
|
mesh.setVerticesData(this.uvs, BABYLON.VertexBuffer.UVKind, updatable);
|
|
|
}
|
|
|
|
|
|
+ if (this.uv2s) {
|
|
|
+ mesh.setVerticesData(this.uv2s, BABYLON.VertexBuffer.UV2Kind, updatable);
|
|
|
+ }
|
|
|
+
|
|
|
if (this.colors) {
|
|
|
mesh.setVerticesData(this.colors, BABYLON.VertexBuffer.ColorKind, updatable);
|
|
|
}
|
|
|
|
|
|
+ if (this.matricesIndices) {
|
|
|
+ mesh.setVerticesData(this.matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind, updatable);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.matricesWeights) {
|
|
|
+ mesh.setVerticesData(this.matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, updatable);
|
|
|
+ }
|
|
|
+
|
|
|
if (this.indices) {
|
|
|
mesh.setIndices(this.indices);
|
|
|
}
|
|
@@ -99,6 +111,33 @@ var BABYLON = BABYLON || {};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (other.uv2s) {
|
|
|
+ if (!this.uv2s) {
|
|
|
+ this.uv2s = [];
|
|
|
+ }
|
|
|
+ for (index = 0; index < other.uv2s.length; index++) {
|
|
|
+ this.uv2s.push(other.uv2s[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (other.matricesIndices) {
|
|
|
+ if (!this.matricesIndices) {
|
|
|
+ this.matricesIndices = [];
|
|
|
+ }
|
|
|
+ for (index = 0; index < other.matricesIndices.length; index++) {
|
|
|
+ this.matricesIndices.push(other.matricesIndices[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (other.matricesWeights) {
|
|
|
+ if (!this.matricesWeights) {
|
|
|
+ this.matricesWeights = [];
|
|
|
+ }
|
|
|
+ for (index = 0; index < other.matricesWeights.length; index++) {
|
|
|
+ this.matricesWeights.push(other.matricesWeights[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (other.colors) {
|
|
|
if (!this.colors) {
|
|
|
this.colors = [];
|
|
@@ -110,7 +149,43 @@ var BABYLON = BABYLON || {};
|
|
|
};
|
|
|
|
|
|
// Statics
|
|
|
- BABYLON.VertexData.CreateBox = function(size) {
|
|
|
+ BABYLON.VertexData.ExtractFromMesh = function (mesh) {
|
|
|
+ var result = new BABYLON.VertexData();
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
|
|
|
+ result.positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
|
|
|
+ result.normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
|
|
|
+ result.uvs = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
|
|
|
+ result.uv2s = mesh.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
|
|
|
+ result.colors = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
|
|
|
+ result.matricesIndices = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
|
|
|
+ result.matricesWeights = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ result.indices = mesh.getIndices();
|
|
|
+
|
|
|
+ return result;
|
|
|
+ };
|
|
|
+
|
|
|
+ BABYLON.VertexData.CreateBox = function (size) {
|
|
|
var normalsSource = [
|
|
|
new BABYLON.Vector3(0, 0, 1),
|
|
|
new BABYLON.Vector3(0, 0, -1),
|
|
@@ -125,6 +200,8 @@ var BABYLON = BABYLON || {};
|
|
|
var normals = [];
|
|
|
var uvs = [];
|
|
|
|
|
|
+ size = size || 1;
|
|
|
+
|
|
|
// Create each face in turn.
|
|
|
for (var index = 0; index < normalsSource.length; index++) {
|
|
|
var normal = normalsSource[index];
|
|
@@ -177,6 +254,10 @@ var BABYLON = BABYLON || {};
|
|
|
};
|
|
|
|
|
|
BABYLON.VertexData.CreateSphere = function (segments, diameter) {
|
|
|
+
|
|
|
+ segments = segments || 32;
|
|
|
+ diameter = diameter || 1;
|
|
|
+
|
|
|
var radius = diameter / 2;
|
|
|
|
|
|
var totalZRotationSteps = 2 + segments;
|
|
@@ -242,6 +323,11 @@ var BABYLON = BABYLON || {};
|
|
|
var normals = [];
|
|
|
var uvs = [];
|
|
|
|
|
|
+ height = height || 1;
|
|
|
+ diameterTop = diameterTop || 0.5;
|
|
|
+ diameterBottom = diameterBottom || 1;
|
|
|
+ tessellation = tessellation || 16;
|
|
|
+
|
|
|
var getCircleVector = function (i) {
|
|
|
var angle = (i * 2.0 * Math.PI / tessellation);
|
|
|
var dx = Math.sin(angle);
|
|
@@ -350,6 +436,10 @@ var BABYLON = BABYLON || {};
|
|
|
var normals = [];
|
|
|
var uvs = [];
|
|
|
|
|
|
+ diameter = diameter || 1;
|
|
|
+ thickness = thickness || 0.5;
|
|
|
+ tessellation = tessellation || 16;
|
|
|
+
|
|
|
var stride = tessellation + 1;
|
|
|
|
|
|
for (var i = 0; i <= tessellation; i++) {
|
|
@@ -410,6 +500,10 @@ var BABYLON = BABYLON || {};
|
|
|
var uvs = [];
|
|
|
var row, col;
|
|
|
|
|
|
+ width = width || 1;
|
|
|
+ height = height || 1;
|
|
|
+ subdivisions = subdivisions || 1;
|
|
|
+
|
|
|
for (row = 0; row <= subdivisions; row++) {
|
|
|
for (col = 0; col <= subdivisions; col++) {
|
|
|
var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
|
|
@@ -450,6 +544,8 @@ var BABYLON = BABYLON || {};
|
|
|
var normals = [];
|
|
|
var uvs = [];
|
|
|
|
|
|
+ size = size || 1;
|
|
|
+
|
|
|
// Vertices
|
|
|
var halfSize = size / 2.0;
|
|
|
positions.push(-halfSize, -halfSize, 0);
|
|
@@ -487,4 +583,139 @@ var BABYLON = BABYLON || {};
|
|
|
|
|
|
return vertexData;
|
|
|
};
|
|
|
+
|
|
|
+ // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
|
|
|
+ BABYLON.VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q) {
|
|
|
+ var indices = [];
|
|
|
+ var positions = [];
|
|
|
+ var normals = [];
|
|
|
+ var uvs = [];
|
|
|
+
|
|
|
+ radius = radius || 2;
|
|
|
+ tube = tube || 0.5;
|
|
|
+ radialSegments = radialSegments || 32;
|
|
|
+ tubularSegments = tubularSegments || 32;
|
|
|
+ p = p || 2;
|
|
|
+ q = q || 3;
|
|
|
+
|
|
|
+ // Helper
|
|
|
+ function getPos(u, q, p, radius) {
|
|
|
+
|
|
|
+ var cu = Math.cos(u);
|
|
|
+ var su = Math.sin(u);
|
|
|
+ var quOverP = q / p * u;
|
|
|
+ var cs = Math.cos(quOverP);
|
|
|
+
|
|
|
+ var tx = radius * (2 + cs) * 0.5 * cu;
|
|
|
+ var ty = radius * (2 + cs) * su * 0.5;
|
|
|
+ var tz = radius * Math.sin(quOverP) * 0.5;
|
|
|
+
|
|
|
+ return new BABYLON.Vector3(tx, ty, tz);
|
|
|
+ };
|
|
|
+
|
|
|
+ // Vertices
|
|
|
+ for (var i = 0; i <= radialSegments; i++) {
|
|
|
+ var modI = i % radialSegments;
|
|
|
+ var u = modI / radialSegments * 2 * p * Math.PI;
|
|
|
+ var p1 = getPos(u, q, p, radius);
|
|
|
+ var p2 = getPos(u + 0.01, q, p, radius);
|
|
|
+ var tang = p2.subtract(p1);
|
|
|
+ var n = p2.add(p1);
|
|
|
+
|
|
|
+ var bitan = BABYLON.Vector3.Cross(tang, n);
|
|
|
+ n = BABYLON.Vector3.Cross(bitan, tang);
|
|
|
+
|
|
|
+ bitan.normalize();
|
|
|
+ n.normalize();
|
|
|
+
|
|
|
+ for (var j = 0; j < tubularSegments; j++) {
|
|
|
+ var modJ = j % tubularSegments;
|
|
|
+ var v = modJ / tubularSegments * 2 * Math.PI;
|
|
|
+ var cx = -tube * Math.cos(v);
|
|
|
+ var cy = tube * Math.sin(v);
|
|
|
+
|
|
|
+ positions.push(p1.x + cx * n.x + cy * bitan.x);
|
|
|
+ positions.push(p1.y + cx * n.y + cy * bitan.y);
|
|
|
+ positions.push(p1.z + cx * n.z + cy * bitan.z);
|
|
|
+
|
|
|
+ uvs.push(i / radialSegments);
|
|
|
+ uvs.push(j / tubularSegments);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var max = radialSegments * tubularSegments - 1;
|
|
|
+
|
|
|
+ for (var i = 0; i < radialSegments; i++) {
|
|
|
+ for (var j = 0; j < tubularSegments; j++) {
|
|
|
+ var jNext = (j + 1) % tubularSegments;
|
|
|
+ var a = i * tubularSegments + j;
|
|
|
+ var b = (i + 1) * tubularSegments + j;
|
|
|
+ var c = (i + 1) * tubularSegments + jNext;
|
|
|
+ var d = i * tubularSegments + jNext;
|
|
|
+
|
|
|
+ indices.push(d); indices.push(b); indices.push(a);
|
|
|
+ indices.push(d); indices.push(c); indices.push(b);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Normals
|
|
|
+ BABYLON.VertexData.ComputeNormals(positions, indices, normals);
|
|
|
+
|
|
|
+ // Result
|
|
|
+ var vertexData = new BABYLON.VertexData();
|
|
|
+
|
|
|
+ vertexData.indices = indices;
|
|
|
+ vertexData.positions = positions;
|
|
|
+ vertexData.normals = normals;
|
|
|
+ vertexData.uvs = uvs;
|
|
|
+
|
|
|
+ return vertexData;
|
|
|
+ };
|
|
|
+
|
|
|
+ // Tools
|
|
|
+ BABYLON.VertexData.ComputeNormals = function (positions, indices, normals) {
|
|
|
+ var positionVectors = [];
|
|
|
+ var facesOfVertices = [];
|
|
|
+ var index;
|
|
|
+
|
|
|
+ for (index = 0; index < positions.length; index += 3) {
|
|
|
+ var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
|
|
|
+ positionVectors.push(vector3);
|
|
|
+ facesOfVertices.push([]);
|
|
|
+ }
|
|
|
+ // Compute normals
|
|
|
+ var facesNormals = [];
|
|
|
+ for (index = 0; index < indices.length / 3; index++) {
|
|
|
+ var i1 = indices[index * 3];
|
|
|
+ var i2 = indices[index * 3 + 1];
|
|
|
+ var i3 = indices[index * 3 + 2];
|
|
|
+
|
|
|
+ var p1 = positionVectors[i1];
|
|
|
+ var p2 = positionVectors[i2];
|
|
|
+ var p3 = positionVectors[i3];
|
|
|
+
|
|
|
+ var p1p2 = p1.subtract(p2);
|
|
|
+ var p3p2 = p3.subtract(p2);
|
|
|
+
|
|
|
+ facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
|
|
|
+ facesOfVertices[i1].push(index);
|
|
|
+ facesOfVertices[i2].push(index);
|
|
|
+ facesOfVertices[i3].push(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (index = 0; index < positionVectors.length; index++) {
|
|
|
+ var faces = facesOfVertices[index];
|
|
|
+
|
|
|
+ var normal = BABYLON.Vector3.Zero();
|
|
|
+ for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
|
|
|
+ normal.addInPlace(facesNormals[faces[faceIndex]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
|
|
|
+
|
|
|
+ normals[index * 3] = normal.x;
|
|
|
+ normals[index * 3 + 1] = normal.y;
|
|
|
+ normals[index * 3 + 2] = normal.z;
|
|
|
+ }
|
|
|
+ };
|
|
|
})();
|