Project.hx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package arm;
  2. import kha.System;
  3. import kha.Window;
  4. import kha.Image;
  5. import zui.Zui;
  6. import zui.Id;
  7. import zui.Nodes;
  8. import iron.data.SceneFormat;
  9. import iron.data.MeshData;
  10. import iron.data.Data;
  11. import iron.object.MeshObject;
  12. import iron.Scene;
  13. import arm.util.RenderUtil;
  14. import arm.Viewport;
  15. import arm.sys.File;
  16. import arm.sys.Path;
  17. import arm.ui.UISidebar;
  18. import arm.ui.UIFiles;
  19. import arm.ui.UIBox;
  20. import arm.ui.UINodes;
  21. import arm.ui.UIHeader;
  22. import arm.data.LayerSlot;
  23. import arm.data.BrushSlot;
  24. import arm.data.FontSlot;
  25. import arm.data.MaterialSlot;
  26. import arm.node.MakeMaterial;
  27. import arm.io.ImportAsset;
  28. import arm.io.ImportArm;
  29. import arm.io.ImportBlend;
  30. import arm.io.ImportMesh;
  31. import arm.io.ImportTexture;
  32. import arm.io.ExportArm;
  33. import arm.node.NodesBrush;
  34. import arm.ProjectFormat;
  35. import arm.Enums;
  36. class Project {
  37. public static var raw: TProjectFormat = {};
  38. public static var filepath = "";
  39. public static var assets: Array<TAsset> = [];
  40. public static var assetNames: Array<String> = [];
  41. public static var assetId = 0;
  42. public static var meshAssets: Array<String> = [];
  43. public static var materials: Array<MaterialSlot> = null;
  44. public static var materialGroups: Array<TNodeGroup> = [];
  45. public static var brushes: Array<BrushSlot> = null;
  46. public static var layers: Array<LayerSlot> = null;
  47. public static var fonts: Array<FontSlot> = null;
  48. public static var paintObjects: Array<MeshObject> = null;
  49. public static var atlasObjects: Array<Int> = null;
  50. public static var atlasNames: Array<String> = null;
  51. public static var assetMap = new Map<Int, Dynamic>(); // kha.Image | kha.Font
  52. static var meshList: Array<String> = null;
  53. public static function projectOpen() {
  54. UIFiles.show("arm", false, false, function(path: String) {
  55. if (!path.endsWith(".arm")) {
  56. Console.error(Strings.error0());
  57. return;
  58. }
  59. var current = @:privateAccess kha.graphics2.Graphics.current;
  60. if (current != null) current.end();
  61. ImportArm.runProject(path);
  62. if (current != null) current.begin(false);
  63. });
  64. }
  65. public static function projectOpenRecentBox() {
  66. UIBox.showCustom(function(ui: Zui) {
  67. if (ui.tab(Id.handle(), tr("Recent Projects"))) {
  68. for (path in Config.raw.recent_projects) {
  69. var file = path;
  70. #if krom_windows
  71. file = path.replace("/", "\\");
  72. #else
  73. file = path.replace("\\", "/");
  74. #end
  75. file = file.substr(file.lastIndexOf(Path.sep) + 1);
  76. if (ui.button(file, Left)) {
  77. var current = @:privateAccess kha.graphics2.Graphics.current;
  78. if (current != null) current.end();
  79. ImportArm.runProject(path);
  80. if (current != null) current.begin(false);
  81. UIBox.show = false;
  82. }
  83. if (ui.isHovered) ui.tooltip(path);
  84. }
  85. if (ui.button("Clear", Left)) {
  86. Config.raw.recent_projects = [];
  87. Config.save();
  88. }
  89. }
  90. }, 400, 320);
  91. }
  92. public static function projectSave(saveAndQuit = false) {
  93. if (filepath == "") {
  94. #if krom_ios
  95. var documentDirectory = Krom.saveDialog("", "");
  96. documentDirectory = documentDirectory.substr(0, documentDirectory.length - 8); // Strip /'untitled'
  97. filepath = documentDirectory + "/project" + Config.raw.recent_projects.length + ".arm";
  98. #elseif krom_android
  99. filepath = Krom.savePath() + "/project" + Config.raw.recent_projects.length + ".arm";
  100. #else
  101. projectSaveAs();
  102. return;
  103. #end
  104. }
  105. var filename = Project.filepath.substring(Project.filepath.lastIndexOf(Path.sep) + 1, Project.filepath.length - 4);
  106. Window.get(0).title = filename + " - " + Main.title;
  107. function _init() {
  108. ExportArm.runProject();
  109. if (saveAndQuit) System.stop();
  110. }
  111. iron.App.notifyOnInit(_init);
  112. }
  113. public static function projectSaveAs() {
  114. UIFiles.show("arm", true, false, function(path: String) {
  115. var f = UIFiles.filename;
  116. if (f == "") f = tr("untitled");
  117. filepath = path + Path.sep + f;
  118. if (!filepath.endsWith(".arm")) filepath += ".arm";
  119. projectSave();
  120. });
  121. }
  122. public static function projectNewBox() {
  123. UIBox.showCustom(function(ui: Zui) {
  124. if (ui.tab(Id.handle(), tr("New Project"))) {
  125. if (meshList == null) {
  126. meshList = File.readDirectory(Path.data() + Path.sep + "meshes");
  127. for (i in 0...meshList.length) meshList[i] = meshList[i].substr(0, meshList[i].length - 4); // Trim .arm
  128. meshList.unshift("plane");
  129. meshList.unshift("sphere");
  130. meshList.unshift("rounded_cube");
  131. }
  132. ui.row([0.5, 0.5]);
  133. Context.projectType = ui.combo(Id.handle({position: Context.projectType}), meshList, tr("Template"), true);
  134. Context.projectAspectRatio = ui.combo(Id.handle({position: Context.projectAspectRatio}), ["1:1", "2:1", "1:2"], tr("Aspect Ratio"), true);
  135. @:privateAccess ui.endElement();
  136. ui.row([0.5, 0.5]);
  137. if (ui.button(tr("Cancel"))) {
  138. UIBox.show = false;
  139. }
  140. if (ui.button(tr("OK")) || ui.isReturnDown) {
  141. Project.projectNew();
  142. Viewport.scaleToBounds();
  143. UIBox.show = false;
  144. App.redrawUI();
  145. }
  146. }
  147. });
  148. }
  149. public static function projectNew(resetLayers = true) {
  150. Window.get(0).title = Main.title;
  151. filepath = "";
  152. if (Context.mergedObject != null) {
  153. Context.mergedObject.remove();
  154. Data.deleteMesh(Context.mergedObject.data.handle);
  155. Context.mergedObject = null;
  156. }
  157. Viewport.reset();
  158. Context.layerPreviewDirty = true;
  159. Context.layerFilter = 0;
  160. Project.meshAssets = [];
  161. Context.paintObject = Context.mainObject();
  162. Context.selectPaintObject(Context.mainObject());
  163. for (i in 1...paintObjects.length) {
  164. var p = paintObjects[i];
  165. if (p == Context.paintObject) continue;
  166. Data.deleteMesh(p.data.handle);
  167. p.remove();
  168. }
  169. var meshes = Scene.active.meshes;
  170. var len = meshes.length;
  171. for (i in 0...len) {
  172. var m = meshes[len - i - 1];
  173. if (Context.projectObjects.indexOf(m) == -1 &&
  174. m.name != ".ParticleEmitter" &&
  175. m.name != ".Particle") {
  176. Data.deleteMesh(m.data.handle);
  177. m.remove();
  178. }
  179. }
  180. var handle = Context.paintObject.data.handle;
  181. if (handle != "SceneSphere" && handle != "ScenePlane") {
  182. Data.deleteMesh(handle);
  183. }
  184. if (Context.projectType != ModelRoundedCube) {
  185. var raw: TMeshData = null;
  186. if (Context.projectType == ModelSphere || Context.projectType == ModelTessellatedPlane) {
  187. var mesh: Dynamic = Context.projectType == ModelSphere ?
  188. new arm.geom.Sphere(1, 512, 256) :
  189. new arm.geom.Plane(1, 1, 512, 512);
  190. raw = {
  191. name: "Tessellated",
  192. vertex_arrays: [
  193. { values: mesh.posa, attrib: "pos", data: "short4norm" },
  194. { values: mesh.nora, attrib: "nor", data: "short2norm" },
  195. { values: mesh.texa, attrib: "tex", data: "short2norm" }
  196. ],
  197. index_arrays: [
  198. { values: mesh.inda, material: 0 }
  199. ],
  200. scale_pos: mesh.scalePos,
  201. scale_tex: mesh.scaleTex
  202. };
  203. }
  204. else {
  205. Data.getBlob("meshes/" + meshList[Context.projectType] + ".arm", function(b: kha.Blob) {
  206. raw = iron.system.ArmPack.decode(b.toBytes()).mesh_datas[0];
  207. });
  208. }
  209. var md = new MeshData(raw, function(md: MeshData) {});
  210. Data.cachedMeshes.set("SceneTessellated", md);
  211. if (Context.projectType == ModelTessellatedPlane) {
  212. Viewport.setView(0, 0, 0.75, 0, 0, 0); // Top
  213. }
  214. }
  215. var n = Context.projectType == ModelRoundedCube ? ".Cube" : "Tessellated";
  216. Data.getMesh("Scene", n, function(md: MeshData) {
  217. var current = @:privateAccess kha.graphics2.Graphics.current;
  218. if (current != null) current.end();
  219. Context.pickerMaskHandle.position = MaskNone;
  220. Context.paintObject.setData(md);
  221. Context.paintObject.transform.scale.set(1, 1, 1);
  222. Context.paintObject.transform.buildMatrix();
  223. Context.paintObject.name = n;
  224. paintObjects = [Context.paintObject];
  225. while (materials.length > 0) materials.pop().unload();
  226. Data.getMaterial("Scene", "Material", function(m: iron.data.MaterialData) {
  227. materials.push(new MaterialSlot(m));
  228. });
  229. Context.material = materials[0];
  230. arm.ui.UINodes.inst.hwnd.redraws = 2;
  231. arm.ui.UINodes.inst.groupStack = [];
  232. materialGroups = [];
  233. brushes = [new BrushSlot()];
  234. Context.brush = brushes[0];
  235. var fontNames = App.font.getFontNames();
  236. fonts = [new FontSlot(fontNames.length > 0 ? fontNames[0] : "default.ttf", App.font)];
  237. Context.font = fonts[0];
  238. Project.setDefaultSwatches();
  239. Context.swatch = Project.raw.swatches[0];
  240. History.reset();
  241. MakeMaterial.parsePaintMaterial();
  242. RenderUtil.makeMaterialPreview();
  243. for (a in assets) Data.deleteImage(a.file);
  244. assets = [];
  245. assetNames = [];
  246. assetMap = [];
  247. assetId = 0;
  248. Project.raw.packed_assets = [];
  249. Context.ddirty = 4;
  250. UISidebar.inst.hwnd0.redraws = 2;
  251. UISidebar.inst.hwnd1.redraws = 2;
  252. if (resetLayers) {
  253. var aspectRatioChanged = layers[0].texpaint.width != Config.getTextureResX() || layers[0].texpaint.height != Config.getTextureResY();
  254. while (layers.length > 0) layers.pop().unload();
  255. var layer = new LayerSlot();
  256. layers.push(layer);
  257. Context.setLayer(layer);
  258. if (aspectRatioChanged) {
  259. iron.App.notifyOnInit(Layers.resizeLayers);
  260. }
  261. iron.App.notifyOnInit(Layers.initLayers);
  262. }
  263. if (current != null) current.begin(false);
  264. Context.savedEnvmap = null;
  265. Context.envmapLoaded = false;
  266. Scene.active.world.envmap = Context.emptyEnvmap;
  267. Scene.active.world.raw.envmap = "World_radiance.k";
  268. Context.showEnvmapHandle.selected = Context.showEnvmap = false;
  269. Scene.active.world.probe.radiance = Context.defaultRadiance;
  270. Scene.active.world.probe.radianceMipmaps = Context.defaultRadianceMipmaps;
  271. Scene.active.world.probe.irradiance = Context.defaultIrradiance;
  272. Scene.active.world.probe.raw.strength = 4.0;
  273. Context.initTool();
  274. });
  275. }
  276. public static function importMaterial() {
  277. UIFiles.show("arm,blend", false, true, function(path: String) {
  278. path.endsWith(".blend") ?
  279. ImportBlend.runMaterial(path) :
  280. ImportArm.runMaterial(path);
  281. });
  282. }
  283. public static function importBrush() {
  284. UIFiles.show("arm," + Path.textureFormats.join(","), false, true, function(path: String) {
  285. // Create brush from texture
  286. if (Path.isTexture(path)) {
  287. // Import texture
  288. ImportAsset.run(path);
  289. var assetIndex = 0;
  290. for (i in 0...Project.assets.length) {
  291. if (Project.assets[i].file == path) {
  292. assetIndex = i;
  293. break;
  294. }
  295. }
  296. // Create a new brush
  297. Context.brush = new BrushSlot();
  298. Project.brushes.push(Context.brush);
  299. // Create and link image node
  300. var n = NodesBrush.createNode("TEX_IMAGE");
  301. n.x = 83;
  302. n.y = 340;
  303. n.buttons[0].default_value = assetIndex;
  304. var links = Context.brush.canvas.links;
  305. links.push({
  306. id: Context.brush.nodes.getLinkId(links),
  307. from_id: n.id,
  308. from_socket: 0,
  309. to_id: 0,
  310. to_socket: 4
  311. });
  312. // Parse brush
  313. MakeMaterial.parseBrush();
  314. UINodes.inst.hwnd.redraws = 2;
  315. function _init() {
  316. RenderUtil.makeBrushPreview();
  317. }
  318. iron.App.notifyOnInit(_init);
  319. }
  320. // Import from project file
  321. else {
  322. ImportArm.runBrush(path);
  323. }
  324. });
  325. }
  326. public static function importMesh(replaceExisting = true) {
  327. UIFiles.show(Path.meshFormats.join(","), false, false, function(path: String) {
  328. importMeshBox(path, replaceExisting);
  329. });
  330. }
  331. public static function importMeshBox(path: String, replaceExisting = true, clearLayers = true) {
  332. #if krom_ios
  333. // Import immediately while access to resource is unlocked
  334. Data.getBlob(path, function(b: kha.Blob) {});
  335. #end
  336. UIBox.showCustom(function(ui: Zui) {
  337. if (ui.tab(Id.handle(), tr("Import Mesh"))) {
  338. if (path.toLowerCase().endsWith(".obj")) {
  339. Context.splitBy = ui.combo(Id.handle(), [
  340. tr("Object"),
  341. tr("Group"),
  342. tr("Material"),
  343. tr("UDIM Tile"),
  344. ], tr("Split By"), true);
  345. if (ui.isHovered) ui.tooltip(tr("Split .obj mesh into objects"));
  346. }
  347. if (path.toLowerCase().endsWith(".fbx")) {
  348. Context.parseTransform = ui.check(Id.handle({selected: Context.parseTransform}), tr("Parse Transforms"));
  349. if (ui.isHovered) ui.tooltip(tr("Load per-object transforms from .fbx"));
  350. }
  351. if (path.toLowerCase().endsWith(".fbx") || path.toLowerCase().endsWith(".blend")) {
  352. Context.parseVCols = ui.check(Id.handle({selected: Context.parseVCols}), tr("Parse Vertex Colors"));
  353. if (ui.isHovered) ui.tooltip(tr("Import vertex color data"));
  354. }
  355. ui.row([0.45, 0.45, 0.1]);
  356. if (ui.button(tr("Cancel"))) {
  357. UIBox.show = false;
  358. }
  359. if (ui.button(tr("Import")) || ui.isReturnDown) {
  360. UIBox.show = false;
  361. App.redrawUI();
  362. ImportMesh.run(path, clearLayers, replaceExisting);
  363. }
  364. if (ui.button(tr("?"))) {
  365. File.loadUrl("https://github.com/armory3d/armorpaint_docs/blob/master/faq.md");
  366. }
  367. }
  368. });
  369. UIBox.clickToHide = false; // Prevent closing when going back to window from file browser
  370. }
  371. public static function reimportMesh() {
  372. if (Project.meshAssets != null && Project.meshAssets.length > 0 && File.exists(Project.meshAssets[0])) {
  373. importMeshBox(Project.meshAssets[0], true, false);
  374. }
  375. else importAsset();
  376. }
  377. public static function importAsset(filters: String = null, hdrAsEnvmap = true) {
  378. if (filters == null) filters = Path.textureFormats.join(",") + "," + Path.meshFormats.join(",");
  379. UIFiles.show(filters, false, true, function(path: String) {
  380. ImportAsset.run(path, -1.0, -1.0, true, hdrAsEnvmap);
  381. });
  382. }
  383. public static function importSwatches() {
  384. UIFiles.show("arm", false, false, function(path: String) {
  385. ImportArm.runSwatches(path);
  386. });
  387. }
  388. public static function reimportTextures() {
  389. for (asset in Project.assets) {
  390. reimportTexture(asset);
  391. }
  392. }
  393. public static function reimportTexture(asset: TAsset) {
  394. function load(path: String) {
  395. asset.file = path;
  396. var i = Project.assets.indexOf(asset);
  397. Data.deleteImage(asset.file);
  398. Project.assetMap.remove(asset.id);
  399. var oldAsset = Project.assets[i];
  400. Project.assets.splice(i, 1);
  401. Project.assetNames.splice(i, 1);
  402. ImportTexture.run(asset.file);
  403. Project.assets.insert(i, Project.assets.pop());
  404. Project.assetNames.insert(i, Project.assetNames.pop());
  405. if (Context.texture == oldAsset) Context.texture = Project.assets[i];
  406. function _next() {
  407. arm.node.MakeMaterial.parsePaintMaterial();
  408. arm.util.RenderUtil.makeMaterialPreview();
  409. UISidebar.inst.hwnd1.redraws = 2;
  410. }
  411. App.notifyOnNextFrame(_next);
  412. }
  413. if (!File.exists(asset.file)) {
  414. var filters = Path.textureFormats.join(",");
  415. UIFiles.show(filters, false, false, function(path: String) {
  416. load(path);
  417. });
  418. }
  419. else load(asset.file);
  420. }
  421. public static function getImage(asset: TAsset): Image {
  422. return asset != null ? Project.assetMap.get(asset.id) : null;
  423. }
  424. public static function getUsedAtlases(): Array<String> {
  425. if (Project.atlasObjects == null) return null;
  426. var used: Array<Int> = [];
  427. for (i in Project.atlasObjects) if (used.indexOf(i) == -1) used.push(i);
  428. if (used.length > 1) {
  429. var res: Array<String> = [];
  430. for (i in used) res.push(Project.atlasNames[i]);
  431. return res;
  432. }
  433. else return null;
  434. }
  435. public static function isAtlasObject(p: MeshObject): Bool {
  436. if (Context.layerFilter <= Project.paintObjects.length) return false;
  437. var atlasName = getUsedAtlases()[Context.layerFilter - Project.paintObjects.length - 1];
  438. var atlasI = Project.atlasNames.indexOf(atlasName);
  439. return atlasI == Project.atlasObjects[Project.paintObjects.indexOf(p)];
  440. }
  441. public static function getAtlasObjects(objectMask: Int): Array<MeshObject> {
  442. var atlasName = Project.getUsedAtlases()[objectMask - Project.paintObjects.length - 1];
  443. var atlasI = Project.atlasNames.indexOf(atlasName);
  444. var visibles: Array<MeshObject> = [];
  445. for (i in 0...Project.paintObjects.length) if (Project.atlasObjects[i] == atlasI) visibles.push(Project.paintObjects[i]);
  446. return visibles;
  447. }
  448. public static function packedAssetExists(packed_assets: Array<TPackedAsset>, name: String): Bool {
  449. for (pa in packed_assets) if (pa.name == name) return true;
  450. return false;
  451. }
  452. public static function exportSwatches() {
  453. UIFiles.show("arm", true, false, function(path: String) {
  454. var f = UIFiles.filename;
  455. if (f == "") f = tr("untitled");
  456. ExportArm.runSwatches(path + Path.sep + f);
  457. });
  458. }
  459. public static function makeSwatch(base = 0xffffffff): TSwatchColor {
  460. return { base: base, opacity: 1.0, occlusion: 1.0, roughness: 0.0, metallic: 0.0, normal: 0xff8080ff, emission: 0.0, height: 0.0, subsurface: 0.0 };
  461. }
  462. public static function setDefaultSwatches() {
  463. // 32-Color Palette by Andrew Kensler
  464. // http://eastfarthing.com/blog/2016-05-06-palette/
  465. Project.raw.swatches = [];
  466. var colors = [0xffffffff, 0xff000000, 0xffd6a090, 0xffa12c32, 0xfffa2f7a, 0xfffb9fda, 0xffe61cf7, 0xff992f7c, 0xff47011f, 0xff051155, 0xff4f02ec, 0xff2d69cb, 0xff00a6ee, 0xff6febff, 0xff08a29a, 0xff2a666a, 0xff063619, 0xff4a4957, 0xff8e7ba4, 0xffb7c0ff, 0xffacbe9c, 0xff827c70, 0xff5a3b1c, 0xffae6507, 0xfff7aa30, 0xfff4ea5c, 0xff9b9500, 0xff566204, 0xff11963b, 0xff51e113, 0xff08fdcc];
  467. for (c in colors) Project.raw.swatches.push(Project.makeSwatch(c));
  468. }
  469. public static function getMaterialGroupByName(groupName: String): TNodeGroup {
  470. for (g in materialGroups) if (g.canvas.name == groupName) return g;
  471. return null;
  472. }
  473. }
  474. typedef TNodeGroup = {
  475. public var nodes: Nodes;
  476. public var canvas: TNodeCanvas;
  477. }