Skip to content

Commit 20ceeae

Browse files
committed
Added generated normal maps to creature sprites.
1 parent 5fe8c5a commit 20ceeae

14 files changed

+244
-21
lines changed

Assets/Editor/BuildPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class BuildFactory
99
[MenuItem("Mytools/Build Release")]
1010
public static void BuildAll()
1111
{
12+
BuildRelease(BuildTarget.StandaloneWindows64);
1213
BuildRelease(BuildTarget.StandaloneOSXIntel64);
1314
BuildRelease(BuildTarget.StandaloneLinux64);
14-
BuildRelease(BuildTarget.StandaloneWindows64);
1515
}
1616

1717
[MenuItem("Mytools/Build Windows Release")]

Assets/Editor/TextureArrayMaker.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ enum ScaleMode
2020
ScaleMode scaleMode = ScaleMode.None;
2121
bool mipmaps = true;
2222
Texture2DArray texArray;
23+
Texture2DArray normalArray;
2324
int previewIndex = 0;
2425
Texture2D previewTexture;
26+
Texture2D normalPreviewTexture;
2527

2628
[MenuItem("Mytools/Texture Array Builder")]
2729
public static void BuildTextureArray()
@@ -141,6 +143,43 @@ private void OnGUI()
141143
AssetDatabase.CreateAsset(texArray, path);
142144
AssetDatabase.Refresh();
143145
}
146+
if(GUILayout.Button("Generate Bump"))
147+
{
148+
normalArray = new Texture2DArray(texArray.width, texArray.height, texArray.depth, TextureFormat.ARGB32, true, true);
149+
for(int i = 0; i < normalArray.depth; i++)
150+
{
151+
normalArray.SetPixels(TextureTools.Bevel(texArray.GetPixels(i), texArray.width, texArray.height), i);
152+
}
153+
normalArray.Apply();
154+
}
155+
}
156+
if (normalArray != null)
157+
{
158+
previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, normalArray.depth - 1);
159+
if (normalPreviewTexture == null)
160+
normalPreviewTexture = new Texture2D(normalArray.width, normalArray.height, TextureFormat.ARGB32, false);
161+
if (normalPreviewTexture.width != normalArray.width || normalPreviewTexture.height != normalArray.height)
162+
normalPreviewTexture.Resize(normalArray.width, normalArray.height);
163+
164+
var previewPixels = normalArray.GetPixels(previewIndex);
165+
166+
for(int i = 0; i < previewPixels.Length; i++)
167+
{
168+
Color color = previewPixels[i];
169+
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)));
170+
}
171+
172+
normalPreviewTexture.SetPixels(previewPixels);
173+
normalPreviewTexture.Apply();
174+
175+
EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, normalPreviewTexture.height), normalPreviewTexture, null, UnityEngine.ScaleMode.ScaleToFit);
176+
177+
if (GUILayout.Button("Save Texture Array"))
178+
{
179+
var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", normalArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to.");
180+
AssetDatabase.CreateAsset(normalArray, path);
181+
AssetDatabase.Refresh();
182+
}
144183
}
145184
EditorGUILayout.EndScrollView();
146185
}

Assets/MapGen/ContentLoader.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ IEnumerator FinalizeTextureAtlases()
453453
yield return null;
454454
specialTextureStorage.CompileTextures("SpecialTexture");
455455

456+
Debug.Log("Updating Material Manager...");
457+
yield return null;
458+
456459
Vector4 arrayCount = new Vector4(materialTextureStorage.Count, shapeTextureStorage.Count, specialTextureStorage.Count);
457460

458461
MaterialManager.Instance.SetTexture("_MainTex", materialTextureStorage.AtlasTexture);
@@ -464,7 +467,7 @@ IEnumerator FinalizeTextureAtlases()
464467
yield return null;
465468
SpriteManager.FinalizeSprites();
466469
Debug.Log("Done!");
467-
470+
yield return null;
468471
//get rid of any un-used textures left over.
469472
Resources.UnloadUnusedAssets();
470473
GC.Collect();

Assets/MapGen/CreatureSpriteManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public void FinalizeSprites()
187187
count += page.Count;
188188
var mat = new Material(baseCreatureMaterial);
189189
mat.SetTexture("_SpriteArray", page.TileArray);
190+
mat.SetTexture("_NormalArray", page.NormalArray);
190191
mats.Add(mat);
191192
}
192193
Debug.LogFormat("Loaded {0} creature sprites.", count);

Assets/MapGen/TextureAtlas/TilePage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class TilePage : ICollection
1919
List<DFCoord2d> coordList = new List<DFCoord2d>();
2020
[SerializeField]
2121
Texture2DArray tileArray;
22+
Texture2DArray normalArray;
2223
public Texture2DArray TileArray { get { return tileArray; } }
24+
public Texture2DArray NormalArray { get { return normalArray; } }
2325

2426
public int Count
2527
{
@@ -95,6 +97,7 @@ public void FinalizeTextures()
9597

9698

9799
tileArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true);
100+
normalArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true, true);
98101

99102
for (int i = 0; i < coordList.Count; i++)
100103
{
@@ -125,8 +128,10 @@ public void FinalizeTextures()
125128
texture.SetPixels32(tileDest32);
126129
TextureScale.Bilinear(texture, Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor));
127130
tileArray.SetPixels(texture.GetPixels(), i);
131+
normalArray.SetPixels(TextureTools.Bevel(texture.GetPixels(), texture.width, texture.height), i);
128132
}
129133
tileArray.Apply();
134+
normalArray.Apply();
130135
}
131136

132137
public void CopyTo(Array array, int index)

Assets/Materials/BaseSpriteMaterial.mat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Material:
2121
m_Texture: {fileID: 0}
2222
m_Scale: {x: 1, y: 1}
2323
m_Offset: {x: 0, y: 0}
24+
- first:
25+
name: _NormalArray
26+
second:
27+
m_Texture: {fileID: 18700000, guid: bd2b876762dd5804f9be4c94e8bbeb5c, type: 2}
28+
m_Scale: {x: 1, y: 1}
29+
m_Offset: {x: 0, y: 0}
2430
- first:
2531
name: _SpriteArray
2632
second:

Assets/Nagidal24x24BeardsArrayNormal.asset

Lines changed: 26 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Nagidal24x24BeardsArrayNormal.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Shaders/ToplitSprite.shader

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Shader "Custom/ToplitSprite" {
44
Properties {
55
_Color ("Color", Color) = (1,1,1,1)
66
_SpriteArray("Sprite Array", 2DArray) = "white" {}
7+
_NormalArray("Normal Array", 2DArray) = "bump" {}
78
_SpriteIndex("Sprite Index", Int) = 1
89
_Glossiness ("Smoothness", Range(0,1)) = 0.5
910
_Metallic ("Metallic", Range(0,1)) = 0.0
@@ -20,6 +21,7 @@ Shader "Custom/ToplitSprite" {
2021
#pragma target 3.5
2122

2223
UNITY_DECLARE_TEX2DARRAY(_SpriteArray);
24+
UNITY_DECLARE_TEX2DARRAY(_NormalArray);
2325

2426
struct Input {
2527
float2 uv_MainTex;
@@ -30,17 +32,14 @@ Shader "Custom/ToplitSprite" {
3032
fixed4 _Color;
3133
int _SpriteIndex;
3234

33-
void vert(inout appdata_full v) {
34-
v.normal = mul(unity_WorldToObject, float4(0, 1, 0, 0));
35-
}
36-
3735
void surf (Input IN, inout SurfaceOutputStandard o) {
3836
// Albedo comes from a texture tinted by color
3937
fixed4 c = _Color * UNITY_SAMPLE_TEX2DARRAY(_SpriteArray, float3(IN.uv_MainTex, _SpriteIndex));
4038
o.Albedo = c.rgb;
4139
// Metallic and smoothness come from slider variables
4240
o.Metallic = _Metallic;
4341
o.Smoothness = _Glossiness;
42+
o.Normal = UnpackNormal(UNITY_SAMPLE_TEX2DARRAY(_NormalArray, float3(IN.uv_MainTex, _SpriteIndex)));
4443
o.Alpha = c.a;
4544
}
4645
ENDCG

Assets/Start.unity

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ RectTransform:
361361
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
362362
m_AnchorMin: {x: 0, y: 0}
363363
m_AnchorMax: {x: 0, y: 0}
364-
m_AnchoredPosition: {x: 580, y: 0}
364+
m_AnchoredPosition: {x: 0, y: 0}
365365
m_SizeDelta: {x: 0, y: 0}
366366
m_Pivot: {x: 0.5, y: 0.5}
367367
--- !u!114 &10743622
@@ -444,7 +444,7 @@ RectTransform:
444444
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
445445
m_AnchorMin: {x: 0, y: 0}
446446
m_AnchorMax: {x: 0, y: 0}
447-
m_AnchoredPosition: {x: 600, y: 0}
447+
m_AnchoredPosition: {x: 0, y: 0}
448448
m_SizeDelta: {x: 0, y: 0}
449449
m_Pivot: {x: 0.5, y: 0.5}
450450
--- !u!114 &58592266
@@ -12050,7 +12050,7 @@ RectTransform:
1205012050
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1205112051
m_AnchorMin: {x: 0, y: 0}
1205212052
m_AnchorMax: {x: 0, y: 0}
12053-
m_AnchoredPosition: {x: 600, y: 0}
12053+
m_AnchoredPosition: {x: 0, y: 0}
1205412054
m_SizeDelta: {x: 0, y: 0}
1205512055
m_Pivot: {x: 0.5, y: 0.5}
1205612056
--- !u!114 &1068523915
@@ -14053,7 +14053,7 @@ RectTransform:
1405314053
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1405414054
m_AnchorMin: {x: 0, y: 0}
1405514055
m_AnchorMax: {x: 0, y: 0}
14056-
m_AnchoredPosition: {x: 600, y: 0}
14056+
m_AnchoredPosition: {x: 0, y: 0}
1405714057
m_SizeDelta: {x: 0, y: 0}
1405814058
m_Pivot: {x: 0.5, y: 0.5}
1405914059
--- !u!114 &1254861003
@@ -21352,7 +21352,7 @@ RectTransform:
2135221352
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2135321353
m_AnchorMin: {x: 0, y: 0}
2135421354
m_AnchorMax: {x: 0, y: 0}
21355-
m_AnchoredPosition: {x: 600, y: 0}
21355+
m_AnchoredPosition: {x: 0, y: 0}
2135621356
m_SizeDelta: {x: 0, y: 0}
2135721357
m_Pivot: {x: 0.5, y: 0.5}
2135821358
--- !u!114 &1993028658
@@ -21817,7 +21817,7 @@ RectTransform:
2181721817
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2181821818
m_AnchorMin: {x: 0, y: 0}
2181921819
m_AnchorMax: {x: 0, y: 0}
21820-
m_AnchoredPosition: {x: 600, y: 0}
21820+
m_AnchoredPosition: {x: 0, y: 0}
2182121821
m_SizeDelta: {x: 0, y: 0}
2182221822
m_Pivot: {x: 0.5, y: 0.5}
2182321823
--- !u!114 &2066374658

0 commit comments

Comments
 (0)