change vehicle's color system to use Color32 instead of indices

This commit is contained in:
in0finite 2021-11-14 23:53:23 +01:00
parent a3e7f7ebe7
commit 6d1b0f0a78
4 changed files with 62 additions and 23 deletions

View file

@ -115,8 +115,10 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public static int MeshLayer => UnityEngine.LayerMask.NameToLayer("VehicleMesh"); public static int MeshLayer => UnityEngine.LayerMask.NameToLayer("VehicleMesh");
private readonly int[] _colors = { 0, 0, 0, 0 }; public static readonly Color32 DefaultVehicleColor = default;
public int[] Colors => _colors;
private readonly Color32[] _colors = { DefaultVehicleColor, DefaultVehicleColor, DefaultVehicleColor, DefaultVehicleColor };
public IReadOnlyList<Color32> Colors => _colors;
private readonly float[] _lights = { 0, 0, 0, 0 }; private readonly float[] _lights = { 0, 0, 0, 0 };
private MaterialPropertyBlock _props; private MaterialPropertyBlock _props;
private static readonly int CarColorPropertyId = Shader.PropertyToID("_CarColor"); private static readonly int CarColorPropertyId = Shader.PropertyToID("_CarColor");
@ -206,10 +208,19 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public void SetColors(params int[] clrIndices) public void SetColors(params int[] clrIndices)
{ {
for (var i = 0; i < 4 && i < clrIndices.Length; ++i) this.SetColors(CarColors.FromIndices(clrIndices));
}
public void SetColors(Color32[] colors)
{
/*if (colors.Length > 4)
throw new System.ArgumentException("Vehicle can not have more than 4 colors");*/
for (int i = 0; i < 4 && i < colors.Length; ++i)
{ {
if (_colors[i].Equals(clrIndices[i])) continue; if (_colors[i].Equals(colors[i]))
_colors[i] = clrIndices[i]; continue;
_colors[i] = colors[i];
_colorsChanged = true; _colorsChanged = true;
} }
} }
@ -437,12 +448,10 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public static void UpdateMaterials( public static void UpdateMaterials(
FrameContainer frames, FrameContainer frames,
int[] colors, Color32[] paintJobColors,
float[] lights, float[] lights,
MaterialPropertyBlock materialPropertyBlock) MaterialPropertyBlock materialPropertyBlock)
{ {
var indices = CarColors.FromIndices(colors);
Color32 headLightColor = new Color32(255, 255, 255, 255); Color32 headLightColor = new Color32(255, 255, 255, 255);
Color32 tailLightColor = new Color32(255, 255, 255, 255); Color32 tailLightColor = new Color32(255, 255, 255, 255);
@ -450,10 +459,10 @@ namespace SanAndreasUnity.Behaviours.Vehicles
Color32[] carColors = new [] Color32[] carColors = new []
{ {
new Color32(255, 255, 255, 255), new Color32(255, 255, 255, 255),
indices[0], paintJobColors[0],
indices[1], paintJobColors[1],
indices[2], paintJobColors[2],
indices[3], paintJobColors[3],
headLightColor, headLightColor,
headLightColor, headLightColor,
tailLightColor, tailLightColor,

View file

@ -3,6 +3,7 @@ using SanAndreasUnity.Net;
using Mirror; using Mirror;
using SanAndreasUnity.Utilities; using SanAndreasUnity.Utilities;
using System.Linq; using System.Linq;
using System.Collections.Generic;
namespace SanAndreasUnity.Behaviours.Vehicles namespace SanAndreasUnity.Behaviours.Vehicles
{ {
@ -62,8 +63,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
{ {
F.RunExceptionSafe( () => { F.RunExceptionSafe( () => {
// load vehicle on clients // load vehicle on clients
int[] colors = DeserializeColors(m_net_carColors);
m_vehicle = Vehicle.Create(this.gameObject, m_net_id, colors, this.transform.position, this.transform.rotation); Color32[] colors = DeserializeColors(m_net_carColors);
m_vehicle = Vehicle.Create(this.gameObject, m_net_id, null, this.transform.position, this.transform.rotation);
m_vehicle.SetColors(colors);
// update rigid body status // update rigid body status
this.EnableOrDisableRigidBody(); this.EnableOrDisableRigidBody();
@ -91,14 +96,38 @@ namespace SanAndreasUnity.Behaviours.Vehicles
this.EnableOrDisableRigidBody(); this.EnableOrDisableRigidBody();
} }
public static string SerializeColors(int[] colors) public static string SerializeColors(IEnumerable<Color32> colors)
{ {
return colors != null ? string.Join(";", colors.Select(c => c.ToString(System.Globalization.CultureInfo.InvariantCulture))) : null; return colors != null
? string.Join("|", colors.Select(c => SerializeColor(c)))
: null;
} }
public static int[] DeserializeColors(string colors) public static Color32[] DeserializeColors(string colors)
{ {
return string.IsNullOrEmpty(colors) ? null : colors.Split(';').Select(s => int.Parse(s, System.Globalization.CultureInfo.InvariantCulture)).ToArray(); return string.IsNullOrEmpty(colors)
? null
: colors.Split('|').Select(s => DeserializeColor(s)).ToArray();
}
public static string SerializeColor(Color32 color)
{
byte[] values = new byte[] { color.r, color.g, color.b, color.a };
return string.Join(";", values.Select(v => v.ToString(System.Globalization.CultureInfo.InvariantCulture)));
}
public static Color32 DeserializeColor(string colorString)
{
string[] splits = colorString.Split(';');
if (splits.Length != 4)
throw new System.ArgumentException($"Failed to deserialize color - expected 4 components, found {splits.Length}");
return new Color32(
byte.Parse(splits[0], System.Globalization.CultureInfo.InvariantCulture),
byte.Parse(splits[1], System.Globalization.CultureInfo.InvariantCulture),
byte.Parse(splits[2], System.Globalization.CultureInfo.InvariantCulture),
byte.Parse(splits[3], System.Globalization.CultureInfo.InvariantCulture));
} }
private void Update() private void Update()

View file

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using SanAndreasUnity.Behaviours.Audio; using SanAndreasUnity.Behaviours.Audio;
using SanAndreasUnity.Net; using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities; using SanAndreasUnity.Utilities;
@ -240,7 +241,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
DetachFrameFromTransformDuringExplosion(this.transform, frame, mass, parentGo, this.NetIdentity.netId, this.Definition.Id, this.Colors); DetachFrameFromTransformDuringExplosion(this.transform, frame, mass, parentGo, this.NetIdentity.netId, this.Definition.Id, this.Colors);
} }
public static void DetachFrameFromTransformDuringExplosion(Transform tr, Frame frame, float mass, GameObject parentGo, uint vehicleNetId, int vehicleModelId, int[] vehicleColors) public static void DetachFrameFromTransformDuringExplosion(Transform tr, Frame frame, float mass, GameObject parentGo, uint vehicleNetId, int vehicleModelId, IReadOnlyList<Color32> vehicleColors)
{ {
if (! tr.IsParentOf(frame.transform)) // already detached ? if (! tr.IsParentOf(frame.transform)) // already detached ?
return; return;

View file

@ -57,7 +57,7 @@ namespace SanAndreasUnity.Net
} }
} }
public void InitializeOnServer(uint vehicleNetId, int vehicleModelId, int[] vehicleColors, string frameName, float mass, Rigidbody rigidbody) public void InitializeOnServer(uint vehicleNetId, int vehicleModelId, IReadOnlyList<Color32> vehicleColors, string frameName, float mass, Rigidbody rigidbody)
{ {
NetStatus.ThrowIfNotOnServer(); NetStatus.ThrowIfNotOnServer();
@ -91,7 +91,7 @@ namespace SanAndreasUnity.Net
VehicleDef def = Item.GetDefinitionOrThrow<VehicleDef>(m_net_vehicleModelId); VehicleDef def = Item.GetDefinitionOrThrow<VehicleDef>(m_net_vehicleModelId);
int[] colors = VehicleController.DeserializeColors(m_net_vehicleColors); Color32[] colors = VehicleController.DeserializeColors(m_net_vehicleColors);
var geometryParts = Vehicle.LoadGeometryParts(def); var geometryParts = Vehicle.LoadGeometryParts(def);
@ -136,7 +136,7 @@ namespace SanAndreasUnity.Net
} }
private static void SetColors(FrameContainer frames, int[] colors) private static void SetColors(FrameContainer frames, Color32[] colors)
{ {
Vehicle.UpdateMaterials(frames, colors, new []{ 0f, 0f, 0f, 0f }, new MaterialPropertyBlock()); Vehicle.UpdateMaterials(frames, colors, new []{ 0f, 0f, 0f, 0f }, new MaterialPropertyBlock());
} }