Files
armorpaint/armorsculpt/Sources/MakeMaterial.ts
T

321 lines
9.3 KiB
TypeScript
Raw Normal View History

2023-04-24 15:20:03 +02:00
class MakeMaterial {
2024-01-17 18:53:31 +01:00
static defaultScon: ShaderContext = null;
static defaultMcon: MaterialContext = null;
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
static heightUsed = false;
static emisUsed = false;
static subsUsed = false;
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
static getMOut = (): bool => {
for (let n of UINodes.inst.getCanvasMaterial().nodes) if (n.type == "OUTPUT_MATERIAL_PBR") return true;
2023-04-24 15:20:03 +02:00
return false;
}
2024-01-17 18:53:31 +01:00
static parseMeshMaterial = () => {
let m = Project.materials[0].data;
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
for (let c of m.shader.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "mesh") {
m.shader.raw.contexts.remove(c.raw);
m.shader.contexts.remove(c);
deleteContext(c);
break;
}
}
if (MakeMesh.layerPassCount > 1) {
2024-01-17 18:53:31 +01:00
let i = 0;
2023-04-24 15:20:03 +02:00
while (i < m.shader.contexts.length) {
2024-01-17 18:53:31 +01:00
let c = m.shader.contexts[i];
for (let j = 1; j < MakeMesh.layerPassCount; ++j) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "mesh" + j) {
m.shader.raw.contexts.remove(c.raw);
m.shader.contexts.remove(c);
deleteContext(c);
i--;
break;
}
}
i++;
}
i = 0;
while (i < m.contexts.length) {
2024-01-17 18:53:31 +01:00
let c = m.contexts[i];
for (let j = 1; j < MakeMesh.layerPassCount; ++j) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "mesh" + j) {
m.raw.contexts.remove(c.raw);
m.contexts.remove(c);
i--;
break;
}
}
i++;
}
}
2024-01-17 18:53:31 +01:00
let con = MakeMesh.run(new NodeShaderData({ name: "Material", canvas: null }));
let scon = new ShaderContext(con.data, (scon: ShaderContext) => {});
2023-04-24 15:20:03 +02:00
scon.overrideContext = {};
if (con.frag.sharedSamplers.length > 0) {
2024-01-17 18:53:31 +01:00
let sampler = con.frag.sharedSamplers[0];
2023-04-24 15:20:03 +02:00
scon.overrideContext.shared_sampler = sampler.substr(sampler.lastIndexOf(" ") + 1);
}
if (!Context.raw.textureFilter) {
scon.overrideContext.filter = "point";
}
m.shader.raw.contexts.push(scon.raw);
m.shader.contexts.push(scon);
2024-01-17 18:53:31 +01:00
for (let i = 1; i < MakeMesh.layerPassCount; ++i) {
let con = MakeMesh.run(new NodeShaderData({ name: "Material", canvas: null }), i);
let scon = new ShaderContext(con.data, (scon: ShaderContext) => {});
2023-04-24 15:20:03 +02:00
scon.overrideContext = {};
if (con.frag.sharedSamplers.length > 0) {
2024-01-17 18:53:31 +01:00
let sampler = con.frag.sharedSamplers[0];
2023-04-24 15:20:03 +02:00
scon.overrideContext.shared_sampler = sampler.substr(sampler.lastIndexOf(" ") + 1);
}
if (!Context.raw.textureFilter) {
scon.overrideContext.filter = "point";
}
m.shader.raw.contexts.push(scon.raw);
m.shader.contexts.push(scon);
2024-01-17 18:53:31 +01:00
let mcon = new MaterialContext({ name: "mesh" + i, bind_textures: [] }, (self: MaterialContext) => {});
2023-04-24 15:20:03 +02:00
m.raw.contexts.push(mcon.raw);
m.contexts.push(mcon);
}
Context.raw.ddirty = 2;
2024-01-17 18:53:31 +01:00
///if arm_voxels
2023-04-24 15:20:03 +02:00
makeVoxel(m);
2024-01-17 18:53:31 +01:00
///end
2023-04-24 15:20:03 +02:00
}
2024-01-17 18:53:31 +01:00
static parseParticleMaterial = () => {
let m = Context.raw.particleMaterial;
let sc: ShaderContext = null;
for (let c of m.shader.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "mesh") {
sc = c;
break;
}
}
if (sc != null) {
m.shader.raw.contexts.remove(sc.raw);
m.shader.contexts.remove(sc);
}
2024-01-17 18:53:31 +01:00
let con = MakeParticle.run(new NodeShaderData({ name: "MaterialParticle", canvas: null }));
2023-04-24 15:20:03 +02:00
if (sc != null) deleteContext(sc);
2024-01-17 18:53:31 +01:00
sc = new ShaderContext(con.data, (sc: ShaderContext) => {});
2023-04-24 15:20:03 +02:00
m.shader.raw.contexts.push(sc.raw);
m.shader.contexts.push(sc);
}
2024-01-17 18:53:31 +01:00
static parseMeshPreviewMaterial = () => {
2023-04-24 15:20:03 +02:00
if (!getMOut()) return;
2024-01-17 18:53:31 +01:00
let m = Project.materials[0].data;
let scon: ShaderContext = null;
for (let c of m.shader.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "mesh") {
scon = c;
break;
}
}
m.shader.raw.contexts.remove(scon.raw);
m.shader.contexts.remove(scon);
2024-01-17 18:53:31 +01:00
let mcon: TMaterialContext = { name: "mesh", bind_textures: [] };
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
let sd = new NodeShaderData({ name: "Material", canvas: null });
let con = MakeMeshPreview.run(sd, mcon);
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
for (let i = 0; i < m.contexts.length; ++i) {
2023-04-24 15:20:03 +02:00
if (m.contexts[i].raw.name == "mesh") {
2024-01-17 18:53:31 +01:00
m.contexts[i] = new MaterialContext(mcon, (self: MaterialContext) => {});
2023-04-24 15:20:03 +02:00
break;
}
}
if (scon != null) deleteContext(scon);
2024-01-17 18:53:31 +01:00
let compileError = false;
scon = new ShaderContext(con.data, (scon: ShaderContext) => {
2023-04-24 15:20:03 +02:00
if (scon == null) compileError = true;
});
if (compileError) return;
m.shader.raw.contexts.push(scon.raw);
m.shader.contexts.push(scon);
}
2024-01-17 18:53:31 +01:00
///if arm_voxels
static makeVoxel = (m: MaterialData) => {
let rebuild = heightUsed;
2023-04-24 15:20:03 +02:00
if (Config.raw.rp_gi != false && rebuild) {
2024-01-17 18:53:31 +01:00
let scon: ShaderContext = null;
for (let c of m.shader.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "voxel") {
scon = c;
break;
}
}
if (scon != null) MakeVoxel.run(scon);
}
}
2024-01-17 18:53:31 +01:00
///end
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
static parsePaintMaterial = (bakePreviews = true) => {
2023-04-24 15:20:03 +02:00
if (!getMOut()) return;
if (bakePreviews) {
2024-01-17 18:53:31 +01:00
let current = Graphics2.current;
2023-04-24 15:20:03 +02:00
if (current != null) current.end();
bakeNodePreviews();
if (current != null) current.begin(false);
}
2024-01-17 18:53:31 +01:00
let m = Project.materials[0].data;
let scon: ShaderContext = null;
let mcon: MaterialContext = null;
for (let c of m.shader.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "paint") {
m.shader.raw.contexts.remove(c.raw);
m.shader.contexts.remove(c);
if (c != defaultScon) deleteContext(c);
break;
}
}
2024-01-17 18:53:31 +01:00
for (let c of m.contexts) {
2023-04-24 15:20:03 +02:00
if (c.raw.name == "paint") {
m.raw.contexts.remove(c.raw);
m.contexts.remove(c);
break;
}
}
2024-01-17 18:53:31 +01:00
let sdata = new NodeShaderData({ name: "Material", canvas: UINodes.inst.getCanvasMaterial() });
let mcon: TMaterialContext = { name: "paint", bind_textures: [] };
let con = MakeSculpt.run(sdata, mcon);
2023-04-24 15:20:03 +02:00
2024-01-17 18:53:31 +01:00
let compileError = false;
let scon = new ShaderContext(con.data, (scon: ShaderContext) => {
2023-04-24 15:20:03 +02:00
if (scon == null) compileError = true;
});
if (compileError) return;
scon.overrideContext = {};
scon.overrideContext.addressing = "repeat";
2024-01-17 18:53:31 +01:00
let mcon = new MaterialContext(mcon, (mcon: MaterialContext) => {});
2023-04-24 15:20:03 +02:00
m.shader.raw.contexts.push(scon.raw);
m.shader.contexts.push(scon);
m.raw.contexts.push(mcon.raw);
m.contexts.push(mcon);
if (defaultScon == null) defaultScon = scon;
if (defaultMcon == null) defaultMcon = mcon;
}
2024-01-17 18:53:31 +01:00
static bakeNodePreviews = () => {
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviewsUsed = [];
if (Context.raw.nodePreviews == null) Context.raw.nodePreviews = [];
traverseNodes(UINodes.inst.getCanvasMaterial().nodes, null, []);
2024-01-17 18:53:31 +01:00
for (let key of Context.raw.nodePreviews.keys()) {
2023-04-24 15:20:03 +02:00
if (Context.raw.nodePreviewsUsed.indexOf(key) == -1) {
2024-01-17 18:53:31 +01:00
let image = Context.raw.nodePreviews.get(key);
2023-12-12 14:57:44 +01:00
Base.notifyOnNextFrame(image.unload);
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviews.remove(key);
}
}
}
2024-01-17 18:53:31 +01:00
static traverseNodes = (nodes: TNode[], group: TNodeCanvas, parents: TNode[]) => {
for (let node of nodes) {
2023-04-24 15:20:03 +02:00
bakeNodePreview(node, group, parents);
if (node.type == "GROUP") {
2024-01-17 18:53:31 +01:00
for (let g of Project.materialGroups) {
2023-04-24 15:20:03 +02:00
if (g.canvas.name == node.name) {
parents.push(node);
traverseNodes(g.canvas.nodes, g.canvas, parents);
parents.pop();
break;
}
}
}
}
}
2024-01-17 18:53:31 +01:00
static bakeNodePreview = (node: TNode, group: TNodeCanvas, parents: TNode[]) => {
2023-04-24 15:20:03 +02:00
if (node.type == "BLUR") {
2024-01-17 18:53:31 +01:00
let id = ParserMaterial.node_name(node, parents);
let image = Context.raw.nodePreviews.get(id);
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviewsUsed.push(id);
2024-01-17 18:53:31 +01:00
let resX = Math.floor(Config.getTextureResX() / 4);
let resY = Math.floor(Config.getTextureResY() / 4);
2023-04-24 15:20:03 +02:00
if (image == null || image.width != resX || image.height != resY) {
if (image != null) image.unload();
2023-12-12 14:57:44 +01:00
image = Image.createRenderTarget(resX, resY);
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviews.set(id, image);
}
2023-12-13 15:52:41 +01:00
ParserMaterial.blur_passthrough = true;
UtilRender.makeNodePreview(UINodes.inst.getCanvasMaterial(), node, image, group, parents);
ParserMaterial.blur_passthrough = false;
2023-04-24 15:20:03 +02:00
}
else if (node.type == "DIRECT_WARP") {
2024-01-17 18:53:31 +01:00
let id = ParserMaterial.node_name(node, parents);
let image = Context.raw.nodePreviews.get(id);
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviewsUsed.push(id);
2024-01-17 18:53:31 +01:00
let resX = Math.floor(Config.getTextureResX());
let resY = Math.floor(Config.getTextureResY());
2023-04-24 15:20:03 +02:00
if (image == null || image.width != resX || image.height != resY) {
if (image != null) image.unload();
2023-12-12 14:57:44 +01:00
image = Image.createRenderTarget(resX, resY);
2023-04-24 15:20:03 +02:00
Context.raw.nodePreviews.set(id, image);
}
2023-12-13 15:52:41 +01:00
ParserMaterial.warp_passthrough = true;
UtilRender.makeNodePreview(UINodes.inst.getCanvasMaterial(), node, image, group, parents);
ParserMaterial.warp_passthrough = false;
2023-04-24 15:20:03 +02:00
}
}
2024-01-17 18:53:31 +01:00
static parseNodePreviewMaterial = (node: TNode, group: TNodeCanvas = null, parents: TNode[] = null): { scon: ShaderContext, mcon: MaterialContext } => {
2023-04-24 15:20:03 +02:00
if (node.outputs.length == 0) return null;
2024-01-17 18:53:31 +01:00
let sdata = new NodeShaderData({ name: "Material", canvas: UINodes.inst.getCanvasMaterial() });
let mcon_raw: TMaterialContext = { name: "mesh", bind_textures: [] };
let con = MakeNodePreview.run(sdata, mcon_raw, node, group, parents);
let compileError = false;
let scon = new ShaderContext(con.data, (scon: ShaderContext) => {
2023-04-24 15:20:03 +02:00
if (scon == null) compileError = true;
});
if (compileError) return null;
2024-01-17 18:53:31 +01:00
let mcon = new MaterialContext(mcon_raw, (mcon: MaterialContext) => {});
2023-04-24 15:20:03 +02:00
return { scon: scon, mcon: mcon };
}
2024-01-17 18:53:31 +01:00
static parseBrush = () => {
2023-12-13 15:52:41 +01:00
ParserLogic.parse(Context.raw.brush.canvas);
2023-04-24 15:20:03 +02:00
}
2024-01-17 18:53:31 +01:00
static getDisplaceStrength = (): f32 => {
let sc = Context.mainObject().transform.scale.x;
2023-04-24 15:20:03 +02:00
return Config.raw.displace_strength * 0.02 * sc;
}
2024-01-17 18:53:31 +01:00
static voxelgiHalfExtents = (): string => {
let ext = Context.raw.vxaoExt;
return `const vec3 voxelgiHalfExtents = vec3(${ext}, ${ext}, ${ext});`;
2023-04-24 15:20:03 +02:00
}
2024-01-17 18:53:31 +01:00
static deleteContext = (c: ShaderContext) => {
Base.notifyOnNextFrame(() => { // Ensure pipeline is no longer in use
2023-04-24 15:20:03 +02:00
c.delete();
});
}
}