babylon.edgesRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var FaceAdjacencies = (function () {
  4. function FaceAdjacencies() {
  5. this.edges = new Array();
  6. this.edgesConnectedCount = 0;
  7. }
  8. return FaceAdjacencies;
  9. })();
  10. var EdgesRenderer = (function () {
  11. // Beware when you use this class with complex objects as the adjacencies computation can be really long
  12. function EdgesRenderer(source, epsilon, checkVerticesInsteadOfIndices) {
  13. if (epsilon === void 0) { epsilon = 0.95; }
  14. if (checkVerticesInsteadOfIndices === void 0) { checkVerticesInsteadOfIndices = false; }
  15. this.edgesWidthScalerForOrthographic = 1000.0;
  16. this.edgesWidthScalerForPerspective = 50.0;
  17. this._linesPositions = new Array();
  18. this._linesNormals = new Array();
  19. this._linesIndices = new Array();
  20. this._buffers = new Array();
  21. this._checkVerticesInsteadOfIndices = false;
  22. this._source = source;
  23. this._checkVerticesInsteadOfIndices = checkVerticesInsteadOfIndices;
  24. this._epsilon = epsilon;
  25. this._prepareRessources();
  26. this._generateEdgesLines();
  27. }
  28. EdgesRenderer.prototype._prepareRessources = function () {
  29. if (this._lineShader) {
  30. return;
  31. }
  32. this._lineShader = new BABYLON.ShaderMaterial("lineShader", this._source.getScene(), "line", {
  33. attributes: ["position", "normal"],
  34. uniforms: ["worldViewProjection", "color", "width", "aspectRatio"]
  35. });
  36. this._lineShader.disableDepthWrite = true;
  37. this._lineShader.backFaceCulling = false;
  38. };
  39. EdgesRenderer.prototype.dispose = function () {
  40. this._vb0.dispose();
  41. this._vb1.dispose();
  42. this._source.getScene().getEngine()._releaseBuffer(this._ib);
  43. this._lineShader.dispose();
  44. };
  45. EdgesRenderer.prototype._processEdgeForAdjacencies = function (pa, pb, p0, p1, p2) {
  46. if (pa === p0 && pb === p1 || pa === p1 && pb === p0) {
  47. return 0;
  48. }
  49. if (pa === p1 && pb === p2 || pa === p2 && pb === p1) {
  50. return 1;
  51. }
  52. if (pa === p2 && pb === p0 || pa === p0 && pb === p2) {
  53. return 2;
  54. }
  55. return -1;
  56. };
  57. EdgesRenderer.prototype._processEdgeForAdjacenciesWithVertices = function (pa, pb, p0, p1, p2) {
  58. if (pa.equalsWithEpsilon(p0) && pb.equalsWithEpsilon(p1) || pa.equalsWithEpsilon(p1) && pb.equalsWithEpsilon(p0)) {
  59. return 0;
  60. }
  61. if (pa.equalsWithEpsilon(p1) && pb.equalsWithEpsilon(p2) || pa.equalsWithEpsilon(p2) && pb.equalsWithEpsilon(p1)) {
  62. return 1;
  63. }
  64. if (pa.equalsWithEpsilon(p2) && pb.equalsWithEpsilon(p0) || pa.equalsWithEpsilon(p0) && pb.equalsWithEpsilon(p2)) {
  65. return 2;
  66. }
  67. return -1;
  68. };
  69. EdgesRenderer.prototype._checkEdge = function (faceIndex, edge, faceNormals, p0, p1) {
  70. var needToCreateLine;
  71. if (edge === undefined) {
  72. needToCreateLine = true;
  73. }
  74. else {
  75. var dotProduct = BABYLON.Vector3.Dot(faceNormals[faceIndex], faceNormals[edge]);
  76. needToCreateLine = dotProduct < this._epsilon;
  77. }
  78. if (needToCreateLine) {
  79. var offset = this._linesPositions.length / 3;
  80. var normal = p0.subtract(p1);
  81. normal.normalize();
  82. // Positions
  83. this._linesPositions.push(p0.x);
  84. this._linesPositions.push(p0.y);
  85. this._linesPositions.push(p0.z);
  86. this._linesPositions.push(p0.x);
  87. this._linesPositions.push(p0.y);
  88. this._linesPositions.push(p0.z);
  89. this._linesPositions.push(p1.x);
  90. this._linesPositions.push(p1.y);
  91. this._linesPositions.push(p1.z);
  92. this._linesPositions.push(p1.x);
  93. this._linesPositions.push(p1.y);
  94. this._linesPositions.push(p1.z);
  95. // Normals
  96. this._linesNormals.push(p1.x);
  97. this._linesNormals.push(p1.y);
  98. this._linesNormals.push(p1.z);
  99. this._linesNormals.push(-1);
  100. this._linesNormals.push(p1.x);
  101. this._linesNormals.push(p1.y);
  102. this._linesNormals.push(p1.z);
  103. this._linesNormals.push(1);
  104. this._linesNormals.push(p0.x);
  105. this._linesNormals.push(p0.y);
  106. this._linesNormals.push(p0.z);
  107. this._linesNormals.push(-1);
  108. this._linesNormals.push(p0.x);
  109. this._linesNormals.push(p0.y);
  110. this._linesNormals.push(p0.z);
  111. this._linesNormals.push(1);
  112. // Indices
  113. this._linesIndices.push(offset);
  114. this._linesIndices.push(offset + 1);
  115. this._linesIndices.push(offset + 2);
  116. this._linesIndices.push(offset);
  117. this._linesIndices.push(offset + 2);
  118. this._linesIndices.push(offset + 3);
  119. }
  120. };
  121. EdgesRenderer.prototype._generateEdgesLines = function () {
  122. var positions = this._source.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  123. var indices = this._source.getIndices();
  124. // First let's find adjacencies
  125. var adjacencies = new Array();
  126. var faceNormals = new Array();
  127. var index;
  128. var faceAdjacencies;
  129. // Prepare faces
  130. for (index = 0; index < indices.length; index += 3) {
  131. faceAdjacencies = new FaceAdjacencies();
  132. var p0Index = indices[index];
  133. var p1Index = indices[index + 1];
  134. var p2Index = indices[index + 2];
  135. faceAdjacencies.p0 = new BABYLON.Vector3(positions[p0Index * 3], positions[p0Index * 3 + 1], positions[p0Index * 3 + 2]);
  136. faceAdjacencies.p1 = new BABYLON.Vector3(positions[p1Index * 3], positions[p1Index * 3 + 1], positions[p1Index * 3 + 2]);
  137. faceAdjacencies.p2 = new BABYLON.Vector3(positions[p2Index * 3], positions[p2Index * 3 + 1], positions[p2Index * 3 + 2]);
  138. var faceNormal = BABYLON.Vector3.Cross(faceAdjacencies.p1.subtract(faceAdjacencies.p0), faceAdjacencies.p2.subtract(faceAdjacencies.p1));
  139. faceNormal.normalize();
  140. faceNormals.push(faceNormal);
  141. adjacencies.push(faceAdjacencies);
  142. }
  143. // Scan
  144. for (index = 0; index < adjacencies.length; index++) {
  145. faceAdjacencies = adjacencies[index];
  146. for (var otherIndex = index + 1; otherIndex < adjacencies.length; otherIndex++) {
  147. var otherFaceAdjacencies = adjacencies[otherIndex];
  148. if (faceAdjacencies.edgesConnectedCount === 3) {
  149. break;
  150. }
  151. if (otherFaceAdjacencies.edgesConnectedCount === 3) {
  152. continue;
  153. }
  154. var otherP0 = indices[otherIndex * 3];
  155. var otherP1 = indices[otherIndex * 3 + 1];
  156. var otherP2 = indices[otherIndex * 3 + 2];
  157. for (var edgeIndex = 0; edgeIndex < 3; edgeIndex++) {
  158. var otherEdgeIndex;
  159. if (faceAdjacencies.edges[edgeIndex] !== undefined) {
  160. continue;
  161. }
  162. switch (edgeIndex) {
  163. case 0:
  164. if (this._checkVerticesInsteadOfIndices) {
  165. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p0, faceAdjacencies.p1, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  166. }
  167. else {
  168. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3], indices[index * 3 + 1], otherP0, otherP1, otherP2);
  169. }
  170. break;
  171. case 1:
  172. if (this._checkVerticesInsteadOfIndices) {
  173. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p1, faceAdjacencies.p2, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  174. }
  175. else {
  176. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 1], indices[index * 3 + 2], otherP0, otherP1, otherP2);
  177. }
  178. break;
  179. case 2:
  180. if (this._checkVerticesInsteadOfIndices) {
  181. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p2, faceAdjacencies.p0, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  182. }
  183. else {
  184. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 2], indices[index * 3], otherP0, otherP1, otherP2);
  185. }
  186. break;
  187. }
  188. if (otherEdgeIndex === -1) {
  189. continue;
  190. }
  191. faceAdjacencies.edges[edgeIndex] = otherIndex;
  192. otherFaceAdjacencies.edges[otherEdgeIndex] = index;
  193. faceAdjacencies.edgesConnectedCount++;
  194. otherFaceAdjacencies.edgesConnectedCount++;
  195. if (faceAdjacencies.edgesConnectedCount === 3) {
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. // Create lines
  202. for (index = 0; index < adjacencies.length; index++) {
  203. // We need a line when a face has no adjacency on a specific edge or if all the adjacencies has an angle greater than epsilon
  204. var current = adjacencies[index];
  205. this._checkEdge(index, current.edges[0], faceNormals, current.p0, current.p1);
  206. this._checkEdge(index, current.edges[1], faceNormals, current.p1, current.p2);
  207. this._checkEdge(index, current.edges[2], faceNormals, current.p2, current.p0);
  208. }
  209. // Merge into a single mesh
  210. var engine = this._source.getScene().getEngine();
  211. this._vb0 = new BABYLON.VertexBuffer(engine, this._linesPositions, BABYLON.VertexBuffer.PositionKind, false);
  212. this._vb1 = new BABYLON.VertexBuffer(engine, this._linesNormals, BABYLON.VertexBuffer.NormalKind, false, false, 4);
  213. this._buffers[BABYLON.VertexBuffer.PositionKind] = this._vb0;
  214. this._buffers[BABYLON.VertexBuffer.NormalKind] = this._vb1;
  215. this._ib = engine.createIndexBuffer(this._linesIndices);
  216. this._indicesCount = this._linesIndices.length;
  217. };
  218. EdgesRenderer.prototype.render = function () {
  219. if (!this._lineShader.isReady()) {
  220. return;
  221. }
  222. var scene = this._source.getScene();
  223. var engine = scene.getEngine();
  224. this._lineShader._preBind();
  225. // VBOs
  226. engine.bindMultiBuffers(this._buffers, this._ib, this._lineShader.getEffect());
  227. scene.resetCachedMaterial();
  228. this._lineShader.setColor4("color", this._source.edgesColor);
  229. if (scene.activeCamera.mode === BABYLON.Camera.ORTHOGRAPHIC_CAMERA) {
  230. this._lineShader.setFloat("width", this._source.edgesWidth / this.edgesWidthScalerForOrthographic);
  231. }
  232. else {
  233. this._lineShader.setFloat("width", this._source.edgesWidth / this.edgesWidthScalerForPerspective);
  234. }
  235. this._lineShader.setFloat("aspectRatio", engine.getAspectRatio(scene.activeCamera));
  236. this._lineShader.bind(this._source.getWorldMatrix());
  237. // Draw order
  238. engine.draw(true, 0, this._indicesCount);
  239. this._lineShader.unbind();
  240. engine.setDepthWrite(true);
  241. };
  242. return EdgesRenderer;
  243. })();
  244. BABYLON.EdgesRenderer = EdgesRenderer;
  245. })(BABYLON || (BABYLON = {}));