Skip to content

Commit

Permalink
Got texture loading from disk fully 100% working. Not done yet: Textu…
Browse files Browse the repository at this point in the history
…re scaling.
  • Loading branch information
RosaryMala committed Jul 3, 2015
1 parent 5cadb2b commit a308235
Show file tree
Hide file tree
Showing 58 changed files with 684 additions and 117 deletions.
8 changes: 7 additions & 1 deletion Assets/Lava_Texture_preview.jpg.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Assets/MapGen/ContentConfiguration/TileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class TileConfiguration<T> : ContentConfiguration<T> where T : IContent, new()
{
TiletypeMatcher<Content> tiletypeMatcher = new TiletypeMatcher<Content>();
Content defaultTile = new Content();

protected override void ParseElementConditions(XElement elemtype, Content content)
{
Expand All @@ -14,7 +15,10 @@ protected override void ParseElementConditions(XElement elemtype, Content conten
XAttribute elemToken = elemTiletype.Attribute("token");
if (elemToken != null)
{
tiletypeMatcher[elemToken.Value] = content;
if (elemToken.Value == "NONE")
defaultTile = content;
else
tiletypeMatcher[elemToken.Value] = content;
continue;
}
}
Expand All @@ -28,7 +32,7 @@ public override bool GetValue(MapDataStore.Tile tile, MeshLayer layer, out T val
value = cont.GetValue(tile, layer);
return true;
}
value = default(T);
value = defaultTile.GetValue(tile, layer);
return false;
}
}
2 changes: 1 addition & 1 deletion Assets/MapGen/ContentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool ParseContentXMLFile(string path)
public void FinalizeTextureAtlases()
{
materialTextureStorage.BuildAtlas("MaterialTexture");
shapeTextureStorage.BuildAtlas("ShapeTexture");
shapeTextureStorage.BuildAtlas("ShapeTexture", TextureFormat.RGBA32, new Color(1.0f, 0.5f, 0.0f, 0.5f));

GameMap gameMap = GameObject.FindObjectOfType<GameMap>();
gameMap.basicTerrainMaterial.SetTexture("_MainTex", materialTextureStorage.AtlasTexture);
Expand Down
37 changes: 33 additions & 4 deletions Assets/MapGen/ContentType/NormalContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,34 @@ public bool AddTypeElement(System.Xml.Linq.XElement elemtype)
}

byte[] normalData = File.ReadAllBytes(normalPath);

Texture2D normalMap = new Texture2D(2, 2);
normalMap.LoadImage(normalData);

XAttribute specularAtt = elemtype.Attribute("specular");
if (specularAtt == null)
{
Debug.LogError("No specular map in " + elemtype);
//Add error message here
return false;
}
string specularPath = Path.Combine(Path.GetDirectoryName(new Uri(elemtype.BaseUri).LocalPath), specularAtt.Value);
specularPath = Path.GetFullPath(specularPath);

if (!File.Exists(specularPath))
{
Debug.LogError("File not found: " + specularPath);
return false;
}

byte[] specularData = File.ReadAllBytes(specularPath);
Texture2D specularMap = new Texture2D(2, 2);
specularMap.LoadImage(specularData);

if ((specularMap.width != normalMap.width) || (specularMap.height != normalMap.height))
{
TextureScale.Bilinear(specularMap, normalMap.width, normalMap.height);
}

XAttribute occlusionAtt = elemtype.Attribute("occlusion");
if (occlusionAtt == null)
{
Expand All @@ -69,19 +94,23 @@ public bool AddTypeElement(System.Xml.Linq.XElement elemtype)

if (occlusionMap.width != normalMap.width || occlusionMap.height != normalMap.height)
{
occlusionMap.Resize(normalMap.width, normalMap.height);
TextureScale.Bilinear(occlusionMap, normalMap.width, normalMap.height);
}

Texture2D combinedMap = new Texture2D(normalMap.width, normalMap.height, TextureFormat.ARGB32, false);

combinedMap.name = normalPath + occlusionAtt.Value;
combinedMap.name = normalPath + occlusionAtt.Value + specularAtt.Value;

Color[] normalColors = normalMap.GetPixels();
Color[] occlusionColors = occlusionMap.GetPixels();
Color[] specularColors = specularMap.GetPixels();

if (normalColors.Length != specularColors.Length)
Debug.LogError("Maps aren't same size!");

for (int i = 0; i < normalColors.Length; i++)
{
normalColors[i].a = occlusionColors[i].r;
normalColors[i] = new Color(occlusionColors[i].r, normalColors[i].g, specularColors[i].r, normalColors[i].r);
}

combinedMap.SetPixels(normalColors);
Expand Down
2 changes: 1 addition & 1 deletion Assets/MapGen/Meshing/BlockMesher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void FillMeshBuffer(out MeshCombineUtility.MeshInstance buffer, MeshLayer layer,
shapeTextTransform = tileTexContent.UVTransform;
Matrix4x4 matTexTransform = Matrix4x4.identity;
TextureContent matTexContent;
if (contentLoader.MaterialTextureConfiguration.GetValue (tile, layer, out matTexContent))
if (contentLoader.MaterialTextureConfiguration.GetValue(tile, layer, out matTexContent))
matTexTransform = matTexContent.UVTransform;
ColorContent newColorContent;
Color newColor;
Expand Down
30 changes: 28 additions & 2 deletions Assets/MapGen/TextureAtlas/AtlasCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,24 @@ public static void SaveAtlas(Atlas atlas, string name)
Debug.Log("SAVE TO: " + file);
}

public static Atlas[] CreateAtlas(string name, Texture2D[] textures, Atlas startWith = null)
public static void SaveImage(Texture2D atlas, string name)
{
if (atlas == null)
return;

var bytes = atlas.EncodeToPNG();

if (!System.IO.Directory.Exists("./Debug/"))
System.IO.Directory.CreateDirectory("./Debug/");

//string name = Time.realtimeSinceStartup.ToString().Replace(".", "-"); //DateTime.UtcNow.ToString().Replace("/", "_").Replace(" ", "_").Replace("\\", "_");
string file = "./Debug/" + name + ".png";

System.IO.File.WriteAllBytes(file, bytes);
Debug.Log("SAVE TO: " + file);
}

public static Atlas[] CreateAtlas(string name, Texture2D[] textures, Atlas startWith = null, TextureFormat format = TextureFormat.RGBA32, Color defaultColor = default(Color))
{
List<Texture2D> toProcess = new List<Texture2D>();
toProcess.AddRange(textures);
Expand All @@ -278,7 +295,16 @@ public static Atlas[] CreateAtlas(string name, Texture2D[] textures, Atlas start
if (_atlas == null)
{
_atlas = new Atlas();
_atlas.texture = new Texture2D(AtlasSize, AtlasSize, TextureFormat.RGBA32, true);
_atlas.texture = new Texture2D(AtlasSize, AtlasSize, format, true);
if(defaultColor != default(Color))
{
Color[] fillcolors = new Color[AtlasSize * AtlasSize];
for(int i = 0; i < fillcolors.Length; i++)
{
fillcolors[i] = defaultColor;
}
_atlas.texture.SetPixels(fillcolors);
}
_atlas.texture.filterMode = FilterMode.Bilinear;
_atlas.root = new AtlasNode();
_atlas.root.rc = new Rect(0, 0, AtlasSize, AtlasSize);
Expand Down
7 changes: 3 additions & 4 deletions Assets/MapGen/TextureAtlas/TextureStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public int AddTexture(Texture2D tex)
return textureList.Count - 1;
}

public void BuildAtlas(string name)
public void BuildAtlas(string name, TextureFormat format = TextureFormat.RGBA32, Color defaultColor = default(Color))
{
Debug.Log("Making atlas from " + textureList.Count + " items");
AtlasCreator.Atlas[] atlasList = AtlasCreator.CreateAtlas(name, textureList.ToArray());
AtlasCreator.Atlas[] atlasList = AtlasCreator.CreateAtlas(name, textureList.ToArray(), null, format, defaultColor);
atlas = atlasList[0];
textureList.Clear();

Expand All @@ -57,6 +56,6 @@ public void BuildAtlas(string name)
texIndexToAtlasIndex[item.Key] = nameToAtlasIndex[item.Value];
}

AtlasCreator.SaveAtlas(atlas, name);
//AtlasCreator.SaveAtlas(atlas, name);
}
}
145 changes: 145 additions & 0 deletions Assets/Materials/New Material.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: New Material
m_Shader: {fileID: 4800000, guid: fe63e4b8831140e4a9b0467514532904, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 5
m_CustomRenderQueue: -1
stringTagMap: {}
m_SavedProperties:
serializedVersion: 2
m_TexEnvs:
data:
first:
name: _MainTex
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _BumpMap
second:
m_Texture: {fileID: 2800000, guid: e263455f560607449ab77e83670280da, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _DetailNormalMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _ParallaxMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _OcclusionMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _EmissionMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _DetailMask
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _DetailAlbedoMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _MetallicGlossMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _Shapetex
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
data:
first:
name: _SrcBlend
second: 1
data:
first:
name: _DstBlend
second: 0
data:
first:
name: _Cutoff
second: .5
data:
first:
name: _Parallax
second: .0199999996
data:
first:
name: _ZWrite
second: 1
data:
first:
name: _Glossiness
second: .5
data:
first:
name: _BumpScale
second: 1
data:
first:
name: _OcclusionStrength
second: 1
data:
first:
name: _DetailNormalMapScale
second: 1
data:
first:
name: _UVSec
second: 0
data:
first:
name: _Mode
second: 0
data:
first:
name: _Metallic
second: 0
m_Colors:
data:
first:
name: _EmissionColor
second: {r: 0, g: 0, b: 0, a: 1}
data:
first:
name: _Color
second: {r: 1, g: 1, b: 1, a: 1}
8 changes: 8 additions & 0 deletions Assets/Materials/New Material.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/Materials/StandardMaterial.mat
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ Material:
m_Offset: {x: 0, y: 0}
data:
first:
name: _Shapetex
name: _SpecTex
second:
m_Texture: {fileID: 0}
m_Texture: {fileID: 2800000, guid: 3a9bc543b0449a14f9076c7c788c8b88, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _SpecTex
name: _AO
second:
m_Texture: {fileID: 2800000, guid: 3a9bc543b0449a14f9076c7c788c8b88, type: 3}
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
data:
first:
name: _AO
name: _Shapetex
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
Expand Down
Loading

0 comments on commit a308235

Please sign in to comment.