Esempio n. 1
0
        public Texture2D[] GetTextures()
        {
            texToIndex = new Dictionary <string, Tuple <int, Texture2D, string> >();
            List <Tuple <int, Texture2D, string> > res = new List <Tuple <int, Texture2D, string> >();
            int i = 0;

            foreach (KeyValuePair <string, string> texture in textures)
            {
                string texName = texture.Key;
                Debug.Log("has texture key with name " + texName);
                // already loaded
                if (texToIndex.ContainsKey(texName))
                {
                    res.Add(texToIndex[texName]);
                }
                // need to be loaded
                else
                {
                    string texFileName = texture.Value;
                    string texFilePath = System.IO.Path.Combine(rootPath, texFileName);
                    Debug.Log("trying to load flower texture with name " + texName + " and path " + texFilePath);
                    if (System.IO.File.Exists(texFilePath))
                    {
                        Texture2D curTex = new Texture2D(10, 10);
                        // will automatically resize and reformat as needed
                        curTex.LoadImage(System.IO.File.ReadAllBytes(texFilePath));
                        curTex.Apply();

                        // convert to argb32
                        Texture2D argbTexture = new Texture2D(curTex.width, curTex.height, TextureFormat.ARGB32, false, true);
                        argbTexture.SetPixels(curTex.GetPixels());
                        argbTexture.Apply();
                        Color32[] argbColors = argbTexture.GetPixels32();

                        if (argbTexture.width == 16 && argbTexture.height == 16)
                        {
                            Debug.Log("tiling texture" + texName + " with path " + texFilePath);

                            argbTexture = new Texture2D(16 * 2, 16 * 3, TextureFormat.ARGB32, false, true);
                            argbTexture.SetPixels(0, 0, 16, 16, curTex.GetPixels());
                            argbTexture.SetPixels(16, 0, 16, 16, curTex.GetPixels());
                            argbTexture.SetPixels(0, 16, 16, 16, curTex.GetPixels());
                            argbTexture.SetPixels(16, 16, 16, 16, curTex.GetPixels());
                            argbTexture.SetPixels(0, 32, 16, 16, curTex.GetPixels());
                            argbTexture.SetPixels(16, 32, 16, 16, curTex.GetPixels());
                            argbTexture.Apply();
                        }
                        // rescale if needed to correct size
                        else if (argbTexture.width != 16 * 2 || argbTexture.height != 16 * 3)
                        {
                            TextureScale.Bilinear(argbTexture, 16 * 2, 16 * 3);
                            argbTexture.Apply();
                            Debug.Log("rescaling texture " + texName + " with path " + texFilePath);
                        }



                        texToIndex[texName] = new Tuple <int, Texture2D, string>(i, argbTexture, texFilePath);
                        i += 1;
                        res.Add(texToIndex[texName]);
                    }
                }
            }
            // sort by index, this will make lowest first so they'll be in the right order
            res.Sort((x, y) => { return(x.a.CompareTo(y.a)); });

            Texture2D[] result = new Texture2D[res.Count];
            for (int j = 0; j < res.Count; j++)
            {
                result[j] = res[j].b;
            }

            return(result);
        }
Esempio n. 2
0
        void AddNewBlock(int id, string name, string packName = "")
        {
            this.name     = name;
            this.packName = packName;
            string texturePath = "";

            if (packName != "")
            {
                texturePath = GetBlockTexturePath(packName, name);
            }
            if (allBlocks == null)
            {
                allBlocks     = new BlockValue[initialSize];
                nameToBlockId = new Dictionary <string, int>();
                largestIdMag  = 2;
                SetupTexture();
            }

            if (allBlocksTexture == null)
            {
                SetupTexture();
            }
            nameToBlockId[name] = id;
            int uid = System.Math.Abs(id);

            // increase number of blocks until we have enough
            while (allBlocks.Length <= uid)
            {
                BlockValue[] newAllBlocks = new BlockValue[allBlocks.Length * 2];
                for (int i = 0; i < allBlocks.Length; i++)
                {
                    newAllBlocks[i] = allBlocks[i];
                }
                allBlocks = newAllBlocks;
                // if we are done doubling the size, create the texture
                if (allBlocks.Length > uid)
                {
                    SetupTexture();
                }
            }
            if (allBlocks[uid] != null)
            {
                UnityEngine.Debug.LogWarning("warning: multiple blocks have id" + uid + "(technically " + id + " with transparency flag)");
            }
            allBlocks[uid] = this;

            _id = id;

            largestIdMag = System.Math.Max(largestIdMag, uid);
            Debug.Log(name + " has id " + id);

            string modelPath = "";

            if (packName != "")
            {
                modelPath = GetBlockModelPath(packName, name);
            }

            if (modelPath != "" && File.Exists(modelPath))
            {
                BlockModel  model    = BlockModel.FromJSONFilePath(modelPath);
                Texture2D[] textures = model.GetTextures();
                for (int i = 0; i < textures.Length; i++)
                {
                    Color[] pixels = textures[i].GetPixels();
                    allBlocksTexture.SetPixels(i * 32, (uid - 1) * 16 * 3, 16 * 2, 16 * 3, pixels);
                }
                allBlocksTexture.Apply();
                File.WriteAllBytes("res.png", allBlocksTexture.EncodeToPNG());

                if (id > 0)
                {
                    _id = -uid;
                    Debug.LogWarning("warning: detected custom model for block " + name + " with updated id " + id + " but we were told it does not have a custom model, set it to custom model anyway");
                }

                customModels[_id] = model;
            }

            else if (texturePath != "")
            {
                if (File.Exists(texturePath))
                {
                    byte[]    imageData    = File.ReadAllBytes(texturePath);
                    Texture2D blockTexture = new Texture2D(2, 2);
                    blockTexture.LoadImage(imageData); // (will automatically resize as needed)
                    blockTexture.Apply();

                    // convert to argb32
                    Texture2D argbTexture = new Texture2D(blockTexture.width, blockTexture.height, TextureFormat.ARGB32, false, true);
                    argbTexture.SetPixels(blockTexture.GetPixels());
                    argbTexture.Apply();
                    Color32[] argbColors = argbTexture.GetPixels32();

                    // check for transparency
                    bool isTransparent = false;
                    for (int j = 0; j < argbColors.Length; j++)
                    {
                        if (argbColors[j].a < 240) // if it is only 240/255 or higher it isn't noticable enough to actually use the transparency
                        {
                            isTransparent = true;
                            break;
                        }
                    }


                    // sign of id should match transparency, fix if not
                    if (isTransparent)
                    {
                        if (id > 0)
                        {
                            _id = -uid;
                            Debug.LogWarning("warning: detected transparent texture for block " + name + " with updated id " + id + " and texture path " + texturePath + " but we were told it is not transparent, set it to transparent anyway");
                        }
                    }
                    else
                    {
                        if (id < 0)
                        {
                            _id = uid;
                            Debug.LogWarning("warning: detected not transparent texture for block " + name + " with updated id " + id + " and texture path " + texturePath + " but we were told it is transparent, set it to not transparent anyway");
                        }
                    }



                    if (argbTexture.width != 16 * 2 || argbTexture.height != 16 * 3)
                    {
                        Debug.Log("rescaling texture of block " + name + " with id " + id + " and texture path " + texturePath);
                        TextureScale.Bilinear(argbTexture, 16 * 2, 16 * 3);
                    }
                    argbTexture.Apply();
                    Color[] pixels = argbTexture.GetPixels();
                    for (int k = 0; k < animFrames; k++)
                    {
                        allBlocksTexture.SetPixels(k * 32, (uid - 1) * 16 * 3, 16 * 2, 16 * 3, pixels);
                    }
                    allBlocksTexture.Apply();
                    File.WriteAllBytes("res.png", allBlocksTexture.EncodeToPNG());
                }
                else
                {
                    //bool animated = false;
                    // test if animated
                    for (int i = 0; i < 32; i++)
                    {
                        string frameITexture = texturePath.Substring(0, texturePath.Length - ".png".Length) + i + ".png";
                        if (File.Exists(frameITexture))
                        {
                            //animated = true;

                            byte[]    imageData    = File.ReadAllBytes(frameITexture);
                            Texture2D blockTexture = new Texture2D(2, 2);
                            blockTexture.LoadImage(imageData); // (will automatically resize as needed)
                            blockTexture.Apply();

                            // convert to argb32
                            Texture2D argbTexture = new Texture2D(blockTexture.width, blockTexture.height, TextureFormat.ARGB32, false, true);
                            argbTexture.SetPixels(blockTexture.GetPixels());
                            argbTexture.Apply();
                            Color32[] argbColors = argbTexture.GetPixels32();

                            // check for transparency
                            bool isTransparent = false;
                            for (int j = 0; j < argbColors.Length; j++)
                            {
                                if (argbColors[j].a < 240) // if it is only 240/255 or higher it isn't noticable enough to actually use the transparency
                                {
                                    isTransparent = true;
                                    break;
                                }
                            }


                            // sign of id should match transparency, fix if not
                            if (isTransparent)
                            {
                                if (id > 0)
                                {
                                    _id = -uid;
                                    Debug.LogWarning("warning: detected transparent texture for block " + name + " with updated id " + id + " and texture path " + texturePath + " but we were told it is not transparent, set it to transparent anyway");
                                }
                            }
                            else
                            {
                                if (id < 0)
                                {
                                    _id = uid;
                                    Debug.LogWarning("warning: detected not transparent texture for block " + name + " with updated id " + id + " and texture path " + texturePath + " but we were told it is transparent, set it to not transparent anyway");
                                }
                            }



                            if (argbTexture.width != 16 * 2 || argbTexture.height != 16 * 3)
                            {
                                Debug.Log("rescaling texture of block " + name + " with id " + id + " and texture path " + texturePath);
                                TextureScale.Bilinear(argbTexture, 16 * 2, 16 * 3);
                            }
                            argbTexture.Apply();
                            Color[] pixels = argbTexture.GetPixels();
                            // offset x by frame count
                            allBlocksTexture.SetPixels(i * 16 * 2, (uid - 1) * 16 * 3, 16 * 2, 16 * 3, pixels);
                            allBlocksTexture.Apply();
                        }
                    }
                    Debug.LogWarning("warning: texture " + texturePath + " for block " + name + " with id " + id + " does not exist");
                }
            }
        }