private void ReloadJson() { editor = null; container = null; modified = false; AssetDatabase.Refresh(); }
public static string SaveMeshAttributesData(Shader shader, z_AttributeLayout[] attributes, bool overwrite = false) { if (shader == null || attributes == null) { Debug.LogError("Cannot save null attributes for shader."); return(null); } string path = FindPolybrushMetaDataForShader(shader); string shader_path = AssetDatabase.GetAssetPath(shader); string shader_directory = Path.GetDirectoryName(shader_path); string shader_filename = Path.GetFileNameWithoutExtension(path); // metadata didn't exist before if (string.IsNullOrEmpty(path)) { if (string.IsNullOrEmpty(shader_path)) { // how!? path = EditorUtility.SaveFilePanelInProject( "Save Polybrush Shader Attributes", shader_filename, SHADER_ATTRIB_FILE_EXTENSION, "Please enter a file name to save Polybrush shader metadata to."); if (string.IsNullOrEmpty(path)) { Debug.LogWarning(string.Format("Could not save Polybrush shader metadata. Please try again, possibly with a different file name or folder path.")); return(null); } } else { shader_filename = Path.GetFileNameWithoutExtension(shader_path); path = string.Format("{0}/{1}.{2}", shader_directory, shader_filename, SHADER_ATTRIB_FILE_EXTENSION); } } if (!overwrite && File.Exists(path)) { // @todo Debug.LogWarning("shader metadata exists. calling function refuses to overwrite and lazy developer didn't add a save dialog here."); return(null); } try { z_AttributeLayoutContainer container = z_AttributeLayoutContainer.Create(shader, attributes); string json = JsonUtility.ToJson(container, true); File.WriteAllText(path, json); return(path); } catch (System.Exception e) { Debug.LogError("Failed saving Polybrush Shader MetaData\n" + e.ToString()); return(path); } }
public override void OnInspectorGUI() { TextAsset asset = target as TextAsset; if (asset == null || !asset.name.EndsWith(".pbs")) { // sfor whatever reason this doesn't work // DrawDefaultInspector(); DrawTextAssetInspector(); return; } if (editor == null) { container = ScriptableObject.CreateInstance <z_AttributeLayoutContainer>(); JsonUtility.FromJsonOverwrite(asset.text, container); editor = Editor.CreateEditor(container); } GUI.enabled = true; EditorGUI.BeginChangeCheck(); editor.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { modified = true; } GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUI.enabled = modified; if (GUILayout.Button("Revert")) { ReloadJson(); } if (GUILayout.Button("Apply")) { z_EditorUtility.SaveMeshAttributesData(container, true); ReloadJson(); } GUILayout.EndHorizontal(); GUI.enabled = false; }
/** * Test a gameObject and it's mesh renderers for compatible shaders, and if one is found * load it's attribute data into meshAttributes. */ private bool CheckForTextureBlendSupport(GameObject go) { z_AttributeLayoutContainer detectedMeshAttributes; foreach (Material mat in z_Util.GetMaterials(go)) { if (z_ShaderUtil.GetMeshAttributes(mat, out detectedMeshAttributes)) { meshAttributesContainer = detectedMeshAttributes; return(true); } } return(false); }
/** * Loads z_AttributeLayout data from a shader. Checks for both legacy (define Z_TEXTURE_CHANNELS) and * .pbs.json metadata. */ public static bool GetMeshAttributes(Material material, out z_AttributeLayoutContainer attribContainer) { attribContainer = null; if (material == null) { return(false); } // first search for json, then fall back on legacy if (z_EditorUtility.FindMeshAttributesForShader(material.shader, out attribContainer)) { Dictionary <string, int> shaderProperties = new Dictionary <string, int>(); for (int i = 0; i < ShaderUtil.GetPropertyCount(material.shader); i++) { shaderProperties.Add(ShaderUtil.GetPropertyName(material.shader, i), i); } foreach (z_AttributeLayout a in attribContainer.attributes) { int index = -1; if (shaderProperties.TryGetValue(a.propertyTarget, out index)) { if (ShaderUtil.GetPropertyType(material.shader, index) == ShaderUtil.ShaderPropertyType.TexEnv) { a.previewTexture = (Texture2D)material.GetTexture(a.propertyTarget); } } } return(true); } z_AttributeLayout[] attribs; if (GetMeshAttributes_Legacy(material, out attribs)) { attribContainer = z_AttributeLayoutContainer.Create(material.shader, attribs); return(true); } return(false); }
public override void OnEnable() { base.OnEnable(); modeIcons[0].image = z_IconUtility.GetIcon("Icon/Brush"); modeIcons[1].image = z_IconUtility.GetIcon("Icon/Roller"); likelySupportsTextureBlending = false; meshAttributesContainer = null; brushColor = null; foreach (GameObject go in Selection.gameObjects) { likelySupportsTextureBlending = CheckForTextureBlendSupport(go); if (likelySupportsTextureBlending) { break; } } }
private static bool TryReadAttributeLayoutsFromJson(string path, out z_AttributeLayoutContainer container) { container = null; if (!File.Exists(path)) { return(false); } string json = File.ReadAllText(path); if (string.IsNullOrEmpty(json)) { return(false); } container = ScriptableObject.CreateInstance <z_AttributeLayoutContainer>(); JsonUtility.FromJsonOverwrite(json, container); return(true); }
/** * Searches only by looking for a compatibly named file in the same directory. */ public static bool FindMeshAttributesForShader(Shader shader, out z_AttributeLayoutContainer attributes) { attributes = null; string path = AssetDatabase.GetAssetPath(shader); string filename = Path.GetFileNameWithoutExtension(path); string directory = Path.GetDirectoryName(path); string[] paths = new string[] { string.Format("{0}/{1}.{2}", directory, filename, SHADER_ATTRIB_FILE_EXTENSION), string.Format("{0}/{1}.{2}", directory, z_ShaderUtil.GetMetaDataPath(shader), SHADER_ATTRIB_FILE_EXTENSION) }; foreach (string str in paths) { if (TryReadAttributeLayoutsFromJson(str, out attributes)) { return(true); } } return(false); }
/** * Store user-set shader attribute information. * Returns the path written to on success, null otherwise. */ public static string SaveMeshAttributesData(z_AttributeLayoutContainer container, bool overwrite = false) { return(SaveMeshAttributesData(container.shader, container.attributes, overwrite)); }