RenderPathPaint.hx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package arm.render;
  2. import iron.object.MeshObject;
  3. import iron.RenderPath;
  4. import iron.Scene;
  5. import arm.ui.UITrait;
  6. import arm.Tool;
  7. class RenderPathPaint {
  8. static var initVoxels = true; // Bake AO
  9. @:access(iron.RenderPath)
  10. public static function commandsPaint() {
  11. var path = RenderPathDeferred.path;
  12. var tid = Context.layer.id;
  13. if (Context.pdirty > 0 && UITrait.inst.worktab.position != SpaceScene) {
  14. if (Context.tool == ToolParticle) {
  15. path.setTarget("texparticle");
  16. path.clearTarget(0x00000000);
  17. path.bindTarget("_main", "gbufferD");
  18. if ((UITrait.inst.xray || UITrait.inst.brushAngleReject) && UITrait.inst.brush3d) path.bindTarget("gbuffer0", "gbuffer0");
  19. var mo:MeshObject = cast Scene.active.getChild(".ParticleEmitter");
  20. mo.visible = true;
  21. mo.render(path.currentG, "mesh", @:privateAccess path.bindParams);
  22. mo.visible = false;
  23. mo = cast Scene.active.getChild(".Particle");
  24. mo.visible = true;
  25. mo.render(path.currentG, "mesh", @:privateAccess path.bindParams);
  26. mo.visible = false;
  27. @:privateAccess path.end();
  28. }
  29. if (Context.tool == ToolColorId) {
  30. path.setTarget("texpaint_colorid");
  31. path.clearTarget(0xff000000);
  32. path.bindTarget("gbuffer2", "gbuffer2");
  33. path.drawMeshes("paint");
  34. UITrait.inst.headerHandle.redraws = 2;
  35. }
  36. else if (Context.tool == ToolPicker) {
  37. path.setTarget("texpaint_picker", ["texpaint_nor_picker", "texpaint_pack_picker"]);
  38. path.clearTarget(0xff000000);
  39. path.bindTarget("gbuffer2", "gbuffer2");
  40. tid = Project.layers[0].id;
  41. path.bindTarget("texpaint" + tid, "texpaint");
  42. path.bindTarget("texpaint_nor" + tid, "texpaint_nor");
  43. path.bindTarget("texpaint_pack" + tid, "texpaint_pack");
  44. path.drawMeshes("paint");
  45. UITrait.inst.headerHandle.redraws = 2;
  46. var texpaint_picker = path.renderTargets.get("texpaint_picker").image;
  47. var texpaint_nor_picker = path.renderTargets.get("texpaint_nor_picker").image;
  48. var texpaint_pack_picker = path.renderTargets.get("texpaint_pack_picker").image;
  49. var a = texpaint_picker.getPixels();
  50. var b = texpaint_nor_picker.getPixels();
  51. var c = texpaint_pack_picker.getPixels();
  52. // Picked surface values
  53. UITrait.inst.baseRPicked = a.get(0) / 255;
  54. UITrait.inst.baseGPicked = a.get(1) / 255;
  55. UITrait.inst.baseBPicked = a.get(2) / 255;
  56. UITrait.inst.normalRPicked = b.get(0) / 255;
  57. UITrait.inst.normalGPicked = b.get(1) / 255;
  58. UITrait.inst.normalBPicked = b.get(2) / 255;
  59. UITrait.inst.occlusionPicked = c.get(0) / 255;
  60. UITrait.inst.roughnessPicked = c.get(1) / 255;
  61. UITrait.inst.metallicPicked = c.get(2) / 255;
  62. // Pick material
  63. if (UITrait.inst.pickerSelectMaterial) {
  64. var matid = b.get(3);
  65. for (m in Project.materials) {
  66. if (m.id == matid) {
  67. Context.setMaterial(m);
  68. UITrait.inst.materialIdPicked = matid;
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. else {
  75. if (Context.tool == ToolBake && UITrait.inst.bakeType == 0) { // AO
  76. if (initVoxels) {
  77. initVoxels = false;
  78. // Init voxel texture
  79. var rp_gi = Config.raw.rp_gi;
  80. Config.raw.rp_gi = true;
  81. #if rp_voxelao
  82. Inc.initGI();
  83. #end
  84. Config.raw.rp_gi = rp_gi;
  85. }
  86. path.clearImage("voxels", 0x00000000);
  87. path.setTarget("");
  88. path.setViewport(256, 256);
  89. path.bindTarget("voxels", "voxels");
  90. path.drawMeshes("voxel");
  91. path.generateMipmaps("voxels");
  92. }
  93. var blendA = "texpaint_blend0";
  94. var blendB = "texpaint_blend1";
  95. path.setTarget(blendB);
  96. path.bindTarget(blendA, "tex");
  97. path.drawShader("shader_datas/copy_pass/copy_pass");
  98. var isMask = Context.layerIsMask;
  99. var texpaint = isMask ? "texpaint_mask" + tid : "texpaint" + tid;
  100. path.setTarget(texpaint, ["texpaint_nor" + tid, "texpaint_pack" + tid, blendA]);
  101. path.bindTarget("_main", "gbufferD");
  102. if ((UITrait.inst.xray || UITrait.inst.brushAngleReject) && UITrait.inst.brush3d) {
  103. path.bindTarget("gbuffer0", "gbuffer0");
  104. }
  105. path.bindTarget(blendB, "paintmask");
  106. if (Context.tool == ToolBake && UITrait.inst.bakeType == 0) { // AO
  107. path.bindTarget("voxels", "voxels");
  108. }
  109. if (UITrait.inst.colorIdPicked) {
  110. path.bindTarget("texpaint_colorid", "texpaint_colorid");
  111. }
  112. // Read texcoords from gbuffer
  113. var readTC = (Context.tool == ToolFill && UITrait.inst.fillTypeHandle.position == 1) || // Face fill
  114. Context.tool == ToolClone ||
  115. Context.tool == ToolBlur;
  116. if (readTC) {
  117. path.bindTarget("gbuffer2", "gbuffer2");
  118. }
  119. path.drawMeshes("paint");
  120. if (Context.tool == ToolBake && UITrait.inst.bakeType == 1 && UITrait.inst.bakeCurvSmooth > 0) { // Curvature
  121. if (path.renderTargets.get("texpaint_blur") == null) {
  122. var t = new RenderTargetRaw();
  123. t.name = "texpaint_blur";
  124. t.width = Std.int(Config.getTextureRes() * 0.95);
  125. t.height = Std.int(Config.getTextureRes() * 0.95);
  126. t.format = 'RGBA32';
  127. RenderPath.active.createRenderTarget(t);
  128. }
  129. var blurs = Math.round(UITrait.inst.bakeCurvSmooth);
  130. for (i in 0...blurs) {
  131. path.setTarget("texpaint_blur");
  132. path.bindTarget(texpaint, "tex");
  133. path.drawShader("shader_datas/copy_pass/copy_pass");
  134. path.setTarget(texpaint);
  135. path.bindTarget("texpaint_blur", "tex");
  136. path.drawShader("shader_datas/copy_pass/copy_pass");
  137. }
  138. }
  139. }
  140. }
  141. }
  142. @:access(iron.RenderPath)
  143. public static function commandsCursor() {
  144. var tool = Context.tool;
  145. if (tool != ToolBrush &&
  146. tool != ToolEraser &&
  147. tool != ToolClone &&
  148. tool != ToolBlur &&
  149. tool != ToolParticle) {
  150. // tool != ToolDecal &&
  151. // tool != ToolText) {
  152. return;
  153. }
  154. if (!App.uienabled ||
  155. UITrait.inst.worktab.position == SpaceScene) {
  156. return;
  157. }
  158. var path = RenderPathDeferred.path;
  159. var plane = cast(Scene.active.getChild(".Plane"), MeshObject);
  160. var geom = plane.data.geom;
  161. var g = path.frameG;
  162. if (Layers.pipeCursor == null) Layers.makeCursorPipe();
  163. path.setTarget("");
  164. g.setPipeline(Layers.pipeCursor);
  165. var decal = Context.tool == ToolDecal || Context.tool == ToolText;
  166. var img = decal ? UITrait.inst.decalImage : Res.get("cursor.png");
  167. g.setTexture(Layers.cursorTex, img);
  168. var gbuffer0 = path.renderTargets.get("gbuffer0").image;
  169. g.setTextureDepth(Layers.cursorGbufferD, gbuffer0);
  170. g.setTexture(Layers.cursorGbuffer0, gbuffer0);
  171. var mx = iron.system.Input.getMouse().x / iron.App.w();
  172. var my = 1.0 - (iron.system.Input.getMouse().y / iron.App.h());
  173. if (UITrait.inst.brushLocked) {
  174. mx = (UITrait.inst.lockStartedX - iron.App.x()) / iron.App.w();
  175. my = 1.0 - (UITrait.inst.lockStartedY - iron.App.y()) / iron.App.h();
  176. }
  177. g.setFloat2(Layers.cursorMouse, mx, my);
  178. g.setFloat2(Layers.cursorStep, 2 / gbuffer0.width, 2 / gbuffer0.height);
  179. g.setFloat(Layers.cursorRadius, UITrait.inst.brushRadius / 3.4);
  180. g.setMatrix(Layers.cursorVP, Scene.active.camera.VP.self);
  181. var helpMat = iron.math.Mat4.identity();
  182. helpMat.getInverse(Scene.active.camera.VP);
  183. g.setMatrix(Layers.cursorInvVP, helpMat.self);
  184. g.setVertexBuffer(geom.vertexBuffer);
  185. g.setIndexBuffer(geom.indexBuffers[0]);
  186. g.drawIndexedVertices();
  187. g.disableScissor();
  188. path.end();
  189. }
  190. }