private void UpdatePropertyInfo(MaterialPropertyInfo pUpdate) { int index = materialPropertiesCache.FindIndex(0, p => p.PropertyName == pUpdate.PropertyName); if (index >= 0) { materialPropertiesCache[index] = pUpdate; } }
private MaterialPropertyInfo GetPropertyInfo(string name) { MaterialPropertyInfo res = default(MaterialPropertyInfo); foreach (MaterialPropertyInfo p in materialPropertiesCache) { if (p.PropertyName == name) { res = p; break; } } return(res); }
private void DrawConfigurationPanel(MaterialPropertyInfo guiInfo, string propertyName, Material mat) { using (new GUILayout.HorizontalScope("box")) { using (new GUILayout.VerticalScope()) { EditorGUI.BeginChangeCheck(); using (new GUILayout.HorizontalScope()) { if (!m_LoadedAttributes.HasAttributes(propertyName)) { GUILayout.Label(propertyName, GUILayout.Width(100)); GUILayout.FlexibleSpace(); if (GUILayout.Button("Create attributes", GUILayout.Width(120), GUILayout.Height(16))) { AttributeLayout newAttr = new AttributeLayout(); newAttr.propertyTarget = propertyName; m_LoadedAttributes.AddAttribute(newAttr); } } else { guiInfo.IsVisible = EditorGUILayout.Foldout(guiInfo.IsVisible, propertyName, true); if (GUILayout.Button("Erase attributes", GUILayout.ExpandWidth(true), GUILayout.MinWidth(60), GUILayout.MaxWidth(120), GUILayout.Height(16))) { m_LoadedAttributes.RemoveAttribute(propertyName); } } } if (EditorGUI.EndChangeCheck()) { UpdatePropertyInfo(guiInfo); } if (m_LoadedAttributes.HasAttributes(propertyName) && guiInfo.IsVisible) { AttributeLayout attr = m_LoadedAttributes.GetAttributes(propertyName); EditorGUILayout.Space(); using (new GUILayout.HorizontalScope()) { using (new GUILayout.VerticalScope(GUILayout.Width(70), GUILayout.ExpandWidth(false))) { Texture tex = mat.GetTexture(propertyName); EditorGUI.DrawPreviewTexture( EditorGUILayout.GetControlRect(GUILayout.Width(64), GUILayout.Height(64)), (tex != null) ? tex : Texture2D.blackTexture); } using (new GUILayout.VerticalScope()) { GUILayout.Label("Channel"); GUILayout.Label("Index"); GUILayout.Label("Range"); GUILayout.Label("Group"); GUILayout.Label("Is Base Texture"); } GUILayout.FlexibleSpace(); using (new GUILayout.VerticalScope()) { // Channel selection attr.channel = Styles.ToMeshChannel(EditorGUILayout.Popup(Styles.ToInt(attr.channel), Styles.k_MeshChannel, GUILayout.Width(140))); // Index selection attr.index = (ComponentIndex)GUILayout.Toolbar((int)attr.index, ComponentIndexUtility.ComponentIndexPopupDescriptions, GUILayout.Width(140)); // Value range attr.range = EditorGUILayout.Vector2Field("", attr.range, GUILayout.Width(140)); // Group selection attr.mask = EditorGUILayout.Popup(attr.mask, AttributeLayout.DefaultMaskDescriptions, GUILayout.Width(140)); attr.isBaseTexture = EditorGUILayout.Toggle(attr.isBaseTexture); } } } } } }
public static MaterialDatabase BuildMaterialDatabase(MaterialReference[] materialReferences) { if (materialReferences == null) { return(new MaterialDatabase(new MaterialInfo[0], new MaterialPropertyInfo[0])); } MaterialInfo[] materialInfos = new MaterialInfo[materialReferences.Length]; int totalPropertyCount = 0; for (int i = 0; i < materialReferences.Length; i++) { Shader shader = materialReferences[i].material.shader; int count = ShaderUtil.GetPropertyCount(shader); materialInfos[i] = new MaterialInfo() { material = materialReferences[i].material, materialName = materialReferences[i].name, propertyRange = new RangeInt(totalPropertyCount, count) }; totalPropertyCount += count; } MaterialPropertyInfo[] propertyInfos = new MaterialPropertyInfo[totalPropertyCount]; int idx = 0; for (int i = 0; i < materialReferences.Length; i++) { Material material = materialReferences[i].material; Shader shader = material.shader; int count = materialInfos[i].propertyRange.length; for (int j = 0; j < count; j++) { ShaderUtil.ShaderPropertyType type = ShaderUtil.GetPropertyType(shader, j); string propertyName = ShaderUtil.GetPropertyName(shader, j); // todo -- use string intern system to handle names instead MaterialPropertyValue materialValue = new MaterialPropertyValue { shaderPropertyId = Shader.PropertyToID(propertyName), propertyType = ConvertPropertyType(type) }; switch (type) { case ShaderUtil.ShaderPropertyType.Color: materialValue.colorValue = material.GetColor(materialValue.shaderPropertyId); break; case ShaderUtil.ShaderPropertyType.Vector: materialValue.vectorValue = material.GetVector(materialValue.shaderPropertyId); break; case ShaderUtil.ShaderPropertyType.Float: materialValue.floatValue = material.GetFloat(materialValue.shaderPropertyId); break; case ShaderUtil.ShaderPropertyType.Range: break; case ShaderUtil.ShaderPropertyType.TexEnv: materialValue.texture = material.GetTexture(materialValue.shaderPropertyId); break; default: throw new ArgumentOutOfRangeException(); } propertyInfos[idx++] = new MaterialPropertyInfo() { propertyName = propertyName, propertyType = ConvertPropertyType(type), propertyId = Shader.PropertyToID(propertyName) }; } } return(new MaterialDatabase(materialInfos, propertyInfos)); }