|
| 1 | +using GraphProcessor; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.Rendering; |
| 6 | + |
| 7 | +namespace Mixture |
| 8 | +{ |
| 9 | + [System.Serializable, NodeMenuItem("Subgraph")] |
| 10 | + public class SubgraphNode : MixtureNode, ICreateNodeFrom<CustomRenderTexture> |
| 11 | + { |
| 12 | + // Placeholders for custom port behaviours |
| 13 | + [Input, System.NonSerialized] public int subgraphInputs; |
| 14 | + [Output, System.NonSerialized] public int subgraphOutputs; |
| 15 | + |
| 16 | + public MixtureGraph subgraph; |
| 17 | + public CustomRenderTexture subgraphTexture; |
| 18 | + public int previewOutputIndex = -1; |
| 19 | + |
| 20 | + List<CustomRenderTexture> _outputTextures = new List<CustomRenderTexture>(); |
| 21 | + |
| 22 | + public override string name => subgraph?.name ?? "Subgraph"; |
| 23 | + public override bool isRenamable => true; |
| 24 | + public override bool hasPreview => -1 < previewOutputIndex && previewOutputIndex < _outputTextures.Count; |
| 25 | + public override Texture previewTexture => hasPreview ? _outputTextures[previewOutputIndex] : null; |
| 26 | + |
| 27 | + public bool InitializeNodeFromObject(CustomRenderTexture texture) |
| 28 | + { |
| 29 | + subgraph = MixtureDatabase.GetGraphFromTexture(texture); |
| 30 | + |
| 31 | + if (subgraph == null) return false; |
| 32 | + |
| 33 | + subgraphTexture = texture; |
| 34 | + previewOutputIndex = Mathf.Clamp(previewOutputIndex, 0, subgraph.outputNode.outputTextureSettings.Count - 1); |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + [CustomPortBehavior(nameof(subgraphInputs))] |
| 40 | + public IEnumerable<PortData> ListGraphInputs(List<SerializableEdge> edges) |
| 41 | + { |
| 42 | + if (subgraph == null || subgraph == graph) yield break; |
| 43 | + |
| 44 | + for (var i = 0; i < subgraph.exposedParameters.Count; i++) |
| 45 | + { |
| 46 | + var parameter = subgraph.exposedParameters[i]; |
| 47 | + |
| 48 | + yield return new PortData |
| 49 | + { |
| 50 | + identifier = System.Convert.ToString(i), |
| 51 | + displayName = parameter.name, |
| 52 | + displayType = parameter.GetValueType(), |
| 53 | + acceptMultipleEdges = false, |
| 54 | + }; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [CustomPortBehavior(nameof(subgraphOutputs))] |
| 59 | + public IEnumerable<PortData> ListGraphOutputs(List<SerializableEdge> edges) |
| 60 | + { |
| 61 | + if (subgraph == null || subgraph == graph) yield break; |
| 62 | + |
| 63 | + var settings = subgraph.outputNode.outputTextureSettings; |
| 64 | + var textureType = GetSubgraphTextureType(); |
| 65 | + |
| 66 | + for (var i = 0; i < settings.Count; i++) |
| 67 | + { |
| 68 | + yield return new PortData |
| 69 | + { |
| 70 | + identifier = System.Convert.ToString(i), |
| 71 | + displayName = settings[i].name, |
| 72 | + displayType = textureType, |
| 73 | + acceptMultipleEdges = true, |
| 74 | + }; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [CustomPortInput(nameof(subgraphInputs), typeof(object))] |
| 79 | + public void AssignGraphInputs(List<SerializableEdge> edges) |
| 80 | + { |
| 81 | + foreach (var edge in edges) |
| 82 | + { |
| 83 | + var index = System.Convert.ToInt32(edge.inputPortIdentifier); |
| 84 | + var parameter = subgraph.exposedParameters[index]; |
| 85 | + |
| 86 | + switch (edge.passThroughBuffer) |
| 87 | + { |
| 88 | + case float v: parameter.value = CoerceVectorValue(parameter, new Vector4(v, v, v, v)); break; |
| 89 | + case Vector2 v: parameter.value = CoerceVectorValue(parameter, v); break; |
| 90 | + case Vector3 v: parameter.value = CoerceVectorValue(parameter, v); break; |
| 91 | + case Vector4 v: parameter.value = CoerceVectorValue(parameter, v); break; |
| 92 | + default: parameter.value = edge.passThroughBuffer; break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [CustomPortOutput(nameof(subgraphOutputs), typeof(object))] |
| 98 | + public void AssignGraphOutputs(List<SerializableEdge> edges) |
| 99 | + { |
| 100 | + foreach (var edge in edges) |
| 101 | + { |
| 102 | + var index = System.Convert.ToInt32(edge.outputPortIdentifier); |
| 103 | + edge.passThroughBuffer = _outputTextures[index]; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public void UpdateOutputTextures() |
| 108 | + { |
| 109 | + ReleaseOutputTextures(); |
| 110 | + |
| 111 | + if (subgraph != null && subgraph != graph) GenerateOutputTextures(); |
| 112 | + } |
| 113 | + |
| 114 | + public void GenerateOutputTextures() |
| 115 | + { |
| 116 | + var settings = subgraph.outputNode.outputTextureSettings; |
| 117 | + |
| 118 | + _outputTextures.Capacity = Mathf.Max(_outputTextures.Capacity, settings.Count); |
| 119 | + |
| 120 | + foreach (var setting in settings) |
| 121 | + { |
| 122 | + CustomRenderTexture outputTexture = null; |
| 123 | + UpdateTempRenderTexture(ref outputTexture); |
| 124 | + _outputTextures.Add(outputTexture); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void ReleaseOutputTextures() |
| 129 | + { |
| 130 | + foreach (var texture in _outputTextures) texture?.Release(); |
| 131 | + |
| 132 | + _outputTextures.Clear(); |
| 133 | + } |
| 134 | + |
| 135 | + protected override void Enable() |
| 136 | + { |
| 137 | + base.Enable(); |
| 138 | + |
| 139 | + UpdateOutputTextures(); |
| 140 | + } |
| 141 | + |
| 142 | + protected override void Disable() |
| 143 | + { |
| 144 | + base.Disable(); |
| 145 | + |
| 146 | + ReleaseOutputTextures(); |
| 147 | + } |
| 148 | + |
| 149 | + public override bool canProcess => base.canProcess && subgraph != null && subgraph != graph; |
| 150 | + |
| 151 | + protected override bool ProcessNode(CommandBuffer cmd) |
| 152 | + { |
| 153 | + if (!base.ProcessNode(cmd)) return false; |
| 154 | + |
| 155 | + MixtureGraphProcessor.RunOnce(subgraph); |
| 156 | + |
| 157 | + using (var copyCmd = new CommandBuffer { name = $"{graph.name}/{subgraph.name}" }) |
| 158 | + { |
| 159 | + for (int i = 0; i < _outputTextures.Count; i++) |
| 160 | + { |
| 161 | + var outputTexture = _outputTextures[i]; |
| 162 | + UpdateTempRenderTexture(ref outputTexture); |
| 163 | + copyCmd.Blit(subgraph.outputNode.outputTextureSettings[i].finalCopyRT, outputTexture); |
| 164 | + } |
| 165 | + |
| 166 | + Graphics.ExecuteCommandBuffer(copyCmd); |
| 167 | + } |
| 168 | + |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + System.Type GetSubgraphTextureType() |
| 173 | + { |
| 174 | + var textureDimension = subgraph.settings.GetResolvedTextureDimension(subgraph); |
| 175 | + |
| 176 | + switch (textureDimension) |
| 177 | + { |
| 178 | + case UnityEngine.Rendering.TextureDimension.Tex2D: return typeof(Texture2D); |
| 179 | + case UnityEngine.Rendering.TextureDimension.Tex3D: return typeof(Texture3D); |
| 180 | + case UnityEngine.Rendering.TextureDimension.Cube: return typeof(Cubemap); |
| 181 | + default: throw new System.Exception($"Texture dimension not supported: {textureDimension}"); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + object CoerceVectorValue(ExposedParameter parameter, Vector4 vector) |
| 186 | + { |
| 187 | + switch (parameter.value) |
| 188 | + { |
| 189 | + case float: return vector.x; |
| 190 | + case Vector2: return (Vector2)vector; |
| 191 | + case Vector3: return (Vector3)vector; |
| 192 | + case Vector4: return vector; |
| 193 | + default: throw new System.Exception($"Cannot cast vector to {parameter.GetValueType()}"); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments