Created
July 28, 2026 12:43
-
-
Save banjun/aaccda556335a26cafd2ccffc88f7afd to your computer and use it in GitHub Desktop.
RealityKit ShaderGraph Node Investigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| import RealityKit | |
| struct ContentView: View { | |
| @State private var defs: [ShaderGraph.NodeDefinition] = [] | |
| @State private var version: ShaderGraph.NodeLibrary.Version = .default | |
| @State private var searchText: String = "" | |
| @State private var platform: ShaderGraph.NodeDefinition.Platform = .visionOS | |
| @State private var osVersion: Int = 27 | |
| @State private var groups: [String] = [] | |
| @State private var group: String? | |
| @FocusState private var searchFocus | |
| var body: some View { | |
| VStack(alignment: .leading) { | |
| let filteredDefs = defs.filter { | |
| (searchText.isEmpty | |
| || ($0.name.localizedCaseInsensitiveContains(searchText) | |
| || $0.inputs.contains {$0.name.localizedCaseInsensitiveContains(searchText)}) | |
| ) | |
| && ($0.availability.isEmpty | |
| || $0.isAvailable(on: platform, version: .init(majorVersion: osVersion, minorVersion: 0, patchVersion: 0)) | |
| ) | |
| && (group == nil | |
| || $0.group == group | |
| ) | |
| } | |
| HStack { | |
| Picker("Library Version", selection: $version) { | |
| Text("default").tag(ShaderGraph.NodeLibrary.Version.default) | |
| Text("materialX138").tag(ShaderGraph.NodeLibrary.Version.materialX138) | |
| Text("materialX139").tag(ShaderGraph.NodeLibrary.Version.materialX139) | |
| } | |
| Picker("Group", selection: $group) { | |
| Text("All").tag(String?.none) | |
| Divider() | |
| ForEach(groups, id: \.self) { group in | |
| Text(group).tag(String?.some(group)) | |
| } | |
| } | |
| Picker("Platform", selection: $platform) { | |
| Text("iOS").tag(ShaderGraph.NodeDefinition.Platform.iOS) | |
| Text("macOS").tag(ShaderGraph.NodeDefinition.Platform.macOS) | |
| Text("visionOS").tag(ShaderGraph.NodeDefinition.Platform.visionOS) | |
| Text("watchOS").tag(ShaderGraph.NodeDefinition.Platform.watchOS) | |
| Text("tvOS").tag(ShaderGraph.NodeDefinition.Platform.tvOS) | |
| } | |
| Picker("OS", selection: $osVersion) { | |
| Text("27.0.0").tag(27) | |
| Text("26.0.0").tag(26) | |
| } | |
| Text("\(filteredDefs.count) nodes") | |
| } | |
| .padding() | |
| List { | |
| ForEach(filteredDefs, id: \.name) { def in | |
| NodeDefView(def: def) | |
| } | |
| .monospaced() | |
| } | |
| .listStyle(.plain) | |
| .textSelection(.enabled) | |
| .searchable(text: $searchText, prompt: "Filter") | |
| .searchFocused($searchFocus) | |
| } | |
| .onChange(of: version, initial: true) { _, version in | |
| defs = ShaderGraph.NodeLibrary(version: version).definitions.sorted {$0.name < $1.name} | |
| } | |
| .onChange(of: defs) { _, defs in | |
| groups = Array(Set(defs.compactMap(\.group))).sorted() | |
| } | |
| } | |
| } | |
| struct NodeDefView: View { | |
| var def: ShaderGraph.NodeDefinition | |
| var body: some View { | |
| let inputs = def.inputs.map { i in | |
| var t = i.name | |
| if let semanticType = i.semanticType { | |
| t += ": **\(semanticType.name)**" | |
| if let semanticTypeValues = semanticType.values, !semanticTypeValues.isEmpty { | |
| t += "{\(semanticTypeValues.joined(separator: ", "))}" | |
| } | |
| } else { | |
| t += ": **\(i.type)**" | |
| } | |
| if let defaultValue = i.defaultValue { | |
| var v = "" | |
| if let range = "\(defaultValue)".range(of: #"\(([^()]+)\)"#, options: .regularExpression) { | |
| v = String("\(defaultValue)"[range]) | |
| } else { | |
| v = "\(defaultValue)" | |
| } | |
| v = v.replacingOccurrences(of: ".0", with: "") | |
| t += " = \(v)" | |
| } | |
| return t | |
| } | |
| let inputs2 = inputs.count <= 1 ? inputs.joined(separator: ", ") : "\n\(inputs.map {"\t\($0),"}.joined(separator: "\n"))\n" | |
| let outputs = def.outputs.map { o in | |
| "\(o.name): **\(o.type)**" | |
| }.joined(separator: ", ") | |
| let md = """ | |
| **\(def.name)**(\(inputs2)) -> (\(outputs)) | |
| """ | |
| Text((try? AttributedString(markdown: md, options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace))) ?? AttributedString(md)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment