2021-06-25 13:51:00 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Items;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Items.Definitions;
|
|
|
|
|
using SanAndreasUnity.Importing.Items.Placements;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-06-25 13:51:00 +00:00
|
|
|
|
using SanAndreasUnity.Importing.RenderWareStream;
|
2021-06-25 20:11:26 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
using UnityEngine;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
using UnityEngine.Rendering;
|
2021-06-25 13:51:00 +00:00
|
|
|
|
using Geometry = SanAndreasUnity.Importing.Conversion.Geometry;
|
2021-06-25 20:11:26 +00:00
|
|
|
|
using Profiler = UnityEngine.Profiling.Profiler;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.World
|
|
|
|
|
{
|
|
|
|
|
public class StaticGeometry : MapObject
|
|
|
|
|
{
|
|
|
|
|
public static StaticGeometry Create()
|
|
|
|
|
{
|
2021-05-04 17:54:47 +00:00
|
|
|
|
if (!s_registeredTimeChangeCallback)
|
|
|
|
|
{
|
|
|
|
|
s_registeredTimeChangeCallback = true;
|
|
|
|
|
DayTimeManager.Singleton.onHourChanged += OnHourChanged;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
return Create<StaticGeometry>(Cell.Instance.staticGeometryPrefab);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 17:54:47 +00:00
|
|
|
|
private static List<StaticGeometry> s_timedObjects = new List<StaticGeometry>();
|
|
|
|
|
public static IReadOnlyList<StaticGeometry> TimedObjects => s_timedObjects;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public Instance Instance { get; private set; }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-05-03 19:24:48 +00:00
|
|
|
|
public ISimpleObjectDefinition ObjectDefinition { get; private set; }
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
private bool _canLoad;
|
|
|
|
|
private bool _isGeometryLoaded = false;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private bool _isFading;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public bool ShouldBeVisibleNow =>
|
|
|
|
|
this.IsVisibleInMapSystem
|
|
|
|
|
&& this.IsVisibleBasedOnCurrentDayTime
|
|
|
|
|
&& _isGeometryLoaded
|
|
|
|
|
&& (LodParent == null || !LodParent.ShouldBeVisibleNow);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
public StaticGeometry LodParent { get; private set; }
|
|
|
|
|
public StaticGeometry LodChild { get; private set; }
|
|
|
|
|
|
2021-05-04 17:54:47 +00:00
|
|
|
|
private static bool s_registeredTimeChangeCallback = false;
|
|
|
|
|
|
|
|
|
|
public bool IsVisibleBasedOnCurrentDayTime => this.ObjectDefinition is TimeObjectDef timeObjectDef ? IsObjectVisibleBasedOnCurrentDayTime(timeObjectDef) : true;
|
|
|
|
|
|
2021-06-25 22:47:50 +00:00
|
|
|
|
// hashset is better because we do lookup/remove often, while iteration is done rarely
|
|
|
|
|
private static HashSet<StaticGeometry> s_activeObjectsWithLights = new HashSet<StaticGeometry>();
|
2021-06-25 23:58:05 +00:00
|
|
|
|
public static IReadOnlyCollection<StaticGeometry> ActiveObjectsWithLights => s_activeObjectsWithLights;
|
2021-06-25 22:47:50 +00:00
|
|
|
|
|
2021-06-25 20:16:27 +00:00
|
|
|
|
// use arrays to save memory
|
|
|
|
|
// set them to null to save memory
|
|
|
|
|
private LightSource[] m_lightSources = null;
|
2021-06-25 21:00:33 +00:00
|
|
|
|
private LightSource[] m_redTrafficLights = null;
|
|
|
|
|
private LightSource[] m_yellowTrafficLights = null;
|
|
|
|
|
private LightSource[] m_greenTrafficLights = null;
|
|
|
|
|
private bool m_hasTrafficLights = false;
|
2021-06-25 13:51:00 +00:00
|
|
|
|
private int m_activeTrafficLightIndex = -1;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public int NumLightSources => m_lightSources?.Length ?? 0;
|
|
|
|
|
|
2021-05-04 17:54:47 +00:00
|
|
|
|
|
2021-06-25 22:47:50 +00:00
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
if (m_lightSources != null)
|
|
|
|
|
s_activeObjectsWithLights.Add(this);
|
|
|
|
|
|
|
|
|
|
this.UpdateLightsBasedOnDayTime();
|
|
|
|
|
this.UpdateTrafficLights();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
s_activeObjectsWithLights.Remove(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
s_timedObjects.Remove(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
public void Initialize(Instance inst, Dictionary<Instance, StaticGeometry> dict)
|
|
|
|
|
{
|
|
|
|
|
Instance = inst;
|
2021-05-03 19:24:48 +00:00
|
|
|
|
ObjectDefinition = Item.GetDefinition<Importing.Items.Definitions.ISimpleObjectDefinition>(inst.ObjectId);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-05-04 17:54:47 +00:00
|
|
|
|
if (ObjectDefinition is TimeObjectDef)
|
|
|
|
|
{
|
|
|
|
|
s_timedObjects.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.Initialize(
|
|
|
|
|
Cell.Instance.GetPositionBasedOnInteriorLevel(inst.Position, inst.InteriorLevel),
|
|
|
|
|
inst.Rotation);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-05-03 19:24:48 +00:00
|
|
|
|
_canLoad = ObjectDefinition != null;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-05-03 19:24:48 +00:00
|
|
|
|
name = _canLoad ? ObjectDefinition.ModelName : string.Format("Unknown ({0})", Instance.ObjectId);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
if (_canLoad && Instance.LodInstance != null)
|
|
|
|
|
{
|
2019-10-20 18:17:06 +00:00
|
|
|
|
if (dict.TryGetValue(Instance.LodInstance, out StaticGeometry dictValue))
|
|
|
|
|
{
|
|
|
|
|
LodChild = dictValue;
|
|
|
|
|
LodChild.LodParent = this;
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.SetDrawDistance(ObjectDefinition?.DrawDist ?? 0);
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
gameObject.isStatic = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLoad()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!_canLoad) return;
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample ("StaticGeometry.OnLoad", this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//var geoms = Geometry.Load(Instance.Object.ModelName, Instance.Object.TextureDictionaryName);
|
|
|
|
|
//OnGeometryLoaded (geoms);
|
|
|
|
|
|
|
|
|
|
// we could start loading collision model here
|
|
|
|
|
// - we can't, because we don't know the name of collision file until clump is loaded
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
Geometry.LoadAsync( ObjectDefinition.ModelName, new string[] {ObjectDefinition.TextureDictionaryName}, this.LoadPriority, (geoms) => {
|
2020-05-31 17:07:22 +00:00
|
|
|
|
if(geoms != null)
|
|
|
|
|
{
|
|
|
|
|
// we can't load collision model asyncly, because it requires a transform to attach to
|
|
|
|
|
// but, we can load collision file asyncly
|
2021-07-18 04:03:43 +00:00
|
|
|
|
Importing.Collision.CollisionFile.FromNameAsync( geoms.Collisions != null ? geoms.Collisions.Name : geoms.Name, this.LoadPriority, (cf) => {
|
2020-05-31 17:07:22 +00:00
|
|
|
|
OnGeometryLoaded (geoms);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Profiler.EndSample ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGeometryLoaded (Geometry.GeometryParts geoms)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample ("Add mesh", this);
|
|
|
|
|
|
|
|
|
|
var mf = gameObject.AddComponent<MeshFilter>();
|
|
|
|
|
var mr = gameObject.AddComponent<MeshRenderer>();
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
mr.receiveShadows = this.ShouldReceiveShadows();
|
|
|
|
|
mr.shadowCastingMode = this.ShouldCastShadows() ? ShadowCastingMode.On : ShadowCastingMode.Off;
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
mf.sharedMesh = geoms.Geometry[0].Mesh;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
mr.sharedMaterials = geoms.Geometry[0].GetMaterials(ObjectDefinition.Flags, mat => mat.SetTexture(NoiseTexPropertyId, NoiseTex));
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
Profiler.EndSample ();
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
Profiler.BeginSample("CreateLights()", this);
|
2021-06-25 21:03:31 +00:00
|
|
|
|
if (!F.IsInHeadlessMode)
|
|
|
|
|
this.CreateLights(geoms);
|
2021-07-18 04:03:43 +00:00
|
|
|
|
Profiler.EndSample();
|
2021-04-17 20:15:07 +00:00
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
geoms.AttachCollisionModel(transform);
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample ("Set layer", this);
|
|
|
|
|
|
2021-05-03 19:24:48 +00:00
|
|
|
|
if (ObjectDefinition.Flags.HasFlag(ObjectFlag.Breakable))
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
gameObject.SetLayerRecursive(BreakableLayer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Profiler.EndSample ();
|
|
|
|
|
|
|
|
|
|
_isGeometryLoaded = true;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.UpdateVisibility();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnCollisionModelAttached ()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 20:12:46 +00:00
|
|
|
|
protected override void OnShow()
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample ("StaticGeometry.OnShow");
|
2021-07-18 04:03:43 +00:00
|
|
|
|
|
|
|
|
|
this.UpdateVisibility();
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
Profiler.EndSample ();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
protected override void OnUnShow()
|
|
|
|
|
{
|
|
|
|
|
this.UpdateVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator FadeCoroutine()
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (_isFading) yield break;
|
|
|
|
|
|
|
|
|
|
_isFading = true;
|
|
|
|
|
|
|
|
|
|
// wait until geometry gets loaded
|
|
|
|
|
while (!_isGeometryLoaded)
|
|
|
|
|
yield return null;
|
|
|
|
|
|
|
|
|
|
var mr = GetComponent<MeshRenderer>();
|
|
|
|
|
if (mr == null)
|
|
|
|
|
{
|
|
|
|
|
_isFading = false;
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
float fadeRate = Cell.Instance.fadeRate;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
var pb = new MaterialPropertyBlock();
|
|
|
|
|
|
|
|
|
|
// continuously change transparency until object becomes fully opaque or fully transparent
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
float val = this.ShouldBeVisibleNow ? 0f : -1f;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
float dest = this.ShouldBeVisibleNow ? 1f : 0f;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
var sign = Math.Sign(dest - val);
|
|
|
|
|
val += sign * fadeRate * Time.deltaTime;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
if (sign == 0 || sign == 1 && val >= dest || sign == -1 && val <= dest)
|
|
|
|
|
break;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
pb.SetFloat(FadePropertyId, val);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
mr.SetPropertyBlock(pb);
|
2021-07-18 04:03:43 +00:00
|
|
|
|
yield return null;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
_isFading = false;
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
mr.SetPropertyBlock(null);
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.gameObject.SetActive(this.ShouldBeVisibleNow);
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private void UpdateVisibility()
|
|
|
|
|
{
|
|
|
|
|
bool needsFading = this.NeedsFading();
|
|
|
|
|
|
|
|
|
|
this.gameObject.SetActive(needsFading || this.ShouldBeVisibleNow);
|
|
|
|
|
|
|
|
|
|
if (needsFading)
|
|
|
|
|
{
|
|
|
|
|
_isFading = false;
|
|
|
|
|
this.StopCoroutine(nameof(FadeCoroutine));
|
|
|
|
|
this.StartCoroutine(nameof(FadeCoroutine));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (LodChild != null)
|
|
|
|
|
LodChild.UpdateVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool NeedsFading()
|
|
|
|
|
{
|
|
|
|
|
if (F.IsInHeadlessMode)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// always fade, except when parent should be visible, but he is still loading
|
|
|
|
|
|
|
|
|
|
if (LodParent == null)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (LodParent.IsVisibleInMapSystem && !LodParent._isGeometryLoaded)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private bool ShouldReceiveShadows()
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
if (null == this.ObjectDefinition)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var flags = ObjectFlag.Additive // transparent
|
|
|
|
|
| ObjectFlag.NoZBufferWrite // transparent
|
|
|
|
|
| ObjectFlag.DontReceiveShadows;
|
|
|
|
|
|
|
|
|
|
if ((this.ObjectDefinition.Flags & flags) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (LodParent != null) // LOD models should not receive shadows
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ShouldCastShadows()
|
|
|
|
|
{
|
|
|
|
|
if (null == this.ObjectDefinition)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var flags = ObjectFlag.Additive // transparent
|
|
|
|
|
| ObjectFlag.NoZBufferWrite; // transparent
|
|
|
|
|
|
|
|
|
|
if ((this.ObjectDefinition.Flags & flags) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if object is LOD, only cast shadows if it has large draw distance
|
|
|
|
|
// - that's because his shadow may be visible from long distance
|
|
|
|
|
if (LodParent != null && this.ObjectDefinition.DrawDist < 1000f)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
2021-05-04 17:54:47 +00:00
|
|
|
|
|
|
|
|
|
private static void OnHourChanged()
|
|
|
|
|
{
|
|
|
|
|
foreach (var timedObject in s_timedObjects)
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
if (timedObject.IsVisibleInMapSystem)
|
2021-05-04 17:54:47 +00:00
|
|
|
|
{
|
|
|
|
|
timedObject.gameObject.SetActive(timedObject.IsVisibleBasedOnCurrentDayTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-25 22:47:50 +00:00
|
|
|
|
|
|
|
|
|
foreach (var activeObjectWithLight in s_activeObjectsWithLights)
|
|
|
|
|
{
|
|
|
|
|
activeObjectWithLight.UpdateLightsBasedOnDayTime();
|
|
|
|
|
}
|
2021-05-04 17:54:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsObjectVisibleBasedOnCurrentDayTime(TimeObjectDef timeObjectDef)
|
|
|
|
|
{
|
|
|
|
|
byte currentHour = DayTimeManager.Singleton.CurrentTimeHours;
|
|
|
|
|
if (timeObjectDef.TimeOnHours < timeObjectDef.TimeOffHours)
|
|
|
|
|
{
|
|
|
|
|
return currentHour >= timeObjectDef.TimeOnHours && currentHour < timeObjectDef.TimeOffHours;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return currentHour >= timeObjectDef.TimeOnHours || currentHour < timeObjectDef.TimeOffHours;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-25 13:51:00 +00:00
|
|
|
|
|
2021-06-25 20:12:46 +00:00
|
|
|
|
private void CreateLights(
|
|
|
|
|
Geometry.GeometryParts geometryParts)
|
|
|
|
|
{
|
|
|
|
|
var lights = CreateLights(this.transform, geometryParts);
|
|
|
|
|
if (lights.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-06-25 20:16:27 +00:00
|
|
|
|
m_lightSources = lights.ToArray();
|
2021-06-25 20:12:46 +00:00
|
|
|
|
|
2021-06-25 22:47:50 +00:00
|
|
|
|
if (this.gameObject.activeInHierarchy)
|
|
|
|
|
s_activeObjectsWithLights.Add(this);
|
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
var trafficLightSources = lights
|
2021-06-25 20:12:46 +00:00
|
|
|
|
.Where(l => l.LightInfo.CoronaShowModeFlags == TwoDEffect.Light.CoronaShowMode.TRAFFICLIGHT)
|
2021-06-25 20:16:27 +00:00
|
|
|
|
.ToArray();
|
2021-06-25 20:12:46 +00:00
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
if (trafficLightSources.Length % 3 != 0)
|
|
|
|
|
Debug.LogError($"Traffic lights count should be multiple of 3, found {trafficLightSources.Length}");
|
|
|
|
|
|
|
|
|
|
var redLights = trafficLightSources.Where(l => IsColorInRange(l.LightInfo.Color, Color.red, 50, 30, 30)).ToArray();
|
|
|
|
|
var yellowLights = trafficLightSources.Where(l => IsColorInRange(l.LightInfo.Color, new Color32(255, 255, 0, 0), 50, 150, 80)).ToArray();
|
|
|
|
|
var greenLights = trafficLightSources.Where(l => IsColorInRange(l.LightInfo.Color, Color.green, 50, 50, 50)).ToArray();
|
|
|
|
|
|
|
|
|
|
if (redLights.Length + yellowLights.Length + greenLights.Length != trafficLightSources.Length)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Failed to identify some traffic light colors");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_redTrafficLights = redLights.Length > 0 ? redLights : null;
|
|
|
|
|
m_yellowTrafficLights = yellowLights.Length > 0 ? yellowLights : null;
|
|
|
|
|
m_greenTrafficLights = greenLights.Length > 0 ? greenLights : null;
|
|
|
|
|
|
|
|
|
|
m_hasTrafficLights = m_redTrafficLights != null || m_yellowTrafficLights != null || m_greenTrafficLights != null;
|
2021-06-25 20:12:46 +00:00
|
|
|
|
|
2021-06-25 22:58:43 +00:00
|
|
|
|
this.UpdateLightsBasedOnDayTime();
|
|
|
|
|
|
2021-06-25 23:47:52 +00:00
|
|
|
|
this.gameObject.AddComponent<FaceTowardsCamera>().transformsToFace = m_lightSources.Select(l => l.transform).ToArray();
|
|
|
|
|
|
2021-06-25 20:12:46 +00:00
|
|
|
|
this.InvokeRepeating(nameof(this.UpdateLights), 0f, 0.2f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<LightSource> CreateLights(
|
|
|
|
|
Transform tr,
|
|
|
|
|
Geometry.GeometryParts geometryParts)
|
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample("CreateLights()", tr);
|
|
|
|
|
|
|
|
|
|
var lights = new List<LightSource>();
|
|
|
|
|
|
|
|
|
|
foreach (var geometry in geometryParts.Geometry)
|
|
|
|
|
{
|
|
|
|
|
var twoDEffect = geometry.RwGeometry.TwoDEffect;
|
|
|
|
|
if (twoDEffect != null && twoDEffect.Lights != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var lightInfo in twoDEffect.Lights)
|
|
|
|
|
{
|
|
|
|
|
lights.Add(LightSource.Create(tr, lightInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
return lights;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float GetTrafficLightTimeOffset()
|
|
|
|
|
{
|
|
|
|
|
// determine time offset based on rotation of object
|
|
|
|
|
float angle = Vector3.Angle(this.transform.forward.WithXAndZ(), Vector3.forward);
|
|
|
|
|
float perc = angle / 180f;
|
|
|
|
|
return perc * GetTrafficLightCycleDuration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static float GetTrafficLightCycleDuration()
|
|
|
|
|
{
|
|
|
|
|
var cell = Cell.Instance;
|
|
|
|
|
return cell.redTrafficLightDuration + cell.yellowTrafficLightDuration + cell.greenTrafficLightDuration;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:56:52 +00:00
|
|
|
|
private int CalculateActiveTrafficLightIndex()
|
|
|
|
|
{
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
|
|
double currentTimeForThisObject = Net.NetManager.NetworkTime + (double)GetTrafficLightTimeOffset();
|
|
|
|
|
double timeInsideCycle = currentTimeForThisObject % (double)GetTrafficLightCycleDuration();
|
|
|
|
|
|
|
|
|
|
var cell = Cell.Instance;
|
|
|
|
|
|
|
|
|
|
if (timeInsideCycle <= cell.redTrafficLightDuration)
|
|
|
|
|
index = 0;
|
|
|
|
|
else if (timeInsideCycle <= cell.redTrafficLightDuration + cell.yellowTrafficLightDuration)
|
|
|
|
|
index = 1;
|
|
|
|
|
else if (timeInsideCycle <= cell.redTrafficLightDuration + cell.yellowTrafficLightDuration + cell.greenTrafficLightDuration)
|
|
|
|
|
index = 2;
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 18:28:11 +00:00
|
|
|
|
private void UpdateLights()
|
2021-06-25 21:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
UpdateTrafficLights();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateTrafficLights()
|
|
|
|
|
{
|
|
|
|
|
if (!m_hasTrafficLights)
|
|
|
|
|
return;
|
2021-06-25 19:30:31 +00:00
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
// update active traffic light
|
|
|
|
|
m_activeTrafficLightIndex = this.CalculateActiveTrafficLightIndex();
|
2021-06-25 19:30:31 +00:00
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
// disable/enable traffic lights based on which one is active
|
2021-06-25 19:30:31 +00:00
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
if (m_redTrafficLights != null)
|
|
|
|
|
EnableLights(m_redTrafficLights, m_activeTrafficLightIndex == 0);
|
|
|
|
|
if (m_yellowTrafficLights != null)
|
|
|
|
|
EnableLights(m_yellowTrafficLights, m_activeTrafficLightIndex == 1);
|
|
|
|
|
if (m_greenTrafficLights != null)
|
|
|
|
|
EnableLights(m_greenTrafficLights, m_activeTrafficLightIndex == 2);
|
|
|
|
|
}
|
2021-06-25 19:30:31 +00:00
|
|
|
|
|
2021-06-25 22:47:50 +00:00
|
|
|
|
private void UpdateLightsBasedOnDayTime()
|
|
|
|
|
{
|
|
|
|
|
if (null == m_lightSources)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
bool isDay = DayTimeManager.Singleton.CurrentTimeHours > 6 && DayTimeManager.Singleton.CurrentTimeHours < 18;
|
2021-06-25 22:47:50 +00:00
|
|
|
|
var flag = isDay ? TwoDEffect.Light.Flags1.AT_DAY : TwoDEffect.Light.Flags1.AT_NIGHT;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < m_lightSources.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
bool b = (m_lightSources[i].LightInfo.Flags_1 & flag) == flag;
|
|
|
|
|
m_lightSources[i].gameObject.SetActive(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:00:33 +00:00
|
|
|
|
private static void EnableLights(LightSource[] lights, bool enable)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < lights.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lights[i].gameObject.SetActive(enable);
|
2021-06-25 13:51:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-25 19:30:31 +00:00
|
|
|
|
|
|
|
|
|
private static bool IsColorInRange(Color32 targetColor, Color32 colorToCheck, int redVar, int greenVar, int blueVar)
|
|
|
|
|
{
|
|
|
|
|
var diffR = targetColor.r - colorToCheck.r;
|
|
|
|
|
var diffG = targetColor.g - colorToCheck.g;
|
|
|
|
|
var diffB = targetColor.b - colorToCheck.b;
|
|
|
|
|
|
|
|
|
|
return Mathf.Abs(diffR) <= redVar && Mathf.Abs(diffG) <= greenVar && Mathf.Abs(diffB) <= blueVar;
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|