From fcc76b257ef5cfb4514669df3b0144f8e8dd76ef Mon Sep 17 00:00:00 2001 From: yum Date: Fri, 2 Jan 2026 16:21:34 -0800 Subject: Fold: implement dynamic branching in shader Switch from static branching approach using per-op data and fixed execution order to dynamic branching approach using dynamic data. This confers the main benefit of letting us dynamically reorder ops and duplicate them. It also lets us eliminate some of the plumbing tedium whenever we want to add new ops. --- Scripts/Fold/Editor/KeyframeNodeView.cs | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'Scripts/Fold/Editor/KeyframeNodeView.cs') diff --git a/Scripts/Fold/Editor/KeyframeNodeView.cs b/Scripts/Fold/Editor/KeyframeNodeView.cs index fd90c75..225b5b6 100644 --- a/Scripts/Fold/Editor/KeyframeNodeView.cs +++ b/Scripts/Fold/Editor/KeyframeNodeView.cs @@ -2,6 +2,7 @@ using GraphProcessor; using UnityEngine.UIElements; using UnityEngine; using System.Linq; +using System.Collections.Generic; [NodeCustomEditor(typeof(KeyframeNode))] public class KeyframeNodeView : BaseNodeView @@ -25,10 +26,58 @@ public class KeyframeNodeView : BaseNodeView void OnGenerateClick() { var go = GetConnectedGameObject(); - if (go?.GetComponent() == null) return; + if (go == null) return; + var rend = go.GetComponent(); + if (rend == null) return; var foldNode = GetConnectedFoldNode(); - Debug.Log($"Generating Keyframe for '{go.name}' using Fold Data: {(foldNode?.name ?? "None")}"); + + // Accumulate nodes. We are going backwards from the output node + // (KeyframeNode) to the root node. + var foldNodes = new Stack(); + var cur = foldNode; + while (cur != null) { + foldNodes.Push(cur); + cur = GetInputFoldNode(cur); + } + + var mpb = new MaterialPropertyBlock(); + rend.GetPropertyBlock(mpb); + + // Set material properties for all 16 slots. + for (int i = 0; i < 16; i++) { + string slotPrefix = $"_Vertex_Deformation_Slot_{i}_"; + BaseFoldNode node = (foldNodes.Count > 0) ? foldNodes.Pop() : null; + FoldNodeSerialized data = node?.Serialize(); + + bool active = data != null; + mpb.SetFloat(slotPrefix + "Enabled", active ? 1.0f : 0.0f); + mpb.SetInt(slotPrefix + "Opcode", active ? data.opcode : 0); + + mpb.SetFloat(slotPrefix + "Float_0", active ? data.float0 : 0.0f); + mpb.SetFloat(slotPrefix + "Float_1", active ? data.float1 : 0.0f); + mpb.SetFloat(slotPrefix + "Float_2", active ? data.float2 : 0.0f); + mpb.SetFloat(slotPrefix + "Float_3", active ? data.float3 : 0.0f); + + mpb.SetVector(slotPrefix + "Vector_0", active ? data.vec0 : Vector4.zero); + mpb.SetVector(slotPrefix + "Vector_1", active ? data.vec1 : Vector4.zero); + mpb.SetVector(slotPrefix + "Vector_2", active ? data.vec2 : Vector4.zero); + mpb.SetVector(slotPrefix + "Vector_3", active ? data.vec3 : Vector4.zero); + } + + rend.SetPropertyBlock(mpb); + Debug.Log($"Generated Keyframe for '{go.name}' with properties applied to all 16 slots."); + } + + BaseFoldNode GetInputFoldNode(BaseFoldNode node) + { + // Traverse via edges in the graph model, as 'input' field might not be populated in Editor + var port = node.GetPort(nameof(BaseFoldNode.input), null); + if (port == null) return null; + + // Find the edge connected to this input port + var edge = node.graph.edges.FirstOrDefault(e => e.inputPort == port); + return edge?.outputNode as BaseFoldNode; } T GetConnectedNode(string fieldName) where T : class -- cgit v1.2.3