Esempio n. 1
0
        public static Texture DuplicateTexture(Texture source, bool copyContent = true)
        {
            TextureCreationFlags flags = source.mipmapCount > 1 ? TextureCreationFlags.MipChain : TextureCreationFlags.None;

            switch (source)
            {
            case Texture2D t2D:
                var new2D = new Texture2D(t2D.width, t2D.height, t2D.graphicsFormat, t2D.mipmapCount, flags);
                CopyCommonTextureSettings(source, new2D);

                if (copyContent)
                {
                    for (int mipLevel = 0; mipLevel < t2D.mipmapCount; mipLevel++)
                    {
                        new2D.SetPixelData(t2D.GetPixelData <byte>(mipLevel), mipLevel);
                    }
                }

                return(new2D);

            case Texture3D t3D:
                var new3D = new Texture3D(t3D.width, t3D.height, t3D.depth, t3D.graphicsFormat, flags, t3D.mipmapCount);
                CopyCommonTextureSettings(source, new3D);

                if (copyContent)
                {
                    for (int mipLevel = 0; mipLevel < t3D.mipmapCount; mipLevel++)
                    {
                        new3D.SetPixelData(t3D.GetPixelData <byte>(mipLevel), mipLevel);
                    }
                }

                return(new3D);

            case Cubemap cube:
                var newCube = new Cubemap(cube.width, cube.graphicsFormat, flags, cube.mipmapCount);
                CopyCommonTextureSettings(source, newCube);

                if (copyContent)
                {
                    for (int slice = 0; slice < 6; slice++)
                    {
                        for (int mipLevel = 0; mipLevel < cube.mipmapCount; mipLevel++)
                        {
                            newCube.SetPixelData(cube.GetPixelData <byte>(mipLevel, (CubemapFace)slice), mipLevel, (CubemapFace)slice);
                        }
                    }
                }

                return(newCube);

            case CustomRenderTexture rt:
                var newRT = new CustomRenderTexture(rt.width, rt.height, rt.graphicsFormat);
                newRT.dimension   = rt.dimension;
                newRT.depth       = rt.depth;
                newRT.volumeDepth = rt.volumeDepth;
                CopyCommonTextureSettings(source, newRT);
                newRT.enableRandomWrite = rt.enableRandomWrite;

                if (copyContent)
                {
                    for (int slice = 0; slice < TextureUtils.GetSliceCount(rt); slice++)
                    {
                        for (int mipLevel = 0; mipLevel < rt.mipmapCount; mipLevel++)
                        {
                            Graphics.CopyTexture(rt, slice, mipLevel, newRT, slice, mipLevel);
                        }
                    }
                }

                return(newRT);

            default:
                throw new System.Exception("Can't duplicate texture of type " + source.GetType());
            }

            void CopyCommonTextureSettings(Texture source, Texture destination)
            {
                destination.name       = source.name;
                destination.wrapMode   = source.wrapMode;
                destination.filterMode = source.filterMode;
                destination.wrapModeU  = source.wrapModeU;
                destination.wrapModeV  = source.wrapModeV;
                destination.wrapModeW  = source.wrapModeW;
                destination.anisoLevel = source.anisoLevel;
            }
        }
Esempio n. 2
0
    private AlienTexture LoadTexture(int EntryIndex, bool loadV1 = true)
    {
        AlienTexture toReturn = new AlienTexture();

        if (EntryIndex < 0 || EntryIndex >= LevelTextures.PAK.Header.EntryCount)
        {
            Debug.LogWarning("Asked to load texture at index " + EntryIndex + ", which is out of bounds!");
            return(null);
        }

        alien_pak_entry           Entry     = LevelTextures.PAK.Entries[EntryIndex];
        alien_texture_bin_texture InTexture = LevelTextures.BIN.Textures[Entry.BINIndex];

        Vector2 textureDims;
        int     textureLength = 0;
        int     mipLevels     = 0;

        if (loadV1)
        {
            textureDims   = new Vector2(InTexture.Size_V1[0], InTexture.Size_V1[1]);
            textureLength = InTexture.Length_V1;
            mipLevels     = InTexture.MipLevelsV1;
        }
        else
        {
            textureDims   = new Vector2(InTexture.Size_V2[0], InTexture.Size_V2[1]);
            textureLength = InTexture.Length_V2;
            mipLevels     = InTexture.MipLevelsV2;
        }

        if (textureLength == 0)
        {
            Debug.LogWarning("LENGTH ZERO - NOT LOADING");
            return(toReturn);
        }

        UnityEngine.TextureFormat format = UnityEngine.TextureFormat.BC7;
        switch (InTexture.Format)
        {
        case alien_texture_format.Alien_R32G32B32A32_SFLOAT:
            format = UnityEngine.TextureFormat.RGBA32;
            break;

        case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM:
            format = UnityEngine.TextureFormat.ETC2_RGBA8;     //?
            break;

        case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM_0:
            format = UnityEngine.TextureFormat.ETC2_RGBA8;     //?
            break;

        case alien_texture_format.Alien_FORMAT_SIGNED_DISTANCE_FIELD:
            Debug.LogWarning("SDF! NOT LOADED");
            return(toReturn);

        case alien_texture_format.Alien_FORMAT_R8:
            format = UnityEngine.TextureFormat.R8;
            break;

        case alien_texture_format.Alien_FORMAT_BC1:
            format = UnityEngine.TextureFormat.DXT1;
            break;

        case alien_texture_format.Alien_FORMAT_BC2:
            Debug.LogWarning("BC2! NOT LOADED");
            return(toReturn);

        case alien_texture_format.Alien_FORMAT_BC5:
            format = UnityEngine.TextureFormat.BC5;     //Is this correct?
            break;

        case alien_texture_format.Alien_FORMAT_BC3:
            format = UnityEngine.TextureFormat.DXT5;
            break;

        case alien_texture_format.Alien_FORMAT_BC7:
            format = UnityEngine.TextureFormat.BC7;
            break;

        case alien_texture_format.Alien_FORMAT_R8G8:
            format = UnityEngine.TextureFormat.BC5;     // is this correct?
            break;
        }

        BinaryReader tempReader = new BinaryReader(new MemoryStream(LevelTextures.PAK.DataStart));

        tempReader.BaseStream.Position = Entry.Offset;

        if (InTexture.Type == 7)
        {
            Cubemap cubemapTex = new Cubemap((int)textureDims.x, format, true);
            cubemapTex.name = LevelTextures.BIN.TextureFilePaths[Entry.BINIndex];
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveX);
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeX);
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveY);
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeY);
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveZ);
            cubemapTex.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeZ);
            cubemapTex.Apply();
            toReturn.cubemap = cubemapTex;
            //AssetDatabase.CreateAsset(cubemapTex, "Assets/Cubemaps/" + Path.GetFileNameWithoutExtension(cubemapTex.name) + ".cubemap");
        }
        else
        {
            Texture2D texture = new Texture2D((int)textureDims[0], (int)textureDims[1], format, mipLevels, true);
            texture.name = LevelTextures.BIN.TextureFilePaths[Entry.BINIndex];
            texture.LoadRawTextureData(tempReader.ReadBytes(textureLength));
            texture.Apply();
            toReturn.texture = texture;
        }

        tempReader.Close();
        return(toReturn);
    }
Esempio n. 3
0
    private void LoadTextureAssets()
    {
        bool[] textureTracker = new bool[levelData.LevelTextures.BIN.Header.EntryCount];
        AssetDatabase.StartAssetEditing();
        for (int i = 0; i < levelData.LevelTextures.PAK.Entries.Count; i++)
        {
            alien_pak_entry           Entry     = levelData.LevelTextures.PAK.Entries[i];
            alien_texture_bin_texture InTexture = levelData.LevelTextures.BIN.Textures[Entry.BINIndex];

            Vector2 textureDims;
            int     textureLength = 0;
            int     mipLevels     = 0;

            if (!textureTracker[Entry.BINIndex])
            {
                textureDims   = new Vector2(InTexture.Size_V1[0], InTexture.Size_V1[1]);
                textureLength = InTexture.Length_V1;
                mipLevels     = InTexture.MipLevelsV1;
            }
            else
            {
                textureDims   = new Vector2(InTexture.Size_V2[0], InTexture.Size_V2[1]);
                textureLength = InTexture.Length_V2;
                mipLevels     = InTexture.MipLevelsV2;
            }
            textureTracker[Entry.BINIndex] = true;

            if (textureLength == 0)
            {
                continue;
            }
            if (InTexture.Format == alien_texture_format.Alien_FORMAT_SIGNED_DISTANCE_FIELD || InTexture.Format == alien_texture_format.Alien_FORMAT_BC2)
            {
                continue;
            }

            TextureFormat format = TextureFormat.BC7;
            switch (InTexture.Format)
            {
            case alien_texture_format.Alien_R32G32B32A32_SFLOAT:
                format = TextureFormat.RGBA32;
                break;

            case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM:
                format = TextureFormat.ETC2_RGBA8;     //?
                break;

            case alien_texture_format.Alien_FORMAT_R8G8B8A8_UNORM_0:
                format = TextureFormat.ETC2_RGBA8;     //?
                break;

            case alien_texture_format.Alien_FORMAT_R8:
                format = TextureFormat.R8;
                break;

            case alien_texture_format.Alien_FORMAT_BC1:
                format = TextureFormat.DXT1;
                break;

            case alien_texture_format.Alien_FORMAT_BC5:
                format = TextureFormat.BC5;     //Is this correct?
                break;

            case alien_texture_format.Alien_FORMAT_BC3:
                format = TextureFormat.DXT5;
                break;

            case alien_texture_format.Alien_FORMAT_BC7:
                format = TextureFormat.BC7;
                break;

            case alien_texture_format.Alien_FORMAT_R8G8:
                format = TextureFormat.BC5;     // is this correct?
                break;
            }

            BinaryReader tempReader = new BinaryReader(new MemoryStream(levelData.LevelTextures.PAK.DataStart));
            tempReader.BaseStream.Position = Entry.Offset;
            if (InTexture.Type == 7)
            {
                Cubemap cubemap = new Cubemap((int)textureDims.x, format, true);
                cubemap.name = levelData.LevelTextures.BIN.TextureFilePaths[Entry.BINIndex];
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveX);
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeX);
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveY);
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeY);
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.PositiveZ);
                cubemap.SetPixelData(tempReader.ReadBytes(textureLength / 6), 0, CubemapFace.NegativeZ);
                cubemap.Apply();

                string fullFilePath  = "Assets/Resources/" + SharedVals.instance.LevelName + "/Cubemaps/" + cubemap.name.Substring(0, cubemap.name.Length - Path.GetExtension(cubemap.name).Length) + ".cubemap";
                string fileDirectory = GetDirectory(fullFilePath);
                if (!Directory.Exists(fileDirectory))
                {
                    Directory.CreateDirectory(fileDirectory);
                }
                AssetDatabase.CreateAsset(cubemap, fullFilePath);
            }
            else
            {
                Texture2D texture = new Texture2D((int)textureDims[0], (int)textureDims[1], format, mipLevels, true);
                texture.name = levelData.LevelTextures.BIN.TextureFilePaths[Entry.BINIndex];
                texture.LoadRawTextureData(tempReader.ReadBytes(textureLength));
                texture.Apply();

                string fullFilePath  = "Assets/Resources/" + SharedVals.instance.LevelName + "/Textures/" + texture.name.Substring(0, texture.name.Length - Path.GetExtension(texture.name).Length) + ".asset";
                string fileDirectory = GetDirectory(fullFilePath);
                if (!Directory.Exists(fileDirectory))
                {
                    Directory.CreateDirectory(fileDirectory);
                }
                AssetDatabase.CreateAsset(texture, fullFilePath);
            }
            tempReader.Close();
        }
        AssetDatabase.StopAssetEditing();
    }