/// <summary> /// Converts the specified <see cref="XAttribute"/> to a <see cref="DRTextureFormat"/> value. /// </summary> /// <param name="attribute">The XML attribute to parse. Can be <see langword="null"/>.</param> /// <param name="defaultValue"> /// The default value, used if <paramref name="attribute"/> is null or empty. /// </param> /// <param name="identity">The content identity.</param> /// <returns>The texture format.</returns> /// <exception cref="InvalidContentException"> /// Error parsing <paramref name="attribute"/>. /// </exception> public static DRTextureFormat ToTextureFormat(this XAttribute attribute, DRTextureFormat defaultValue, ContentIdentity identity) { string value = (string)attribute; if (string.IsNullOrWhiteSpace(value)) { return(defaultValue); } switch (value.Trim().ToUpperInvariant()) { case "NOCHANGE": return(DRTextureFormat.NoChange); case "COLOR": return(DRTextureFormat.Color); case "DXT": return(DRTextureFormat.Dxt); case "NORMAL": return(DRTextureFormat.Normal); case "NORMALINVERTY": return(DRTextureFormat.NormalInvertY); default: var message = GetExceptionMessage(attribute, "Could not parse texture format: '{0}'", value); throw new InvalidContentException(message, identity); } }
/// <summary> /// Converts the specified <see cref="XAttribute"/> to a <see cref="DRTextureFormat"/> value. /// </summary> /// <param name="attribute">The XML attribute to parse. Can be <see langword="null"/>.</param> /// <param name="defaultValue"> /// The default value, used if <paramref name="attribute"/> is null or empty. /// </param> /// <param name="identity">The content identity.</param> /// <returns>The texture format.</returns> /// <exception cref="InvalidContentException"> /// Error parsing <paramref name="attribute"/>. /// </exception> public static DRTextureFormat ToTextureFormat(this XAttribute attribute, DRTextureFormat defaultValue, ContentIdentity identity) { string value = (string)attribute; if (string.IsNullOrWhiteSpace(value)) return defaultValue; switch (value.Trim().ToUpperInvariant()) { case "NOCHANGE": return DRTextureFormat.NoChange; case "COLOR": return DRTextureFormat.Color; case "DXT": return DRTextureFormat.Dxt; case "NORMAL": return DRTextureFormat.Normal; case "NORMALINVERTY": return DRTextureFormat.NormalInvertY; default: var message = GetExceptionMessage(attribute, "Could not parse texture format: '{0}'", value); throw new InvalidContentException(message, identity); } }
private DRMaterialContent ProcessInternal(DRMaterialContent material, ContentProcessorContext context) { material.Passes = new Dictionary<string, DREffectBindingContent>(); // Parse XML file. var document = material.Definition; var identity = material.Identity; var materialElement = document.Root; if (materialElement == null || materialElement.Name != "Material") { string message = string.Format(CultureInfo.InvariantCulture, "Root element \"<Material>\" is missing in XML."); throw new InvalidContentException(message, identity); } // Override material name, if attribute is set. material.Name = (string)materialElement.Attribute("Name") ?? material.Name; // Create effect bindings for all render passes. foreach (var passElement in materialElement.Elements("Pass")) { string pass = passElement.GetMandatoryAttribute("Name", identity); if (material.Passes.ContainsKey(pass)) { string message = XmlHelper.GetExceptionMessage(passElement, "Duplicate entry. The pass \"{0}\" was already defined.", pass); throw new InvalidContentException(message, identity); } var binding = new DREffectBindingContent { Name = pass, Identity = identity }; // Skip this pass if the graphics profile does not match the actual target profile. string profile = (string)passElement.Attribute("Profile") ?? "ANY"; string profileLower = profile.ToUpperInvariant(); if (profileLower == "REACH") { if (context.TargetProfile != GraphicsProfile.Reach) continue; } else if (profileLower == "HIDEF") { if (context.TargetProfile != GraphicsProfile.HiDef) continue; } else if (profileLower != "ANY") { string message = XmlHelper.GetExceptionMessage(passElement, "Unknown profile: \"{0}\". Allowed values are \"Reach\", \"HiDef\" or \"Any\"", profile); throw new InvalidContentException(message, identity); } // ----- Effect string effectName = passElement.GetMandatoryAttribute("Effect", identity); switch (effectName) { case "AlphaTestEffect": binding.EffectType = DREffectType.AlphaTestEffect; break; case "BasicEffect": binding.EffectType = DREffectType.BasicEffect; break; case "DualTextureEffect": binding.EffectType = DREffectType.DualTextureEffect; break; case "EnvironmentMapEffect": binding.EffectType = DREffectType.EnvironmentMapEffect; break; case "SkinnedEffect": binding.EffectType = DREffectType.SkinnedEffect; break; default: binding.EffectType = DREffectType.CustomEffect; if (string.IsNullOrEmpty(Path.GetExtension(effectName))) { // The effect is a prebuilt asset. effectName is the name of the asset, // for example: effectName = "DigitalRune\GBuffer". binding.EffectAsset = effectName; } else { // The effect is a local .fx file. effectName = ContentHelper.FindFile(effectName, identity); binding.Effect = new ExternalReference<EffectContent>(effectName); } break; } ProcessEffect(binding, context); // ----- Parameters foreach (var parameterElement in passElement.Elements("Parameter")) { string name = parameterElement.GetMandatoryAttribute("Name", identity); if (binding.OpaqueData.ContainsKey(name)) { string message = XmlHelper.GetExceptionMessage(parameterElement, "Duplicate entry. The parameter \"{0}\" was already defined.", name); throw new InvalidContentException(message, identity); } object value = parameterElement.Attribute("Value").ToParameterValue(null, identity); if (value != null) binding.OpaqueData.Add(name, value); } // ----- Textures foreach (var textureElement in passElement.Elements("Texture")) { string name = textureElement.GetMandatoryAttribute("Name", identity); if (binding.Textures.ContainsKey(name)) { string message = XmlHelper.GetExceptionMessage(textureElement, "Duplicate entry. The texture \"{0}\" was already defined.", name); throw new InvalidContentException(message, identity); } string fileName = textureElement.GetMandatoryAttribute("File", identity); fileName = ContentHelper.FindFile(fileName, identity); // Texture processor parameters. var colorKeyAttribute = textureElement.Attribute("ColorKey"); bool colorKeyEnabled = colorKeyAttribute != null; Color colorKeyColor = colorKeyAttribute.ToColor(Color.Magenta, identity); bool generateMipmaps = (bool?)textureElement.Attribute("GenerateMipmaps") ?? true; float inputGamma = (float?)textureElement.Attribute("InputGamma") ?? 2.2f; float outputGamma = (float?)textureElement.Attribute("OutputGamma") ?? 2.2f; bool premultiplyAlpha = (bool?)textureElement.Attribute("PremultiplyAlpha") ?? true; bool resizeToPowerOfTwo = (bool?)textureElement.Attribute("ResizeToPowerOfTwo") ?? false; DRTextureFormat textureFormat = textureElement.Attribute("Format").ToTextureFormat(DRTextureFormat.Dxt, identity); float referenceAlpha = (float?)textureElement.Attribute("ReferenceAlpha") ?? 0.9f; bool scaleAlphaToCoverage = (bool?)textureElement.Attribute("ScaleAlphaToCoverage") ?? false; // Store texture parameters in opaque data. var texture = new ExternalReference<TextureContent>(fileName); var defaultTextureProcessor = new DRTextureProcessor(); if (colorKeyColor != defaultTextureProcessor.ColorKeyColor) texture.OpaqueData.Add("ColorKeyColor", colorKeyColor); if (colorKeyEnabled != defaultTextureProcessor.ColorKeyEnabled) texture.OpaqueData.Add("ColorKeyEnabled", colorKeyEnabled); if (generateMipmaps != defaultTextureProcessor.GenerateMipmaps) texture.OpaqueData.Add("GenerateMipmaps", generateMipmaps); if (inputGamma != defaultTextureProcessor.InputGamma) texture.OpaqueData.Add("InputGamma", inputGamma); if (outputGamma != defaultTextureProcessor.OutputGamma) texture.OpaqueData.Add("OutputGamma", outputGamma); if (premultiplyAlpha != defaultTextureProcessor.PremultiplyAlpha) texture.OpaqueData.Add("PremultiplyAlpha", premultiplyAlpha); if (resizeToPowerOfTwo != defaultTextureProcessor.ResizeToPowerOfTwo) texture.OpaqueData.Add("ResizeToPowerOfTwo", resizeToPowerOfTwo); if (textureFormat != defaultTextureProcessor.Format) texture.OpaqueData.Add("Format", textureFormat); if (referenceAlpha != defaultTextureProcessor.ReferenceAlpha) texture.OpaqueData.Add("ReferenceAlpha", referenceAlpha); if (scaleAlphaToCoverage != defaultTextureProcessor.ScaleAlphaToCoverage) texture.OpaqueData.Add("ScaleAlphaToCoverage", scaleAlphaToCoverage); binding.Textures.Add(name, texture); } ProcessTextures(binding, context, identity); material.Passes.Add(pass, binding); } return material; }