using System.Collections.Generic; using System.Linq; using System.Text; namespace UnityEditor.ShaderGraph { class PropertyCollector { public struct TextureInfo { public string name; public int textureId; public bool modifiable; } public readonly List properties = new List(); public void AddShaderProperty(AbstractShaderProperty chunk) { if (properties.Any(x => x.referenceName == chunk.referenceName)) return; properties.Add(chunk); } public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision) { foreach (var prop in properties) { prop.ValidateConcretePrecision(inheritedPrecision); } var batchAll = mode == GenerationMode.Preview; builder.AppendLine("CBUFFER_START(UnityPerMaterial)"); foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable))) { builder.AppendLine(prop.GetPropertyDeclarationString()); } builder.AppendLine("CBUFFER_END"); builder.AppendNewLine(); if (batchAll) return; foreach (var prop in properties.Where(n => !n.isBatchable || !n.generatePropertyBlock)) { builder.AppendLine(prop.GetPropertyDeclarationString()); } } public List GetConfiguredTexutres() { var result = new List(); foreach (var prop in properties.OfType()) { if (prop.referenceName != null) { var textureInfo = new TextureInfo { name = prop.referenceName, textureId = prop.value.texture != null ? prop.value.texture.GetInstanceID() : 0, modifiable = prop.modifiable }; result.Add(textureInfo); } } foreach (var prop in properties.OfType()) { if (prop.referenceName != null) { var textureInfo = new TextureInfo { name = prop.referenceName, textureId = prop.value.textureArray != null ? prop.value.textureArray.GetInstanceID() : 0, modifiable = prop.modifiable }; result.Add(textureInfo); } } foreach (var prop in properties.OfType()) { if (prop.referenceName != null) { var textureInfo = new TextureInfo { name = prop.referenceName, textureId = prop.value.texture != null ? prop.value.texture.GetInstanceID() : 0, modifiable = prop.modifiable }; result.Add(textureInfo); } } foreach (var prop in properties.OfType()) { if (prop.referenceName != null) { var textureInfo = new TextureInfo { name = prop.referenceName, textureId = prop.value.cubemap != null ? prop.value.cubemap.GetInstanceID() : 0, modifiable = prop.modifiable }; result.Add(textureInfo); } } return result; } } }