LayerSlot.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package arm.data;
  2. import kha.graphics4.TextureFormat;
  3. import kha.Image;
  4. import iron.RenderPath;
  5. import arm.ui.UITrait;
  6. import arm.node.MaterialParser;
  7. import arm.Tool;
  8. class LayerSlot {
  9. public var id = 0;
  10. public var visible = true;
  11. public var ext = "";
  12. public var name: String;
  13. public var texpaint: Image;
  14. public var texpaint_nor: Image;
  15. public var texpaint_pack: Image;
  16. public var texpaint_preview: Image; // Layer preview
  17. public var texpaint_mask: Image = null; // Texture mask
  18. public var texpaint_mask_preview: Image;
  19. public var maskOpacity = 1.0; // Opacity mask
  20. public var material_mask: MaterialSlot = null; // Fill layer
  21. public var blending = BlendMix;
  22. public var objectMask = 0;
  23. public var uvScale = 1.0;
  24. public var uvRot = 0.0;
  25. public var uvType = UVMap;
  26. public var paintBase = true;
  27. public var paintOpac = true;
  28. public var paintOcc = true;
  29. public var paintRough = true;
  30. public var paintMet = true;
  31. public var paintNor = true;
  32. public var paintHeight = true;
  33. public var paintEmis = true;
  34. public var paintSubs = true;
  35. var createMaskColor: Int;
  36. var createMaskImage: Image;
  37. public function new(ext = "") {
  38. if (ext == "") {
  39. id = 0;
  40. for (l in Project.layers) if (l.id >= id) id = l.id + 1;
  41. ext = id + "";
  42. }
  43. this.ext = ext;
  44. name = "Layer " + (id + 1);
  45. var format = App.bitsHandle.position == Bits8 ? "RGBA32" :
  46. App.bitsHandle.position == Bits16 ? "RGBA64" :
  47. "RGBA128";
  48. {
  49. var t = new RenderTargetRaw();
  50. t.name = "texpaint" + ext;
  51. t.width = Config.getTextureRes();
  52. t.height = Config.getTextureRes();
  53. t.format = format;
  54. texpaint = RenderPath.active.createRenderTarget(t).image;
  55. }
  56. {
  57. var t = new RenderTargetRaw();
  58. t.name = "texpaint_nor" + ext;
  59. t.width = Config.getTextureRes();
  60. t.height = Config.getTextureRes();
  61. t.format = format;
  62. texpaint_nor = RenderPath.active.createRenderTarget(t).image;
  63. }
  64. {
  65. var t = new RenderTargetRaw();
  66. t.name = "texpaint_pack" + ext;
  67. t.width = Config.getTextureRes();
  68. t.height = Config.getTextureRes();
  69. t.format = format;
  70. texpaint_pack = RenderPath.active.createRenderTarget(t).image;
  71. }
  72. texpaint_preview = Image.createRenderTarget(200, 200, TextureFormat.RGBA32);
  73. }
  74. public function delete() {
  75. unload();
  76. var lpos = Project.layers.indexOf(this);
  77. Project.layers.remove(this);
  78. // Undo can remove base layer and then restore it from undo layers
  79. if (lpos > 0) Context.setLayer(Project.layers[lpos - 1]);
  80. }
  81. public function unload() {
  82. texpaint.unload();
  83. texpaint_nor.unload();
  84. texpaint_pack.unload();
  85. RenderPath.active.renderTargets.remove("texpaint" + ext);
  86. RenderPath.active.renderTargets.remove("texpaint_nor" + ext);
  87. RenderPath.active.renderTargets.remove("texpaint_pack" + ext);
  88. texpaint_preview.unload();
  89. deleteMask();
  90. }
  91. public function swap(other: LayerSlot) {
  92. RenderPath.active.renderTargets.get("texpaint" + ext).image = other.texpaint;
  93. RenderPath.active.renderTargets.get("texpaint_nor" + ext).image = other.texpaint_nor;
  94. RenderPath.active.renderTargets.get("texpaint_pack" + ext).image = other.texpaint_pack;
  95. RenderPath.active.renderTargets.get("texpaint" + other.ext).image = texpaint;
  96. RenderPath.active.renderTargets.get("texpaint_nor" + other.ext).image = texpaint_nor;
  97. RenderPath.active.renderTargets.get("texpaint_pack" + other.ext).image = texpaint_pack;
  98. var _texpaint = texpaint;
  99. var _texpaint_nor = texpaint_nor;
  100. var _texpaint_pack = texpaint_pack;
  101. texpaint = other.texpaint;
  102. texpaint_nor = other.texpaint_nor;
  103. texpaint_pack = other.texpaint_pack;
  104. other.texpaint = _texpaint;
  105. other.texpaint_nor = _texpaint_nor;
  106. other.texpaint_pack = _texpaint_pack;
  107. }
  108. public function swapMask(other: LayerSlot) {
  109. RenderPath.active.renderTargets.get("texpaint_mask" + ext).image = other.texpaint_mask;
  110. RenderPath.active.renderTargets.get("texpaint_mask" + other.ext).image = texpaint_mask;
  111. var _texpaint_mask = texpaint_mask;
  112. texpaint_mask = other.texpaint_mask;
  113. other.texpaint_mask = _texpaint_mask;
  114. }
  115. public function createMask(color: Int, clear = true, image: Image = null) {
  116. if (texpaint_mask != null) return;
  117. {
  118. var t = new RenderTargetRaw();
  119. t.name = "texpaint_mask" + ext;
  120. t.width = Config.getTextureRes();
  121. t.height = Config.getTextureRes();
  122. t.format = "R8";
  123. texpaint_mask = RenderPath.active.createRenderTarget(t).image;
  124. }
  125. texpaint_mask_preview = Image.createRenderTarget(200, 200, TextureFormat.L8);
  126. if (clear) {
  127. createMaskColor = color;
  128. createMaskImage = image;
  129. iron.App.notifyOnRender(clearMask);
  130. }
  131. }
  132. function clearMask(g: kha.graphics4.Graphics) {
  133. g.end();
  134. texpaint_mask.g2.begin();
  135. if (createMaskImage != null) texpaint_mask.g2.drawScaledImage(createMaskImage, 0, 0, texpaint_mask.width, texpaint_mask.height);
  136. else texpaint_mask.g2.clear(createMaskColor);
  137. texpaint_mask.g2.end();
  138. g.begin();
  139. iron.App.removeRender(clearMask);
  140. Context.layerPreviewDirty = true;
  141. createMaskColor = 0;
  142. createMaskImage = null;
  143. }
  144. public function deleteMask() {
  145. if (texpaint_mask == null) return;
  146. texpaint_mask.unload();
  147. RenderPath.active.renderTargets.remove("texpaint_mask" + ext);
  148. texpaint_mask = null;
  149. texpaint_mask_preview.unload();
  150. }
  151. public function applyMask() {
  152. if (texpaint_mask == null) return;
  153. if (Layers.pipeMerge == null) Layers.makePipe();
  154. Layers.makeTempImg();
  155. // Copy layer to temp
  156. Layers.imga.g2.begin(false);
  157. Layers.imga.g2.pipeline = Layers.pipeCopy;
  158. Layers.imga.g2.drawImage(texpaint, 0, 0);
  159. Layers.imga.g2.end();
  160. // Merge mask
  161. if (iron.data.ConstData.screenAlignedVB == null) iron.data.ConstData.createScreenAlignedData();
  162. texpaint.g4.begin();
  163. texpaint.g4.setPipeline(Layers.pipeMask);
  164. texpaint.g4.setTexture(Layers.tex0Mask, Layers.imga);
  165. texpaint.g4.setTexture(Layers.texaMask, texpaint_mask);
  166. texpaint.g4.setVertexBuffer(iron.data.ConstData.screenAlignedVB);
  167. texpaint.g4.setIndexBuffer(iron.data.ConstData.screenAlignedIB);
  168. texpaint.g4.drawIndexedVertices();
  169. texpaint.g4.end();
  170. deleteMask();
  171. }
  172. public function duplicate(): LayerSlot {
  173. var layers = Project.layers;
  174. var i = 0;
  175. while (i++ < layers.length) if (layers[i] == this) break;
  176. i++;
  177. var l = new LayerSlot();
  178. layers.insert(i, l);
  179. if (Layers.pipeMerge == null) Layers.makePipe();
  180. l.texpaint.g2.begin(false);
  181. l.texpaint.g2.pipeline = Layers.pipeCopy;
  182. l.texpaint.g2.drawImage(texpaint, 0, 0);
  183. l.texpaint.g2.end();
  184. l.texpaint_nor.g2.begin(false);
  185. l.texpaint_nor.g2.pipeline = Layers.pipeCopy;
  186. l.texpaint_nor.g2.drawImage(texpaint_nor, 0, 0);
  187. l.texpaint_nor.g2.end();
  188. l.texpaint_pack.g2.begin(false);
  189. l.texpaint_pack.g2.pipeline = Layers.pipeCopy;
  190. l.texpaint_pack.g2.drawImage(texpaint_pack, 0, 0);
  191. l.texpaint_pack.g2.end();
  192. l.texpaint_preview.g2.begin(true, 0xff000000);
  193. l.texpaint_preview.g2.drawScaledImage(texpaint_preview, 0, 0, texpaint_preview.width, texpaint_preview.height);
  194. l.texpaint_preview.g2.end();
  195. if (texpaint_mask != null) {
  196. l.createMask(0, false);
  197. l.texpaint_mask.g2.begin(false);
  198. l.texpaint_mask.g2.pipeline = Layers.pipeCopy;
  199. l.texpaint_mask.g2.drawImage(texpaint_mask, 0, 0);
  200. l.texpaint_mask.g2.end();
  201. l.texpaint_mask_preview.g2.begin(true, 0xff000000);
  202. l.texpaint_mask_preview.g2.drawScaledImage(texpaint_mask_preview, 0, 0, texpaint_mask_preview.width, texpaint_mask_preview.height);
  203. l.texpaint_mask_preview.g2.end();
  204. }
  205. l.visible = visible;
  206. l.maskOpacity = maskOpacity;
  207. l.material_mask = material_mask;
  208. l.objectMask = objectMask;
  209. l.blending = blending;
  210. l.paintBase = paintBase;
  211. l.paintOpac = paintOpac;
  212. l.paintOcc = paintOcc;
  213. l.paintRough = paintRough;
  214. l.paintMet = paintMet;
  215. l.paintNor = paintNor;
  216. l.paintHeight = paintHeight;
  217. l.paintEmis = paintEmis;
  218. l.paintSubs = paintSubs;
  219. return l;
  220. }
  221. public function resizeAndSetBits() {
  222. var format = App.bitsHandle.position == Bits8 ? TextureFormat.RGBA32 :
  223. App.bitsHandle.position == Bits16 ? TextureFormat.RGBA64 :
  224. TextureFormat.RGBA128;
  225. var res = Config.getTextureRes();
  226. var rts = RenderPath.active.renderTargets;
  227. var texpaint = this.texpaint;
  228. var texpaint_nor = this.texpaint_nor;
  229. var texpaint_pack = this.texpaint_pack;
  230. this.texpaint = Image.createRenderTarget(res, res, format);
  231. this.texpaint_nor = Image.createRenderTarget(res, res, format);
  232. this.texpaint_pack = Image.createRenderTarget(res, res, format);
  233. this.texpaint.g2.begin(false);
  234. this.texpaint.g2.drawScaledImage(texpaint, 0, 0, res, res);
  235. this.texpaint.g2.end();
  236. this.texpaint_nor.g2.begin(false);
  237. this.texpaint_nor.g2.drawScaledImage(texpaint_nor, 0, 0, res, res);
  238. this.texpaint_nor.g2.end();
  239. this.texpaint_pack.g2.begin(false);
  240. this.texpaint_pack.g2.drawScaledImage(texpaint_pack, 0, 0, res, res);
  241. this.texpaint_pack.g2.end();
  242. iron.App.notifyOnInit(function() { // Out of command list execution
  243. texpaint.unload();
  244. texpaint_nor.unload();
  245. texpaint_pack.unload();
  246. });
  247. rts.get("texpaint" + this.ext).image = this.texpaint;
  248. rts.get("texpaint_nor" + this.ext).image = this.texpaint_nor;
  249. rts.get("texpaint_pack" + this.ext).image = this.texpaint_pack;
  250. if (this.texpaint_mask != null && this.texpaint_mask.width != res) {
  251. var texpaint_mask = this.texpaint_mask;
  252. this.texpaint_mask = Image.createRenderTarget(res, res, TextureFormat.L8);
  253. this.texpaint_mask.g2.begin(false);
  254. this.texpaint_mask.g2.drawScaledImage(texpaint_mask, 0, 0, res, res);
  255. this.texpaint_mask.g2.end();
  256. iron.App.notifyOnInit(function() { // Out of command list execution
  257. texpaint_mask.unload();
  258. });
  259. rts.get("texpaint_mask" + this.ext).image = this.texpaint_mask;
  260. }
  261. }
  262. public function clear(g: kha.graphics4.Graphics) {
  263. g.end();
  264. texpaint.g4.begin();
  265. texpaint.g4.clear(kha.Color.fromFloats(0.0, 0.0, 0.0, 0.0)); // Base
  266. texpaint.g4.end();
  267. texpaint_nor.g4.begin();
  268. texpaint_nor.g4.clear(kha.Color.fromFloats(0.5, 0.5, 1.0, 0.0)); // Nor
  269. texpaint_nor.g4.end();
  270. texpaint_pack.g4.begin();
  271. texpaint_pack.g4.clear(kha.Color.fromFloats(1.0, 0.0, 0.0, 0.0)); // Occ, rough, met
  272. texpaint_pack.g4.end();
  273. g.begin();
  274. iron.App.removeRender(clear);
  275. #if krom_linux
  276. Context.layerPreviewDirty = true;
  277. #end
  278. }
  279. public function toFillLayer() {
  280. Context.setLayer(this);
  281. material_mask = Context.material;
  282. Layers.updateFillLayers(4);
  283. function _parse(_) {
  284. MaterialParser.parsePaintMaterial();
  285. Context.layerPreviewDirty = true;
  286. UITrait.inst.hwnd.redraws = 2;
  287. iron.App.removeRender(_parse);
  288. }
  289. iron.App.notifyOnRender(_parse);
  290. }
  291. public function toPaintLayer() {
  292. Context.setLayer(this);
  293. material_mask = null;
  294. MaterialParser.parsePaintMaterial();
  295. Context.layerPreviewDirty = true;
  296. UITrait.inst.hwnd.redraws = 2;
  297. }
  298. }