Skip to content

Commit

Permalink
GrassEditor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GER-Space committed Nov 13, 2019
1 parent a271b0a commit e887d6c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("1.8.1.3")]
[assembly: AssemblyInformationalVersion("1.8.1.3")]
[assembly: AssemblyFileVersion("1.8.1.4")]
[assembly: AssemblyInformationalVersion("1.8.1.4")]
[assembly: KSPAssembly("KerbalKonstructs", 0, 9)]

4 changes: 4 additions & 0 deletions src/UI2/Editor/Instance/GrassEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ internal static void CreateContent()
new DialogGUILabel("Tarmac: G ", HighLogic.UISkin.label),
new DialogGUISlider(GetTarmacFloatG, 0, 1, false, 140, 25, SetTarmacFloatG),
new DialogGUITextInput("0", false, 10, SetTarmacGStr, GetTarmacGStr, TMPro.TMP_InputField.ContentType.DecimalNumber, 25)),
new DialogGUIHorizontalLayout(
new DialogGUILabel("Tarmac: B ", HighLogic.UISkin.label),
new DialogGUISlider(GetTarmacFloatB, 0, 1, false, 140, 25, SetTarmacFloatB),
new DialogGUITextInput("0", false, 10, SetTarmacBStr, GetTarmacBStr, TMPro.TMP_InputField.ContentType.DecimalNumber, 25)),
new DialogGUIHorizontalLayout(
new DialogGUILabel("Tarmac Tiling: U: ", HighLogic.UISkin.label),
new DialogGUITextInput("0", false, 10, SetTarmacUStr, GetTarmacUStr, TMPro.TMP_InputField.ContentType.DecimalNumber, 25),
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities/ClassExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public static Transform FindRecursive(this Transform transform, string name)
//return null;
}



public static Transform FindRecursiveContains(this Transform transform, string name)
{
foreach (Transform child in transform.gameObject.GetComponentsInChildren<Transform>(true))
Expand Down
63 changes: 55 additions & 8 deletions src/Utilities/KKGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class KKGraphics
private static Dictionary<string, Texture2D> cachedTextures = new Dictionary<string, Texture2D>();
private static Dictionary<string, Material> cachedMaterials = new Dictionary<string, Material>();

private static List<string> imageExtentions = new List<string> { ".png", ".tga", ".jpg" };
private static List<string> imageExtentions = new List<string> { ".png", ".tga", ".jpg" , ".dds" };

private static Dictionary<string, Texture> builtinTextures = new Dictionary<string, Texture>();
private static bool texturesAreCached = false;
Expand Down Expand Up @@ -345,21 +345,28 @@ internal static Texture2D GetTexture(string textureName, bool asNormal = false,
{

Texture2D tmpTexture = null;
string fileExtension = GetImageExtention(textureName);

//// Otherwise search the game database for one loaded from GameData/
if (GameDatabase.Instance.ExistsTexture(textureName) && (GetImageExtention(textureName) != null))
if (GameDatabase.Instance.ExistsTexture(textureName) && (fileExtension != null))
{
// Get the texture URL
tmpTexture = GameDatabase.Instance.GetTexture(textureName, asNormal);


foundTexture = new Texture2D(tmpTexture.width, tmpTexture.height, TextureFormat.ARGB32, createMibMaps);
foundTexture.LoadImage(System.IO.File.ReadAllBytes("GameData/" + textureName + GetImageExtention(textureName)), false);
if (fileExtension == ".dds")
{
foundTexture = LoadTextureDXT(File.ReadAllBytes("GameData/" + textureName + GetImageExtention(textureName)), createMibMaps);
}
else
{
foundTexture.LoadImage(File.ReadAllBytes("GameData/" + textureName + GetImageExtention(textureName)), false);
}
foundTexture.Apply(createMibMaps, false);
}
else
{
Log.UserWarning("AdvTexture: TextureLoader faild. Fallback to GameDatabase");
Log.UserWarning("Failed: TextureLoader faild. Fallback to GameDatabase");
foundTexture = GameDatabase.Instance.GetTexture(textureName, asNormal);
}

Expand All @@ -374,6 +381,43 @@ internal static Texture2D GetTexture(string textureName, bool asNormal = false,
return foundTexture;
}


public static Texture2D LoadTextureDXT(byte[] ddsBytes, bool createMibMaps)
{
byte[] exampleByteArray = new byte[] { ddsBytes[84], ddsBytes[85], ddsBytes[86], ddsBytes[87] };
string textureFormat = System.Text.Encoding.ASCII.GetString(exampleByteArray);



if (textureFormat != "DXT1" && textureFormat != "DXT5")
{
Log.Error("Invalid TextureFormat. Only DXT1 and DXT5 formats are supported by this method.");
return null;
}
TextureFormat format = (TextureFormat)Enum.Parse(typeof(TextureFormat), textureFormat, createMibMaps);
//Log.Normal("Found DXT Texture Format: " + format.ToString());

byte ddsSizeCheck = ddsBytes[4];
if (ddsSizeCheck != 124)
{
Log.Error("Invalid DDS DXTn texture. Unable to read"); //this header byte should be 124 for DDS image files
return null;
}

int height = ddsBytes[13] * 256 + ddsBytes[12];
int width = ddsBytes[17] * 256 + ddsBytes[16];

int DDS_HEADER_SIZE = 128;
byte[] dxtBytes = new byte[ddsBytes.Length - DDS_HEADER_SIZE];
Buffer.BlockCopy(ddsBytes, DDS_HEADER_SIZE, dxtBytes, 0, ddsBytes.Length - DDS_HEADER_SIZE);

Texture2D texture = new Texture2D(width, height, format, true);
texture.LoadRawTextureData(dxtBytes);

return (texture);
}


internal static Texture2D GetBuiltinTexture(string textureName, int index)
{
Texture2D foundTexture = null;
Expand Down Expand Up @@ -447,9 +491,12 @@ internal static Material GetMaterial(string materialName)

private static string GetImageExtention(string imageName)
{
int pathIndex = (KSPUtil.ApplicationRootPath + "GameData/" + imageName).LastIndexOf('/');
string path = (KSPUtil.ApplicationRootPath + "GameData/" + imageName).Substring(0, pathIndex + 1);
string imageShortName = (KSPUtil.ApplicationRootPath + "GameData/" + imageName).Substring(pathIndex + 1);

string fullPath = KSPUtil.ApplicationRootPath + "GameData/" + imageName;

int pathIndex = (fullPath).LastIndexOf('/');
string path = (fullPath).Substring(0, pathIndex + 1);
string imageShortName = (fullPath).Substring(pathIndex + 1);

//Log.Normal("path: " + path);
//Log.Normal("imageShortName: " + imageShortName);
Expand Down

0 comments on commit e887d6c

Please sign in to comment.