Ejemplo n.º 1
0
    void ReadTextureFromArray()
    {
        if (null == tex)
        {
            return;
        }
        editorArray.Clear();

        bMiniMap = tex.mipMapBias > 0;
        for (int i = 0; i < tex.depth; i++)
        {
            var cols = tex.GetPixels(i);
            if (null == cols || cols.Length < 1)
            {
                editorArray.Add(null);
            }
            else
            {
                Debug.Log(tex.format);
                Texture2D temp = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, bMiniMap);
                temp.SetPixels(cols);
                temp.Apply();
                editorArray.Add(temp);
            }
        }
    }
Ejemplo n.º 2
0
        public void Texture2DArrayPreview(Texture2DArray textureArray, bool update)
        {
            if (textureArray == null)
            {
                return;
            }
            var fieldSettings = GetGUISettingData(() => new PWGUISettings());

            if (update)
            {
                if (fieldSettings.textures == null || fieldSettings.textures.Length < textureArray.depth)
                {
                    fieldSettings.textures = new Texture2D[textureArray.depth];
                }
                for (int i = 0; i < textureArray.depth; i++)
                {
                    Texture2D tex = new Texture2D(textureArray.width, textureArray.height, textureArray.format, false);
                    tex.SetPixels(textureArray.GetPixels(i));
                    tex.Apply();
                    fieldSettings.textures[i] = tex;
                }
            }

            if (fieldSettings.textures != null)
            {
                foreach (var tex in fieldSettings.textures)
                {
                    TexturePreview(tex, false, false, false);
                }
            }
        }
Ejemplo n.º 3
0
    IEnumerator realTimeMovie(int i)
    {
        if (realTime && GoBack == 1)
        {
            if (lateSec - fixTime > 0)
            {
                fixTime += lateSpeed;
            }
            else
            {
                fixTime  = lateSec;
                realTime = false;
            }
        }
        else if (realTime && GoBack == 0)
        {
            if (fixTime > 0)
            {
                fixTime -= lateSpeed;
            }
            else
            {
                fixTime  = 0;
                realTime = false;
            }
        }

        yield return(new WaitForSeconds((float)lateSec - fixTime));

        setTexture.SetPixels(textureArray.GetPixels(i));
        setTexture.Apply();
    }
Ejemplo n.º 4
0
        public Texture2DArray Generate(TamIdSoleImagesPack pack, List <TAMTone> tones, List <TAMMipmapLevel> levels, int layersCount)
        {
            var mipmapLevelsCount = levels.Count;
            var tonesCount        = tones.Count;
            var maxResolution     = new IntVector2(
                pack.Columns[tones[0]][levels[mipmapLevelsCount - 1]][0].width,
                pack.Columns[tones[0]][levels[mipmapLevelsCount - 1]][0].height);

            var slicesCount  = tonesCount * layersCount;
            var tamIdTexture = new Texture2DArray(maxResolution.X, maxResolution.Y, slicesCount, TextureFormat.RGBA32, true, true)
            {
                filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp
            };
            var currentResolution = maxResolution;

            for (var mipmapLevelIndex = 0; mipmapLevelIndex < mipmapLevelsCount; mipmapLevelIndex++)
            {
                for (int toneIndex = 0; toneIndex < tonesCount; toneIndex++)
                {
                    for (int layerIndex = 0; layerIndex < layersCount; layerIndex++)
                    {
                        var array = new Color[currentResolution.X * currentResolution.Y];
                        for (int x = 0; x < currentResolution.X; x++)
                        {
                            for (int y = 0; y < currentResolution.Y; y++)
                            {
                                Color newColor = pack.Columns[tones[toneIndex]][levels[mipmapLevelsCount - 1 - mipmapLevelIndex]][layerIndex].GetPixel(x, y);
                                array[x + (y * currentResolution.X)] = newColor;
                            }
                        }

                        tamIdTexture.SetPixels(array, toneIndex * layersCount + layerIndex, mipmapLevelIndex);
                    }
                }

                currentResolution = new IntVector2(currentResolution.X / 2, currentResolution.Y / 2);
            }

            var neededMipLevelsCount = (int)Mathf.Log(currentResolution.X, 2);

            for (int toneIndex = 0; toneIndex < tonesCount; toneIndex++)
            {
                for (int layerIndex = 0; layerIndex < layersCount; layerIndex++)
                {
                    var depthIndex              = toneIndex * layersCount + layerIndex;
                    var lastSetMipmapIndex      = mipmapLevelsCount - 1;
                    var lowerLevelMipmapTexture =
                        CreateAutomaticGeneratedLowerMipmaps(currentResolution * 2, tamIdTexture.GetPixels(depthIndex, lastSetMipmapIndex));

                    for (var mipmapLevelIndex = 0; mipmapLevelIndex < neededMipLevelsCount; mipmapLevelIndex++)
                    {
                        tamIdTexture.SetPixels32(lowerLevelMipmapTexture.GetPixels32(mipmapLevelIndex + 1), depthIndex, mipmapLevelIndex + mipmapLevelsCount);
                    }
                }
            }

            tamIdTexture.Apply(false);
            return(tamIdTexture);
        }
Ejemplo n.º 5
0
 public static int GetMipMapCount(this Texture2DArray texArr)
 {
     for (int i = 0; i < 100; i++)
     {
         try { texArr.GetPixels(0, i); } catch { return(i); }
     }
     return(-1);
 }
Ejemplo n.º 6
0
 void loadArray(Texture2DArray textureArray)
 {
     for (int i = 0; i <= textureArray.depth - 1; i++)
     {
         Texture2D t = new Texture2D(textureArray.width, textureArray.height);
         t.SetPixels(textureArray.GetPixels(i));
         t.Apply();
         textures.Add(t);
     }
 }
Ejemplo n.º 7
0
 internal void AddTextureArray(Texture2DArray array)
 {
     for (int i = 0; i < array.depth; i++)
     {
         var tex = new Texture2D(array.width, array.height, array.format, false);
         tex.name = array.name + "[" + i + "]";
         tex.SetPixels(array.GetPixels(i));
         tex.Apply();
         AddTexture(tex);
     }
 }
        private void Extract(string baseSourcePath, string baseTargetDir)
        {
            if (asset == null)
            {
                return;
            }

            if (!Directory.Exists(baseTargetDir))
            {
                Directory.CreateDirectory(baseTargetDir);
            }
            var baseTargetPath = baseTargetDir + "\\" + Path.GetFileName(baseSourcePath);

            for (var i = 0; i < asset.depth; i++)
            {
                var tex = new Texture2D(asset.width, asset.height, (TextureFormat)targetFormat, true);
                tex.SetPixels(asset.GetPixels(i));
                tex.Apply();
                var texTargetPath = baseTargetPath + $"_tex_{i}.png";
                if (File.Exists(texTargetPath))
                {
                    File.Delete(texTargetPath);
                }

                var t = tex.EncodeToJPG();

                // ReSharper disable once ReturnTypeCanBeEnumerable.Local
                byte[] EncodeTex()
                {
                    switch (exportType)
                    {
                    case ExportType.PNG: return(tex.EncodeToPNG());

                    case ExportType.TGA: return(tex.EncodeToTGA());

                    case ExportType.EXR: return(tex.EncodeToEXR());

                    case ExportType.JPG: return(tex.EncodeToJPG());

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                File.WriteAllBytes(texTargetPath, EncodeTex());
            }

            Debug.Log("All textures extracted.");
        }
Ejemplo n.º 9
0
    private Cubemap ToCubemap(Texture2DArray array)
    {
        CubemapFace[] faces = new CubemapFace[] {
            CubemapFace.PositiveX, CubemapFace.NegativeX,
            CubemapFace.PositiveY, CubemapFace.NegativeY,
            CubemapFace.PositiveZ, CubemapFace.NegativeZ
        };

        Cubemap cubemap = new Cubemap(array.width, array.format, false);

        for (int i = 0; i < 6; ++i)
        {
            cubemap.SetPixels(array.GetPixels(i), faces[i]);
        }

        return(cubemap);
    }
    public static int GetPixels(IntPtr l)
    {
        int result;

        try
        {
            int num = LuaDLL.lua_gettop(l);
            if (num == 2)
            {
                Texture2DArray texture2DArray = (Texture2DArray)LuaObject.checkSelf(l);
                int            arrayElement;
                LuaObject.checkType(l, 2, out arrayElement);
                Color[] pixels = texture2DArray.GetPixels(arrayElement);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, pixels);
                result = 2;
            }
            else if (num == 3)
            {
                Texture2DArray texture2DArray2 = (Texture2DArray)LuaObject.checkSelf(l);
                int            arrayElement2;
                LuaObject.checkType(l, 2, out arrayElement2);
                int miplevel;
                LuaObject.checkType(l, 3, out miplevel);
                Color[] pixels2 = texture2DArray2.GetPixels(arrayElement2, miplevel);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, pixels2);
                result = 2;
            }
            else
            {
                LuaObject.pushValue(l, false);
                LuaDLL.lua_pushstring(l, "No matched override function GetPixels to call");
                result = 2;
            }
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Ejemplo n.º 11
0
    private void OnGUI()
    {
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
        GUILayout.Label("Texure array maker", EditorStyles.boldLabel);
        baseTexture = (Texture2D)EditorGUILayout.ObjectField("Base texture image", baseTexture, typeof(Texture2D), false);
        tiles_x     = EditorGUILayout.IntField("Horizontal Tiles", tiles_x);
        tiles_y     = EditorGUILayout.IntField("Vertical Tiles", tiles_y);
        if (tiles_x <= 0)
        {
            tiles_x = 1;
        }
        if (tiles_y <= 0)
        {
            tiles_y = 1;
        }

        mipmaps   = EditorGUILayout.Toggle("Enable Mipmaps", mipmaps);
        skipEmpty = EditorGUILayout.Toggle("Skip Empty", skipEmpty);

        scaleMode = (ScaleMode)EditorGUILayout.EnumPopup("Scale mode", scaleMode);
        int scale = 1;

        switch (scaleMode)
        {
        case ScaleMode.None:
            scale = 1;
            break;

        case ScaleMode.HQ2x:
            scale = 2;
            break;

        case ScaleMode.HQ3x:
            scale = 3;
            break;

        case ScaleMode.HQ4x:
            scale = 4;
            break;

        default:
            break;
        }
        if (baseTexture != null)
        {
            int sourceWidth  = baseTexture.width / tiles_x;
            int sourceHeight = baseTexture.height / tiles_y;
            GUILayout.Label("Source Width: " + sourceWidth);
            GUILayout.Label("Source Height: " + sourceHeight);

            int scaledWidth  = sourceWidth * scale;
            int scaledHeight = sourceHeight * scale;
            GUILayout.Label("Scaled Width: " + scaledWidth);
            GUILayout.Label("Scaled Height: " + scaledHeight);

            int potWidth  = Mathf.ClosestPowerOfTwo(scaledWidth);
            int potHeight = Mathf.ClosestPowerOfTwo(scaledHeight);
            GUILayout.Label("Final Width: " + potWidth);
            GUILayout.Label("Final Height: " + potHeight);

            if (GUILayout.Button("Build Array"))
            {
                var tempList = new List <Texture2D>();
                for (int y = tiles_y - 1; y >= 0; y--)
                {
                    for (int x = 0; x < tiles_x; x++)
                    {
                        var pixels = baseTexture.GetPixels(sourceWidth * x, sourceHeight * y, sourceWidth, sourceHeight);
                        if (skipEmpty)
                        {
                            float totalAlpha = 0;
                            foreach (var pixel in pixels)
                            {
                                totalAlpha += pixel.a;
                            }
                            if (totalAlpha < 1) //it's empty, bro.
                            {
                                continue;
                            }
                        }
                        var tempTex = new Texture2D(sourceWidth, sourceHeight, TextureFormat.ARGB32, false);
                        tempTex.SetPixels(pixels);
                        switch (scaleMode)
                        {
                        case ScaleMode.HQ2x:
                            HqxSharp.Scale2(tempTex);
                            break;

                        case ScaleMode.HQ3x:
                            HqxSharp.Scale3(tempTex);
                            break;

                        case ScaleMode.HQ4x:
                            HqxSharp.Scale4(tempTex);
                            break;

                        default:
                            break;
                        }
                        TextureScale.Bilinear(tempTex, potWidth, potHeight);
                        tempList.Add(tempTex);
                    }
                }
                texArray = new Texture2DArray(potWidth, potHeight, tempList.Count, TextureFormat.ARGB32, mipmaps);
                for (int i = 0; i < tempList.Count; i++)
                {
                    texArray.SetPixels(tempList[i].GetPixels(), i);
                }
                texArray.Apply();
                texArray.name = baseTexture.name + "Array";
                tempList.Clear();
                Resources.UnloadUnusedAssets();
            }
        }

        texArray = (Texture2DArray)EditorGUILayout.ObjectField("Texture Array", texArray, typeof(Texture2DArray), true);

        if (texArray != null)
        {
            previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, texArray.depth - 1);
            if (previewTexture == null)
            {
                previewTexture = new Texture2D(texArray.width, texArray.height, TextureFormat.ARGB32, false);
            }
            if (previewTexture.width != texArray.width || previewTexture.height != texArray.height)
            {
                previewTexture.Resize(texArray.width, texArray.height);
            }

            var previewPixels = texArray.GetPixels(previewIndex);

            for (int i = 0; i < previewPixels.Length; i++)
            {
                previewPixels[i] = Color.Lerp(Color.magenta, previewPixels[i], previewPixels[i].a);
            }

            previewTexture.SetPixels(previewPixels);
            previewTexture.Apply();

            EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, previewTexture.height), previewTexture, null, UnityEngine.ScaleMode.ScaleToFit);

            if (GUILayout.Button("Save Texture Array"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", texArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to.");
                AssetDatabase.CreateAsset(texArray, path);
                AssetDatabase.Refresh();
            }
            if (GUILayout.Button("Generate Bump"))
            {
                normalArray = new Texture2DArray(texArray.width, texArray.height, texArray.depth, TextureFormat.ARGB32, true, true);
                for (int i = 0; i < normalArray.depth; i++)
                {
                    normalArray.SetPixels(TextureTools.Bevel(texArray.GetPixels(i), texArray.width, texArray.height), i);
                }
                normalArray.Apply();
            }
        }
        if (normalArray != null)
        {
            previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, normalArray.depth - 1);
            if (normalPreviewTexture == null)
            {
                normalPreviewTexture = new Texture2D(normalArray.width, normalArray.height, TextureFormat.ARGB32, false);
            }
            if (normalPreviewTexture.width != normalArray.width || normalPreviewTexture.height != normalArray.height)
            {
                normalPreviewTexture.Resize(normalArray.width, normalArray.height);
            }

            var previewPixels = normalArray.GetPixels(previewIndex);

            for (int i = 0; i < previewPixels.Length; i++)
            {
                Color color = previewPixels[i];
                previewPixels[i] = new Color(color.a, color.g, Mathf.Sqrt(1 - (color.a * 2 - 1) * (color.a * 2 - 1) - (color.g * 2 - 1) * (color.g * 2 - 1)));
            }

            normalPreviewTexture.SetPixels(previewPixels);
            normalPreviewTexture.Apply();

            EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, normalPreviewTexture.height), normalPreviewTexture, null, UnityEngine.ScaleMode.ScaleToFit);

            if (GUILayout.Button("Save Texture Array"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", normalArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to.");
                AssetDatabase.CreateAsset(normalArray, path);
                AssetDatabase.Refresh();
            }
        }
        EditorGUILayout.EndScrollView();
    }
Ejemplo n.º 12
0
        public Texture2D GetTexture2D()
        {
            Texture2D newTexture2D = null;

            if (texture is Cubemap)
            {
                var format = TextureFormat.RGBA32;
                if (isHDR)
                {
                    format = TextureFormat.RGBAFloat;
                }
                newTexture2D = new Texture2D(texture.width * 6, texture.height, format, texture.mipmapCount, true);

                for (var mipLevel = 0; mipLevel < texture.mipmapCount; mipLevel++)
                {
                    var width  = (int)(texture.width / Math.Pow(2, mipLevel));
                    var height = (int)(texture.height / Math.Pow(2, mipLevel));

                    var array     = new Texture2DArray(texture.width, texture.height, 6, texture.graphicsFormat, UnityEngine.Experimental.Rendering.TextureCreationFlags.MipChain);
                    var allPixels = new List <Color[]>();
                    for (var i = 0; i < 6; i++)
                    {
                        Graphics.CopyTexture(texture, i, array, i);

                        allPixels.Add(array.GetPixels(i, mipLevel));
                    }

                    var pixels = new List <Color>();
                    for (var h = 0; h < height; h++)
                    {
                        for (var fi = 0; fi < 6; fi++)
                        {
                            for (var w = 0; w < width; w++)
                            {
                                var colors = allPixels[fi];
                                pixels.Add(colors[w + h * width]);
                            }
                        }
                    }
                    newTexture2D.SetPixels(pixels.ToArray(), mipLevel);
                }
            }
            else if (texture is Texture2D)
            {
                if (!texture.isReadable)
                {
                    newTexture2D = new Texture2D(texture.width, texture.height, texture.graphicsFormat, UnityEngine.Experimental.Rendering.TextureCreationFlags.MipChain);
                    Graphics.CopyTexture(texture, newTexture2D);
                }
                else
                {
                    newTexture2D = texture as Texture2D;
                }

                if (extName == ".psd")
                {
                    flipY = !flipY;
                }
            }

            return(newTexture2D);
        }