From ef6208ea314a6bd6f098156ca003768e441000ea Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 2 Jul 2019 21:56:39 +0200 Subject: [PATCH] Tangent normal bake stubs --- Sources/arm/History.hx | 2 +- Sources/arm/nodes/MaterialBuilder.hx | 29 ++++++++++++++++-------- Sources/arm/render/RenderPathDeferred.hx | 26 +++++++++++++++++++-- Sources/arm/ui/UIMenu.hx | 2 +- Sources/arm/ui/UITrait.hx | 8 ++++++- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Sources/arm/History.hx b/Sources/arm/History.hx index c7515a05..a50c257f 100644 --- a/Sources/arm/History.hx +++ b/Sources/arm/History.hx @@ -221,7 +221,7 @@ class History { copyToUndo(Context.layer.id, undoI, isMask); pushUndo = false; - var step = push(UITrait.inst.toolNames[Context.tool]); + push(UITrait.inst.toolNames[Context.tool]); } public static function newLayer() { diff --git a/Sources/arm/nodes/MaterialBuilder.hx b/Sources/arm/nodes/MaterialBuilder.hx index 6ae3b1eb..2d1f81eb 100644 --- a/Sources/arm/nodes/MaterialBuilder.hx +++ b/Sources/arm/nodes/MaterialBuilder.hx @@ -648,18 +648,27 @@ class MaterialBuilder { } frag.write('fragColor[0] = vec4(curvature, curvature, curvature, 1.0);'); } - else if (UITrait.inst.bakeType == 2) { // Position - frag.wposition = true; - frag.write('fragColor[0] = vec4(wposition * vec3(0.5, 0.5, 0.5) + vec3(0.5, 0.5, 0.5), 1.0);'); + else if (UITrait.inst.bakeType == 2) { // Normal (Tangent) + frag.n = true; + frag.add_uniform('sampler2D texpaint_undo', '_texpaint_undo'); + frag.write('vec3 n0 = textureLod(texpaint_undo, texCoord, 0.0).rgb * vec3(2.0, 2.0, 2.0) - vec3(1.0, 1.0, 1.0);'); + frag.add_function(CyclesFunctions.str_cotangentFrame); + frag.write('mat3 invTBN = transpose(cotangentFrame(n, n, texCoord));'); + frag.write('vec3 res = normalize(mul(n0, invTBN)) * vec3(0.5, 0.5, 0.5) + vec3(0.5, 0.5, 0.5);'); + frag.write('fragColor[0] = vec4(res, 1.0);'); } - else if (UITrait.inst.bakeType == 3) { // TexCoord - frag.write('fragColor[0] = vec4(texCoord.xy, 0.0, 1.0);'); - } - else if (UITrait.inst.bakeType == 4) { // Normal (World) + else if (UITrait.inst.bakeType == 3) { // Normal (World) frag.n = true; frag.write('fragColor[0] = vec4(n * vec3(0.5, 0.5, 0.5) + vec3(0.5, 0.5, 0.5), 1.0);'); } - else if (UITrait.inst.bakeType == 5) { // Material ID + else if (UITrait.inst.bakeType == 4) { // Position + frag.wposition = true; + frag.write('fragColor[0] = vec4(wposition * vec3(0.5, 0.5, 0.5) + vec3(0.5, 0.5, 0.5), 1.0);'); + } + else if (UITrait.inst.bakeType == 5) { // TexCoord + frag.write('fragColor[0] = vec4(texCoord.xy, 0.0, 1.0);'); + } + else if (UITrait.inst.bakeType == 6) { // Material ID frag.add_uniform('sampler2D texpaint_nor_undo', '_texpaint_nor_undo'); frag.write('float sample_matid = textureLod(texpaint_nor_undo, texCoord, 0.0).a;'); frag.write('float matid_r = fract(sin(dot(vec2(sample_matid, sample_matid * 20.0), vec2(12.9898, 78.233))) * 43758.5453);'); @@ -667,7 +676,7 @@ class MaterialBuilder { frag.write('float matid_b = fract(sin(dot(vec2(sample_matid, sample_matid * 40.0), vec2(12.9898, 78.233))) * 43758.5453);'); frag.write('fragColor[0] = vec4(matid_r, matid_g, matid_b, 1.0);'); } - else if (UITrait.inst.bakeType == 6) { // Object ID + else if (UITrait.inst.bakeType == 7) { // Object ID frag.add_uniform('float objectId', '_objectId'); frag.write('float id_r = fract(sin(dot(vec2(objectId, objectId * 20.0), vec2(12.9898, 78.233))) * 43758.5453);'); frag.write('float id_g = fract(sin(dot(vec2(objectId * 20.0, objectId), vec2(12.9898, 78.233))) * 43758.5453);'); @@ -1169,7 +1178,7 @@ class MaterialBuilder { frag.write('fragColor[1] = vec4(nAttr, packFloat2(1.0, 1.0));'); // occ/spec } else if (UITrait.inst.viewportMode == 8) { // MaterialID - frag.write('float sample_matid = textureLod(texpaint_nor, texCoord, 0.0).a;'); + frag.write('float sample_matid = textureLodShared(texpaint_nor, texCoord, 0.0).a;'); frag.write('float matid_r = fract(sin(dot(vec2(sample_matid, sample_matid * 2.0), vec2(12.9898, 78.233))) * 43758.5453);'); frag.write('float matid_g = fract(sin(dot(vec2(sample_matid * 2.0, sample_matid), vec2(12.9898, 78.233))) * 43758.5453);'); frag.write('float matid_b = fract(sin(dot(vec2(sample_matid, sample_matid * 4.0), vec2(12.9898, 78.233))) * 43758.5453);'); diff --git a/Sources/arm/render/RenderPathDeferred.hx b/Sources/arm/render/RenderPathDeferred.hx index aaa7e77e..37057bc1 100644 --- a/Sources/arm/render/RenderPathDeferred.hx +++ b/Sources/arm/render/RenderPathDeferred.hx @@ -12,6 +12,7 @@ import arm.util.ViewportUtil; import arm.util.RenderUtil; import arm.ui.UITrait; import arm.ui.UIView2D; +import arm.nodes.MaterialParser; import arm.Tool; class RenderPathDeferred { @@ -517,6 +518,7 @@ class RenderPathDeferred { @:privateAccess Scene.active.camera.frame = taaFrame; @:privateAccess Scene.active.camera.projectionJitter(); + var pushUndoLast = History.pushUndo; if (History.pushUndo && History.undoLayers != null) { History.paint(); } @@ -626,7 +628,25 @@ class RenderPathDeferred { } if (Context.tool == ToolBake) { - if (UITrait.inst.bakeType == 6) { // Object ID + if (UITrait.inst.bakeType == 2) { // Normal (Tangent) + UITrait.inst.bakeType = 3; // Bake high poly world normals + MaterialParser.parsePaintMaterial(); + var _paintObject = Context.paintObject; + var highPoly = Project.paintObjects[UITrait.inst.bakeHighPoly]; + var _visible = highPoly.visible; + highPoly.visible = true; + Context.selectPaintObject(highPoly); + RenderPathPaint.commandsPaint(); + highPoly.visible = _visible; + UITrait.inst.sub--; + if (pushUndoLast) History.paint(); + + UITrait.inst.bakeType = 2; + MaterialParser.parsePaintMaterial(); + Context.selectPaintObject(_paintObject); + RenderPathPaint.commandsPaint(); + } + else if (UITrait.inst.bakeType == 7) { // Object ID var _layerFilter = UITrait.inst.layerFilter; var _paintObject = Context.paintObject; var isMerged = Context.mergedObject != null; @@ -643,7 +663,9 @@ class RenderPathDeferred { Context.selectPaintObject(_paintObject); if (isMerged) Context.mergedObject.visible = _visible; } - else RenderPathPaint.commandsPaint(); + else { + RenderPathPaint.commandsPaint(); + } } else { // Paint RenderPathPaint.commandsPaint(); diff --git a/Sources/arm/ui/UIMenu.hx b/Sources/arm/ui/UIMenu.hx index 44c1dfc9..c1a315d8 100644 --- a/Sources/arm/ui/UIMenu.hx +++ b/Sources/arm/ui/UIMenu.hx @@ -30,7 +30,7 @@ class UIMenu { var menuButtonW = Std.int(ui.ELEMENT_W() * 0.5); var px = panelx + menuButtonW * menuCategory; var py = UITrait.inst.headerh; - var menuItems = [5, 2, 13, 4]; + var menuItems = [5, 2, 13, 5]; var ph = 24 * menuItems[menuCategory] * ui.SCALE; g.color = ui.t.SEPARATOR_COL; diff --git a/Sources/arm/ui/UITrait.hx b/Sources/arm/ui/UITrait.hx index 5322ad47..5565972a 100644 --- a/Sources/arm/ui/UITrait.hx +++ b/Sources/arm/ui/UITrait.hx @@ -173,6 +173,7 @@ class UITrait { public var bakeCurvRadius = 1.0; public var bakeCurvOffset = 0.0; public var bakeCurvSmooth = 1; + public var bakeHighPoly = 0; public var xray = false; public var symX = false; @@ -931,7 +932,7 @@ class UITrait { else if (Context.tool == ToolBake) { ui.changed = false; var bakeHandle = Id.handle({position: bakeType}); - bakeType = ui.combo(bakeHandle, ["AO", "Curvature", "Position", "TexCoord", "Normal (World)", "Material ID", "Object ID"], "Bake"); + bakeType = ui.combo(bakeHandle, ["AO", "Curvature", "Normal (Tang)", "Normal (World)", "Position", "TexCoord", "Material ID", "Object ID"], "Bake"); if (bakeType == 0 || bakeType == 1) { var bakeAxisHandle = Id.handle({position: bakeAxis}); bakeAxis = ui.combo(bakeAxisHandle, ["XYZ", "X", "Y", "Z", "-X", "-Y", "-Z"], "Axis"); @@ -954,6 +955,11 @@ class UITrait { var smoothHandle = Id.handle({value: bakeCurvSmooth}); bakeCurvSmooth = Std.int(ui.slider(smoothHandle, "Smooth", 0, 5, false, 1)); } + if (bakeType == 2) { // Normal (Tang) + var ar = [for (p in Project.paintObjects) p.name]; + var polyHandle = Id.handle({position: bakeHighPoly}); + bakeHighPoly = ui.combo(polyHandle, ar, "High Poly"); + } if (ui.changed) { MaterialParser.parsePaintMaterial(); // Context.pdirty = 4;