babylon.edgesRenderer.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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) {
  13. if (epsilon === void 0) { epsilon = 0.95; }
  14. this._linesPositions = new Array();
  15. this._linesNormals = new Array();
  16. this._linesIndices = new Array();
  17. this._buffers = new Array();
  18. this._source = source;
  19. this._epsilon = epsilon;
  20. this._prepareRessources();
  21. this._generateEdgesLines();
  22. }
  23. EdgesRenderer.prototype._prepareRessources = function () {
  24. if (this._lineShader) {
  25. return;
  26. }
  27. this._lineShader = new BABYLON.ShaderMaterial("lineShader", this._source.getScene(), "line", {
  28. attributes: ["position", "normal"],
  29. uniforms: ["worldViewProjection", "color", "width"]
  30. });
  31. this._lineShader.disableDepthWrite = true;
  32. this._lineShader.backFaceCulling = false;
  33. };
  34. EdgesRenderer.prototype.dispose = function () {
  35. this._vb0.dispose();
  36. this._vb1.dispose();
  37. this._source.getScene().getEngine()._releaseBuffer(this._ib);
  38. this._lineShader.dispose();
  39. };
  40. EdgesRenderer.prototype._processEdgeForAdjacencies = function (pa, pb, p0, p1, p2) {
  41. if (pa === p0 && pb === p1 || pa === p1 && pb === p0) {
  42. return 0;
  43. }
  44. if (pa === p1 && pb === p2 || pa === p2 && pb === p1) {
  45. return 1;
  46. }
  47. if (pa === p2 && pb === p0 || pa === p0 && pb === p2) {
  48. return 2;
  49. }
  50. return -1;
  51. };
  52. EdgesRenderer.prototype._checkEdge = function (faceIndex, edge, faceNormals, p0, p1) {
  53. var needToCreateLine;
  54. if (edge === undefined) {
  55. needToCreateLine = true;
  56. }
  57. else {
  58. var dotProduct = BABYLON.Vector3.Dot(faceNormals[faceIndex], faceNormals[edge]);
  59. needToCreateLine = dotProduct < this._epsilon;
  60. }
  61. if (needToCreateLine) {
  62. var offset = this._linesPositions.length / 3;
  63. var normal = p0.subtract(p1);
  64. normal.normalize();
  65. // Positions
  66. this._linesPositions.push(p0.x);
  67. this._linesPositions.push(p0.y);
  68. this._linesPositions.push(p0.z);
  69. this._linesPositions.push(p0.x);
  70. this._linesPositions.push(p0.y);
  71. this._linesPositions.push(p0.z);
  72. this._linesPositions.push(p1.x);
  73. this._linesPositions.push(p1.y);
  74. this._linesPositions.push(p1.z);
  75. this._linesPositions.push(p1.x);
  76. this._linesPositions.push(p1.y);
  77. this._linesPositions.push(p1.z);
  78. // Normals
  79. this._linesNormals.push(normal.x);
  80. this._linesNormals.push(normal.y);
  81. this._linesNormals.push(normal.z);
  82. this._linesNormals.push(-normal.x);
  83. this._linesNormals.push(-normal.y);
  84. this._linesNormals.push(-normal.z);
  85. this._linesNormals.push(-normal.x);
  86. this._linesNormals.push(-normal.y);
  87. this._linesNormals.push(-normal.z);
  88. this._linesNormals.push(normal.x);
  89. this._linesNormals.push(normal.y);
  90. this._linesNormals.push(normal.z);
  91. // Indices
  92. this._linesIndices.push(offset);
  93. this._linesIndices.push(offset + 1);
  94. this._linesIndices.push(offset + 2);
  95. this._linesIndices.push(offset);
  96. this._linesIndices.push(offset + 2);
  97. this._linesIndices.push(offset + 3);
  98. }
  99. };
  100. EdgesRenderer.prototype._generateEdgesLines = function () {
  101. var positions = this._source.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  102. var indices = this._source.getIndices();
  103. // First let's find adjacencies
  104. var adjacencies = new Array();
  105. var faceNormals = new Array();
  106. var index;
  107. var faceAdjacencies;
  108. // Prepare faces
  109. for (index = 0; index < indices.length; index += 3) {
  110. faceAdjacencies = new FaceAdjacencies();
  111. var p0Index = indices[index];
  112. var p1Index = indices[index + 1];
  113. var p2Index = indices[index + 2];
  114. faceAdjacencies.p0 = new BABYLON.Vector3(positions[p0Index * 3], positions[p0Index * 3 + 1], positions[p0Index * 3 + 2]);
  115. faceAdjacencies.p1 = new BABYLON.Vector3(positions[p1Index * 3], positions[p1Index * 3 + 1], positions[p1Index * 3 + 2]);
  116. faceAdjacencies.p2 = new BABYLON.Vector3(positions[p2Index * 3], positions[p2Index * 3 + 1], positions[p2Index * 3 + 2]);
  117. var faceNormal = BABYLON.Vector3.Cross(faceAdjacencies.p1.subtract(faceAdjacencies.p0), faceAdjacencies.p2.subtract(faceAdjacencies.p1));
  118. faceNormal.normalize();
  119. faceNormals.push(faceNormal);
  120. adjacencies.push(faceAdjacencies);
  121. }
  122. // Scan
  123. for (index = 0; index < adjacencies.length; index++) {
  124. faceAdjacencies = adjacencies[index];
  125. for (var otherIndex = index + 1; otherIndex < adjacencies.length; otherIndex++) {
  126. var otherFaceAdjacencies = adjacencies[otherIndex];
  127. if (faceAdjacencies.edgesConnectedCount === 3) {
  128. break;
  129. }
  130. if (otherFaceAdjacencies.edgesConnectedCount === 3) {
  131. continue;
  132. }
  133. var otherP0 = indices[otherIndex * 3];
  134. var otherP1 = indices[otherIndex * 3 + 1];
  135. var otherP2 = indices[otherIndex * 3 + 2];
  136. for (var edgeIndex = 0; edgeIndex < 3; edgeIndex++) {
  137. var otherEdgeIndex;
  138. if (faceAdjacencies.edges[edgeIndex] !== undefined) {
  139. continue;
  140. }
  141. switch (edgeIndex) {
  142. case 0:
  143. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3], indices[index * 3 + 1], otherP0, otherP1, otherP2);
  144. break;
  145. case 1:
  146. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 1], indices[index * 3 + 2], otherP0, otherP1, otherP2);
  147. break;
  148. case 2:
  149. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 2], indices[index * 3], otherP0, otherP1, otherP2);
  150. break;
  151. }
  152. if (otherEdgeIndex === -1) {
  153. continue;
  154. }
  155. faceAdjacencies.edges[edgeIndex] = otherIndex;
  156. otherFaceAdjacencies.edges[otherEdgeIndex] = index;
  157. faceAdjacencies.edgesConnectedCount++;
  158. otherFaceAdjacencies.edgesConnectedCount++;
  159. if (faceAdjacencies.edgesConnectedCount === 3) {
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. // Create lines
  166. for (index = 0; index < adjacencies.length; index++) {
  167. // 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
  168. var current = adjacencies[index];
  169. this._checkEdge(index, current.edges[0], faceNormals, current.p0, current.p1);
  170. this._checkEdge(index, current.edges[1], faceNormals, current.p1, current.p2);
  171. this._checkEdge(index, current.edges[2], faceNormals, current.p2, current.p0);
  172. }
  173. // Merge into a single mesh
  174. var engine = this._source.getScene().getEngine();
  175. this._vb0 = new BABYLON.VertexBuffer(engine, this._linesPositions, BABYLON.VertexBuffer.PositionKind, false);
  176. this._vb1 = new BABYLON.VertexBuffer(engine, this._linesNormals, BABYLON.VertexBuffer.NormalKind, false);
  177. this._buffers[BABYLON.VertexBuffer.PositionKind] = this._vb0;
  178. this._buffers[BABYLON.VertexBuffer.NormalKind] = this._vb1;
  179. this._ib = engine.createIndexBuffer(this._linesIndices);
  180. this._indicesCount = this._linesIndices.length;
  181. };
  182. EdgesRenderer.prototype.render = function () {
  183. if (!this._lineShader.isReady()) {
  184. return;
  185. }
  186. var scene = this._source.getScene();
  187. var engine = scene.getEngine();
  188. this._lineShader._preBind();
  189. // VBOs
  190. engine.bindMultiBuffers(this._buffers, this._ib, this._lineShader.getEffect());
  191. scene.resetCachedMaterial();
  192. this._lineShader.setColor4("color", this._source.edgesColor);
  193. this._lineShader.setFloat("width", this._source.edgesWidth / 100.0);
  194. this._lineShader.bind(this._source.getWorldMatrix());
  195. // Draw order
  196. engine.draw(true, 0, this._indicesCount);
  197. this._lineShader.unbind();
  198. engine.setDepthWrite(true);
  199. };
  200. return EdgesRenderer;
  201. })();
  202. BABYLON.EdgesRenderer = EdgesRenderer;
  203. })(BABYLON || (BABYLON = {}));