Files
workinf_Blender_Wasm/web/protocol/shader-graph-commands.ts
mes123456 4eb08faf31 chore(web): checkpoint export fixes, operator routing and three-domain evidence
- Web engine CMake: apply WITH_WEB_USD_PYTHON to blender_web_engine_core,
  embed /usd resources, link pxr Python bindings after the core archive and
  whole-archive the monolithic OpenUSD archive. Fixes the wm.usd_export abort
  and restores Alembic/USD embedded export.
- ?ui=web menus: fix inverted localOnly gating and route queueLocalDesktopOperator
  and nativeAdd through the desktop operator channel with a VIEW_3D context;
  non-mesh edit-mode selection uses the native VIEW3D_SELECT bridge.
- Refresh WebEngine wasm/js artifacts, manual/course pages and the complete
  course video asset.
- Record focused Chromium probes for Add/F3/parameters/modifier/UV/curve/deform,
  scene operations, OPFS save/reopen and worker restart. Evidence is NOT_CLAIMED.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-14 06:12:44 +08:00

97 lines
8.1 KiB
TypeScript

import { SHADER_GRAPH_SCHEMA, type ShaderGraphIR, type ShaderLinkIR, type ShaderNodeIR, type ShaderNodeType, type ShaderSocketIR } from "./shader-graph";
const input = (id: string, name: string, dataType: ShaderSocketIR["dataType"], defaultValue?: ShaderSocketIR["defaultValue"]): ShaderSocketIR => ({ id, name, direction: "INPUT", dataType, ...(defaultValue === undefined ? {} : { defaultValue }) });
const output = (id: string, name: string, dataType: ShaderSocketIR["dataType"]): ShaderSocketIR => ({ id, name, direction: "OUTPUT", dataType });
const clone = <T,>(value: T): T => JSON.parse(JSON.stringify(value)) as T;
const nodeSockets = (type: ShaderNodeType): ShaderSocketIR[] => {
if (type === "RGB") return [output("Color", "Color", "COLOR")];
if (type === "VALUE") return [output("Value", "Value", "VALUE")];
if (type === "MATH") return [input("Value", "Value", "VALUE", 0), input("Value_001", "Value", "VALUE", 0), output("Value", "Value", "VALUE")];
if (type === "PRINCIPLED") return [input("Base Color", "Base Color", "COLOR", [0.8, 0.8, 0.8, 1]), input("Metallic", "Metallic", "VALUE", 0), input("Roughness", "Roughness", "VALUE", 0.4), output("BSDF", "BSDF", "SHADER")];
if (type === "MATERIAL_OUTPUT") return [input("Surface", "Surface", "SHADER")];
if (type === "IMAGE_TEXTURE") return [input("Vector", "Vector", "VECTOR", [0, 0, 0]), output("Color", "Color", "COLOR")];
if (type === "MAPPING") return [input("Vector", "Vector", "VECTOR", [0, 0, 0]), input("Location", "Location", "VECTOR", [0, 0, 0]), input("Rotation", "Rotation", "VECTOR", [0, 0, 0]), input("Scale", "Scale", "VECTOR", [1, 1, 1]), output("Vector", "Vector", "VECTOR")];
if (type === "TEX_COORD") return ["Generated", "Normal", "UV", "Object", "Camera", "Window", "Reflection"].map((name) => output(name, name, "VECTOR"));
if (type === "UV_MAP") return [output("UV", "UV", "VECTOR")];
if (type === "BUMP") return [input("Strength", "Strength", "VALUE", 1), input("Distance", "Distance", "VALUE", 1), input("Height", "Height", "VALUE", 1), input("Normal", "Normal", "VECTOR", [0, 0, 0]), output("Normal", "Normal", "VECTOR")];
if (type === "SEPARATE_COLOR") return [input("Color", "Color", "COLOR", [0, 0, 0, 1]), output("Red", "Red", "VALUE"), output("Green", "Green", "VALUE"), output("Blue", "Blue", "VALUE")];
if (type === "OBJECT_INFO") return [output("Location", "Location", "VECTOR"), output("Color", "Color", "COLOR"), output("Alpha", "Alpha", "VALUE"), output("Object Index", "Object Index", "VALUE"), output("Material Index", "Material Index", "VALUE"), output("Random", "Random", "VALUE")];
if (type === "MIX") return [input("Factor_Float", "Factor", "VALUE", 0.5), input("A_Float", "A", "VALUE", 0), input("B_Float", "B", "VALUE", 0), output("Result_Float", "Result", "VALUE")];
if (type === "DIFFUSE") return [input("Color", "Color", "COLOR", [0.8, 0.8, 0.8, 1]), input("Roughness", "Roughness", "VALUE", 0), input("Normal", "Normal", "VECTOR", [0, 0, 0]), output("BSDF", "BSDF", "SHADER")];
if (type === "GLOSSY") return [input("Color", "Color", "COLOR", [0.8, 0.8, 0.8, 1]), input("Roughness", "Roughness", "VALUE", 0.2), input("Normal", "Normal", "VECTOR", [0, 0, 0]), output("BSDF", "BSDF", "SHADER")];
if (type === "EMISSION") return [input("Color", "Color", "COLOR", [1, 1, 1, 1]), input("Strength", "Strength", "VALUE", 1), output("Emission", "Emission", "SHADER")];
if (type === "MIX_SHADER") return [input("Fac", "Fac", "VALUE", 0.5), input("Shader", "Shader", "SHADER"), input("Shader_001", "Shader", "SHADER"), output("Shader", "Shader", "SHADER")];
if (type === "ADD_SHADER") return [input("Shader", "Shader", "SHADER"), input("Shader_001", "Shader", "SHADER"), output("Shader", "Shader", "SHADER")];
if (type === "NOISE") return [input("Vector", "Vector", "VECTOR", [0, 0, 0]), input("Scale", "Scale", "VALUE", 5), input("Detail", "Detail", "VALUE", 2), input("Roughness", "Roughness", "VALUE", 0.5), input("Distortion", "Distortion", "VALUE", 0), output("Fac", "Fac", "VALUE"), output("Color", "Color", "COLOR")];
if (type === "GRADIENT") return [input("Vector", "Vector", "VECTOR", [0, 0, 0]), output("Fac", "Fac", "VALUE"), output("Color", "Color", "COLOR")];
if (type === "CHECKER") return [input("Vector", "Vector", "VECTOR", [0, 0, 0]), input("Color1", "Color1", "COLOR", [0.8, 0.8, 0.8, 1]), input("Color2", "Color2", "COLOR", [0.2, 0.2, 0.2, 1]), input("Scale", "Scale", "VALUE", 5), output("Color", "Color", "COLOR"), output("Fac", "Fac", "VALUE")];
if (type === "FRESNEL") return [input("IOR", "IOR", "VALUE", 1.45), input("Normal", "Normal", "VECTOR", [0, 0, 0]), output("Fac", "Fac", "VALUE")];
if (type === "LAYER_WEIGHT") return [input("Blend", "Blend", "VALUE", 0.5), input("Normal", "Normal", "VECTOR", [0, 0, 0]), output("Fresnel", "Fresnel", "VALUE"), output("Facing", "Facing", "VALUE")];
return [];
};
function uniqueId(base: string, ids: ReadonlySet<string>): string {
let id = base;
let suffix = 2;
while (ids.has(id)) id = `${base}-${suffix++}`;
return id;
}
/** Build a valid bounded graph for the source-backed Principled writer. */
export const shaderGraphCommands = Object.freeze({
addNode(graph: ShaderGraphIR, type: ShaderNodeType, name: string = type): ShaderGraphIR {
const next = clone(graph);
const id = uniqueId(`node-${type.toLowerCase()}`, new Set(next.nodes.map((node) => node.id)));
const node: ShaderNodeIR = { id, type, name: name.trim() || type, sockets: nodeSockets(type), ...(type === "MATH" ? { properties: { operation: "ADD" } } : {}) };
next.nodes.push(node);
if (type === "MATERIAL_OUTPUT") next.outputNodeId = id;
return next;
},
removeNode(graph: ShaderGraphIR, nodeId: string): ShaderGraphIR {
const next = clone(graph);
next.nodes = next.nodes.filter((node) => node.id !== nodeId);
next.links = next.links.filter((link) => link.fromNodeId !== nodeId && link.toNodeId !== nodeId);
if (next.outputNodeId === nodeId) next.outputNodeId = next.nodes.find((node) => node.type === "MATERIAL_OUTPUT")?.id;
return next;
},
connect(graph: ShaderGraphIR, link: ShaderLinkIR): ShaderGraphIR {
const next = clone(graph);
next.links = next.links.filter((item) => !(item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId));
if (!next.links.some((item) => item.fromNodeId === link.fromNodeId && item.fromSocketId === link.fromSocketId && item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId)) next.links.push(link);
return next;
},
disconnect(graph: ShaderGraphIR, link: ShaderLinkIR): ShaderGraphIR {
const next = clone(graph);
next.links = next.links.filter((item) => !(item.fromNodeId === link.fromNodeId && item.fromSocketId === link.fromSocketId && item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId));
return next;
},
setSocketValue(graph: ShaderGraphIR, nodeId: string, socketId: string, value: ShaderSocketIR["defaultValue"]): ShaderGraphIR {
const next = clone(graph);
const node = next.nodes.find((candidate) => candidate.id === nodeId);
const target = node?.sockets.find((candidate) => candidate.id === socketId && candidate.direction === "INPUT");
if (target) target.defaultValue = value;
return next;
},
setPrincipledGraph(materialId: string, baseColor: readonly number[], roughness: number, metallic: number): ShaderGraphIR {
const principled = "shader:principled";
const materialOutput = "shader:material-output";
return {
schemaVersion: SHADER_GRAPH_SCHEMA,
id: `shader-graph:${materialId}`,
materialId,
nodes: [
{ id: principled, type: "PRINCIPLED", name: "Principled BSDF", sockets: [
input("Base Color", "Base Color", "COLOR", [...baseColor].slice(0, 4)),
input("Metallic", "Metallic", "VALUE", metallic),
input("Roughness", "Roughness", "VALUE", roughness),
output("BSDF", "BSDF", "SHADER"),
] },
{ id: materialOutput, type: "MATERIAL_OUTPUT", name: "Material Output", sockets: [input("Surface", "Surface", "SHADER")] },
],
links: [{ fromNodeId: principled, fromSocketId: "BSDF", toNodeId: materialOutput, toSocketId: "Surface" }],
outputNodeId: materialOutput,
};
},
} as const);