Files
armorpaint/paint/sources/make_mesh_preview.ts
T

118 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-03-14 15:31:22 +01:00
let make_mesh_preview_opacity_discard_decal: f32 = 0.05;
2024-03-20 16:13:54 +01:00
function make_mesh_preview_run(data: material_t, matcon: material_context_t): node_shader_context_t {
2025-10-15 18:39:55 +02:00
let context_id: string = "mesh";
2024-04-25 22:04:43 +02:00
let props: shader_context_t = {
2025-10-15 18:39:55 +02:00
name : context_id,
depth_write : true,
compare_mode : "less",
cull_mode : "clockwise",
vertex_elements : [ {name : "pos", data : "short4norm"}, {name : "nor", data : "short2norm"}, {name : "tex", data : "short2norm"} ],
color_attachments : [ "RGBA64", "RGBA64" ],
depth_attachment : "D32"
2024-04-25 22:04:43 +02:00
};
let con_mesh: node_shader_context_t = node_shader_context_create(data, props);
2024-03-14 15:31:22 +01:00
2025-04-02 22:52:04 +02:00
let kong: node_shader_t = node_shader_context_make_kong(con_mesh);
2025-04-04 23:38:01 +02:00
let pos: string = "input.pos";
2024-03-14 15:31:22 +01:00
2025-04-04 23:38:01 +02:00
node_shader_add_constant(kong, "WVP: float4x4", "_world_view_proj_matrix");
node_shader_write_attrib_vert(kong, "output.pos = constants.WVP * float4(" + pos + ".xyz, 1.0);");
2024-03-14 15:31:22 +01:00
2025-10-15 18:39:55 +02:00
let sc: f32 = context_raw.brush_scale * context_raw.brush_nodes_scale;
2024-05-03 21:29:39 +02:00
let brush_scale: string = sc + "";
2025-04-03 21:29:51 +02:00
node_shader_add_out(kong, "tex_coord: float2");
2025-04-04 23:38:01 +02:00
node_shader_write_attrib_vert(kong, "output.tex_coord = input.tex * float(" + brush_scale + ");");
2025-07-18 16:50:01 +02:00
node_shader_write_attrib_frag(kong, "var tex_coord: float2 = input.tex_coord;");
2024-03-14 15:31:22 +01:00
2025-10-15 18:39:55 +02:00
let decal: bool = context_raw.decal_preview;
parser_material_sample_keep_aspect = decal;
parser_material_sample_uv_scale = brush_scale;
parser_material_parse_height = make_material_height_used;
2024-03-14 15:31:22 +01:00
parser_material_parse_height_as_channel = true;
2025-10-15 18:39:55 +02:00
let sout: shader_out_t = parser_material_parse(context_raw.material.canvas, con_mesh, kong, matcon);
parser_material_parse_height = false;
2024-03-14 15:31:22 +01:00
parser_material_parse_height_as_channel = false;
2025-10-15 18:39:55 +02:00
parser_material_sample_keep_aspect = false;
let base: string = sout.out_basecol;
let rough: string = sout.out_roughness;
let met: string = sout.out_metallic;
let occ: string = sout.out_occlusion;
let opac: string = sout.out_opacity;
let height: string = sout.out_height;
let nortan: string = parser_material_out_normaltan;
2025-04-05 22:22:43 +02:00
node_shader_write_frag(kong, "var basecol: float3 = pow3(" + base + ", float3(2.2, 2.2, 2.2));");
2025-04-02 22:52:04 +02:00
node_shader_write_frag(kong, "var roughness: float = " + rough + ";");
node_shader_write_frag(kong, "var metallic: float = " + met + ";");
node_shader_write_frag(kong, "var occlusion: float = " + occ + ";");
node_shader_write_frag(kong, "var opacity: float = " + opac + ";");
node_shader_write_frag(kong, "var nortan: float3 = " + nortan + ";");
node_shader_write_frag(kong, "var height: float = " + height + ";");
2024-03-14 15:31:22 +01:00
if (decal) {
2025-08-07 19:43:11 +02:00
if (context_raw.tool == tool_type_t.TEXT) {
2025-04-04 23:38:01 +02:00
node_shader_add_texture(kong, "textexttool", "_textexttool");
2025-07-18 16:50:01 +02:00
node_shader_write_frag(kong, "opacity *= sample_lod(textexttool, sampler_linear, tex_coord / float(" + brush_scale + "), 0.0).r;");
2024-03-14 15:31:22 +01:00
}
}
if (decal) {
let opac: f32 = make_mesh_preview_opacity_discard_decal;
2025-04-02 22:52:04 +02:00
node_shader_write_frag(kong, "if (opacity < " + opac + ") { discard; }");
2024-03-14 15:31:22 +01:00
}
2025-08-20 17:18:03 +02:00
kong.frag_out = "float4[2]";
2025-10-15 18:39:55 +02:00
kong.frag_n = true;
2024-03-14 15:31:22 +01:00
2025-04-02 22:52:04 +02:00
node_shader_add_function(kong, str_pack_float_int16);
node_shader_add_function(kong, str_cotangent_frame);
node_shader_add_function(kong, str_octahedron_wrap);
2024-03-14 15:31:22 +01:00
if (make_material_height_used) {
2025-04-02 22:52:04 +02:00
node_shader_write_frag(kong, "if (height > 0.0) {");
2025-04-03 21:29:51 +02:00
node_shader_write_frag(kong, "var height_dx: float = ddx(height * 2.0);");
node_shader_write_frag(kong, "var height_dy: float = ddy(height * 2.0);");
2024-03-14 15:31:22 +01:00
// Whiteout blend
2025-04-02 22:52:04 +02:00
node_shader_write_frag(kong, "var n1: float3 = nortan * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
node_shader_write_frag(kong, "var n2: float3 = normalize(float3(height_dx * 16.0, height_dy * 16.0, 1.0));");
node_shader_write_frag(kong, "nortan = normalize(float3(n1.xy + n2.xy, n1.z * n2.z)) * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5);");
node_shader_write_frag(kong, "}");
2024-03-14 15:31:22 +01:00
}
// Apply normal channel
if (decal) {
// TODO
}
else {
2025-04-02 22:52:04 +02:00
kong.frag_vvec = true;
2025-07-18 16:50:01 +02:00
node_shader_write_frag(kong, "var TBN: float3x3 = cotangent_frame(n, vvec, tex_coord);");
2025-04-02 22:52:04 +02:00
node_shader_write_frag(kong, "n = nortan * 2.0 - 1.0;");
node_shader_write_frag(kong, "n.y = -n.y;");
node_shader_write_frag(kong, "n = normalize(TBN * n);");
2024-03-14 15:31:22 +01:00
}
2025-06-08 21:44:05 +02:00
node_shader_write_frag(kong, "n = n / (abs(n.x) + abs(n.y) + abs(n.z));");
2025-04-03 21:29:51 +02:00
// node_shader_write_frag(kong, "n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);");
node_shader_write_frag(kong, "if (n.z < 0.0) { n.xy = octahedron_wrap(n.xy); }");
2024-11-14 23:12:30 +01:00
// uint matid = uint(0);
2024-03-14 15:31:22 +01:00
if (decal) {
2025-04-11 10:09:04 +02:00
node_shader_write_frag(kong, "output[0] = float4(n.x, n.y, roughness, pack_f32_i16(metallic, uint(0)));"); // metallic/matid
node_shader_write_frag(kong, "output[1] = float4(basecol, occlusion);");
2024-03-14 15:31:22 +01:00
}
else {
2025-10-15 18:39:55 +02:00
node_shader_write_frag(
kong, "output[0] = float4(n.x, n.y, lerp(1.0, roughness, opacity), pack_f32_i16(lerp(1.0, metallic, opacity), uint(0)));"); // metallic/matid
2025-04-11 10:09:04 +02:00
node_shader_write_frag(kong, "output[1] = float4(lerp3(float3(0.0, 0.0, 0.0), basecol, opacity), occlusion);");
2024-03-14 15:31:22 +01:00
}
parser_material_finalize(con_mesh);
con_mesh.data.shader_from_source = true;
2025-10-15 18:39:55 +02:00
gpu_create_shaders_from_kong(node_shader_get(kong), ADDRESS(con_mesh.data.vertex_shader), ADDRESS(con_mesh.data.fragment_shader),
ADDRESS(con_mesh.data._.vertex_shader_size), ADDRESS(con_mesh.data._.fragment_shader_size));
2024-03-14 15:31:22 +01:00
return con_mesh;
}