Draw brush icon previews

This commit is contained in:
luboslenco
2020-03-11 22:37:57 +01:00
parent 65221a9e7b
commit ffc49a7ac1
10 changed files with 257 additions and 77 deletions
+153
View File
@@ -7,8 +7,11 @@ import iron.Scene;
import iron.RenderPath;
import iron.object.MeshObject;
import iron.math.Mat4;
import iron.math.Vec4;
import arm.ui.UITrait;
import arm.render.RenderPathPreview;
import arm.render.RenderPathPaint;
import arm.render.RenderPathDeferred;
import arm.node.MaterialParser;
import arm.io.ImportFont;
import arm.Tool;
@@ -17,6 +20,7 @@ class RenderUtil {
public static inline var matPreviewSize = 256;
public static inline var decalPreviewSize = 512;
static var defaultMaterial: arm.data.MaterialSlot;
public static function makeMaterialPreview() {
UITrait.inst.materialPreview = true;
@@ -179,4 +183,153 @@ class RenderUtil {
if (fontName == "default.ttf") return UITrait.inst.ui.ops.font;
return ImportFont.fontMap.get(fontName);
}
public static function makeBrushPreview() {
// Prepare layers
if (RenderPathPaint.liveLayer == null) {
RenderPathPaint.liveLayer = new arm.data.LayerSlot("_live");
RenderPathPaint.liveLayer.createMask(0x00000000);
}
var l = RenderPathPaint.liveLayer;
l.texpaint.g4.begin();
l.texpaint.g4.clear(kha.Color.fromFloats(0.0, 0.0, 0.0, 0.0)); // Base
l.texpaint.g4.end();
l.texpaint_nor.g4.begin();
l.texpaint_nor.g4.clear(kha.Color.fromFloats(0.5, 0.5, 1.0, 0.0)); // Nor
l.texpaint_nor.g4.end();
l.texpaint_pack.g4.begin();
l.texpaint_pack.g4.clear(kha.Color.fromFloats(1.0, 0.0, 0.0, 0.0)); // Occ, rough, met
l.texpaint_pack.g4.end();
if (Context.brush.image == null) {
Context.brush.image = Image.createRenderTarget(matPreviewSize, matPreviewSize);
Context.brush.imageIcon = Image.createRenderTarget(50, 50);
}
// var _material = Context.material;
// if (defaultMaterial == null) {
// defaultMaterial = new arm.data.MaterialSlot();
// }
// Context.material = defaultMaterial;
// MaterialParser.parsePaintMaterial();
RenderPathPaint.useLiveLayer(true);
var path = RenderPath.active;
var hid = History.undoI - 1 < 0 ? Config.raw.undo_steps - 1 : History.undoI - 1;
path.renderTargets.set("texpaint_undo" + hid, path.renderTargets.get("empty_black"));
// Set plane mesh
var painto = Context.paintObject;
var visibles: Array<Bool> = [];
for (p in Project.paintObjects) {
visibles.push(p.visible);
p.visible = false;
}
var mergedObjectVisible = false;
if (Context.mergedObject != null) {
mergedObjectVisible = Context.mergedObject.visible;
Context.mergedObject.visible = false;
}
var cam = Scene.active.camera;
UITrait.inst.savedCamera.setFrom(cam.transform.local);
var savedFov = cam.data.raw.fov;
ViewportUtil.updateCameraType(CameraPerspective);
var m = Mat4.identity();
m.translate(0, 0, 0.5);
cam.transform.setMatrix(m);
cam.data.raw.fov = 0.92;
cam.buildProjection();
cam.buildMatrix();
m.getInverse(Scene.active.camera.VP);
var planeo: MeshObject = cast Scene.active.getChild(".Plane");
planeo.visible = true;
Context.paintObject = planeo;
var v = new Vec4();
var sx = v.set(m._00, m._01, m._02).length();
planeo.transform.rot.fromEuler(-Math.PI / 2, 0, 0);
planeo.transform.scale.set(sx, 1.0, sx);
planeo.transform.loc.set(m._30, -m._31, 0.0);
planeo.transform.buildMatrix();
RenderPathDeferred.drawGbuffer();
// Paint brush preview
var _brushRadius = UITrait.inst.brushRadius;
var _brushOpacity = UITrait.inst.brushOpacity;
var _brushHardness = UITrait.inst.brushHardness;
UITrait.inst.brushRadius = 0.25;
UITrait.inst.brushOpacity = 1.0;
UITrait.inst.brushHardness = 0.8;
var _x = UITrait.inst.paintVec.x;
var _y = UITrait.inst.paintVec.y;
var _lastX = UITrait.inst.lastPaintVecX;
var _lastY = UITrait.inst.lastPaintVecY;
var _pdirty = Context.pdirty;
Context.pdirty = 2;
// var pointsX = [0.2, 0.5, 0.5, 0.8, 0.8];
// var pointsY = [0.5, 0.2, 0.6, 0.3, 0.7];
var pointsX = [0.2, 0.2, 0.35, 0.5, 0.5, 0.5, 0.65, 0.8, 0.8, 0.8];
var pointsY = [0.5, 0.5, 0.35, 0.2, 0.4, 0.6, 0.45, 0.3, 0.5, 0.7];
for (i in 1...pointsX.length) {
UITrait.inst.lastPaintVecX = pointsX[i - 1];
UITrait.inst.lastPaintVecY = pointsY[i - 1];
UITrait.inst.paintVec.x = pointsX[i];
UITrait.inst.paintVec.y = pointsY[i];
RenderPathPaint.commandsPaint();
}
UITrait.inst.brushRadius = _brushRadius;
UITrait.inst.brushOpacity = _brushOpacity;
UITrait.inst.brushHardness = _brushHardness;
UITrait.inst.paintVec.x = _x;
UITrait.inst.paintVec.y = _y;
UITrait.inst.lastPaintVecX = _lastX;
UITrait.inst.lastPaintVecY = _lastY;
Context.pdirty = _pdirty;
// Context.material = _material;
// Restore paint mesh
planeo.visible = false;
for (i in 0...Project.paintObjects.length) {
Project.paintObjects[i].visible = visibles[i];
}
if (Context.mergedObject != null) {
Context.mergedObject.visible = mergedObjectVisible;
}
Context.paintObject = painto;
Scene.active.camera.transform.setMatrix(UITrait.inst.savedCamera);
Scene.active.camera.data.raw.fov = savedFov;
ViewportUtil.updateCameraType(UITrait.inst.cameraType);
Scene.active.camera.buildProjection();
Scene.active.camera.buildMatrix();
RenderPathPaint.useLiveLayer(false);
// Scale layer down to to image preview
var l = RenderPathPaint.liveLayer;
var target = Context.brush.image;
target.g2.begin(true, 0x00000000);
target.g2.drawScaledImage(l.texpaint, 0, 0, target.width, target.height);
target.g2.end();
// Scale image preview down to to icon
path.renderTargets.get("texpreview").image = Context.brush.image;
path.renderTargets.get("texpreview_icon").image = Context.brush.imageIcon;
path.setTarget("texpreview_icon");
path.bindTarget("texpreview", "tex");
path.drawShader("shader_datas/supersample_resolve/supersample_resolve");
Context.brush.previewReady = true;
Context.brushBlendDirty = true;
}
}