Ambient light module (#529)

Ambient light is now a module with additional options for outerRadius,
innerRadius, isShell, and tint.
This commit is contained in:
Will Corby 2023-02-25 20:09:55 -08:00 committed by GitHub
commit e2b4bd2c51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 246 additions and 95 deletions

View File

@ -1,12 +1,12 @@
ManifestFileVersion: 0
CRC: 2314570133
CRC: 192875128
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 73793e57d83c10dbd917c9f721a4e60e
Hash: bd3accfa8245e7e8c1fe0bf8cfa04eb1
TypeTreeHash:
serializedVersion: 2
Hash: fae511e2df3c6c74353879b4753cf2eb
Hash: de71b9c55befb829b1640ea21774b932
HashAppended: 0
ClassTypes:
- Class: 1
@ -33,6 +33,8 @@ ClassTypes:
Script: {instanceID: 0}
- Class: 82
Script: {instanceID: 0}
- Class: 89
Script: {instanceID: 0}
- Class: 108
Script: {instanceID: 0}
- Class: 114
@ -169,6 +171,7 @@ ClassTypes:
Script: {instanceID: 0}
SerializeReferenceClassIdentifiers: []
Assets:
- Assets/AmbientLight_QM.png
- Assets/BrambleCollision.prefab
- Assets/Vessel_Body.prefab
Dependencies: []

View File

@ -25,7 +25,7 @@ namespace NewHorizons.Builder.Body
if (_shockLayerMaterial == null) _shockLayerMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Shocklayer_GD").GetComponent<MeshRenderer>().sharedMaterial).Rename("ShockLayer_mat").DontDestroyOnLoad();
}
public static NHSupernovaPlanetEffectController Make(GameObject planetGO, Sector sector, PlanetConfig config, IModBehaviour mod, GameObject procGen, Light ambientLight, PlanetaryFogController fog, LODGroup atmosphere, Renderer atmosphereRenderer, Renderer fogImpostor)
public static NHSupernovaPlanetEffectController Make(GameObject planetGO, Sector sector, PlanetConfig config, IModBehaviour mod, GameObject procGen, Light[] ambientLight, PlanetaryFogController fog, LODGroup atmosphere, Renderer atmosphereRenderer, Renderer fogImpostor)
{
InitPrefabs();
@ -41,7 +41,8 @@ namespace NewHorizons.Builder.Body
if (currentController._ambientLight == null && ambientLight != null)
{
currentController._ambientLight = ambientLight;
currentController._ambientLightOrigIntensity = config.Base.ambientLight;
currentController._ambientLightOrigIntensity = new float[ambientLight.Length];
for (int i = 0; i < ambientLight.Length; i++) currentController._ambientLightOrigIntensity[i] = ambientLight[i].intensity;
}
if (currentController._atmosphere == null && atmosphere != null) currentController._atmosphere = atmosphere;
@ -57,8 +58,12 @@ namespace NewHorizons.Builder.Body
var supernovaController = new GameObject("SupernovaController");
supernovaController.transform.SetParent(sector?.transform ?? planetGO.transform, false);
var supernovaEffectController = supernovaController.AddComponent<NHSupernovaPlanetEffectController>();
supernovaEffectController._ambientLight = ambientLight;
supernovaEffectController._ambientLightOrigIntensity = config.Base.ambientLight;
if (ambientLight != null)
{
supernovaEffectController._ambientLight = ambientLight;
supernovaEffectController._ambientLightOrigIntensity = new float[ambientLight.Length];
for (int i = 0; i < ambientLight.Length; i++) supernovaEffectController._ambientLightOrigIntensity[i] = ambientLight[i].intensity;
}
if (config.Atmosphere != null && config.Atmosphere.atmosphereSunIntensity != 0) supernovaEffectController._atmosphereOrigSunIntensity = config.Atmosphere.atmosphereSunIntensity;
supernovaEffectController._atmosphere = atmosphere;
supernovaEffectController._atmosphereRenderer = atmosphereRenderer;
@ -180,8 +185,8 @@ namespace NewHorizons.Builder.Body
if (vanillaController._shockLayer != null) vanillaController._shockLayer.gameObject.SetActive(true);
var supernovaEffectController = vanillaController.gameObject.GetAddComponent<NHSupernovaPlanetEffectController>();
supernovaEffectController._atmosphere = vanillaController._atmosphere;
supernovaEffectController._ambientLight = vanillaController._ambientLight;
supernovaEffectController._ambientLightOrigIntensity = vanillaController._ambientLightOrigIntensity;
supernovaEffectController._ambientLight = new Light[] { vanillaController._ambientLight };
supernovaEffectController._ambientLightOrigIntensity = new float[] { vanillaController._ambientLightOrigIntensity };
supernovaEffectController._atmosphere = vanillaController._atmosphere;
supernovaEffectController._fog = vanillaController._fog;
supernovaEffectController._fogOrigTint = vanillaController._fogOrigTint;

View File

@ -1,34 +1,63 @@
using UnityEngine;
using NewHorizons.Utility;
using NewHorizons.External.Modules;
namespace NewHorizons.Builder.General
{
public static class AmbientLightBuilder
{
public static Light Make(GameObject planetGO, Sector sector, float scale, float intensity)
public static Light Make(GameObject planetGO, Sector sector, AmbientLightModule config, float surfaceSize)
{
var ambientLight = Main.Instance.CurrentStarSystem == "EyeOfTheUniverse" ? SearchUtilities.Find("EyeOfTheUniverse_Body/Sector_EyeOfTheUniverse/SixthPlanet_Root/QuantumMoonProxy_Pivot/QuantumMoonProxy_Root/MoonState_Root/AmbientLight_QM") : SearchUtilities.Find("QuantumMoon_Body/AmbientLight_QM");
if (ambientLight == null) return null;
GameObject lightGO = GameObject.Instantiate(ambientLight, sector?.transform ?? planetGO.transform);
lightGO.transform.position = planetGO.transform.position;
lightGO.transform.position = config.position == null ? planetGO.transform.position : planetGO.transform.TransformPoint(config.position);
lightGO.name = "AmbientLight";
var light = lightGO.GetComponent<Light>();
/* R is related to the inner radius of the ambient light volume
* G is if its a shell or not. 1.0f for shell else 0.0f.
* B is just 1.0 always I think, altho no because changing it changes the brightness so idk
* A is the intensity and its like square rooted and squared and idgi
/*
* R is inner radius
* G is shell (1 for shell, 0 for no shell)
* B is always 1
* A is falloff exponent
*/
light.color = new Color(0.5f, 0.0f, 0.8f, 0.0225f);
light.range = scale;
light.intensity = intensity;
/*if (tint != null)
light.intensity = config.intensity;
light.range = config.outerRadius ?? surfaceSize * 2;
var innerRadius = config.innerRadius ?? surfaceSize;
innerRadius = Mathf.Sqrt(innerRadius / light.range);
var shell = config.isShell ? 1f : 0f;
light.color = new Color(innerRadius, shell, 1f, 0.0225f/*from timber hearth*/);
if (config.tint != null)
{
var cubemap = ImageUtilities.TintImage(ImageUtilities.GetTexture(Main.Instance, "Assets/textures/AmbientLight_QM.png"), tint.ToColor());
var tint = config.tint.ToColor();
var cubemap = Main.NHPrivateAssetBundle.LoadAsset<Cubemap>("AmbientLight_QM");
var cubemapFace = CubemapFace.Unknown;
for (int i = 0; i < 6; i++)
{
switch (i)
{
case 0: cubemapFace = CubemapFace.PositiveX; break;
case 1: cubemapFace = CubemapFace.NegativeX; break;
case 2: cubemapFace = CubemapFace.PositiveY; break;
case 3: cubemapFace = CubemapFace.NegativeY; break;
case 4: cubemapFace = CubemapFace.PositiveZ; break;
case 5: cubemapFace = CubemapFace.NegativeZ; break;
default: break;
}
var sourceColors = cubemap.GetPixels(cubemapFace, 0);
var newColors = new Color[sourceColors.Length];
for (int j = 0; j < sourceColors.Length; j++)
{
var grey = sourceColors[j].grayscale * 2;
newColors[j] = tint * new Color(grey, grey, grey);
}
cubemap.SetPixels(newColors, cubemapFace);
}
cubemap.Apply();
light.cookie = cubemap;
}*/
}
return light;
}

View File

@ -10,8 +10,8 @@ namespace NewHorizons.Components
{
public class NHSupernovaPlanetEffectController : MonoBehaviour
{
public Light _ambientLight;
public float _ambientLightOrigIntensity;
public Light[] _ambientLight;
public float[] _ambientLightOrigIntensity;
public LODGroup _atmosphere;
public Renderer _atmosphereRenderer;
public float _atmosphereOrigSunIntensity = 1;
@ -131,7 +131,13 @@ namespace NewHorizons.Components
{
float collapseProgress = StarEvolutionController.GetCollapseProgress();
if (_ambientLight != null) _ambientLight.intensity = _ambientLightOrigIntensity * (1f - collapseProgress);
if (_ambientLight != null)
{
for (int i = 0; i < _ambientLight.Length; i++)
{
_ambientLight[i].intensity = _ambientLightOrigIntensity[i] * (1f - collapseProgress);
}
}
if (_atmosphere != null)
{
@ -181,7 +187,13 @@ namespace NewHorizons.Components
{
float collapseProgress = SunController.GetCollapseProgress();
if (_ambientLight != null) _ambientLight.intensity = _ambientLightOrigIntensity * (1f - collapseProgress);
if (_ambientLight != null)
{
for (int i = 0; i < _ambientLight.Length; i++)
{
_ambientLight[i].intensity = _ambientLightOrigIntensity[i] * (1f - collapseProgress);
}
}
if (_atmosphere != null)
{
@ -212,7 +224,13 @@ namespace NewHorizons.Components
{
if (_shockLayer != null) _shockLayer.enabled = false;
if (_ambientLight != null) _ambientLight.intensity = _ambientLightOrigIntensity;
if (_ambientLight != null)
{
for (int i = 0; i < _ambientLight.Length; i++)
{
_ambientLight[i].intensity = _ambientLightOrigIntensity[i];
}
}
if (_fog != null) _fog.fogTint = _fogOrigTint;

View File

@ -27,6 +27,11 @@ namespace NewHorizons.External.Configs
/// </summary>
[DefaultValue("SolarSystem")] public string starSystem = "SolarSystem";
/// <summary>
/// Add ambient lights to this body
/// </summary>
public AmbientLightModule[] AmbientLights;
/// <summary>
/// Generate asteroids around this body
/// </summary>
@ -286,7 +291,11 @@ namespace NewHorizons.External.Configs
radius = Base.cloakRadius
};
if (Base.hasAmbientLight) Base.ambientLight = 0.5f;
if (Base.hasAmbientLight || Base.ambientLight != 0)
{
if (AmbientLights == null) AmbientLights = new AmbientLightModule[0];
AmbientLights = AmbientLights.Append(new AmbientLightModule { intensity = Base.ambientLight != 0 ? Base.ambientLight : 0.5f }).ToArray();
}
if (Atmosphere != null)
{

View File

@ -0,0 +1,42 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NewHorizons.Utility;
using Newtonsoft.Json;
namespace NewHorizons.External.Modules
{
[JsonObject]
public class AmbientLightModule
{
/// <summary>
/// The range of the light. Defaults to surfaceSize * 2.
/// </summary>
[Range(0, double.MaxValue)] public float? outerRadius;
/// <summary>
/// The lower radius where the light is brightest, fading in from outerRadius. Defaults to surfaceSize.
/// </summary>
[Range(0, double.MaxValue)] public float? innerRadius;
/// <summary>
/// The brightness of the light. For reference, Timber Hearth is `1.4`, and Giant's Deep is `0.8`.
/// </summary>
[Range(0, double.MaxValue)][DefaultValue(1f)] public float intensity = 1f;
/// <summary>
/// The tint of the light
/// </summary>
public MColor tint;
/// <summary>
/// If true, the light will work as a shell between inner and outer radius.
/// </summary>
[DefaultValue(false)] public bool isShell = false;
/// <summary>
/// The position of the light
/// </summary>
public MVector3 position;
}
}

View File

@ -19,11 +19,6 @@ namespace NewHorizons.External.Modules
[JsonObject]
public class BaseModule
{
/// <summary>
/// The intensity of light the dark side of the body should have. Timber Hearth has `1.4` for reference
/// </summary>
public float ambientLight;
/// <summary>
/// Set this to true if you are replacing the sun with a different body. Only one object in a star system should ever
/// have this set to true.
@ -103,9 +98,12 @@ namespace NewHorizons.External.Modules
[Obsolete("WaterTint is deprecated, please use WaterModule instead")]
public MColor waterTint;
[Obsolete("HasAmbientLight is deprecated, please use AmbientLight instead")]
[Obsolete("HasAmbientLight is deprecated, please use AmbientLightModule instead")]
public bool hasAmbientLight;
[Obsolete("AmbientLight is deprecated, please use AmbientLightModule instead")]
public float ambientLight;
[Obsolete("HasReferenceFrame is deprecated, please use ReferenceModule instead")]
[DefaultValue(true)] public bool hasReferenceFrame = true;

View File

@ -497,10 +497,11 @@ namespace NewHorizons.Handlers
{
var sphereOfInfluence = GetSphereOfInfluence(body);
Light ambientLight = null;
if (body.Config.Base.ambientLight != 0)
Light[] ambientLight = null;
if (body.Config.AmbientLights != null)
{
ambientLight = AmbientLightBuilder.Make(go, sector, sphereOfInfluence, body.Config.Base.ambientLight);
ambientLight = new Light[0];
foreach (var light in body.Config.AmbientLights) ambientLight.Append(AmbientLightBuilder.Make(go, sector, light, body.Config.Base.surfaceSize)).ToArray();
}
if (body.Config.Base.groundSize != 0)

View File

@ -18,6 +18,13 @@
"description": "Unique star system containing your planet. If you set this to be a custom solar system remember to add a Spawn module to one of the bodies, or else you can't get to the system.",
"default": "SolarSystem"
},
"AmbientLights": {
"type": "array",
"description": "Add ambient lights to this body",
"items": {
"$ref": "#/definitions/AmbientLightModule"
}
},
"AsteroidBelt": {
"description": "Generate asteroids around this body",
"$ref": "#/definitions/AsteroidBeltModule"
@ -142,6 +149,103 @@
}
},
"definitions": {
"AmbientLightModule": {
"type": "object",
"additionalProperties": false,
"properties": {
"outerRadius": {
"type": [
"null",
"number"
],
"description": "The range of the light. Defaults to surfaceSize * 2.",
"format": "float",
"minimum": 0.0
},
"innerRadius": {
"type": [
"null",
"number"
],
"description": "The lower radius where the light is brightest, fading in from outerRadius. Defaults to surfaceSize.",
"format": "float",
"minimum": 0.0
},
"intensity": {
"type": "number",
"description": "The brightness of the light. For reference, Timber Hearth is `1.4`, and Giant's Deep is `0.8`.",
"format": "float",
"default": 1.0,
"minimum": 0.0
},
"tint": {
"description": "The tint of the light",
"$ref": "#/definitions/MColor"
},
"isShell": {
"type": "boolean",
"description": "If true, the light will work as a shell between inner and outer radius.",
"default": false
},
"position": {
"description": "The position of the light",
"$ref": "#/definitions/MVector3"
}
}
},
"MColor": {
"type": "object",
"additionalProperties": false,
"properties": {
"r": {
"type": "integer",
"description": "The red component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"g": {
"type": "integer",
"description": "The green component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"b": {
"type": "integer",
"description": "The blue component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"a": {
"type": "integer",
"description": "The alpha (opacity) component of this colour",
"format": "int32",
"default": 255,
"maximum": 255.0,
"minimum": 0.0
}
}
},
"MVector3": {
"type": "object",
"additionalProperties": false,
"properties": {
"x": {
"type": "number",
"format": "float"
},
"y": {
"type": "number",
"format": "float"
},
"z": {
"type": "number",
"format": "float"
}
}
},
"AsteroidBeltModule": {
"type": "object",
"additionalProperties": false,
@ -215,41 +319,6 @@
}
}
},
"MColor": {
"type": "object",
"additionalProperties": false,
"properties": {
"r": {
"type": "integer",
"description": "The red component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"g": {
"type": "integer",
"description": "The green component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"b": {
"type": "integer",
"description": "The blue component of this colour from 0-255, higher values will make the colour glow if applicable.",
"format": "int32",
"maximum": 2147483647.0,
"minimum": 0.0
},
"a": {
"type": "integer",
"description": "The alpha (opacity) component of this colour",
"format": "int32",
"default": 255,
"maximum": 255.0,
"minimum": 0.0
}
}
},
"AtmosphereModule": {
"type": "object",
"additionalProperties": false,
@ -442,11 +511,6 @@
"type": "object",
"additionalProperties": false,
"properties": {
"ambientLight": {
"type": "number",
"description": "The intensity of light the dark side of the body should have. Timber Hearth has `1.4` for reference",
"format": "float"
},
"centerOfSolarSystem": {
"type": "boolean",
"description": "Set this to true if you are replacing the sun with a different body. Only one object in a star system should ever\nhave this set to true."
@ -504,24 +568,6 @@
}
}
},
"MVector3": {
"type": "object",
"additionalProperties": false,
"properties": {
"x": {
"type": "number",
"format": "float"
},
"y": {
"type": "number",
"format": "float"
},
"z": {
"type": "number",
"format": "float"
}
}
},
"GravityFallOff": {
"type": "string",
"description": "",