/// <summary> /// Creates a new material with the default shader in the specified folder. /// </summary> /// <param name="folder">Folder relative to project library to create the material in.</param> public static void CreateEmptyMaterial(string folder) { string path = Path.Combine(folder, "New Material.asset"); path = GetUniquePath(path); Material material = new Material(Builtin.DiffuseShader); ProjectLibrary.Create(material, path); }
/// <summary> /// Creates a set of objects in which each object represents a GUI for a material parameter. /// </summary> /// <param name="mat">Material for whose parameters to create GUI for.</param> /// <param name="layout">Layout to add the parameter GUI elements to.</param> /// <returns>A material parameter GUI object for each supported material parameter.</returns> internal static MaterialParamGUI[] CreateMaterialGUI(Material mat, GUILayout layout) { Shader shader = mat.Shader; if (shader == null) return new MaterialParamGUI[0]; List<MaterialParamGUI> guiParams = new List<MaterialParamGUI>(); ShaderParameter[] shaderParams = shader.Parameters; foreach (var param in shaderParams) { if (param.Internal) continue; switch (param.Type) { case ShaderParameterType.Float: layout.AddSpace(5); guiParams.Add(new MaterialParamFloatGUI(param, mat, layout)); break; case ShaderParameterType.Vector2: layout.AddSpace(5); guiParams.Add(new MaterialParamVec2GUI(param, mat, layout)); break; case ShaderParameterType.Vector3: layout.AddSpace(5); guiParams.Add(new MaterialParamVec3GUI(param, mat, layout)); break; case ShaderParameterType.Vector4: layout.AddSpace(5); guiParams.Add(new MaterialParamVec4GUI(param, mat, layout)); break; case ShaderParameterType.Matrix3: layout.AddSpace(5); guiParams.Add(new MaterialParamMat3GUI(param, mat, layout)); break; case ShaderParameterType.Matrix4: layout.AddSpace(5); guiParams.Add(new MaterialParamMat4GUI(param, mat, layout)); break; case ShaderParameterType.Color: layout.AddSpace(5); guiParams.Add(new MaterialParamColorGUI(param, mat, layout)); break; case ShaderParameterType.Texture2D: case ShaderParameterType.Texture3D: case ShaderParameterType.TextureCube: layout.AddSpace(5); guiParams.Add(new MaterialParamTextureGUI(param, mat, layout)); break; } } return guiParams.ToArray(); }
private static extern void Internal_SetMaterials(IntPtr thisPtr, Material[] materials);
internal void SetMaterial(Material material, int index = 0) { materials[index] = material; IntPtr materialPtr = IntPtr.Zero; if (material != null) materialPtr = material.GetCachedPtr(); Internal_SetMaterial(mCachedPtr, materialPtr, index); }
/// <summary> /// Sets a material for a specific sub-mesh. /// </summary> /// <param name="material">Material to use for rendering the sub-mesh at the specified index.</param> /// <param name="index">Index of the sub-mesh.</param> public void SetMaterial(Material material, int index = 0) { _native.SetMaterial(material, index); serializableData.materials[index] = material; }
private static extern void Internal_CreateInstance(Material instance, IntPtr shader);
/// <inheritdoc/> internal override void Refresh(Material material) { guiElem.Value = material.GetVector4(shaderParam.Name); }
/// <summary> /// Creates a new material parameter GUI. /// </summary> /// <param name="shaderParam">Shader parameter to create the GUI for. Must be of 4D vector type.</param> /// <param name="material">Material the parameter is a part of.</param> /// <param name="layout">Layout to append the GUI elements to.</param> internal MaterialParamVec4GUI(ShaderParameter shaderParam, Material material, GUILayout layout) : base(shaderParam) { LocString title = new LocEdString(shaderParam.Name); guiElem = new GUIVector4Field(title); guiElem.OnChanged += (x) => { material.SetVector4(shaderParam.Name, x); EditorApplication.SetDirty(material); }; layout.AddElement(guiElem); }
/// <inheritdoc/> internal override void Refresh(Material material) { Texture value = null; switch (shaderParam.Type) { case ShaderParameterType.Texture2D: value = material.GetTexture2D(shaderParam.Name); break; case ShaderParameterType.Texture3D: value = material.GetTexture3D(shaderParam.Name); break; case ShaderParameterType.TextureCube: value = material.GetTextureCube(shaderParam.Name); break; } guiElem.Value = value; }
/// <summary> /// Creates a new material parameter GUI. /// </summary> /// <param name="shaderParam">Shader parameter to create the GUI for. Must be of texture type.</param> /// <param name="material">Material the parameter is a part of.</param> /// <param name="layout">Layout to append the GUI elements to.</param> internal MaterialParamTextureGUI(ShaderParameter shaderParam, Material material, GUILayout layout) : base(shaderParam) { LocString title = new LocEdString(shaderParam.Name); guiElem = new GUITextureField(title); switch (shaderParam.Type) { case ShaderParameterType.Texture2D: guiElem.OnChanged += (x) => { Texture2D texture = Resources.Load<Texture2D>(x); material.SetTexture2D(shaderParam.Name, texture); EditorApplication.SetDirty(material); }; break; case ShaderParameterType.Texture3D: guiElem.OnChanged += (x) => { Texture3D texture = Resources.Load<Texture3D>(x); material.SetTexture3D(shaderParam.Name, texture); EditorApplication.SetDirty(material); }; break; case ShaderParameterType.TextureCube: guiElem.OnChanged += (x) => { TextureCube texture = Resources.Load<TextureCube>(x); material.SetTextureCube(shaderParam.Name, texture); EditorApplication.SetDirty(material); }; break; } layout.AddElement(guiElem); }
/// <inheritdoc/> internal override void Refresh(Material material) { Matrix4 value = material.GetMatrix4(shaderParam.Name); for (int row = 0; row < MAT_SIZE; row++) { for (int col = 0; col < MAT_SIZE; col++) { int index = row * MAT_SIZE + col; guiMatFields[index].Value = value[row, col]; } } }
/// <summary> /// Creates a new material parameter GUI. /// </summary> /// <param name="shaderParam">Shader parameter to create the GUI for. Must be of 4x4 matrix type.</param> /// <param name="material">Material the parameter is a part of.</param> /// <param name="layout">Layout to append the GUI elements to.</param> internal MaterialParamMat4GUI(ShaderParameter shaderParam, Material material, GUILayout layout) : base(shaderParam) { LocString title = new LocEdString(shaderParam.Name); GUILabel guiTitle = new GUILabel(title, GUIOption.FixedWidth(100)); mainLayout = layout.AddLayoutY(); GUILayoutX titleLayout = mainLayout.AddLayoutX(); titleLayout.AddElement(guiTitle); titleLayout.AddFlexibleSpace(); GUILayoutY contentLayout = mainLayout.AddLayoutY(); GUILayoutX[] rows = new GUILayoutX[MAT_SIZE]; for (int i = 0; i < rows.Length; i++) rows[i] = contentLayout.AddLayoutX(); for (int row = 0; row < MAT_SIZE; row++) { for (int col = 0; col < MAT_SIZE; col++) { int index = row * MAT_SIZE + col; guiMatFields[index] = new GUIFloatField(row + "," + col, 20, "", GUIOption.FixedWidth(80)); GUIFloatField field = guiMatFields[index]; rows[row].AddElement(field); rows[row].AddSpace(5); int hoistedRow = row; int hoistedCol = col; field.OnChanged += (x) => { Matrix4 value = material.GetMatrix4(shaderParam.Name); value[hoistedRow, hoistedCol] = x; material.SetMatrix4(shaderParam.Name, value); EditorApplication.SetDirty(material); }; } } }
/// <summary> /// Checks if the data stored in GUI and in the material matches, and updates the GUI if it doesn't. /// </summary> /// <param name="material">Material whose data to check.</param> internal abstract void Refresh(Material material);
/// <summary> /// Recreates GUI elements for all material parameters. /// </summary> /// <param name="material">Material to create parameters for</param> private void RebuildParamGUI(Material material) { if (guiParams != null) { foreach (var param in guiParams) param.Destroy(); guiParams = null; } if (material != null && material.Shader != null) guiParams = CreateMaterialGUI(material, Layout); }