From 990f5025998b37bf80f353588686285e90d0bdf7 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 2 Jul 2019 15:17:38 +0200 Subject: [PATCH] Object ID baker, bake axis --- Sources/arm/nodes/MaterialBuilder.hx | 72 ++++++++++++++++-------- Sources/arm/render/RenderPathDeferred.hx | 24 +++++++- Sources/arm/render/RenderPathPaint.hx | 2 +- Sources/arm/render/Uniforms.hx | 3 + Sources/arm/ui/UITrait.hx | 9 ++- 5 files changed, 81 insertions(+), 29 deletions(-) diff --git a/Sources/arm/nodes/MaterialBuilder.hx b/Sources/arm/nodes/MaterialBuilder.hx index acbaf23d..6ae3b1eb 100644 --- a/Sources/arm/nodes/MaterialBuilder.hx +++ b/Sources/arm/nodes/MaterialBuilder.hx @@ -625,32 +625,15 @@ class MaterialBuilder { var strength = UITrait.inst.bakeAoStrength; var radius = UITrait.inst.bakeAoRadius; var offset = UITrait.inst.bakeAoOffset; - // frag.write('fragColor[2].r = 1.0 - traceAO(voxpos, n, $radius, $offset) * $strength;'); - frag.write('fragColor[0].r = 1.0 - traceAO(voxpos, n, $radius, $offset) * $strength;'); - frag.write('fragColor[0].g = fragColor[0].r;'); - frag.write('fragColor[0].b = fragColor[0].r;'); - frag.write('fragColor[0].a = 1.0;'); + frag.write('float ao = traceAO(voxpos, n, $radius, $offset) * $strength;'); + if (UITrait.inst.bakeAxis > 0) { + var axis = axisString(UITrait.inst.bakeAxis); + frag.write('ao *= dot(n, $axis);'); + } + frag.write('ao = 1.0 - ao;'); + frag.write('fragColor[0] = vec4(ao, ao, ao, 1.0);'); } - else if (UITrait.inst.bakeType == 1) { // 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) { // TexCoord - frag.write('fragColor[0] = vec4(texCoord.xy, 0.0, 1.0);'); - } - else if (UITrait.inst.bakeType == 3) { // 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 * 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);'); - frag.write('fragColor[0] = vec4(matid_r, matid_g, matid_b, 1.0);'); - } - else if (UITrait.inst.bakeType == 4) { // 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) { // Curvature + else if (UITrait.inst.bakeType == 1) { // Curvature var strength = UITrait.inst.bakeCurvStrength * 2.0; var radius = (1.0 / UITrait.inst.bakeCurvRadius) * 0.25; var offset = UITrait.inst.bakeCurvOffset / 10; @@ -659,8 +642,38 @@ class MaterialBuilder { frag.write('vec3 dy = dFdy(n);'); frag.write('float curvature = max(dot(dx, dx), dot(dy, dy));'); frag.write('curvature = clamp(pow(curvature, $radius) * $strength + $offset, 0.0, 1.0);'); + if (UITrait.inst.bakeAxis > 0) { + var axis = axisString(UITrait.inst.bakeAxis); + frag.write('curvature *= dot(n, $axis);'); + } 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 == 3) { // TexCoord + frag.write('fragColor[0] = vec4(texCoord.xy, 0.0, 1.0);'); + } + else if (UITrait.inst.bakeType == 4) { // 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 + 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);'); + frag.write('float matid_g = fract(sin(dot(vec2(sample_matid * 20.0, sample_matid), vec2(12.9898, 78.233))) * 43758.5453);'); + 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 + 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);'); + frag.write('float id_b = fract(sin(dot(vec2(objectId, objectId * 40.0), vec2(12.9898, 78.233))) * 43758.5453);'); + frag.write('fragColor[0] = vec4(id_r, id_g, id_b, 1.0);'); + } } Cycles.finalize(con_paint); @@ -672,6 +685,15 @@ class MaterialBuilder { return con_paint; } + static function axisString(i:Int):String { + return i == 1 ? "vec3(1,0,0)" : + i == 2 ? "vec3(0,1,0)" : + i == 3 ? "vec3(0,0,1)" : + i == 4 ? "vec3(-1,0,0)" : + i == 5 ? "vec3(0,-1,0)" : + "vec3(0,0,-1)"; + } + public static function make_mesh_preview(data:CyclesShaderData, matcon:TMaterialContext):CyclesShaderContext { var context_id = 'mesh'; var con_mesh:CyclesShaderContext = data.add_context({ diff --git a/Sources/arm/render/RenderPathDeferred.hx b/Sources/arm/render/RenderPathDeferred.hx index c50847ca..aaa7e77e 100644 --- a/Sources/arm/render/RenderPathDeferred.hx +++ b/Sources/arm/render/RenderPathDeferred.hx @@ -625,7 +625,29 @@ class RenderPathDeferred { t.buildMatrix(); } - RenderPathPaint.commandsPaint(); + if (Context.tool == ToolBake) { + if (UITrait.inst.bakeType == 6) { // Object ID + var _layerFilter = UITrait.inst.layerFilter; + var _paintObject = Context.paintObject; + var isMerged = Context.mergedObject != null; + var _visible = isMerged && Context.mergedObject.visible; + UITrait.inst.layerFilter = 1; + if (isMerged) Context.mergedObject.visible = false; + + for (p in Project.paintObjects) { + Context.selectPaintObject(p); + RenderPathPaint.commandsPaint(); + } + + UITrait.inst.layerFilter = _layerFilter; + Context.selectPaintObject(_paintObject); + if (isMerged) Context.mergedObject.visible = _visible; + } + else RenderPathPaint.commandsPaint(); + } + else { // Paint + RenderPathPaint.commandsPaint(); + } } // diff --git a/Sources/arm/render/RenderPathPaint.hx b/Sources/arm/render/RenderPathPaint.hx index 18192032..6a743d08 100644 --- a/Sources/arm/render/RenderPathPaint.hx +++ b/Sources/arm/render/RenderPathPaint.hx @@ -131,7 +131,7 @@ class RenderPathPaint { path.drawMeshes("paint"); - if (Context.tool == ToolBake && UITrait.inst.bakeType == 5 && UITrait.inst.bakeCurvSmooth > 0) { // Curvature + if (Context.tool == ToolBake && UITrait.inst.bakeType == 1 && UITrait.inst.bakeCurvSmooth > 0) { // Curvature if (path.renderTargets.get("texpaint_blur") == null) { var t = new RenderTargetRaw(); t.name = "texpaint_blur"; diff --git a/Sources/arm/render/Uniforms.hx b/Sources/arm/render/Uniforms.hx index e6413649..6013fe39 100644 --- a/Sources/arm/render/Uniforms.hx +++ b/Sources/arm/render/Uniforms.hx @@ -49,6 +49,9 @@ class Uniforms { else if (link == '_texpaintSize') { return Config.getTextureRes(); } + else if (link == '_objectId') { + return Project.paintObjects.indexOf(Context.paintObject); + } return null; } diff --git a/Sources/arm/ui/UITrait.hx b/Sources/arm/ui/UITrait.hx index 835f51be..5322ad47 100644 --- a/Sources/arm/ui/UITrait.hx +++ b/Sources/arm/ui/UITrait.hx @@ -165,6 +165,7 @@ class UITrait { public var brushAngleReject = true; public var brushAngleRejectDot = 0.5; public var bakeType = 0; + public var bakeAxis = 0; public var bakeAoStrength = 1.0; public var bakeAoRadius = 1.0; public var bakeAoOffset = 1.0; @@ -930,7 +931,11 @@ class UITrait { else if (Context.tool == ToolBake) { ui.changed = false; var bakeHandle = Id.handle({position: bakeType}); - bakeType = ui.combo(bakeHandle, ["AO", "Position", "TexCoord", "Material ID", "Normal (World)", "Curvature"], "Bake"); + bakeType = ui.combo(bakeHandle, ["AO", "Curvature", "Position", "TexCoord", "Normal (World)", "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"); + } if (bakeType == 0) { // AO var strengthHandle = Id.handle({value: bakeAoStrength}); bakeAoStrength = ui.slider(strengthHandle, "Strength", 0.0, 2.0, true); @@ -939,7 +944,7 @@ class UITrait { var offsetHandle = Id.handle({value: bakeAoOffset}); bakeAoOffset = ui.slider(offsetHandle, "Offset", 0.0, 2.0, true); } - if (bakeType == 5) { // Curvature + if (bakeType == 1) { // Curvature var strengthHandle = Id.handle({value: bakeCurvStrength}); bakeCurvStrength = ui.slider(strengthHandle, "Strength", 0.0, 2.0, true); var radiusHandle = Id.handle({value: bakeCurvRadius});