Skip to content

Commit

Permalink
Merge pull request #1969 from joshcamas/master
Browse files Browse the repository at this point in the history
Added Custom Sky Loading
  • Loading branch information
Interkarma committed Jan 22, 2021
2 parents 57dfdca + 1eab4f0 commit 21b5731
Showing 1 changed file with 175 additions and 25 deletions.
200 changes: 175 additions & 25 deletions Assets/Scripts/Internal/DaggerfallSky.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using DaggerfallConnect.Utility;
using DaggerfallWorkshop.Utility;
using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Utility.AssetInjection;

namespace DaggerfallWorkshop
{
Expand Down Expand Up @@ -59,7 +60,6 @@ public class DaggerfallSky : MonoBehaviour

DaggerfallUnity dfUnity;
WeatherManager weatherManager;
public SkyFile skyFile;
public ImgFile imgFile;
Camera mainCamera;
Camera myCamera;
Expand All @@ -83,6 +83,7 @@ public struct SkyColors
public Color32[] west;
public Color32[] east;
public Color clearColor;
public Vector2Int imageSize;
}

#endregion
Expand Down Expand Up @@ -261,26 +262,13 @@ private void DrawSky()

private void PromoteToTexture(SkyColors colors, bool flip = false)
{
const int dayWidth = 512;
const int dayHeight = 220;
const int nightWidth = 512;
const int nightHeight = 219;

// Destroy old textures
Destroy(westTexture);
Destroy(eastTexture);

// Create new textures
if (!IsNight || !showNightSky)
{
westTexture = new Texture2D(dayWidth, dayHeight, TextureFormat.ARGB32, false);
eastTexture = new Texture2D(dayWidth, dayHeight, TextureFormat.ARGB32, false);
}
else
{
westTexture = new Texture2D(nightWidth, nightHeight, TextureFormat.ARGB32, false);
eastTexture = new Texture2D(nightWidth, nightHeight, TextureFormat.ARGB32, false);
}
westTexture = new Texture2D(colors.imageSize.x, colors.imageSize.y, TextureFormat.ARGB32, false);
eastTexture = new Texture2D(colors.imageSize.x, colors.imageSize.y, TextureFormat.ARGB32, false);

// Set pixels, flipping hemisphere if required
if (!flip)
Expand Down Expand Up @@ -400,31 +388,189 @@ private void LoadCurrentSky(int targetFrame)
if (!IsNight || !showNightSky)
LoadDaySky(targetFrame);
else
LoadNightSky();
LoadNightSky(targetFrame);
}

private void LoadDaySky(int frame)
{
skyFile = new SkyFile(Path.Combine(dfUnity.Arena2Path, SkyFile.IndexToFileName(SkyIndex)), FileUsage.UseMemory, true);
if (DaggerfallUnity.Settings.AssetInjection)
{
TryLoadDayTextures(SkyIndex, frame,
out Texture2D westTexture, out Texture2D eastTexture);

if (westTexture && eastTexture)
{
skyColors = new SkyColors();
skyColors.east = eastTexture.GetPixels32();
skyColors.west = westTexture.GetPixels32();
skyColors.clearColor = skyColors.west[0];
skyColors.imageSize = new Vector2Int(westTexture.width, westTexture.height);
return;
}
}

skyColors = LoadVanillaDaySky(SkyIndex, frame);
}

private void LoadNightSky(int frame)
{
if (DaggerfallUnity.Settings.AssetInjection)
{
TryLoadNightSkyTextures(SkyIndex, frame,
out Texture2D westTexture, out Texture2D eastTexture);

if (westTexture && eastTexture)
{
skyColors = new SkyColors();
skyColors.east = eastTexture.GetPixels32();
skyColors.west = westTexture.GetPixels32();
skyColors.clearColor = skyColors.west[0];
skyColors.imageSize = new Vector2Int(westTexture.width, westTexture.height);

return;
}
}

skyColors = LoadVanillaNightSky(SkyIndex);
}

/// <summary>
/// Tries loading day sky files from mods and loose files
/// </summary>
/// <param name="skyIndex"></param>
/// <param name="frame"></param>
/// <param name="westTexture"></param>
/// <param name="eastTexture"></param>
/// <param name="applyStars"></param>
/// <returns></returns>
private bool TryLoadDayTextures(int skyIndex, int frame, out Texture2D westTexture, out Texture2D eastTexture)
{
string baseName = string.Format("SKY{0:00}.DAT", skyIndex);

//In format SKY00_0-0.DAT
string eastName = Path.GetFileNameWithoutExtension(baseName) + "_0-" + frame + Path.GetExtension(baseName);
string westName = Path.GetFileNameWithoutExtension(baseName) + "_1-" + frame + Path.GetExtension(baseName);

TextureReplacement.TryImportTexture(eastName, false, out eastTexture);
TextureReplacement.TryImportTexture(westName, false, out westTexture);

if (eastTexture != null && westTexture != null)
return true;

return false;
}

/// <summary>
/// Tries loading day sky files from mods and loose files
/// </summary>
/// <param name="skyIndex"></param>
/// <param name="frame"></param>
/// <param name="westTexture"></param>
/// <param name="eastTexture"></param>
/// <param name="applyStars"></param>
/// <returns></returns>
private bool TryLoadNightSkyTextures(int skyIndex, int frame, out Texture2D westTexture, out Texture2D eastTexture)
{
// Get night sky matching sky index
int vanillaNightSky;
if (skyIndex >= 0 && skyIndex <= 7)
vanillaNightSky = 3;
else if (skyIndex >= 8 && skyIndex <= 15)
vanillaNightSky = 1;
else if (skyIndex >= 16 && skyIndex <= 23)
vanillaNightSky = 2;
else
vanillaNightSky = 0;

//TYPE 1: NIGHTIME VANILLA
string baseName = string.Format("NITE{0:00}I0.IMG", vanillaNightSky);
TextureReplacement.TryImportTexture(baseName, false, out westTexture);

//Vanilla worked!
if (westTexture != null)
{
//Note east is copied from west
eastTexture = westTexture;
return true;
}

//TYPE 2: FULL NIGHTIME
baseName = string.Format("NITEFULL{0:00}I0.IMG", skyIndex);
TextureReplacement.TryImportTexture(baseName, false, out westTexture);

//Full worked!
if (westTexture != null)
{
//Note east is copied from west
eastTexture = westTexture;
return true;
}

//TYPE 3: NIGHTIME VANILLA
baseName = string.Format("NITE{0:00}I0.IMG", vanillaNightSky);
string eastName = Path.GetFileNameWithoutExtension(baseName) + "-0" + Path.GetExtension(baseName);
string westName = Path.GetFileNameWithoutExtension(baseName) + "-1" + Path.GetExtension(baseName);

TextureReplacement.TryImportTexture(eastName, false, out eastTexture);
TextureReplacement.TryImportTexture(westName, false, out westTexture);

//Vanilla with east+west worked!
if (westTexture != null && eastTexture != null)
return true;

//TYPE 2: FULL NIGHTIME
baseName = string.Format("NITEFULL{0:00}I0.IMG", skyIndex);
eastName = Path.GetFileNameWithoutExtension(baseName) + "-0" + Path.GetExtension(baseName);
westName = Path.GetFileNameWithoutExtension(baseName) + "-1" + Path.GetExtension(baseName);

TextureReplacement.TryImportTexture(eastName, false, out eastTexture);
TextureReplacement.TryImportTexture(westName, false, out westTexture);

//Full with east+west worked!
if (westTexture != null && eastTexture != null)
return true;

westTexture = null;
eastTexture = null;
return false;
}

/// <summary>
/// Loads day sky from arena files
/// </summary>
/// <param name="skyIndex"></param>
/// <returns>SkyColors loaded</returns>
private SkyColors LoadVanillaDaySky(int skyIndex, int frame)
{
SkyFile skyFile = new SkyFile(Path.Combine(dfUnity.Arena2Path, SkyFile.IndexToFileName(skyIndex)), FileUsage.UseMemory, true);
skyFile.Palette = skyFile.GetDFPalette(frame);
skyColors.east = skyFile.GetColor32(0, frame);
skyColors.west = skyFile.GetColor32(1, frame);
skyColors.clearColor = skyColors.west[0];

SkyColors colors = new SkyColors();
colors.east = skyFile.GetColor32(0, frame);
colors.west = skyFile.GetColor32(1, frame);
colors.clearColor = colors.west[0];
colors.imageSize = new Vector2Int(512, 220);

return colors;
}

private void LoadNightSky()
/// <summary>
/// Loads night sky from arena files and adds stars if enabled in settings
/// </summary>
/// <param name="skyIndex"></param>
/// <returns>SkyColors loaded</returns>
private SkyColors LoadVanillaNightSky(int skyIndex)
{
const int width = 512;
const int height = 219;

// Get night sky matching sky index
int nightSky;
if (SkyIndex >= 0 && SkyIndex <= 7)
if (skyIndex >= 0 && skyIndex <= 7)
nightSky = 3;
else if (SkyIndex >= 8 && SkyIndex <= 15)
else if (skyIndex >= 8 && skyIndex <= 15)
nightSky = 1;
else if (SkyIndex >= 16 && SkyIndex <= 23)
else if (skyIndex >= 16 && skyIndex <= 23)
nightSky = 2;
else
nightSky = 0;
Expand Down Expand Up @@ -461,9 +607,13 @@ private void LoadNightSky()
colors[pos + 1] = colors[pos];
}

SkyColors skyColors = new SkyColors();

skyColors.west = colors;
skyColors.east = colors;
skyColors.clearColor = skyColors.west[0];
skyColors.imageSize = new Vector2Int(512, 219);
return skyColors;
}

private void SetupCameras()
Expand Down

0 comments on commit 21b5731

Please sign in to comment.