babylon.edgesRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 = {};
  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. var buffer = this._buffers[BABYLON.VertexBuffer.PositionKind];
  41. if (buffer) {
  42. buffer.dispose();
  43. this._buffers[BABYLON.VertexBuffer.PositionKind] = null;
  44. }
  45. buffer = this._buffers[BABYLON.VertexBuffer.NormalKind];
  46. if (buffer) {
  47. buffer.dispose();
  48. this._buffers[BABYLON.VertexBuffer.NormalKind] = null;
  49. }
  50. this._source.getScene().getEngine()._releaseBuffer(this._ib);
  51. this._lineShader.dispose();
  52. };
  53. EdgesRenderer.prototype._processEdgeForAdjacencies = function (pa, pb, p0, p1, p2) {
  54. if (pa === p0 && pb === p1 || pa === p1 && pb === p0) {
  55. return 0;
  56. }
  57. if (pa === p1 && pb === p2 || pa === p2 && pb === p1) {
  58. return 1;
  59. }
  60. if (pa === p2 && pb === p0 || pa === p0 && pb === p2) {
  61. return 2;
  62. }
  63. return -1;
  64. };
  65. EdgesRenderer.prototype._processEdgeForAdjacenciesWithVertices = function (pa, pb, p0, p1, p2) {
  66. if (pa.equalsWithEpsilon(p0) && pb.equalsWithEpsilon(p1) || pa.equalsWithEpsilon(p1) && pb.equalsWithEpsilon(p0)) {
  67. return 0;
  68. }
  69. if (pa.equalsWithEpsilon(p1) && pb.equalsWithEpsilon(p2) || pa.equalsWithEpsilon(p2) && pb.equalsWithEpsilon(p1)) {
  70. return 1;
  71. }
  72. if (pa.equalsWithEpsilon(p2) && pb.equalsWithEpsilon(p0) || pa.equalsWithEpsilon(p0) && pb.equalsWithEpsilon(p2)) {
  73. return 2;
  74. }
  75. return -1;
  76. };
  77. EdgesRenderer.prototype._checkEdge = function (faceIndex, edge, faceNormals, p0, p1) {
  78. var needToCreateLine;
  79. if (edge === undefined) {
  80. needToCreateLine = true;
  81. }
  82. else {
  83. var dotProduct = BABYLON.Vector3.Dot(faceNormals[faceIndex], faceNormals[edge]);
  84. needToCreateLine = dotProduct < this._epsilon;
  85. }
  86. if (needToCreateLine) {
  87. var offset = this._linesPositions.length / 3;
  88. var normal = p0.subtract(p1);
  89. normal.normalize();
  90. // Positions
  91. this._linesPositions.push(p0.x);
  92. this._linesPositions.push(p0.y);
  93. this._linesPositions.push(p0.z);
  94. this._linesPositions.push(p0.x);
  95. this._linesPositions.push(p0.y);
  96. this._linesPositions.push(p0.z);
  97. this._linesPositions.push(p1.x);
  98. this._linesPositions.push(p1.y);
  99. this._linesPositions.push(p1.z);
  100. this._linesPositions.push(p1.x);
  101. this._linesPositions.push(p1.y);
  102. this._linesPositions.push(p1.z);
  103. // Normals
  104. this._linesNormals.push(p1.x);
  105. this._linesNormals.push(p1.y);
  106. this._linesNormals.push(p1.z);
  107. this._linesNormals.push(-1);
  108. this._linesNormals.push(p1.x);
  109. this._linesNormals.push(p1.y);
  110. this._linesNormals.push(p1.z);
  111. this._linesNormals.push(1);
  112. this._linesNormals.push(p0.x);
  113. this._linesNormals.push(p0.y);
  114. this._linesNormals.push(p0.z);
  115. this._linesNormals.push(-1);
  116. this._linesNormals.push(p0.x);
  117. this._linesNormals.push(p0.y);
  118. this._linesNormals.push(p0.z);
  119. this._linesNormals.push(1);
  120. // Indices
  121. this._linesIndices.push(offset);
  122. this._linesIndices.push(offset + 1);
  123. this._linesIndices.push(offset + 2);
  124. this._linesIndices.push(offset);
  125. this._linesIndices.push(offset + 2);
  126. this._linesIndices.push(offset + 3);
  127. }
  128. };
  129. EdgesRenderer.prototype._generateEdgesLines = function () {
  130. var positions = this._source.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  131. var indices = this._source.getIndices();
  132. // First let's find adjacencies
  133. var adjacencies = new Array();
  134. var faceNormals = new Array();
  135. var index;
  136. var faceAdjacencies;
  137. // Prepare faces
  138. for (index = 0; index < indices.length; index += 3) {
  139. faceAdjacencies = new FaceAdjacencies();
  140. var p0Index = indices[index];
  141. var p1Index = indices[index + 1];
  142. var p2Index = indices[index + 2];
  143. faceAdjacencies.p0 = new BABYLON.Vector3(positions[p0Index * 3], positions[p0Index * 3 + 1], positions[p0Index * 3 + 2]);
  144. faceAdjacencies.p1 = new BABYLON.Vector3(positions[p1Index * 3], positions[p1Index * 3 + 1], positions[p1Index * 3 + 2]);
  145. faceAdjacencies.p2 = new BABYLON.Vector3(positions[p2Index * 3], positions[p2Index * 3 + 1], positions[p2Index * 3 + 2]);
  146. var faceNormal = BABYLON.Vector3.Cross(faceAdjacencies.p1.subtract(faceAdjacencies.p0), faceAdjacencies.p2.subtract(faceAdjacencies.p1));
  147. faceNormal.normalize();
  148. faceNormals.push(faceNormal);
  149. adjacencies.push(faceAdjacencies);
  150. }
  151. // Scan
  152. for (index = 0; index < adjacencies.length; index++) {
  153. faceAdjacencies = adjacencies[index];
  154. for (var otherIndex = index + 1; otherIndex < adjacencies.length; otherIndex++) {
  155. var otherFaceAdjacencies = adjacencies[otherIndex];
  156. if (faceAdjacencies.edgesConnectedCount === 3) {
  157. break;
  158. }
  159. if (otherFaceAdjacencies.edgesConnectedCount === 3) {
  160. continue;
  161. }
  162. var otherP0 = indices[otherIndex * 3];
  163. var otherP1 = indices[otherIndex * 3 + 1];
  164. var otherP2 = indices[otherIndex * 3 + 2];
  165. for (var edgeIndex = 0; edgeIndex < 3; edgeIndex++) {
  166. var otherEdgeIndex;
  167. if (faceAdjacencies.edges[edgeIndex] !== undefined) {
  168. continue;
  169. }
  170. switch (edgeIndex) {
  171. case 0:
  172. if (this._checkVerticesInsteadOfIndices) {
  173. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p0, faceAdjacencies.p1, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  174. }
  175. else {
  176. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3], indices[index * 3 + 1], otherP0, otherP1, otherP2);
  177. }
  178. break;
  179. case 1:
  180. if (this._checkVerticesInsteadOfIndices) {
  181. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p1, faceAdjacencies.p2, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  182. }
  183. else {
  184. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 1], indices[index * 3 + 2], otherP0, otherP1, otherP2);
  185. }
  186. break;
  187. case 2:
  188. if (this._checkVerticesInsteadOfIndices) {
  189. otherEdgeIndex = this._processEdgeForAdjacenciesWithVertices(faceAdjacencies.p2, faceAdjacencies.p0, otherFaceAdjacencies.p0, otherFaceAdjacencies.p1, otherFaceAdjacencies.p2);
  190. }
  191. else {
  192. otherEdgeIndex = this._processEdgeForAdjacencies(indices[index * 3 + 2], indices[index * 3], otherP0, otherP1, otherP2);
  193. }
  194. break;
  195. }
  196. if (otherEdgeIndex === -1) {
  197. continue;
  198. }
  199. faceAdjacencies.edges[edgeIndex] = otherIndex;
  200. otherFaceAdjacencies.edges[otherEdgeIndex] = index;
  201. faceAdjacencies.edgesConnectedCount++;
  202. otherFaceAdjacencies.edgesConnectedCount++;
  203. if (faceAdjacencies.edgesConnectedCount === 3) {
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. // Create lines
  210. for (index = 0; index < adjacencies.length; index++) {
  211. // 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
  212. var current = adjacencies[index];
  213. this._checkEdge(index, current.edges[0], faceNormals, current.p0, current.p1);
  214. this._checkEdge(index, current.edges[1], faceNormals, current.p1, current.p2);
  215. this._checkEdge(index, current.edges[2], faceNormals, current.p2, current.p0);
  216. }
  217. // Merge into a single mesh
  218. var engine = this._source.getScene().getEngine();
  219. this._buffers[BABYLON.VertexBuffer.PositionKind] = new BABYLON.VertexBuffer(engine, this._linesPositions, BABYLON.VertexBuffer.PositionKind, false);
  220. this._buffers[BABYLON.VertexBuffer.NormalKind] = new BABYLON.VertexBuffer(engine, this._linesNormals, BABYLON.VertexBuffer.NormalKind, false, false, 4);
  221. this._ib = engine.createIndexBuffer(this._linesIndices);
  222. this._indicesCount = this._linesIndices.length;
  223. };
  224. EdgesRenderer.prototype.render = function () {
  225. if (!this._lineShader.isReady()) {
  226. return;
  227. }
  228. var scene = this._source.getScene();
  229. var engine = scene.getEngine();
  230. this._lineShader._preBind();
  231. // VBOs
  232. engine.bindBuffers(this._buffers, this._ib, this._lineShader.getEffect());
  233. scene.resetCachedMaterial();
  234. this._lineShader.setColor4("color", this._source.edgesColor);
  235. if (scene.activeCamera.mode === BABYLON.Camera.ORTHOGRAPHIC_CAMERA) {
  236. this._lineShader.setFloat("width", this._source.edgesWidth / this.edgesWidthScalerForOrthographic);
  237. }
  238. else {
  239. this._lineShader.setFloat("width", this._source.edgesWidth / this.edgesWidthScalerForPerspective);
  240. }
  241. this._lineShader.setFloat("aspectRatio", engine.getAspectRatio(scene.activeCamera));
  242. this._lineShader.bind(this._source.getWorldMatrix());
  243. // Draw order
  244. engine.draw(true, 0, this._indicesCount);
  245. this._lineShader.unbind();
  246. engine.setDepthWrite(true);
  247. };
  248. return EdgesRenderer;
  249. })();
  250. BABYLON.EdgesRenderer = EdgesRenderer;
  251. })(BABYLON || (BABYLON = {}));