Esempio n. 1
0
    private Material ReadMaterial(MessagePackObjectDictionary matDict, bool forceOverlay,
                                  Dictionary <string, Material> customTextureNames)
    {
        string name;

        if (matDict.ContainsKey(FileKeys.MATERIAL_NAME))
        {
            name = matDict[FileKeys.MATERIAL_NAME].AsString();
        }
        else if (matDict.ContainsKey(FileKeys.MATERIAL_MODE))  // version 9 and earlier
        {
            name = matDict[FileKeys.MATERIAL_MODE].AsString();
            // ignore MATERIAL_ALPHA key, it's usually wrong
            if (matDict.ContainsKey(FileKeys.MATERIAL_COLOR))
            {
                if (ReadColor(matDict[FileKeys.MATERIAL_COLOR]).a < 1)
                {
                    forceOverlay = true;
                }
            }
            if (forceOverlay)
            {
                name += "_overlay";
            }
        }
        else
        {
            warnings.Add("Error reading material");
            return(ReadWorldFile.MissingMaterial(forceOverlay));
        }

        Material mat;
        bool     isCustom = customTextureNames != null && customTextureNames.ContainsKey(name);

        if (isCustom)
        {
            mat = customTextureNames[name];
        }
        else
        {
            mat = ResourcesDirectory.FindMaterial(name, editor);
        }
        if (mat == null)
        {
            warnings.Add("Unrecognized material: " + name);
            return(ReadWorldFile.MissingMaterial(forceOverlay));
        }
        if (!isCustom && matDict.ContainsKey(FileKeys.MATERIAL_COLOR))
        {
            // custom textures can't have colors
            string colorProp = ResourcesDirectory.MaterialColorProperty(mat);
            if (colorProp != null)
            {
                Color color    = ReadColor(matDict[FileKeys.MATERIAL_COLOR]);
                bool  setColor = color != mat.GetColor(colorProp);

                var colorStyle = ResourcesDirectory.ColorStyle.TINT;
                if (matDict.ContainsKey(FileKeys.MATERIAL_COLOR_STYLE))
                {
                    Enum.TryParse(matDict[FileKeys.MATERIAL_COLOR_STYLE].AsString(), out colorStyle);
                }
                bool setStyle = colorStyle == ResourcesDirectory.ColorStyle.PAINT &&
                                ResourcesDirectory.GetMaterialColorStyle(mat) != ResourcesDirectory.ColorStyle.PAINT;

                if (setColor || setStyle)
                {
                    mat = ResourcesDirectory.InstantiateMaterial(mat);
                }
                if (setColor)
                {
                    mat.SetColor(colorProp, color);
                }
                if (setStyle)
                {
                    ResourcesDirectory.SetMaterialColorStyle(mat, colorStyle);
                }
            }
        }
        return(mat);
    }