Ejemplo n.º 1
0
 public void Add(Texture tex, PartToolsLib.TextureType type)
 {
     if (!Contains(tex))
     {
         Add(new TextureDummy(tex, type));
     }
 }
Ejemplo n.º 2
0
        public static bool Write2D(Texture texture, string newPath, PartToolsLib.TextureType texType)
        {
            string oldPath = AssetDatabase.GetAssetPath(texture);

            TextureImporter texImporter = (TextureImporter)AssetImporter.GetAtPath(oldPath);

            TextureImporterSettings texSettingsBackup = new TextureImporterSettings();

            texImporter.ReadTextureSettings(texSettingsBackup);


            if (texImporter == null)
            {
                return(false);
            }

            texImporter.isReadable    = true;
            texImporter.mipmapEnabled = false;

            bool wasNormalMap = texImporter.convertToNormalmap;

            texImporter.convertToNormalmap = false;

            // force the update of the above settings
            AssetDatabase.ImportAsset(oldPath, ImportAssetOptions.ForceUpdate);


            if (texType == PartToolsLib.TextureType.NormalMap && wasNormalMap)
            {
                WriteTexture2D(ConvertTextureToNormal((Texture2D)texture, texImporter.heightmapScale), texType, newPath);
            }
            else
            {
                WriteTexture2D((Texture2D)texture, texType, newPath);
            }


            // reapply the old settings
            texImporter.SetTextureSettings(texSettingsBackup);
            AssetDatabase.ImportAsset(oldPath, ImportAssetOptions.ForceUpdate);

            return(true);
        }
Ejemplo n.º 3
0
        static void AddTextureInstance(BinaryWriter bw, Material mat, string textureName, PartToolsLib.TextureType type)
        {
            Texture tex = mat.GetTexture(textureName);

            if (tex != null)
            {
                if (!textures.Contains(tex))
                {
                    textures.Add(tex, type);
                }

                bw.Write(textures.IndexOf(tex));
            }
            else
            {
                bw.Write(-1);
            }
        }
Ejemplo n.º 4
0
        static void WriteMaterialTexture(BinaryWriter bw, Material mat, string textureName, PartToolsLib.TextureType type)
        {
            AddTextureInstance(bw, mat, textureName, type);

            Vector2 tempV2 = mat.GetTextureScale(textureName);

            bw.Write(tempV2.x);
            bw.Write(tempV2.y);

            tempV2 = mat.GetTextureOffset(textureName);
            bw.Write(tempV2.x);
            bw.Write(tempV2.y);
        }
Ejemplo n.º 5
0
 public TextureDummy(Texture texture, PartToolsLib.TextureType type)
 {
     this.texture = texture;
     this.type    = type;
 }
Ejemplo n.º 6
0
        private static void WriteTexture2D(Texture2D texture, PartToolsLib.TextureType texType, string newPath)
        {
            BinaryWriter bw = new BinaryWriter(File.Open(newPath, FileMode.Create));

            // write header
            bw.Write("KSP");
            bw.Write(texture.width);
            bw.Write(texture.height);
            bw.Write((int)texType);


            Color32[] colors  = texture.GetPixels32(0);
            int       nPixels = colors.Length;
            Color32   color;

            if (texType == PartToolsLib.TextureType.Texture)
            {
                bool writeAlpha = false;
                switch (texture.format)
                {
                case TextureFormat.ARGB32:
                case TextureFormat.RGBA32:
                case TextureFormat.DXT5:

                    writeAlpha = true;
                    bw.Write(32);

                    break;

                case TextureFormat.DXT1:
                case TextureFormat.RGB24:

                    writeAlpha = false;
                    bw.Write(24);

                    break;
                }

                for (int i = 0; i < nPixels; i++)
                {
                    color = colors[i];

                    bw.Write(color.r);
                    bw.Write(color.g);
                    bw.Write(color.b);

                    if (writeAlpha)
                    {
                        bw.Write(color.a);
                    }

                    if (i % texture.height == 0)
                    {
                        bw.Flush();
                    }
                }
            }
            else if (texType == PartToolsLib.TextureType.NormalMap)
            {
                bw.Write(32);

                for (int i = 0; i < nPixels; i++)
                {
                    color = colors[i];

                    bw.Write(color.r);
                    bw.Write(color.g);
                    bw.Write(color.b);
                    bw.Write(color.a);

                    if (i % texture.height == 0)
                    {
                        bw.Flush();
                    }
                }
            }

            bw.Close();
        }