Skip to content

Commit e3b3e31

Browse files
authored
Merge pull request #155 from ToniMacaroni/development
Fix SettingsSetter error after soft reload
2 parents bebd0c5 + f2ca0ec commit e3b3e31

File tree

6 files changed

+157
-71
lines changed

6 files changed

+157
-71
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Linq;
3+
using UnityEngine;
4+
using Object = UnityEngine.Object;
5+
6+
namespace SaberFactory.Helpers
7+
{
8+
internal class ImmediateDrawer
9+
{
10+
public bool IsInitialized { get; private set; }
11+
12+
public ImmediateDrawer()
13+
{
14+
var prim = GameObject.CreatePrimitive(PrimitiveType.Sphere);
15+
_ballmesh = prim.GetComponent<MeshFilter>().mesh;
16+
Object.Destroy(prim);
17+
18+
var shader = FindShader();
19+
if (!shader)
20+
{
21+
return;
22+
}
23+
24+
_mat = new Material(shader);
25+
IsInitialized = true;
26+
}
27+
28+
public void DrawBall(Vector3 pos, float size, Color color)
29+
{
30+
_mat.color = color;
31+
_mat.SetPass(0);
32+
Graphics.DrawMesh(_ballmesh, Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one * size), _mat, 0);
33+
}
34+
35+
public void DrawSmallBall(Vector3 pos, Color? color = null)
36+
{
37+
if(color == null)
38+
color = Color.red;
39+
DrawBall(pos, 0.05f, color.Value);
40+
}
41+
42+
private Shader FindShader()
43+
{
44+
var possibleShaders = new string[]
45+
{
46+
"BeatSaber/Unlit Glow",
47+
"Standard",
48+
};
49+
50+
return Resources.FindObjectsOfTypeAll<Shader>().FirstOrDefault(x=> possibleShaders.Contains(x.name));
51+
}
52+
53+
private Mesh _ballmesh;
54+
private Material _mat;
55+
}
56+
}

SaberFactory/Installers/HeckSettableSettingsInitializer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace SaberFactory.Installers
66
internal class HeckSettableSettingsInitializer : IInitializable
77

88
{
9+
public static bool Initialized { get; private set; }
10+
911
private const string GroupName = "_saberFactory";
1012

1113
public SettableSetting<bool> RelativeTrailMode;
@@ -17,7 +19,11 @@ public HeckSettableSettingsInitializer(SaberSettableSettings saberSettableSettin
1719

1820
public void Initialize()
1921
{
22+
if (!Initialized)
23+
return;
24+
2025
RegisterSetting(ref RelativeTrailMode, _saberSettableSettings.RelativeTrailMode, "_relativeTrailMode");
26+
Initialized = true;
2127
}
2228

2329
private void RegisterSetting<T>(ref SettableSetting<T> setting, SaberSettableSettings.SettableSettingAbstraction<T> abstraction,

SaberFactory/Instances/Trail/SFTrail.cs

+64-48
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using SaberFactory.Helpers;
23
using SaberFactory.Misc;
34
using UnityEngine;
45

@@ -7,9 +8,11 @@ namespace SaberFactory.Instances.Trail
78
internal class SFTrail : MonoBehaviour
89
{
910
public static bool CapFps;
11+
1012
protected float TrailWidth => (PointStart.position - PointEnd.position).magnitude;
1113

1214
public Vector3 CurHeadPos => (PStartPos + PEndPos) / 2f;
15+
1316
public Color Color = Color.white;
1417
public int Granularity = 60;
1518
public int SamplingFrequency = 20;
@@ -31,9 +34,11 @@ internal class SFTrail : MonoBehaviour
3134
protected VertexPool.VertexSegment _vertexSegment;
3235

3336
private readonly int _skipFirstFrames = 4;
37+
3438
private int _frameNum;
3539
private float _time;
3640
private bool _relativeMode;
41+
private ImmediateDrawer _immediateDrawer;
3742

3843
public bool RelativeMode
3944
{
@@ -60,6 +65,56 @@ private Vector3 GetPlayerOffset()
6065
return PlayerTransforms.transform.position;
6166
}
6267

68+
private void OnEnable()
69+
{
70+
_vertexPool?.SetMeshObjectActive(true);
71+
}
72+
73+
private void OnDisable()
74+
{
75+
_vertexPool?.SetMeshObjectActive(false);
76+
}
77+
78+
private void OnDestroy()
79+
{
80+
if (!_inited || _vertexPool == null)
81+
{
82+
return;
83+
}
84+
85+
_vertexPool.Destroy();
86+
}
87+
88+
public void Setup(TrailInitData initData, Transform pointStart, Transform pointEnd, Material material, bool editor)
89+
{
90+
PointStart = pointStart;
91+
PointEnd = pointEnd;
92+
Material = material;
93+
Granularity = initData.Granularity;
94+
TrailLength = initData.TrailLength;
95+
Whitestep = initData.Whitestep;
96+
SamplingFrequency = initData.SamplingFrequency;
97+
98+
#if DEBUG
99+
_immediateDrawer = new ImmediateDrawer();
100+
#endif
101+
102+
gameObject.layer = 12;
103+
if (editor)
104+
{
105+
SortingOrder = 3;
106+
}
107+
108+
_elemPool = new ElementPool(TrailLength);
109+
_vertexPool = new VertexPool(Material, this);
110+
_vertexSegment = _vertexPool.GetVertices(Granularity * 3, (Granularity - 1) * 12);
111+
UpdateIndices();
112+
113+
_vertexPool.SetMeshObjectActive(false);
114+
115+
_inited = true;
116+
}
117+
63118
private void LateUpdate()
64119
{
65120
// if (PlayerTransforms)
@@ -124,58 +179,12 @@ private void LateUpdate()
124179
_snapshotList[1].PointStart -= offset;
125180
_snapshotList[1].PointEnd -= offset;
126181
}
127-
182+
128183
RefreshSpline();
129184
UpdateVertex();
130185
_vertexPool.LateUpdate();
131186
}
132187

133-
private void OnEnable()
134-
{
135-
_vertexPool?.SetMeshObjectActive(true);
136-
}
137-
138-
private void OnDisable()
139-
{
140-
_vertexPool?.SetMeshObjectActive(false);
141-
}
142-
143-
private void OnDestroy()
144-
{
145-
if (!_inited || _vertexPool == null)
146-
{
147-
return;
148-
}
149-
150-
_vertexPool.Destroy();
151-
}
152-
153-
public void Setup(TrailInitData initData, Transform pointStart, Transform pointEnd, Material material, bool editor)
154-
{
155-
PointStart = pointStart;
156-
PointEnd = pointEnd;
157-
Material = material;
158-
Granularity = initData.Granularity;
159-
TrailLength = initData.TrailLength;
160-
Whitestep = initData.Whitestep;
161-
SamplingFrequency = initData.SamplingFrequency;
162-
163-
gameObject.layer = 12;
164-
if (editor)
165-
{
166-
SortingOrder = 3;
167-
}
168-
169-
_elemPool = new ElementPool(TrailLength);
170-
_vertexPool = new VertexPool(Material, this);
171-
_vertexSegment = _vertexPool.GetVertices(Granularity * 3, (Granularity - 1) * 12);
172-
UpdateIndices();
173-
174-
_vertexPool.SetMeshObjectActive(false);
175-
176-
_inited = true;
177-
}
178-
179188
public void SetMaterialBlock(MaterialPropertyBlock block)
180189
{
181190
if (_vertexPool == null || !_vertexPool.MeshRenderer)
@@ -191,6 +200,9 @@ private void RefreshSpline()
191200
for (var i = 0; i < _snapshotList.Count; i++)
192201
{
193202
_spline.ControlPoints[i].Position = _snapshotList[i].Pos;
203+
#if DEBUG
204+
_immediateDrawer.DrawSmallBall(_spline.ControlPoints[i].Position);
205+
#endif
194206
_spline.ControlPoints[i].Normal = _snapshotList[i].PointEnd - _snapshotList[i].PointStart;
195207
}
196208

@@ -249,10 +261,14 @@ private void UpdateVertex()
249261
uvCoord.x = 1f;
250262
uvCoord.y = uvSegment;
251263
pool.UVs[baseIdx + 2] = uvCoord;
264+
265+
#if DEBUG
266+
_immediateDrawer.DrawSmallBall(pos0, Color.green);
267+
_immediateDrawer.DrawSmallBall(pos1, Color.green);
268+
#endif
252269
}
253270

254271
_vertexSegment.Pool.UVChanged = true;
255-
_vertexSegment.Pool.VertChanged = true;
256272
_vertexSegment.Pool.ColorChanged = true;
257273
}
258274

SaberFactory/Misc/RemotePartRetriever.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public async Task Retrieve()
4646
try
4747
{
4848
#if DEBUG
49-
var content = File.ReadAllText(@"I:\repos\SaberFactory\SaberList.json");
50-
//var response = await _httpService.GetAsync("https://raw.githubusercontent.com/ToniMacaroni/SaberFactory/development/SaberList.json");
51-
//var content = await response.ReadAsStringAsync();
49+
// var content = File.ReadAllText(@"C:\Users\name1\Desktop\SaberFactory\SaberList.json");
50+
var response = await _httpService.GetAsync("https://raw.githubusercontent.com/ToniMacaroni/SaberFactory/development/SaberList.json");
51+
var content = await response.ReadAsStringAsync();
5252
#else
5353
var response = await _httpService.GetAsync(RemoteSaberUrl);
5454
var content = await response.ReadAsStringAsync();

SaberFactory/Misc/VertexPool.cs

+15-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ internal class VertexPool
99
{
1010
public const int BlockSize = 108;
1111

12-
public Mesh MyMesh => _meshFilter != null ? _meshFilter.sharedMesh : null;
1312
public MeshRenderer MeshRenderer;
1413

1514
public float BoundsScheduleTime = 1f;
@@ -20,13 +19,12 @@ internal class VertexPool
2019

2120
public bool IndiceChanged;
2221
public int[] Indices;
23-
public bool UV2Changed;
2422
public bool UVChanged;
2523
public Vector2[] UVs;
26-
public bool VertChanged;
2724

2825
public Vector3[] Vertices;
2926

27+
protected Mesh _mesh;
3028
protected GameObject _gameObject;
3129
protected int _indexTotal;
3230
protected int _indexUsed;
@@ -40,6 +38,8 @@ internal class VertexPool
4038
protected int _vertexTotal;
4139
protected int _vertexUsed;
4240

41+
private bool _canTick;
42+
4343
public VertexPool(Material material, SFTrail owner)
4444
{
4545
_vertexTotal = _vertexUsed = 0;
@@ -48,18 +48,18 @@ public VertexPool(Material material, SFTrail owner)
4848
CreateMeshObj(owner, material);
4949
_material = material;
5050
InitArrays();
51-
IndiceChanged = ColorChanged = UVChanged = UV2Changed = VertChanged = true;
51+
IndiceChanged = ColorChanged = UVChanged = true;
5252
}
5353

5454
public void RecalculateBounds()
5555
{
56-
MyMesh.RecalculateBounds();
56+
_mesh.RecalculateBounds();
5757
}
5858

5959

6060
public void SetMeshObjectActive(bool flag)
6161
{
62-
if (_meshFilter == null)
62+
if (!_meshFilter)
6363
{
6464
return;
6565
}
@@ -81,7 +81,9 @@ private void CreateMeshObj(SFTrail owner, Material material)
8181
MeshRenderer.sharedMaterial = material;
8282
MeshRenderer.sortingLayerName = _owner.SortingLayerName;
8383
MeshRenderer.sortingOrder = _owner.SortingOrder;
84-
_meshFilter.sharedMesh = new Mesh();
84+
_meshFilter.sharedMesh = _mesh = new Mesh();
85+
86+
_canTick = true;
8587
}
8688

8789
public void Destroy()
@@ -152,36 +154,32 @@ public void EnlargeArrays(int count, int icount)
152154
IndiceChanged = true;
153155
ColorChanged = true;
154156
UVChanged = true;
155-
VertChanged = true;
156-
UV2Changed = true;
157157
}
158158

159159
public void LateUpdate()
160160
{
161-
if (MyMesh == null)
162-
{
161+
if (!_canTick)
163162
return;
164-
}
165163

166164
if (_vertCountChanged)
167165
{
168-
MyMesh.Clear();
166+
_mesh.Clear();
169167
}
170168

171-
MyMesh.vertices = Vertices;
169+
_mesh.vertices = Vertices;
172170
if (UVChanged)
173171
{
174-
MyMesh.uv = UVs;
172+
_mesh.uv = UVs;
175173
}
176174

177175
if (ColorChanged)
178176
{
179-
MyMesh.colors = Colors;
177+
_mesh.colors = Colors;
180178
}
181179

182180
if (IndiceChanged)
183181
{
184-
MyMesh.triangles = Indices;
182+
_mesh.triangles = Indices;
185183
}
186184

187185
ElapsedTime += Time.deltaTime;
@@ -200,8 +198,6 @@ public void LateUpdate()
200198
IndiceChanged = false;
201199
ColorChanged = false;
202200
UVChanged = false;
203-
UV2Changed = false;
204-
VertChanged = false;
205201
}
206202

207203
public class VertexSegment

0 commit comments

Comments
 (0)