ShaderTab.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var INSPECTOR;
  7. (function (INSPECTOR) {
  8. var ShaderTab = (function (_super) {
  9. __extends(ShaderTab, _super);
  10. function ShaderTab(tabbar, insp) {
  11. _super.call(this, tabbar, 'Shader');
  12. this._inspector = insp;
  13. // Build the shaders panel : a div that will contains the shaders tree and both shaders panels
  14. this._panel = INSPECTOR.Helpers.CreateDiv('tab-panel');
  15. var shaderPanel = INSPECTOR.Helpers.CreateDiv('shader-tree-panel');
  16. this._vertexPanel = INSPECTOR.Helpers.CreateDiv('shader-panel');
  17. this._fragmentPanel = INSPECTOR.Helpers.CreateDiv('shader-panel');
  18. this._panel.appendChild(shaderPanel);
  19. this._panel.appendChild(this._vertexPanel);
  20. this._panel.appendChild(this._fragmentPanel);
  21. INSPECTOR.Helpers.LoadScript();
  22. Split([this._vertexPanel, this._fragmentPanel], {
  23. sizes: [50, 50],
  24. direction: 'vertical' });
  25. var comboBox = INSPECTOR.Helpers.CreateElement('select', '', shaderPanel);
  26. comboBox.addEventListener('change', this._selectShader.bind(this));
  27. var option = INSPECTOR.Helpers.CreateElement('option', '', comboBox);
  28. option.textContent = 'Select a shader';
  29. option.setAttribute('value', "");
  30. option.setAttribute('disabled', 'true');
  31. option.setAttribute('selected', 'true');
  32. // Build shaders combobox
  33. for (var _i = 0, _a = this._inspector.scene.materials; _i < _a.length; _i++) {
  34. var mat = _a[_i];
  35. if (mat instanceof BABYLON.ShaderMaterial) {
  36. var option_1 = INSPECTOR.Helpers.CreateElement('option', '', comboBox);
  37. option_1.setAttribute('value', mat.id);
  38. option_1.textContent = mat.name + " - " + mat.id;
  39. }
  40. }
  41. }
  42. ShaderTab.prototype._selectShader = function (event) {
  43. var id = event.target.value;
  44. var mat = this._inspector.scene.getMaterialByID(id);
  45. // Clean shader panel
  46. INSPECTOR.Helpers.CleanDiv(this._vertexPanel);
  47. // add the title - vertex shader
  48. var title = INSPECTOR.Helpers.CreateDiv('shader-panel-title', this._vertexPanel);
  49. title.textContent = 'Vertex shader';
  50. // add code
  51. var code = INSPECTOR.Helpers.CreateElement('code', 'glsl', INSPECTOR.Helpers.CreateElement('pre', '', this._vertexPanel));
  52. code.textContent = this._beautify(mat.getEffect().getVertexShaderSource());
  53. INSPECTOR.Helpers.CleanDiv(this._fragmentPanel);
  54. // add the title - fragment shader
  55. title = INSPECTOR.Helpers.CreateDiv('shader-panel-title', this._fragmentPanel);
  56. title.textContent = 'Frgament shader';
  57. // add code
  58. code = INSPECTOR.Helpers.CreateElement('code', 'glsl', INSPECTOR.Helpers.CreateElement('pre', '', this._fragmentPanel));
  59. code.textContent = this._beautify(mat.getEffect().getFragmentShaderSource());
  60. // Init the syntax highlighting
  61. var styleInit = INSPECTOR.Helpers.CreateElement('script', '', INSPECTOR.Inspector.DOCUMENT.body);
  62. styleInit.textContent = 'hljs.initHighlighting();';
  63. };
  64. /** Overrides super.dispose */
  65. ShaderTab.prototype.dispose = function () {
  66. };
  67. /** Returns the position of the first { and the corresponding } */
  68. ShaderTab.prototype._getBracket = function (str) {
  69. var fb = str.indexOf('{');
  70. var arr = str.substr(fb + 1).split('');
  71. var counter = 1;
  72. var currentPosInString = fb;
  73. var lastBracketIndex = 0;
  74. for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
  75. var char = arr_1[_i];
  76. currentPosInString++;
  77. if (char === '{') {
  78. counter++;
  79. }
  80. if (char === '}') {
  81. counter--;
  82. }
  83. if (counter == 0) {
  84. lastBracketIndex = currentPosInString;
  85. break;
  86. }
  87. }
  88. return { firstBracket: fb, lastBracket: lastBracketIndex };
  89. };
  90. /**
  91. * Beautify the given string : correct indentation
  92. */
  93. ShaderTab.prototype._beautify = function (glsl, level) {
  94. if (level === void 0) { level = 0; }
  95. // return condition : no brackets at all
  96. var brackets = this._getBracket(glsl);
  97. var firstBracket = brackets.firstBracket;
  98. var lastBracket = brackets.lastBracket;
  99. var spaces = "";
  100. for (var i = 0; i < level; i++) {
  101. spaces += " "; // 4 spaces
  102. }
  103. // If no brackets, return the indented string
  104. if (firstBracket == -1) {
  105. glsl = spaces + glsl; // indent first line
  106. glsl = glsl
  107. .replace(/;./g, function (x) { return '\n' + x.substr(1); }); // new line after ; except the last one
  108. glsl = glsl.replace(/=/g, " = "); // space around =
  109. glsl = glsl.replace(/\n/g, "\n" + spaces); // indentation
  110. return glsl;
  111. }
  112. else {
  113. // if brackets, beautify the inside
  114. // let insideWithBrackets = glsl.substr(firstBracket, lastBracket-firstBracket+1);
  115. var left = glsl.substr(0, firstBracket);
  116. var right = glsl.substr(lastBracket + 1, glsl.length);
  117. var inside = glsl.substr(firstBracket + 1, lastBracket - firstBracket - 1);
  118. inside = this._beautify(inside, level + 1);
  119. return this._beautify(left, level) + '{\n' + inside + '\n' + spaces + '}\n' + this._beautify(right, level);
  120. }
  121. // // Replace bracket with @1 and @2 with correct indentation
  122. // let newInside = "@1\n\t" + inside + "\n@2";
  123. // newInside = newInside.replace(/;\n/g, ";\n\t");
  124. // glsl = glsl.replace(insideWithBrackets, newInside);
  125. // firstBracket = glsl.indexOf('{');
  126. // lastBracket = glsl.lastIndexOf('}');
  127. // }
  128. // console.log(glsl);
  129. // let regex = /(\{(?:\{??[^\{]*?}))+/gmi;
  130. // let tmp = glsl;
  131. // let m;
  132. // while ((m = regex.exec(tmp)) !== null) {
  133. // // This is necessary to avoid infinite loops with zero-width matches
  134. // if (m.index === regex.lastIndex) {
  135. // regex.lastIndex++;
  136. // }
  137. // // The result can be accessed through the `m`-variable.
  138. // m.forEach((match, groupIndex) => {
  139. // // Remove the first and the last bracket only
  140. // let matchWithoutBrackets = match.replace(/{/, "").replace(/}/, "");
  141. // // Indent the content inside brackets with tabs
  142. // glsl = glsl.replace(match, `{\n\t${matchWithoutBrackets}\n}\n`);
  143. // // removes the match from tmp
  144. // tmp = tmp.replace(match, "");
  145. // // and continue
  146. // });
  147. // }
  148. // return
  149. };
  150. return ShaderTab;
  151. }(INSPECTOR.Tab));
  152. INSPECTOR.ShaderTab = ShaderTab;
  153. })(INSPECTOR || (INSPECTOR = {}));
  154. //# sourceMappingURL=ShaderTab.js.map