Merge branch 'dev' into stellar-remnants

This commit is contained in:
Noah Pilarski 2022-08-23 01:41:26 -04:00
commit 3afdebbb35
4 changed files with 49 additions and 8 deletions

Binary file not shown.

View File

@ -13,12 +13,14 @@ namespace NewHorizons.Builder.Body
public static class HeightMapBuilder
{
public static Shader PlanetShader;
private static readonly int EmissionMap = Shader.PropertyToID("_EmissionMap");
private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor");
public static GameObject Make(GameObject planetGO, Sector sector, HeightMapModule module, IModBehaviour mod, int resolution, bool useLOD = false)
{
var deleteHeightmapFlag = false;
Texture2D heightMap, textureMap;
Texture2D heightMap, textureMap, emissionMap;
try
{
if (module.heightMap != null && !File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, module.heightMap)))
@ -26,11 +28,16 @@ namespace NewHorizons.Builder.Body
Logger.LogError($"Bad path for {planetGO.name} heightMap: {module.heightMap} couldn't be found.");
module.heightMap = null;
}
if (module.textureMap != null && !File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, module.textureMap ?? "")))
if (module.textureMap != null && !File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, module.textureMap)))
{
Logger.LogError($"Bad path for {planetGO.name} textureMap: {module.textureMap} couldn't be found.");
module.textureMap = null;
}
if (module.emissionMap != null && !File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, module.emissionMap)))
{
Logger.LogError($"Bad path for {planetGO.name} emissionMap: {module.emissionMap} couldn't be found.");
module.emissionMap = null;
}
if (module.heightMap == null)
{
@ -54,8 +61,18 @@ namespace NewHorizons.Builder.Body
textureMap = ImageUtilities.GetTexture(mod, module.textureMap);
}
if (module.emissionMap == null)
{
emissionMap = Texture2D.blackTexture;
}
else
{
emissionMap = ImageUtilities.GetTexture(mod, module.emissionMap);
}
// If the texturemap is the same as the heightmap don't delete it #176
if (textureMap == heightMap) deleteHeightmapFlag = false;
// Do the same with emissionmap
if (textureMap == heightMap || emissionMap == heightMap) deleteHeightmapFlag = false;
}
catch (Exception e)
{
@ -63,15 +80,18 @@ namespace NewHorizons.Builder.Body
return null;
}
GameObject cubeSphere = new GameObject("CubeSphere");
var cubeSphere = new GameObject("CubeSphere");
cubeSphere.SetActive(false);
cubeSphere.transform.parent = sector?.transform ?? planetGO.transform;
if (PlanetShader == null) PlanetShader = Main.NHAssetBundle.LoadAsset<Shader>("Assets/Shaders/SphereTextureWrapper.shader");
Vector3 stretch = module.stretch != null ? (Vector3)module.stretch : Vector3.one;
var stretch = module.stretch != null ? (Vector3)module.stretch : Vector3.one;
var level1 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, resolution, stretch);
var emissionColor = module.emissionColor != null ? module.emissionColor.ToColor() : Color.white;
var level1 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, resolution, stretch,
emissionMap, emissionColor);
var cubeSphereMC = cubeSphere.AddComponent<MeshCollider>();
cubeSphereMC.sharedMesh = level1.gameObject.GetComponent<MeshFilter>().mesh;
@ -79,7 +99,8 @@ namespace NewHorizons.Builder.Body
if (useLOD)
{
var level2Res = (int)Mathf.Clamp(resolution / 2f, 1 /*cube moment*/, 100);
var level2 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, level2Res, stretch);
var level2 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, level2Res, stretch,
emissionMap, emissionColor);
var LODGroup = cubeSphere.AddComponent<LODGroup>();
LODGroup.size = module.maxHeight;
@ -115,7 +136,7 @@ namespace NewHorizons.Builder.Body
return cubeSphere;
}
public static MeshRenderer MakeLODTerrain(GameObject root, Texture2D heightMap, Texture2D textureMap, float minHeight, float maxHeight, int resolution, Vector3 stretch)
private static MeshRenderer MakeLODTerrain(GameObject root, Texture2D heightMap, Texture2D textureMap, float minHeight, float maxHeight, int resolution, Vector3 stretch, Texture2D emissionMap, Color emissionColor)
{
var LODCubeSphere = new GameObject("LODCubeSphere");
@ -126,6 +147,8 @@ namespace NewHorizons.Builder.Body
cubeSphereMR.material = material;
material.name = textureMap.name;
material.mainTexture = textureMap;
material.SetTexture(EmissionMap, emissionMap);
material.SetColor(EmissionColor, emissionColor);
LODCubeSphere.transform.parent = root.transform;
LODCubeSphere.transform.localPosition = Vector3.zero;

View File

@ -41,5 +41,15 @@ namespace NewHorizons.External.Modules
[Range(1 * 4, 500 * 4)]
[DefaultValue(51 * 4)]
public int resolution = 51 * 4;
/// <summary>
/// Relative filepath to the texture used for emission. Optional.
/// </summary>
public string emissionMap;
/// <summary>
/// Color multiplier of the emission texture. Defaults to white.
/// </summary>
public MColor emissionColor;
}
}

View File

@ -748,6 +748,14 @@
"default": 204,
"maximum": 2000.0,
"minimum": 4.0
},
"emissionMap": {
"type": "string",
"description": "Relative filepath to the texture used for emission. Optional."
},
"emissionColor": {
"description": "Color multiplier of the emission texture. Defaults to white.",
"$ref": "#/definitions/MColor"
}
}
},