public ShaderPropertyData(string name, ShaderPropertyType type, string defaultValue = null, string defaultValueAB = null) { Name = name; Type = type; DefaultValue = defaultValue.IsNullOrEmpty() ? null : defaultValue; DefaultValueAssetBundle = defaultValueAB.IsNullOrEmpty() ? null : defaultValueAB; }
/// <summary> /// 构造一个CardAnimation /// </summary> /// <param name="propertyName">对应的材质属性名(eg. _Layer1_Color)</param> /// <param name="type">属性的类型</param> /// <param name="mat">被控制的Material。对于NGUI的UITexture,应该是其dynamicMat对象</param> public CardAnimation(string propertyName, ShaderPropertyType type, Material mat) { this.propertyName = propertyName; this.type = type; this.mat = mat; loopType = LoopType.Repeat; }
static CodeMethodInvokeExpression MaterialGetExp(ShaderPropertyType t, string name) { var mat = new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), "material"); string methodName = "InvalidExp"; switch (t) { case ShaderPropertyType.Color: methodName = "GetColor"; break; case ShaderPropertyType.Vector: methodName = "GetVector"; break; case ShaderPropertyType.Range: case ShaderPropertyType.Float: methodName = "GetFloat"; break; case ShaderPropertyType.Texture: methodName = "GetTexture"; break; default: break; } return(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(mat, methodName), new CodePrimitiveExpression(name))); }
private void LoadXML(XElement materialEditorElement) { if (materialEditorElement == null) { return; } foreach (var shaderElement in materialEditorElement.Elements("Shader")) { string shaderName = shaderElement.Attribute("Name").Value; LoadedShaders[shaderName] = new ShaderData(shaderName, shaderElement.Attribute("AssetBundle")?.Value, shaderElement.Attribute("RenderQueue")?.Value, shaderElement.Attribute("Asset")?.Value); XMLShaderProperties[shaderName] = new Dictionary <string, ShaderPropertyData>(); foreach (XElement element in shaderElement.Elements("Property")) { string propertyName = element.Attribute("Name").Value; ShaderPropertyType propertyType = (ShaderPropertyType)Enum.Parse(typeof(ShaderPropertyType), element.Attribute("Type").Value); string defaultValue = element.Attribute("DefaultValue")?.Value; string defaultValueAB = element.Attribute("DefaultValueAssetBundle")?.Value; ShaderPropertyData shaderPropertyData = new ShaderPropertyData(propertyName, propertyType, defaultValue, defaultValueAB); XMLShaderProperties["default"][propertyName] = shaderPropertyData; XMLShaderProperties[shaderName][propertyName] = shaderPropertyData; } } }
/// <summary> /// Copies material property value from one material to another when the property is defined in both materials. /// </summary> public static void TransferMaterialProperty(int propertyId, ShaderPropertyType propertyType, Material fromMaterial, Material toMaterial) { if (!fromMaterial || !toMaterial) { return; } if (!fromMaterial.HasProperty(propertyId) || !toMaterial.HasProperty(propertyId)) { return; } switch (propertyType) { case ShaderPropertyType.Color: toMaterial.SetColor(propertyId, fromMaterial.GetColor(propertyId)); break; case ShaderPropertyType.Vector: toMaterial.SetVector(propertyId, fromMaterial.GetVector(propertyId)); break; case ShaderPropertyType.Float: toMaterial.SetFloat(propertyId, fromMaterial.GetFloat(propertyId)); break; case ShaderPropertyType.Texture: toMaterial.SetTexture(propertyId, fromMaterial.GetTexture(propertyId)); break; } }
public void SetValueElement(ShaderPropertyType propertyType) { _valueElement?.RemoveFromHierarchy(); _valueElement?.Unbind(); switch (propertyType) { case ShaderPropertyType.Color: _valueElement = new ColorField(); _valueContainer.Add(_valueElement); (_valueElement as ColorField).BindProperty(_colorProperty); break; case ShaderPropertyType.Vector: break; case ShaderPropertyType.Float: _valueElement = new FloatField(); _valueContainer.Add(_valueElement); (_valueElement as FloatField).BindProperty(_valueProperty); break; case ShaderPropertyType.Range: _valueElement = new FloatField(); (_valueElement as FloatField).BindProperty(_valueProperty); break; case ShaderPropertyType.Texture: break; default: break; } }
public TerrainLayerProperty(string label, ShaderPropertyType type, TerrainLayerVariable variable, bool hdr, Vector2 rangeLimits = new Vector2()) { this.label = label; this.type = type; this.variable = variable; this.hdr = hdr; this.rangeLimits = rangeLimits; this.isVector4 = this.variable == TerrainLayerVariable.diffuseRemapMax || this.variable == TerrainLayerVariable.diffuseRemapMin || this.variable == TerrainLayerVariable.maskMapRemapMax || this.variable == TerrainLayerVariable.maskMapRemapMin; if (isVector4 && type == ShaderPropertyType.Range) { string[] labels = this.label.Split(','); this.label = labels[0]; this.labelAlpha = labels[1]; this.showA = labelAlpha != "Unused"; } else if (isVector4 && type == ShaderPropertyType.Float) { string[] labels = this.label.Split(','); this.labelR = labels[0]; this.labelG = labels[1]; this.labelB = labels[2]; this.labelAlpha = labels[3]; this.showR = labelR != "Unused"; this.showG = labelG != "Unused"; this.showB = labelB != "Unused"; this.showA = labelAlpha != "Unused"; } }
/// <summary> /// Attempts to set provided property value object to the material. /// Will silently fail if the property is of a wrong type or doesn't exist. /// </summary> public static void SetProperty(this Material material, ShaderPropertyType type, string name, object value) { if (!material || !material.HasProperty(name) || !CheckPropertyValueType(value, type)) { return; } switch (type) { case ShaderPropertyType.Color: material.SetColor(name, (Color)value); break; case ShaderPropertyType.Vector: material.SetVector(name, (Vector4)value); break; case ShaderPropertyType.Float: material.SetFloat(name, (float)value); break; case ShaderPropertyType.Texture: material.SetTexture(name, (Texture)value); break; } }
void TryAddNewAnimation(string propertyName) { if (animationNames.Contains(propertyName)) { Debug.LogError("已经存在这个属性动画:" + propertyName + ", 请不要重复添加。"); return; } Material mat = mTarget.Mat; if (mat == null || mat.shader == null) { return; } int index; if (!propertyNameIndex.TryGetValue(propertyName, out index)) { return; } ShaderPropertyType type = mat.GetPropertyType(index); CardAnimation ca = new CardAnimation(propertyName, type, mTarget.Mat); mTarget.animations.Add(ca); mTarget.animations.Sort(CompareAnim); }
private static void LoadXML() { XMLShaderProperties["default"] = new Dictionary <string, ShaderPropertyData>(); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{nameof(MaterialEditorAPI)}.Resources.default.xml")) if (stream != null) { using (XmlReader reader = XmlReader.Create(stream)) { XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlElement materialEditorElement = doc.DocumentElement; var shaderElements = materialEditorElement.GetElementsByTagName("Shader"); foreach (var shaderElementObj in shaderElements) { if (shaderElementObj != null) { var shaderElement = (XmlElement)shaderElementObj; { string shaderName = shaderElement.GetAttribute("Name"); XMLShaderProperties[shaderName] = new Dictionary <string, ShaderPropertyData>(); var shaderPropertyElements = shaderElement.GetElementsByTagName("Property"); foreach (var shaderPropertyElementObj in shaderPropertyElements) { if (shaderPropertyElementObj != null) { var shaderPropertyElement = (XmlElement)shaderPropertyElementObj; { string propertyName = shaderPropertyElement.GetAttribute("Name"); ShaderPropertyType propertyType = (ShaderPropertyType)Enum.Parse(typeof(ShaderPropertyType), shaderPropertyElement.GetAttribute("Type")); string defaultValue = shaderPropertyElement.GetAttribute("DefaultValue"); string defaultValueAB = shaderPropertyElement.GetAttribute("DefaultValueAssetBundle"); string range = shaderPropertyElement.GetAttribute("Range"); string min = null; string max = null; if (!range.IsNullOrWhiteSpace()) { var rangeSplit = range.Split(','); if (rangeSplit.Length == 2) { min = rangeSplit[0]; max = rangeSplit[1]; } } ShaderPropertyData shaderPropertyData = new ShaderPropertyData(propertyName, propertyType, defaultValue, defaultValueAB, min, max); XMLShaderProperties["default"][propertyName] = shaderPropertyData; } } } } } } } } }
private EPropertyType GetTypeFromShaderType(ShaderPropertyType type) { return(type switch { ShaderPropertyType.Float => EPropertyType.Float, ShaderPropertyType.Color => EPropertyType.Color, ShaderPropertyType.Texture => EPropertyType.Texture, _ => EPropertyType.Unhandled });
public ShaderProperty(ShaderProperty shaderProperty) { name = shaderProperty.name; type = shaderProperty.type; colorValue = shaderProperty.colorValue; vectorValue = shaderProperty.vectorValue; floatValue = shaderProperty.floatValue; textureValue = shaderProperty.textureValue; }
public void Clone(ShaderFeature src) { hide = src.hide; readOnly = src.readOnly; shaderGroupName = src.shaderGroupName; propertyName = src.propertyName; type = src.type; dependencyPropertys.Clone(src.dependencyPropertys); }
protected BaseProperty(Shader shader, int idx) { Index = idx; Name = shader.GetPropertyName(idx); PropId = Shader.PropertyToID(Name); Description = shader.GetPropertyDescription(idx); Attributes = shader.GetPropertyAttributes(idx).ToList(); Type = shader.GetPropertyType(idx); }
private static void LoadXML(XmlElement materialEditorElement) { if (materialEditorElement == null) { return; } var shaderElements = materialEditorElement.GetElementsByTagName("Shader"); foreach (var shaderElementObj in shaderElements) { if (shaderElementObj != null) { var shaderElement = (XmlElement)shaderElementObj; string shaderName = shaderElement.GetAttribute("Name"); if (LoadedShaders.ContainsKey(shaderName)) { Destroy(LoadedShaders[shaderName].Shader); LoadedShaders.Remove(shaderName); } var shader = LoadShader(shaderName, shaderElement.GetAttribute("AssetBundle"), shaderElement.GetAttribute("Asset")); LoadedShaders[shaderName] = new ShaderData(shader, shaderName, shaderElement.GetAttribute("RenderQueue"), shaderElement.GetAttribute("ShaderOptimization")); XMLShaderProperties[shaderName] = new Dictionary <string, ShaderPropertyData>(); var shaderPropertyElements = shaderElement.GetElementsByTagName("Property"); foreach (var shaderPropertyElementObj in shaderPropertyElements) { if (shaderPropertyElementObj != null) { var shaderPropertyElement = (XmlElement)shaderPropertyElementObj; string propertyName = shaderPropertyElement.GetAttribute("Name"); ShaderPropertyType propertyType = (ShaderPropertyType)Enum.Parse(typeof(ShaderPropertyType), shaderPropertyElement.GetAttribute("Type")); string defaultValue = shaderPropertyElement.GetAttribute("DefaultValue"); string defaultValueAB = shaderPropertyElement.GetAttribute("DefaultValueAssetBundle"); string range = shaderPropertyElement.GetAttribute("Range"); string min = null; string max = null; if (!range.IsNullOrWhiteSpace()) { var rangeSplit = range.Split(','); if (rangeSplit.Length == 2) { min = rangeSplit[0]; max = rangeSplit[1]; } } ShaderPropertyData shaderPropertyData = new ShaderPropertyData(propertyName, propertyType, defaultValue, defaultValueAB, min, max); XMLShaderProperties["default"][propertyName] = shaderPropertyData; XMLShaderProperties[shaderName][propertyName] = shaderPropertyData; } } } } }
public static IEnumerable <string> GetPropertyNames(this Shader shader, ShaderPropertyType propertyType) { for (int i = 0; i < shader.GetPropertyCount(); i++) { if (shader.GetPropertyType(i) == propertyType) { yield return(shader.GetPropertyName(i)); } } }
private void SetShaderProperty(Enum value) { int propertyIndex = _propertiesNames.IndexOf(value.ToString()); ShaderPropertyType propertyType = _materialShader.GetPropertyType(propertyIndex); _propertyTypeProperty.enumValueIndex = (int)propertyType; _propertyName.stringValue = value.ToString(); _propertyName.serializedObject.ApplyModifiedProperties(); _property.serializedObject.ApplyModifiedProperties(); SetValueElement(propertyType); }
public float GetPropertyDefaultFloatValue(int propertyIndex) { Shader.CheckPropertyIndex(this, propertyIndex); ShaderPropertyType propertyType = this.GetPropertyType(propertyIndex); bool flag = propertyType != ShaderPropertyType.Float && propertyType != ShaderPropertyType.Range; if (flag) { throw new ArgumentException("Property type is not Float or Range."); } return(Shader.GetPropertyDefaultValue(this, propertyIndex)[0]); }
public Vector4 GetPropertyDefaultVectorValue(int propertyIndex) { Shader.CheckPropertyIndex(this, propertyIndex); ShaderPropertyType propertyType = this.GetPropertyType(propertyIndex); bool flag = propertyType != ShaderPropertyType.Color && propertyType != ShaderPropertyType.Vector; if (flag) { throw new ArgumentException("Property type is not Color or Vector."); } return(Shader.GetPropertyDefaultValue(this, propertyIndex)); }
public string GetPropertyTextureDefaultName(int propertyIndex) { Shader.CheckPropertyIndex(this, propertyIndex); ShaderPropertyType propertyType = this.GetPropertyType(propertyIndex); bool flag = propertyType != ShaderPropertyType.Texture; if (flag) { throw new ArgumentException("Property type is not Texture."); } return(Shader.GetPropertyTextureDefaultName(this, propertyIndex)); }
public bool FindTextureStack(int propertyIndex, out string stackName, out int layerIndex) { Shader.CheckPropertyIndex(this, propertyIndex); ShaderPropertyType propertyType = this.GetPropertyType(propertyIndex); bool flag = propertyType != ShaderPropertyType.Texture; if (flag) { throw new ArgumentException("Property type is not Texture."); } return(Shader.FindTextureStackImpl(this, propertyIndex, out stackName, out layerIndex)); }
public static Offset <ShaderProperty> CreateShaderProperty(FlatBufferBuilder builder, StringOffset namesOffset = default(StringOffset), ShaderPropertyType type = ShaderPropertyType.Float, ShaderPropertyValue value_type = ShaderPropertyValue.NONE, int valueOffset = 0) { builder.StartObject(4); ShaderProperty.AddValue(builder, valueOffset); ShaderProperty.AddNames(builder, namesOffset); ShaderProperty.AddValueType(builder, value_type); ShaderProperty.AddType(builder, type); return(ShaderProperty.EndShaderProperty(builder)); }
public ShaderPropertyData(string name, ShaderPropertyType type, string defaultValue = null, string defaultValueAB = null, string minValue = null, string maxValue = null) { Name = name; Type = type; DefaultValue = defaultValue.IsNullOrEmpty() ? null : defaultValue; DefaultValueAssetBundle = defaultValueAB.IsNullOrEmpty() ? null : defaultValueAB; if (!minValue.IsNullOrWhiteSpace() && !maxValue.IsNullOrWhiteSpace()) { if (float.TryParse(minValue, out float min) && float.TryParse(maxValue, out float max)) { MinValue = min; MaxValue = max; } } }
public static SerializableMaterialProperty Get(Material material, ShaderPropertyType type, string name) { object value = type switch { ShaderPropertyType.Float => material.GetFloat(name), ShaderPropertyType.Range => material.GetFloat(name), ShaderPropertyType.Color => SerializableColor.FromColor(material.GetColor(name)), ShaderPropertyType.Vector => SerializableVector4.FromVector(material.GetVector(name)), ShaderPropertyType.Texture => material.GetTexture(name)?.name, _ => null }; return(value == null ? null : new SerializableMaterialProperty { Name = name, Type = type, Value = value }); } }
private void LoadXML(XElement materialEditorElement) { if (materialEditorElement == null) { return; } foreach (var shaderElement in materialEditorElement.Elements("Shader")) { string shaderName = shaderElement.Attribute("Name").Value; if (LoadedShaders.ContainsKey(shaderName)) { Destroy(LoadedShaders[shaderName].Shader); LoadedShaders.Remove(shaderName); } LoadedShaders[shaderName] = new ShaderData(shaderName, shaderElement.Attribute("AssetBundle")?.Value, shaderElement.Attribute("RenderQueue")?.Value, shaderElement.Attribute("Asset")?.Value); XMLShaderProperties[shaderName] = new Dictionary <string, ShaderPropertyData>(); foreach (XElement element in shaderElement.Elements("Property")) { string propertyName = element.Attribute("Name").Value; ShaderPropertyType propertyType = (ShaderPropertyType)Enum.Parse(typeof(ShaderPropertyType), element.Attribute("Type").Value); string defaultValue = element.Attribute("DefaultValue")?.Value; string defaultValueAB = element.Attribute("DefaultValueAssetBundle")?.Value; string range = element.Attribute("Range")?.Value; string min = null; string max = null; if (!range.IsNullOrWhiteSpace()) { var rangeSplit = range.Split(','); if (rangeSplit.Length == 2) { min = rangeSplit[0]; max = rangeSplit[1]; } } ShaderPropertyData shaderPropertyData = new ShaderPropertyData(propertyName, propertyType, defaultValue, defaultValueAB, min, max); XMLShaderProperties["default"][propertyName] = shaderPropertyData; XMLShaderProperties[shaderName][propertyName] = shaderPropertyData; } } }
public static int GetTypeFromPropertyType(ShaderPropertyType t) { switch (t) { case ShaderPropertyType.Color: return(ParameterTypeColor); case ShaderPropertyType.Float: return(ParameterTypeFloat); case ShaderPropertyType.Texture: return(ParameterTypeTexture2D); case ShaderPropertyType.Vector: return(ParameterTypeFloat4); } return(0); }
protected void BindMaterialDepend(List<MaterialAsset> _materialAssetList) { foreach (var materialAsset in _materialAssetList) { string matName = materialAsset.matName.ToLower(); if (!LoadCache.HasLoaded(matName, AssetType.material)) { Debug.Log(matName + "找不到"); continue; } MaterialRelationship rl = materialAsset.materialRelationship; Material mat = LoadCache.GetMaterial(matName);//materialAssetBundleRefDic[materialAsset.matName].mainAsset as Material; if (rl != null && mat.name.ToLower().Equals(rl.matName.ToLower())) { mat.shader = Shader.Find(rl.sdName);//GetShader(rl.sdName);//shaderAssetBundleRefDic[rl.sdName].mainAsset as Shader; foreach (var item in rl.propertyPairList) { ShaderPropertyType type = (ShaderPropertyType)item.type; switch (type) { case ShaderPropertyType.Color: mat.SetColor(item.propertyName, item.color); break; case ShaderPropertyType.Vector: mat.SetVector(item.propertyName, item.v4); break; case ShaderPropertyType.Float: case ShaderPropertyType.Range: mat.SetFloat(item.propertyName, item.value); break; case ShaderPropertyType.TexEnv: if (LoadCache.HasLoaded(item.texName, AssetType.texture)) { mat.SetTexture(item.propertyName, LoadCache.GetTexture(item.texName));//textureAssetBundleRefDic[item.texName].mainAsset as Texture); } break; default: break; } } } } }
public static IList <string> GetPropertyNamesByType(this Shader that, ShaderPropertyType shaderPropertyType, IList <string> list = null) { #if !UNITY_EDITOR return(null); #else if (list == null) { list = new List <string>(); } for (int i = 0; i < UnityEditor.ShaderUtil.GetPropertyCount(that); i++) { if (UnityEditor.ShaderUtil.GetPropertyType(that, i) != (UnityEditor.ShaderUtil.ShaderPropertyType)shaderPropertyType) { continue; } list.Add(UnityEditor.ShaderUtil.GetPropertyName(that, i)); } return(list); #endif }
private static void AddMaterialPropertyData(Material mat, MaterialRestoreData data, string name, ShaderPropertyType type) { switch (type) { case ShaderPropertyType.Color: var c = mat.GetColor(name); data.colorDatas.Add(name, c); break; case ShaderPropertyType.Vector: var v = mat.GetVector(name); data.vectorDatas.Add(name, v); break; case ShaderPropertyType.Float: var f = mat.GetFloat(name); data.floatDatas.Add(name, f); break; case ShaderPropertyType.TexEnv: var offset = mat.GetTextureOffset(name); var scale = mat.GetTextureScale(name); data.textureScaleOffsets.Add(name, new Vector4Data(scale, offset)); var res = mat as RestorableMaterial; if (res == null) { break; } if (!res.textureNames.TryGetValue(name, out string tn)) { break; } data.textureNames.Add(name, tn); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
public static object GetProperty(this Material material, int nameId, ShaderPropertyType type) { switch (type) { case ShaderPropertyType.Color: return(material.GetColor(nameId)); case ShaderPropertyType.Vector: return(material.GetVector(nameId)); case ShaderPropertyType.Float: return(material.GetFloat(nameId)); case ShaderPropertyType.Range: return(material.GetFloat(nameId)); case ShaderPropertyType.Texture: return(material.GetTexture(nameId)); } return(null); }