babylon.meshBuilder.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var MeshBuilder = (function () {
  4. function MeshBuilder() {
  5. }
  6. MeshBuilder.CreateBox = function (name, options, scene) {
  7. var box = new BABYLON.Mesh(name, scene);
  8. var vertexData = BABYLON.VertexData.CreateBox(options);
  9. vertexData.applyToMesh(box, options.updatable);
  10. return box;
  11. };
  12. MeshBuilder.CreateSphere = function (name, options, scene) {
  13. var sphere = new BABYLON.Mesh(name, scene);
  14. var vertexData = BABYLON.VertexData.CreateSphere(options);
  15. vertexData.applyToMesh(sphere, options.updatable);
  16. return sphere;
  17. };
  18. MeshBuilder.CreateDisc = function (name, options, scene) {
  19. var disc = new BABYLON.Mesh(name, scene);
  20. var vertexData = BABYLON.VertexData.CreateDisc(options);
  21. vertexData.applyToMesh(disc, options.updatable);
  22. return disc;
  23. };
  24. MeshBuilder.CreateIcoSphere = function (name, options, scene) {
  25. var sphere = new BABYLON.Mesh(name, scene);
  26. var vertexData = BABYLON.VertexData.CreateIcoSphere(options);
  27. vertexData.applyToMesh(sphere, options.updatable);
  28. return sphere;
  29. };
  30. ;
  31. MeshBuilder.CreateRibbon = function (name, options, scene) {
  32. var pathArray = options.pathArray;
  33. var closeArray = options.closeArray;
  34. var closePath = options.closePath;
  35. var offset = options.offset;
  36. var sideOrientation = options.sideOrientation;
  37. var instance = options.instance;
  38. var updatable = options.updatable;
  39. if (instance) {
  40. // positionFunction : ribbon case
  41. // only pathArray and sideOrientation parameters are taken into account for positions update
  42. var positionFunction = function (positions) {
  43. var minlg = pathArray[0].length;
  44. var i = 0;
  45. var ns = (instance.sideOrientation === BABYLON.Mesh.DOUBLESIDE) ? 2 : 1;
  46. for (var si = 1; si <= ns; si++) {
  47. for (var p = 0; p < pathArray.length; p++) {
  48. var path = pathArray[p];
  49. var l = path.length;
  50. minlg = (minlg < l) ? minlg : l;
  51. var j = 0;
  52. while (j < minlg) {
  53. positions[i] = path[j].x;
  54. positions[i + 1] = path[j].y;
  55. positions[i + 2] = path[j].z;
  56. j++;
  57. i += 3;
  58. }
  59. if (instance._closePath) {
  60. positions[i] = path[0].x;
  61. positions[i + 1] = path[0].y;
  62. positions[i + 2] = path[0].z;
  63. i += 3;
  64. }
  65. }
  66. }
  67. };
  68. var positions = instance.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  69. positionFunction(positions);
  70. instance.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions, false, false);
  71. if (!(instance.areNormalsFrozen)) {
  72. var indices = instance.getIndices();
  73. var normals = instance.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  74. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  75. if (instance._closePath) {
  76. var indexFirst = 0;
  77. var indexLast = 0;
  78. for (var p = 0; p < pathArray.length; p++) {
  79. indexFirst = instance._idx[p] * 3;
  80. if (p + 1 < pathArray.length) {
  81. indexLast = (instance._idx[p + 1] - 1) * 3;
  82. }
  83. else {
  84. indexLast = normals.length - 3;
  85. }
  86. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  87. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  88. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  89. normals[indexLast] = normals[indexFirst];
  90. normals[indexLast + 1] = normals[indexFirst + 1];
  91. normals[indexLast + 2] = normals[indexFirst + 2];
  92. }
  93. }
  94. instance.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals, false, false);
  95. }
  96. return instance;
  97. }
  98. else {
  99. var ribbon = new BABYLON.Mesh(name, scene);
  100. ribbon.sideOrientation = sideOrientation;
  101. var vertexData = BABYLON.VertexData.CreateRibbon(options);
  102. if (closePath) {
  103. ribbon._idx = vertexData._idx;
  104. }
  105. ribbon._closePath = closePath;
  106. ribbon._closeArray = closeArray;
  107. vertexData.applyToMesh(ribbon, updatable);
  108. return ribbon;
  109. }
  110. };
  111. MeshBuilder.CreateCylinder = function (name, options, scene) {
  112. var cylinder = new BABYLON.Mesh(name, scene);
  113. var vertexData = BABYLON.VertexData.CreateCylinder(options);
  114. vertexData.applyToMesh(cylinder, options.updatable);
  115. return cylinder;
  116. };
  117. MeshBuilder.CreateTorus = function (name, options, scene) {
  118. var torus = new BABYLON.Mesh(name, scene);
  119. var vertexData = BABYLON.VertexData.CreateTorus(options);
  120. vertexData.applyToMesh(torus, options.updatable);
  121. return torus;
  122. };
  123. MeshBuilder.CreateTorusKnot = function (name, options, scene) {
  124. var torusKnot = new BABYLON.Mesh(name, scene);
  125. var vertexData = BABYLON.VertexData.CreateTorusKnot(options);
  126. vertexData.applyToMesh(torusKnot, options.updatable);
  127. return torusKnot;
  128. };
  129. MeshBuilder.CreateLines = function (name, options, scene) {
  130. var instance = options.instance;
  131. var points = options.points;
  132. if (instance) {
  133. var positionFunction = function (positions) {
  134. var i = 0;
  135. for (var p = 0; p < points.length; p++) {
  136. positions[i] = points[p].x;
  137. positions[i + 1] = points[p].y;
  138. positions[i + 2] = points[p].z;
  139. i += 3;
  140. }
  141. };
  142. instance.updateMeshPositions(positionFunction, false);
  143. return instance;
  144. }
  145. // lines creation
  146. var lines = new BABYLON.LinesMesh(name, scene);
  147. var vertexData = BABYLON.VertexData.CreateLines(options);
  148. vertexData.applyToMesh(lines, options.updatable);
  149. return lines;
  150. };
  151. MeshBuilder.CreateDashedLines = function (name, options, scene) {
  152. var points = options.points;
  153. var instance = options.instance;
  154. var gapSize = options.gapSize;
  155. var dashNb = options.dashNb;
  156. var dashSize = options.dashSize;
  157. if (instance) {
  158. var positionFunction = function (positions) {
  159. var curvect = BABYLON.Vector3.Zero();
  160. var nbSeg = positions.length / 6;
  161. var lg = 0;
  162. var nb = 0;
  163. var shft = 0;
  164. var dashshft = 0;
  165. var curshft = 0;
  166. var p = 0;
  167. var i = 0;
  168. var j = 0;
  169. for (i = 0; i < points.length - 1; i++) {
  170. points[i + 1].subtractToRef(points[i], curvect);
  171. lg += curvect.length();
  172. }
  173. shft = lg / nbSeg;
  174. dashshft = instance.dashSize * shft / (instance.dashSize + instance.gapSize);
  175. for (i = 0; i < points.length - 1; i++) {
  176. points[i + 1].subtractToRef(points[i], curvect);
  177. nb = Math.floor(curvect.length() / shft);
  178. curvect.normalize();
  179. j = 0;
  180. while (j < nb && p < positions.length) {
  181. curshft = shft * j;
  182. positions[p] = points[i].x + curshft * curvect.x;
  183. positions[p + 1] = points[i].y + curshft * curvect.y;
  184. positions[p + 2] = points[i].z + curshft * curvect.z;
  185. positions[p + 3] = points[i].x + (curshft + dashshft) * curvect.x;
  186. positions[p + 4] = points[i].y + (curshft + dashshft) * curvect.y;
  187. positions[p + 5] = points[i].z + (curshft + dashshft) * curvect.z;
  188. p += 6;
  189. j++;
  190. }
  191. }
  192. while (p < positions.length) {
  193. positions[p] = points[i].x;
  194. positions[p + 1] = points[i].y;
  195. positions[p + 2] = points[i].z;
  196. p += 3;
  197. }
  198. };
  199. instance.updateMeshPositions(positionFunction, false);
  200. return instance;
  201. }
  202. // dashed lines creation
  203. var dashedLines = new BABYLON.LinesMesh(name, scene);
  204. var vertexData = BABYLON.VertexData.CreateDashedLines(options);
  205. vertexData.applyToMesh(dashedLines, options.updatable);
  206. dashedLines.dashSize = dashSize;
  207. dashedLines.gapSize = gapSize;
  208. return dashedLines;
  209. };
  210. MeshBuilder.ExtrudeShape = function (name, options, scene) {
  211. var path = options.path;
  212. var shape = options.shape;
  213. var scale = options.scale || 1;
  214. var rotation = options.rotation || 0;
  215. var cap = (options.cap === 0) ? 0 : options.cap || BABYLON.Mesh.NO_CAP;
  216. var updatable = options.updatable;
  217. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  218. var instance = options.instance;
  219. return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, false, false, cap, false, scene, updatable, sideOrientation, instance);
  220. };
  221. MeshBuilder.ExtrudeShapeCustom = function (name, options, scene) {
  222. var path = options.path;
  223. var shape = options.shape;
  224. var scaleFunction = options.scaleFunction || (function () { return 1; });
  225. var rotationFunction = options.rotationFunction || (function () { return 0; });
  226. var ribbonCloseArray = options.ribbonCloseArray || false;
  227. var ribbonClosePath = options.ribbonClosePath || false;
  228. var cap = (options.cap === 0) ? 0 : options.cap || BABYLON.Mesh.NO_CAP;
  229. var updatable = options.updatable;
  230. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  231. var instance = options.instance;
  232. return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, true, scene, updatable, sideOrientation, instance);
  233. };
  234. MeshBuilder.CreateLathe = function (name, options, scene) {
  235. var arc = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  236. var closed = (options.closed === undefined) ? true : options.closed;
  237. var shape = options.shape;
  238. var radius = options.radius || 1;
  239. var tessellation = options.tessellation || 64;
  240. var updatable = options.updatable;
  241. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  242. var pi2 = Math.PI * 2;
  243. var shapeLathe = new Array();
  244. // first rotatable point
  245. var i = 0;
  246. while (shape[i].x === 0) {
  247. i++;
  248. }
  249. var pt = shape[i];
  250. for (i = 0; i < shape.length; i++) {
  251. shapeLathe.push(shape[i].subtract(pt));
  252. }
  253. // circle path
  254. var step = pi2 / tessellation * arc;
  255. var rotated;
  256. var path = new Array();
  257. ;
  258. for (i = 0; i <= tessellation; i++) {
  259. rotated = new BABYLON.Vector3(Math.cos(i * step) * radius, 0, Math.sin(i * step) * radius);
  260. path.push(rotated);
  261. }
  262. if (closed) {
  263. path.push(path[0]);
  264. }
  265. // extrusion
  266. var scaleFunction = function () { return 1; };
  267. var rotateFunction = function () { return 0; };
  268. var lathe = BABYLON.Mesh.ExtrudeShapeCustom(name, shapeLathe, path, scaleFunction, rotateFunction, closed, false, BABYLON.Mesh.NO_CAP, scene, updatable, sideOrientation);
  269. return lathe;
  270. };
  271. MeshBuilder.CreatePlane = function (name, options, scene) {
  272. var plane = new BABYLON.Mesh(name, scene);
  273. var vertexData = BABYLON.VertexData.CreatePlane(options);
  274. vertexData.applyToMesh(plane, options.updatable);
  275. if (options.sourcePlane) {
  276. plane.translate(options.sourcePlane.normal, options.sourcePlane.d);
  277. var product = Math.acos(BABYLON.Vector3.Dot(options.sourcePlane.normal, BABYLON.Axis.Z));
  278. var vectorProduct = BABYLON.Vector3.Cross(BABYLON.Axis.Z, options.sourcePlane.normal);
  279. plane.rotate(vectorProduct, product);
  280. }
  281. return plane;
  282. };
  283. MeshBuilder.CreateGround = function (name, options, scene) {
  284. var ground = new BABYLON.GroundMesh(name, scene);
  285. ground._setReady(false);
  286. ground._subdivisions = options.subdivisions || 1;
  287. var vertexData = BABYLON.VertexData.CreateGround(options);
  288. vertexData.applyToMesh(ground, options.updatable);
  289. ground._setReady(true);
  290. return ground;
  291. };
  292. MeshBuilder.CreateTiledGround = function (name, options, scene) {
  293. var tiledGround = new BABYLON.Mesh(name, scene);
  294. var vertexData = BABYLON.VertexData.CreateTiledGround(options);
  295. vertexData.applyToMesh(tiledGround, options.updatable);
  296. return tiledGround;
  297. };
  298. MeshBuilder.CreateGroundFromHeightMap = function (name, url, options, scene) {
  299. var width = options.width || 10;
  300. var height = options.height || 10;
  301. var subdivisions = options.subdivisions || 1;
  302. var minHeight = options.minHeight;
  303. var maxHeight = options.maxHeight || 10;
  304. var updatable = options.updatable;
  305. var onReady = options.onReady;
  306. var ground = new BABYLON.GroundMesh(name, scene);
  307. ground._subdivisions = subdivisions;
  308. ground._setReady(false);
  309. var onload = function (img) {
  310. // Getting height map data
  311. var canvas = document.createElement("canvas");
  312. var context = canvas.getContext("2d");
  313. var bufferWidth = img.width;
  314. var bufferHeight = img.height;
  315. canvas.width = bufferWidth;
  316. canvas.height = bufferHeight;
  317. context.drawImage(img, 0, 0);
  318. // Create VertexData from map data
  319. // Cast is due to wrong definition in lib.d.ts from ts 1.3 - https://github.com/Microsoft/TypeScript/issues/949
  320. var buffer = context.getImageData(0, 0, bufferWidth, bufferHeight).data;
  321. var vertexData = BABYLON.VertexData.CreateGroundFromHeightMap({
  322. width: width, height: height,
  323. subdivisions: subdivisions,
  324. minHeight: minHeight, maxHeight: maxHeight,
  325. buffer: buffer, bufferWidth: bufferWidth, bufferHeight: bufferHeight
  326. });
  327. vertexData.applyToMesh(ground, updatable);
  328. ground._setReady(true);
  329. //execute ready callback, if set
  330. if (onReady) {
  331. onReady(ground);
  332. }
  333. };
  334. BABYLON.Tools.LoadImage(url, onload, function () { }, scene.database);
  335. return ground;
  336. };
  337. MeshBuilder.CreateTube = function (name, options, scene) {
  338. var path = options.path;
  339. var radius = options.radius || 1;
  340. var tessellation = options.tessellation || 64;
  341. var radiusFunction = options.radiusFunction;
  342. var cap = options.cap || BABYLON.Mesh.NO_CAP;
  343. var updatable = options.updatable;
  344. var sideOrientation = options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  345. var instance = options.instance;
  346. options.arc = (options.arc <= 0 || options.arc > 1) ? 1 : options.arc || 1;
  347. // tube geometry
  348. var tubePathArray = function (path, path3D, circlePaths, radius, tessellation, radiusFunction, cap, arc) {
  349. var tangents = path3D.getTangents();
  350. var normals = path3D.getNormals();
  351. var distances = path3D.getDistances();
  352. var pi2 = Math.PI * 2;
  353. var step = pi2 / tessellation * arc;
  354. var returnRadius = function () { return radius; };
  355. var radiusFunctionFinal = radiusFunction || returnRadius;
  356. var circlePath;
  357. var rad;
  358. var normal;
  359. var rotated;
  360. var rotationMatrix = BABYLON.Matrix.Zero();
  361. var index = (cap === BABYLON.Mesh._NO_CAP || cap === BABYLON.Mesh.CAP_END) ? 0 : 2;
  362. for (var i = 0; i < path.length; i++) {
  363. rad = radiusFunctionFinal(i, distances[i]); // current radius
  364. circlePath = Array(); // current circle array
  365. normal = normals[i]; // current normal
  366. for (var t = 0; t < tessellation; t++) {
  367. BABYLON.Matrix.RotationAxisToRef(tangents[i], step * t, rotationMatrix);
  368. rotated = BABYLON.Vector3.TransformCoordinates(normal, rotationMatrix).scaleInPlace(rad).add(path[i]);
  369. circlePath.push(rotated);
  370. }
  371. circlePaths[index] = circlePath;
  372. index++;
  373. }
  374. // cap
  375. var capPath = function (nbPoints, pathIndex) {
  376. var pointCap = Array();
  377. for (var i = 0; i < nbPoints; i++) {
  378. pointCap.push(path[pathIndex]);
  379. }
  380. return pointCap;
  381. };
  382. switch (cap) {
  383. case BABYLON.Mesh.NO_CAP:
  384. break;
  385. case BABYLON.Mesh.CAP_START:
  386. circlePaths[0] = capPath(tessellation, 0);
  387. circlePaths[1] = circlePaths[2].slice(0);
  388. break;
  389. case BABYLON.Mesh.CAP_END:
  390. circlePaths[index] = circlePaths[index - 1].slice(0);
  391. circlePaths[index + 1] = capPath(tessellation, path.length - 1);
  392. break;
  393. case BABYLON.Mesh.CAP_ALL:
  394. circlePaths[0] = capPath(tessellation, 0);
  395. circlePaths[1] = circlePaths[2].slice(0);
  396. circlePaths[index] = circlePaths[index - 1].slice(0);
  397. circlePaths[index + 1] = capPath(tessellation, path.length - 1);
  398. break;
  399. default:
  400. break;
  401. }
  402. return circlePaths;
  403. };
  404. var path3D;
  405. var pathArray;
  406. if (instance) {
  407. var arc = options.arc || instance.arc;
  408. path3D = (instance.path3D).update(path);
  409. pathArray = tubePathArray(path, path3D, instance.pathArray, radius, instance.tessellation, radiusFunction, instance.cap, arc);
  410. instance = MeshBuilder.CreateRibbon(null, { pathArray: pathArray, instance: instance });
  411. instance.path3D = path3D;
  412. instance.pathArray = pathArray;
  413. instance.arc = arc;
  414. return instance;
  415. }
  416. // tube creation
  417. path3D = new BABYLON.Path3D(path);
  418. var newPathArray = new Array();
  419. cap = (cap < 0 || cap > 3) ? 0 : cap;
  420. pathArray = tubePathArray(path, path3D, newPathArray, radius, tessellation, radiusFunction, cap, options.arc);
  421. var tube = MeshBuilder.CreateRibbon(name, { pathArray: pathArray, closePath: true, closeArray: false, updatable: updatable, sideOrientation: sideOrientation }, scene);
  422. tube.pathArray = pathArray;
  423. tube.path3D = path3D;
  424. tube.tessellation = tessellation;
  425. tube.cap = cap;
  426. tube.arc = options.arc;
  427. return tube;
  428. };
  429. MeshBuilder.CreatePolyhedron = function (name, options, scene) {
  430. var polyhedron = new BABYLON.Mesh(name, scene);
  431. var vertexData = BABYLON.VertexData.CreatePolyhedron(options);
  432. vertexData.applyToMesh(polyhedron, options.updatable);
  433. return polyhedron;
  434. };
  435. MeshBuilder.CreateDecal = function (name, sourceMesh, options) {
  436. var indices = sourceMesh.getIndices();
  437. var positions = sourceMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  438. var normals = sourceMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  439. var position = options.position || BABYLON.Vector3.Zero();
  440. var normal = options.normal || BABYLON.Vector3.Up();
  441. var size = options.size || new BABYLON.Vector3(1, 1, 1);
  442. var angle = options.angle || 0;
  443. // Getting correct rotation
  444. if (!normal) {
  445. var target = new BABYLON.Vector3(0, 0, 1);
  446. var camera = sourceMesh.getScene().activeCamera;
  447. var cameraWorldTarget = BABYLON.Vector3.TransformCoordinates(target, camera.getWorldMatrix());
  448. normal = camera.globalPosition.subtract(cameraWorldTarget);
  449. }
  450. var yaw = -Math.atan2(normal.z, normal.x) - Math.PI / 2;
  451. var len = Math.sqrt(normal.x * normal.x + normal.z * normal.z);
  452. var pitch = Math.atan2(normal.y, len);
  453. // Matrix
  454. var decalWorldMatrix = BABYLON.Matrix.RotationYawPitchRoll(yaw, pitch, angle).multiply(BABYLON.Matrix.Translation(position.x, position.y, position.z));
  455. var inverseDecalWorldMatrix = BABYLON.Matrix.Invert(decalWorldMatrix);
  456. var meshWorldMatrix = sourceMesh.getWorldMatrix();
  457. var transformMatrix = meshWorldMatrix.multiply(inverseDecalWorldMatrix);
  458. var vertexData = new BABYLON.VertexData();
  459. vertexData.indices = [];
  460. vertexData.positions = [];
  461. vertexData.normals = [];
  462. vertexData.uvs = [];
  463. var currentVertexDataIndex = 0;
  464. var extractDecalVector3 = function (indexId) {
  465. var vertexId = indices[indexId];
  466. var result = new BABYLON.PositionNormalVertex();
  467. result.position = new BABYLON.Vector3(positions[vertexId * 3], positions[vertexId * 3 + 1], positions[vertexId * 3 + 2]);
  468. // Send vector to decal local world
  469. result.position = BABYLON.Vector3.TransformCoordinates(result.position, transformMatrix);
  470. // Get normal
  471. result.normal = new BABYLON.Vector3(normals[vertexId * 3], normals[vertexId * 3 + 1], normals[vertexId * 3 + 2]);
  472. return result;
  473. }; // Inspired by https://github.com/mrdoob/three.js/blob/eee231960882f6f3b6113405f524956145148146/examples/js/geometries/DecalGeometry.js
  474. var clip = function (vertices, axis) {
  475. if (vertices.length === 0) {
  476. return vertices;
  477. }
  478. var clipSize = 0.5 * Math.abs(BABYLON.Vector3.Dot(size, axis));
  479. var clipVertices = function (v0, v1) {
  480. var clipFactor = BABYLON.Vector3.GetClipFactor(v0.position, v1.position, axis, clipSize);
  481. return new BABYLON.PositionNormalVertex(BABYLON.Vector3.Lerp(v0.position, v1.position, clipFactor), BABYLON.Vector3.Lerp(v0.normal, v1.normal, clipFactor));
  482. };
  483. var result = new Array();
  484. for (var index = 0; index < vertices.length; index += 3) {
  485. var v1Out;
  486. var v2Out;
  487. var v3Out;
  488. var total = 0;
  489. var nV1, nV2, nV3, nV4;
  490. var d1 = BABYLON.Vector3.Dot(vertices[index].position, axis) - clipSize;
  491. var d2 = BABYLON.Vector3.Dot(vertices[index + 1].position, axis) - clipSize;
  492. var d3 = BABYLON.Vector3.Dot(vertices[index + 2].position, axis) - clipSize;
  493. v1Out = d1 > 0;
  494. v2Out = d2 > 0;
  495. v3Out = d3 > 0;
  496. total = (v1Out ? 1 : 0) + (v2Out ? 1 : 0) + (v3Out ? 1 : 0);
  497. switch (total) {
  498. case 0:
  499. result.push(vertices[index]);
  500. result.push(vertices[index + 1]);
  501. result.push(vertices[index + 2]);
  502. break;
  503. case 1:
  504. if (v1Out) {
  505. nV1 = vertices[index + 1];
  506. nV2 = vertices[index + 2];
  507. nV3 = clipVertices(vertices[index], nV1);
  508. nV4 = clipVertices(vertices[index], nV2);
  509. }
  510. if (v2Out) {
  511. nV1 = vertices[index];
  512. nV2 = vertices[index + 2];
  513. nV3 = clipVertices(vertices[index + 1], nV1);
  514. nV4 = clipVertices(vertices[index + 1], nV2);
  515. result.push(nV3);
  516. result.push(nV2.clone());
  517. result.push(nV1.clone());
  518. result.push(nV2.clone());
  519. result.push(nV3.clone());
  520. result.push(nV4);
  521. break;
  522. }
  523. if (v3Out) {
  524. nV1 = vertices[index];
  525. nV2 = vertices[index + 1];
  526. nV3 = clipVertices(vertices[index + 2], nV1);
  527. nV4 = clipVertices(vertices[index + 2], nV2);
  528. }
  529. result.push(nV1.clone());
  530. result.push(nV2.clone());
  531. result.push(nV3);
  532. result.push(nV4);
  533. result.push(nV3.clone());
  534. result.push(nV2.clone());
  535. break;
  536. case 2:
  537. if (!v1Out) {
  538. nV1 = vertices[index].clone();
  539. nV2 = clipVertices(nV1, vertices[index + 1]);
  540. nV3 = clipVertices(nV1, vertices[index + 2]);
  541. result.push(nV1);
  542. result.push(nV2);
  543. result.push(nV3);
  544. }
  545. if (!v2Out) {
  546. nV1 = vertices[index + 1].clone();
  547. nV2 = clipVertices(nV1, vertices[index + 2]);
  548. nV3 = clipVertices(nV1, vertices[index]);
  549. result.push(nV1);
  550. result.push(nV2);
  551. result.push(nV3);
  552. }
  553. if (!v3Out) {
  554. nV1 = vertices[index + 2].clone();
  555. nV2 = clipVertices(nV1, vertices[index]);
  556. nV3 = clipVertices(nV1, vertices[index + 1]);
  557. result.push(nV1);
  558. result.push(nV2);
  559. result.push(nV3);
  560. }
  561. break;
  562. case 3:
  563. break;
  564. }
  565. }
  566. return result;
  567. };
  568. for (var index = 0; index < indices.length; index += 3) {
  569. var faceVertices = new Array();
  570. faceVertices.push(extractDecalVector3(index));
  571. faceVertices.push(extractDecalVector3(index + 1));
  572. faceVertices.push(extractDecalVector3(index + 2));
  573. // Clip
  574. faceVertices = clip(faceVertices, new BABYLON.Vector3(1, 0, 0));
  575. faceVertices = clip(faceVertices, new BABYLON.Vector3(-1, 0, 0));
  576. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 1, 0));
  577. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, -1, 0));
  578. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, 1));
  579. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, -1));
  580. if (faceVertices.length === 0) {
  581. continue;
  582. }
  583. // Add UVs and get back to world
  584. for (var vIndex = 0; vIndex < faceVertices.length; vIndex++) {
  585. var vertex = faceVertices[vIndex];
  586. //TODO check for Int32Array
  587. vertexData.indices.push(currentVertexDataIndex);
  588. vertex.position.toArray(vertexData.positions, currentVertexDataIndex * 3);
  589. vertex.normal.toArray(vertexData.normals, currentVertexDataIndex * 3);
  590. vertexData.uvs.push(0.5 + vertex.position.x / size.x);
  591. vertexData.uvs.push(0.5 + vertex.position.y / size.y);
  592. currentVertexDataIndex++;
  593. }
  594. }
  595. // Return mesh
  596. var decal = new BABYLON.Mesh(name, sourceMesh.getScene());
  597. vertexData.applyToMesh(decal);
  598. decal.position = position.clone();
  599. decal.rotation = new BABYLON.Vector3(pitch, yaw, angle);
  600. return decal;
  601. };
  602. // Privates
  603. MeshBuilder._ExtrudeShapeGeneric = function (name, shape, curve, scale, rotation, scaleFunction, rotateFunction, rbCA, rbCP, cap, custom, scene, updtbl, side, instance) {
  604. // extrusion geometry
  605. var extrusionPathArray = function (shape, curve, path3D, shapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom) {
  606. var tangents = path3D.getTangents();
  607. var normals = path3D.getNormals();
  608. var binormals = path3D.getBinormals();
  609. var distances = path3D.getDistances();
  610. var angle = 0;
  611. var returnScale = function () { return scale; };
  612. var returnRotation = function () { return rotation; };
  613. var rotate = custom ? rotateFunction : returnRotation;
  614. var scl = custom ? scaleFunction : returnScale;
  615. var index = (cap === BABYLON.Mesh.NO_CAP || cap === BABYLON.Mesh.CAP_END) ? 0 : 2;
  616. var rotationMatrix = BABYLON.Matrix.Zero();
  617. for (var i = 0; i < curve.length; i++) {
  618. var shapePath = new Array();
  619. var angleStep = rotate(i, distances[i]);
  620. var scaleRatio = scl(i, distances[i]);
  621. for (var p = 0; p < shape.length; p++) {
  622. BABYLON.Matrix.RotationAxisToRef(tangents[i], angle, rotationMatrix);
  623. var planed = ((tangents[i].scale(shape[p].z)).add(normals[i].scale(shape[p].x)).add(binormals[i].scale(shape[p].y)));
  624. var rotated = BABYLON.Vector3.TransformCoordinates(planed, rotationMatrix).scaleInPlace(scaleRatio).add(curve[i]);
  625. shapePath.push(rotated);
  626. }
  627. shapePaths[index] = shapePath;
  628. angle += angleStep;
  629. index++;
  630. }
  631. // cap
  632. var capPath = function (shapePath) {
  633. var pointCap = Array();
  634. var barycenter = BABYLON.Vector3.Zero();
  635. var i;
  636. for (i = 0; i < shapePath.length; i++) {
  637. barycenter.addInPlace(shapePath[i]);
  638. }
  639. barycenter.scaleInPlace(1 / shapePath.length);
  640. for (i = 0; i < shapePath.length; i++) {
  641. pointCap.push(barycenter);
  642. }
  643. return pointCap;
  644. };
  645. switch (cap) {
  646. case BABYLON.Mesh.NO_CAP:
  647. break;
  648. case BABYLON.Mesh.CAP_START:
  649. shapePaths[0] = capPath(shapePaths[2]);
  650. shapePaths[1] = shapePaths[2].slice(0);
  651. break;
  652. case BABYLON.Mesh.CAP_END:
  653. shapePaths[index] = shapePaths[index - 1];
  654. shapePaths[index + 1] = capPath(shapePaths[index - 1]);
  655. break;
  656. case BABYLON.Mesh.CAP_ALL:
  657. shapePaths[0] = capPath(shapePaths[2]);
  658. shapePaths[1] = shapePaths[2].slice(0);
  659. shapePaths[index] = shapePaths[index - 1];
  660. shapePaths[index + 1] = capPath(shapePaths[index - 1]);
  661. break;
  662. default:
  663. break;
  664. }
  665. return shapePaths;
  666. };
  667. var path3D;
  668. var pathArray;
  669. if (instance) {
  670. path3D = (instance.path3D).update(curve);
  671. pathArray = extrusionPathArray(shape, curve, instance.path3D, instance.pathArray, scale, rotation, scaleFunction, rotateFunction, instance.cap, custom);
  672. instance = BABYLON.Mesh.CreateRibbon(null, pathArray, null, null, null, null, null, null, instance);
  673. return instance;
  674. }
  675. // extruded shape creation
  676. path3D = new BABYLON.Path3D(curve);
  677. var newShapePaths = new Array();
  678. cap = (cap < 0 || cap > 3) ? 0 : cap;
  679. pathArray = extrusionPathArray(shape, curve, path3D, newShapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom);
  680. var extrudedGeneric = BABYLON.Mesh.CreateRibbon(name, pathArray, rbCA, rbCP, 0, scene, updtbl, side);
  681. extrudedGeneric.pathArray = pathArray;
  682. extrudedGeneric.path3D = path3D;
  683. extrudedGeneric.cap = cap;
  684. return extrudedGeneric;
  685. };
  686. return MeshBuilder;
  687. })();
  688. BABYLON.MeshBuilder = MeshBuilder;
  689. })(BABYLON || (BABYLON = {}));