Esempio n. 1
0
    private Material ReadMaterial(JSONObject matObject)
    {
        string name;
        Color  color    = Color.white;
        bool   setColor = false;

        if (matObject["name"] != null)
        {
            name = matObject["name"];
        }
        else if (matObject["mode"] != null)
        {
            name = matObject["mode"];
            if (matObject["color"] != null)
            {
                color    = ReadColor(matObject["color"].AsArray);
                setColor = true;
                bool overlay = color.a != 1;
                if (matObject["alpha"] != null)
                {
                    overlay = matObject["alpha"].AsBool; // new with version 4
                }
                if (overlay)
                {
                    name += "_overlay";
                }
            }
        }
        else
        {
            warnings.Add("Error reading material");
            return(ReadWorldFile.MissingMaterial(false));
        }

        Material mat = ResourcesDirectory.FindMaterial(name, editor);

        if (mat == null)
        {
            warnings.Add("Unrecognized material: " + name);
            return(ReadWorldFile.MissingMaterial(false));
        }
        if (setColor)
        {
            string colorProp = ResourcesDirectory.MaterialColorProperty(mat);
            if (colorProp != null)
            {
                mat = ResourcesDirectory.InstantiateMaterial(mat);
                mat.SetColor(colorProp, color);
            }
        }
        return(mat);
    }
Esempio n. 2
0
    private Material ReadCustomTexture(MessagePackObjectDictionary texDict,
                                       Dictionary <string, Material> customTextureNames, bool overlay)
    {
        CustomTexture customTex = new CustomTexture(
            new Material(ReadWorldFile.MissingMaterial(overlay).shader), overlay);

        ReadPropertiesObject(texDict, customTex);
        Material mat = customTex.material;

        if (texDict.ContainsKey(FileKeys.CUSTOM_MATERIAL_NAME))
        {
            mat.name = texDict[FileKeys.CUSTOM_MATERIAL_NAME].AsString();
            customTextureNames[mat.name] = mat;
        }
        return(mat);
    }
Esempio n. 3
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);
    }
Esempio n. 4
0
    private void ReadWorld(JSONObject world, VoxelArray voxelArray)
    {
        var materials = new List <Material>();

        if (world["materials"] != null)
        {
            foreach (JSONNode matNode in world["materials"].AsArray)
            {
                JSONObject matObject = matNode.AsObject;
                materials.Add(ReadMaterial(matObject));
            }
        }

        var substances = new List <Substance>();

        if (world["substances"] != null)
        {
            foreach (JSONNode subNode in world["substances"].AsArray)
            {
                Substance s = new Substance();
                ReadEntity(subNode.AsObject, s);
                substances.Add(s);
            }
        }

        if (world["global"] != null)
        {
            ReadPropertiesObject(world["global"].AsObject, voxelArray.world);
        }
        if (fileWriterVersion <= 2 && world["sky"] != null)
        {
            Material sky = materials[world["sky"].AsInt];
            if (sky != ReadWorldFile.MissingMaterial(false)) // default skybox is null
            {
                voxelArray.world.SetSky(sky);
            }
        }
        if (world["map"] != null)
        {
            ReadMap(world["map"].AsObject, voxelArray, materials, substances);
        }
        if (fileWriterVersion <= 2 && world["player"] != null)
        {
            PlayerObject player = new PlayerObject();
            ReadObjectEntity(world["player"].AsObject, player);
            voxelArray.AddObject(player);
        }
        if (world["objects"] != null)
        {
            foreach (JSONNode objNode in world["objects"].AsArray)
            {
                JSONObject objObject = objNode.AsObject;
                string     typeName  = objObject["name"];
                var        objType   = GameScripts.FindTypeWithName(GameScripts.objects, typeName);
                if (objType == null)
                {
                    warnings.Add("Unrecognized object type: " + typeName);
                    continue;
                }
                ObjectEntity obj = (ObjectEntity)objType.Create();
                ReadObjectEntity(objObject, obj);
                voxelArray.AddObject(obj);
            }
        }

        if (!editor)
        {
            // start the game
            foreach (Substance s in substances)
            {
                s.InitEntityGameObject(voxelArray);
            }
            foreach (ObjectEntity obj in voxelArray.IterateObjects())
            {
                obj.InitEntityGameObject(voxelArray);
            }
        }
        else // editor
        {
            foreach (ObjectEntity obj in voxelArray.IterateObjects())
            {
                obj.InitObjectMarker((VoxelArrayEditor)voxelArray);
            }
        }
    }