MSDFView.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package hide.view;
  2. private class MSDFViewerShader extends h3d.shader.AlphaMSDF {
  3. static var SRC = {
  4. @param var comparisonFactor : Float;
  5. function fragment() {
  6. if (calculatedUV.x > comparisonFactor)
  7. pixelColor = texture.get(calculatedUV);
  8. else {
  9. pixelColor.rgb = vec3(1.0);
  10. var sample = texture.get(calculatedUV);
  11. var sd = median(sample.r, sample.g, sample.b);
  12. var screenPxDistance = screenPxRange(calculatedUV)*(sd - 0.5);
  13. pixelColor.a = clamp(screenPxDistance + 0.5, 0.0, 1.0);
  14. }
  15. }
  16. }
  17. }
  18. enum MSDFViewMode {
  19. MSDF;
  20. Raw;
  21. Comparison;
  22. }
  23. class MSDFView extends FileView {
  24. var bmp : h2d.Bitmap;
  25. var sliderBmp : h2d.Graphics;
  26. var shader : MSDFViewerShader;
  27. var scene : hide.comp.Scene;
  28. var viewMode : MSDFViewMode = MSDF;
  29. var interactive : h2d.Interactive;
  30. var tools : hide.comp.Toolbar;
  31. var cam : Dynamic;
  32. var currentSize : Float = 0;
  33. override function onDisplay() {
  34. cleanUp();
  35. element.html('
  36. <div class="flex vertical">
  37. <div class="toolbar"></div>
  38. <div class="scene-partition" style="display: flex; flex-direction: row; flex: 1; overflow: hidden;">
  39. <div class="heaps-scene"></div>
  40. <div class="image-properties">
  41. <div class="title">MSDF infos</div>
  42. <div class="msdf-infos">
  43. <p class="tex-weight">Texture weight : missing info </p>
  44. <p class="current-size">Current size : 0 px </p>
  45. <p>Resize :
  46. <input type="number" class="size" value=0 style="width:7ch;"></input>
  47. <input type="button" class="save-size" value="Save" title="Save current size options into a props.json file."/>
  48. </p>
  49. </div>
  50. </div>
  51. </div>
  52. <div class="identifiers">
  53. <label>MSDF</label>
  54. <label>Raw</label>
  55. </div>
  56. </div>
  57. ');
  58. scene = new hide.comp.Scene(config, null, element.find(".heaps-scene"));
  59. var msdfInfos = element.find(".msdf-infos");
  60. var resize = msdfInfos.find(".size");
  61. var currentSizeField = msdfInfos.find(".current-size");
  62. var fs:hxd.fs.LocalFileSystem = Std.downcast(hxd.res.Loader.currentInstance.fs, hxd.fs.LocalFileSystem);
  63. var dirPos = state.path.lastIndexOf("/");
  64. var dirPath = dirPos < 0 ? state.path : state.path.substr(0, dirPos + 1);
  65. var name = dirPos < 0 ? state.path : state.path.substr(dirPos + 1);
  66. var propsFilePath = ide.getPath(dirPath + "props.json");
  67. var saveSize = element.find(".save-size");
  68. saveSize.on("click", function(_) {
  69. if (resize.val() == currentSize)
  70. return;
  71. var bytes = new haxe.io.BytesOutput();
  72. var convertRule = { convert : "png", priority: 10000000 };
  73. Reflect.setField(convertRule, "size", resize.val());
  74. if (sys.FileSystem.exists(propsFilePath)) {
  75. var propsJson = haxe.Json.parse(sys.io.File.getContent(propsFilePath));
  76. if (Reflect.hasField(propsJson, "fs.convert")) {
  77. var fsConvertObj = Reflect.getProperty(propsJson, "fs.convert");
  78. Reflect.setField(fsConvertObj, state.path, convertRule);
  79. }
  80. else {
  81. var fsConvertObj = {} ;
  82. Reflect.setField(fsConvertObj, state.path, convertRule);
  83. Reflect.setProperty(propsJson, "fs.convert", fsConvertObj);
  84. }
  85. var data = haxe.Json.stringify(propsJson, "\t");
  86. bytes.writeString(data);
  87. hxd.File.saveBytes(propsFilePath, bytes.getBytes());
  88. } else {
  89. var fsConvertObj = { };
  90. var pathObj = { };
  91. Reflect.setProperty(pathObj, state.path, convertRule);
  92. Reflect.setProperty(fsConvertObj, "fs.convert", pathObj);
  93. var data = haxe.Json.stringify(fsConvertObj, "\t");
  94. bytes.writeString(data);
  95. hxd.File.saveBytes(propsFilePath, bytes.getBytes());
  96. }
  97. @:privateAccess fs.convert.configs.clear();
  98. @:privateAccess fs.convert.loadConfig(state.path);
  99. var localEntry = @:privateAccess new hxd.fs.LocalFileSystem.LocalEntry(fs, name, state.path, Ide.inst.getPath(state.path));
  100. fs.convert.run(localEntry);
  101. currentSize = resize.val();
  102. currentSizeField.text('Current size : ${currentSize} px');
  103. replaceTexture(@:privateAccess localEntry.file);
  104. });
  105. this.saveDisplayKey = state.path;
  106. this.viewMode = getDisplayState("ViewMode");
  107. if (this.viewMode == null)
  108. this.viewMode = MSDF;
  109. var identifiers = element.find(".identifiers");
  110. identifiers.css(this.viewMode.match(MSDFViewMode.Comparison) ? {"visibility":"inherit"} : {"visibility":"hidden"});
  111. shader = new MSDFViewerShader();
  112. tools = new hide.comp.Toolbar(null,element.find(".toolbar"));
  113. tools.addSeparator();
  114. var tgMSDF = tools.addToggle("show-msdf", "file-zip-o", "Show MSDF", "", function (e) {
  115. tools.element.find(".show-raw").removeAttr("checked");
  116. tools.element.find(".show-comparison").removeAttr("checked");
  117. if (bmp != null) {
  118. this.saveDisplayState("ViewMode", MSDF);
  119. this.viewMode = MSDF;
  120. var identifiers = element.find(".identifiers");
  121. identifiers.css(this.viewMode.match(MSDFViewMode.Comparison) ? {"visibility":"inherit"} : {"visibility":"hidden"});
  122. applyShaderConfiguration();
  123. }
  124. }, this.viewMode.match(MSDF), null, false);
  125. tgMSDF.element.addClass("show-msdf");
  126. var tgRaw = tools.addToggle("show-raw","file-image-o", "Show Raw", "", function (e) {
  127. tools.element.find(".show-msdf").removeAttr("checked");
  128. tools.element.find(".show-comparison").removeAttr("checked");
  129. if (bmp != null) {
  130. this.saveDisplayState("ViewMode", Raw);
  131. this.viewMode = Raw;
  132. var identifiers = element.find(".identifiers");
  133. identifiers.css(this.viewMode.match(MSDFViewMode.Comparison) ? {"visibility":"inherit"} : {"visibility":"hidden"});
  134. applyShaderConfiguration();
  135. }
  136. }, this.viewMode.match(Raw), null, false);
  137. tgRaw.element.addClass("show-raw");
  138. var tgComparison = tools.addToggle("show-comparison","arrows-h", "Show comparison between MSDF and Raw", "", function (e) {
  139. tools.element.find(".show-raw").removeAttr("checked");
  140. tools.element.find(".show-msdf").removeAttr("checked");
  141. if (bmp != null) {
  142. this.saveDisplayState("ViewMode", Comparison);
  143. this.viewMode = Comparison;
  144. var identifiers = element.find(".identifiers");
  145. identifiers.css(this.viewMode.match(MSDFViewMode.Comparison) ? {"visibility":"inherit"} : {"visibility":"hidden"});
  146. applyShaderConfiguration();
  147. }
  148. }, this.viewMode.match(Comparison), null, false);
  149. tgComparison.element.addClass("show-comparison");
  150. tools.addSeparator();
  151. // We don't want to load old texture from cache because convert rule might
  152. // have been changed
  153. @:privateAccess fs.fileCache.remove(state.path);
  154. scene.onReady = function() {
  155. scene.loadTexture(state.path, state.path, function(texture) {
  156. onTextureLoaded(texture);
  157. }, false);
  158. };
  159. }
  160. override function onActivate() {
  161. if (tools != null)
  162. tools.refreshToggles();
  163. }
  164. override function onRebuild() {
  165. if ( scene != null ) {
  166. scene.dispose();
  167. scene = null;
  168. }
  169. super.onRebuild();
  170. }
  171. override function onResize() {
  172. if( bmp == null ) return;
  173. var scale = Math.min(1,Math.min((contentWidth - 20) / bmp.tile.width, (contentHeight - 20) / bmp.tile.height));
  174. var cam2d = Std.downcast(cam, hide.view.l3d.CameraController2D);
  175. if (cam2d != null) {
  176. @:privateAccess cam2d.curPos.set(bmp.tile.width / 2, bmp.tile.width / 2, (1 / bmp.tile.width) * 500);
  177. }
  178. else {
  179. bmp.setScale(scale * js.Browser.window.devicePixelRatio);
  180. bmp.x = -Std.int(bmp.tile.width * bmp.scaleX) >> 1;
  181. bmp.y = -Std.int(bmp.tile.height * bmp.scaleY) >> 1;
  182. }
  183. updateSliderVisual();
  184. }
  185. public function onTextureLoaded(texture: Null<h3d.mat.Texture>) {
  186. scene.element.on("wheel", function(_) {
  187. updateSliderVisual();
  188. });
  189. bmp = new h2d.Bitmap(h2d.Tile.fromTexture(texture), scene.s2d);
  190. bmp.smooth = true;
  191. bmp.addShader(shader);
  192. shader.texture = texture;
  193. shader.blur = 1.0;
  194. shader.comparisonFactor = 0.5;
  195. this.cam = new hide.view.l3d.CameraController2D(scene.s2d);
  196. var texMemSize = element.find(".tex-weight");
  197. texMemSize.text('Texture weight : ${@:privateAccess floatToStringPrecision(texture.mem.memSize(texture) / (1024 * 1024)) } mb');
  198. currentSize = texture.width;
  199. var resize = element.find(".resize");
  200. resize.val(currentSize);
  201. var currentSizeField = element.find(".current-size");
  202. currentSizeField.text('Current size : ${currentSize} px');
  203. applyShaderConfiguration();
  204. onResize();
  205. }
  206. public function applyShaderConfiguration() {
  207. switch (this.viewMode) {
  208. case MSDF:
  209. {
  210. shader.comparisonFactor = 1;
  211. if (interactive != null)
  212. interactive.remove();
  213. if (sliderBmp != null)
  214. sliderBmp.alpha = 0;
  215. }
  216. case Raw:
  217. {
  218. shader.comparisonFactor = 0;
  219. if (interactive != null)
  220. interactive.remove();
  221. if (sliderBmp != null)
  222. sliderBmp.alpha = 0;
  223. }
  224. case Comparison:
  225. {
  226. if (sliderBmp == null)
  227. drawSlider();
  228. else
  229. sliderBmp.alpha = 1;
  230. bmp.addChild(sliderBmp);
  231. var bounds = new h2d.col.Bounds();
  232. sliderBmp.getSize(bounds);
  233. if (interactive != null)
  234. interactive.remove();
  235. interactive = new h2d.Interactive(bmp.tile.width,bmp.tile.height,bmp);
  236. interactive.propagateEvents = true;
  237. interactive.x = bmp.tile.dx;
  238. interactive.y = bmp.tile.dy;
  239. sliderBmp.x = bmp.tile.width / 2.0 - bounds.width / 2.0;
  240. shader.comparisonFactor = 0.5;
  241. var clicked = false;
  242. function updateSlider(e: hxd.Event) {
  243. if (!clicked)
  244. return;
  245. sliderBmp.x = e.relX - (bounds.width * sliderBmp.scaleX) / 2.0;
  246. shader.comparisonFactor = e.relX / interactive.width;
  247. }
  248. interactive.onPush = function (e) {
  249. clicked = true;
  250. updateSlider(e);
  251. }
  252. interactive.onRelease = function (e) {
  253. clicked = false;
  254. }
  255. interactive.onMove = function (e) {
  256. updateSlider(e);
  257. };
  258. }
  259. }
  260. }
  261. public function cleanUp() {
  262. if (scene != null)
  263. scene.dispose();
  264. sliderBmp = null;
  265. if (bmp != null && !bmp.tile.isDisposed())
  266. bmp.tile.dispose();
  267. bmp = null;
  268. interactive = null;
  269. shader = null;
  270. }
  271. public function floatToStringPrecision(number:Float, ?precision=2) {
  272. number *= Math.pow(10, precision);
  273. return Math.round(number) / Math.pow(10, precision);
  274. }
  275. public function updateSliderVisual() {
  276. var cam2d = Std.downcast(cam, hide.view.l3d.CameraController2D);
  277. if (cam2d != null && sliderBmp != null) {
  278. var oldWidth = sliderBmp.getSize().width;
  279. @:privateAccess sliderBmp.scaleX = (1 / (cam2d.curPos.z)) * 2;
  280. var offset = sliderBmp.getSize().width - oldWidth;
  281. sliderBmp.x -= offset / 4;
  282. }
  283. // todo : handle slider zoom for cam 3d
  284. }
  285. public function drawSlider() {
  286. if (sliderBmp == null)
  287. sliderBmp = new h2d.Graphics(scene.s2d);
  288. sliderBmp.clear();
  289. sliderBmp.beginFill(0xFFFFFF, 1);
  290. sliderBmp.drawRect(0,0,2, shader.texture.height);
  291. sliderBmp.endFill();
  292. updateSliderVisual();
  293. }
  294. public function replaceTexture(path : String) {
  295. var bytes = sys.io.File.getBytes(path);
  296. var res = hxd.res.Any.fromBytes(path, bytes);
  297. var texture = res.toTexture();
  298. if (bmp != null) {
  299. if (!bmp.tile.isDisposed())
  300. bmp.tile.dispose();
  301. bmp.remove();
  302. bmp = null;
  303. }
  304. tools.element.find(".hide-range").remove();
  305. bmp = new h2d.Bitmap(h2d.Tile.fromTexture(texture), scene.s2d);
  306. bmp.smooth = true;
  307. bmp.addShader(shader);
  308. shader.texture = texture;
  309. shader.blur = 1.0;
  310. shader.comparisonFactor = 0.5;
  311. this.cam = new hide.view.l3d.CameraController2D(scene.s2d);
  312. var texMemSize = element.find(".tex-weight");
  313. texMemSize.text('Texture weight : ${@:privateAccess floatToStringPrecision(texture.mem.memSize(texture) / (1024 * 1024)) } mb');
  314. currentSize = texture.width;
  315. var resize = element.find(".resize");
  316. resize.val(currentSize);
  317. var currentSizeField = element.find(".current-size");
  318. currentSizeField.text('Current size : ${currentSize} px');
  319. applyShaderConfiguration();
  320. onResize();
  321. }
  322. static var _ = Extension.registerExtension(MSDFView,["svg"],{ icon : "picture-o", name: "MSDF" });
  323. }