Skip to content

Commit

Permalink
Merge pull request #155 from ToniMacaroni/development
Browse files Browse the repository at this point in the history
Fix SettingsSetter error after soft reload
  • Loading branch information
ToniMacaroni authored Apr 10, 2023
2 parents bebd0c5 + f2ca0ec commit e3b3e31
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 71 deletions.
56 changes: 56 additions & 0 deletions SaberFactory/Helpers/ImmediateDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Linq;
using UnityEngine;
using Object = UnityEngine.Object;

namespace SaberFactory.Helpers
{
internal class ImmediateDrawer
{
public bool IsInitialized { get; private set; }

public ImmediateDrawer()
{
var prim = GameObject.CreatePrimitive(PrimitiveType.Sphere);
_ballmesh = prim.GetComponent<MeshFilter>().mesh;
Object.Destroy(prim);

var shader = FindShader();
if (!shader)
{
return;
}

_mat = new Material(shader);
IsInitialized = true;
}

public void DrawBall(Vector3 pos, float size, Color color)
{
_mat.color = color;
_mat.SetPass(0);
Graphics.DrawMesh(_ballmesh, Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one * size), _mat, 0);
}

public void DrawSmallBall(Vector3 pos, Color? color = null)
{
if(color == null)
color = Color.red;
DrawBall(pos, 0.05f, color.Value);
}

private Shader FindShader()
{
var possibleShaders = new string[]
{
"BeatSaber/Unlit Glow",
"Standard",
};

return Resources.FindObjectsOfTypeAll<Shader>().FirstOrDefault(x=> possibleShaders.Contains(x.name));
}

private Mesh _ballmesh;
private Material _mat;
}
}
6 changes: 6 additions & 0 deletions SaberFactory/Installers/HeckSettableSettingsInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace SaberFactory.Installers
internal class HeckSettableSettingsInitializer : IInitializable

{
public static bool Initialized { get; private set; }

private const string GroupName = "_saberFactory";

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

public void Initialize()
{
if (!Initialized)
return;

RegisterSetting(ref RelativeTrailMode, _saberSettableSettings.RelativeTrailMode, "_relativeTrailMode");
Initialized = true;
}

private void RegisterSetting<T>(ref SettableSetting<T> setting, SaberSettableSettings.SettableSettingAbstraction<T> abstraction,
Expand Down
112 changes: 64 additions & 48 deletions SaberFactory/Instances/Trail/SFTrail.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using SaberFactory.Helpers;
using SaberFactory.Misc;
using UnityEngine;

Expand All @@ -7,9 +8,11 @@ namespace SaberFactory.Instances.Trail
internal class SFTrail : MonoBehaviour
{
public static bool CapFps;

protected float TrailWidth => (PointStart.position - PointEnd.position).magnitude;

public Vector3 CurHeadPos => (PStartPos + PEndPos) / 2f;

public Color Color = Color.white;
public int Granularity = 60;
public int SamplingFrequency = 20;
Expand All @@ -31,9 +34,11 @@ internal class SFTrail : MonoBehaviour
protected VertexPool.VertexSegment _vertexSegment;

private readonly int _skipFirstFrames = 4;

private int _frameNum;
private float _time;
private bool _relativeMode;
private ImmediateDrawer _immediateDrawer;

public bool RelativeMode
{
Expand All @@ -60,6 +65,56 @@ private Vector3 GetPlayerOffset()
return PlayerTransforms.transform.position;
}

private void OnEnable()
{
_vertexPool?.SetMeshObjectActive(true);
}

private void OnDisable()
{
_vertexPool?.SetMeshObjectActive(false);
}

private void OnDestroy()
{
if (!_inited || _vertexPool == null)
{
return;
}

_vertexPool.Destroy();
}

public void Setup(TrailInitData initData, Transform pointStart, Transform pointEnd, Material material, bool editor)
{
PointStart = pointStart;
PointEnd = pointEnd;
Material = material;
Granularity = initData.Granularity;
TrailLength = initData.TrailLength;
Whitestep = initData.Whitestep;
SamplingFrequency = initData.SamplingFrequency;

#if DEBUG
_immediateDrawer = new ImmediateDrawer();
#endif

gameObject.layer = 12;
if (editor)
{
SortingOrder = 3;
}

_elemPool = new ElementPool(TrailLength);
_vertexPool = new VertexPool(Material, this);
_vertexSegment = _vertexPool.GetVertices(Granularity * 3, (Granularity - 1) * 12);
UpdateIndices();

_vertexPool.SetMeshObjectActive(false);

_inited = true;
}

private void LateUpdate()
{
// if (PlayerTransforms)
Expand Down Expand Up @@ -124,58 +179,12 @@ private void LateUpdate()
_snapshotList[1].PointStart -= offset;
_snapshotList[1].PointEnd -= offset;
}

RefreshSpline();
UpdateVertex();
_vertexPool.LateUpdate();
}

private void OnEnable()
{
_vertexPool?.SetMeshObjectActive(true);
}

private void OnDisable()
{
_vertexPool?.SetMeshObjectActive(false);
}

private void OnDestroy()
{
if (!_inited || _vertexPool == null)
{
return;
}

_vertexPool.Destroy();
}

public void Setup(TrailInitData initData, Transform pointStart, Transform pointEnd, Material material, bool editor)
{
PointStart = pointStart;
PointEnd = pointEnd;
Material = material;
Granularity = initData.Granularity;
TrailLength = initData.TrailLength;
Whitestep = initData.Whitestep;
SamplingFrequency = initData.SamplingFrequency;

gameObject.layer = 12;
if (editor)
{
SortingOrder = 3;
}

_elemPool = new ElementPool(TrailLength);
_vertexPool = new VertexPool(Material, this);
_vertexSegment = _vertexPool.GetVertices(Granularity * 3, (Granularity - 1) * 12);
UpdateIndices();

_vertexPool.SetMeshObjectActive(false);

_inited = true;
}

public void SetMaterialBlock(MaterialPropertyBlock block)
{
if (_vertexPool == null || !_vertexPool.MeshRenderer)
Expand All @@ -191,6 +200,9 @@ private void RefreshSpline()
for (var i = 0; i < _snapshotList.Count; i++)
{
_spline.ControlPoints[i].Position = _snapshotList[i].Pos;
#if DEBUG
_immediateDrawer.DrawSmallBall(_spline.ControlPoints[i].Position);
#endif
_spline.ControlPoints[i].Normal = _snapshotList[i].PointEnd - _snapshotList[i].PointStart;
}

Expand Down Expand Up @@ -249,10 +261,14 @@ private void UpdateVertex()
uvCoord.x = 1f;
uvCoord.y = uvSegment;
pool.UVs[baseIdx + 2] = uvCoord;

#if DEBUG
_immediateDrawer.DrawSmallBall(pos0, Color.green);
_immediateDrawer.DrawSmallBall(pos1, Color.green);
#endif
}

_vertexSegment.Pool.UVChanged = true;
_vertexSegment.Pool.VertChanged = true;
_vertexSegment.Pool.ColorChanged = true;
}

Expand Down
6 changes: 3 additions & 3 deletions SaberFactory/Misc/RemotePartRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public async Task Retrieve()
try
{
#if DEBUG
var content = File.ReadAllText(@"I:\repos\SaberFactory\SaberList.json");
//var response = await _httpService.GetAsync("https://raw.githubusercontent.com/ToniMacaroni/SaberFactory/development/SaberList.json");
//var content = await response.ReadAsStringAsync();
// var content = File.ReadAllText(@"C:\Users\name1\Desktop\SaberFactory\SaberList.json");
var response = await _httpService.GetAsync("https://raw.githubusercontent.com/ToniMacaroni/SaberFactory/development/SaberList.json");
var content = await response.ReadAsStringAsync();
#else
var response = await _httpService.GetAsync(RemoteSaberUrl);
var content = await response.ReadAsStringAsync();
Expand Down
34 changes: 15 additions & 19 deletions SaberFactory/Misc/VertexPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ internal class VertexPool
{
public const int BlockSize = 108;

public Mesh MyMesh => _meshFilter != null ? _meshFilter.sharedMesh : null;
public MeshRenderer MeshRenderer;

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

public bool IndiceChanged;
public int[] Indices;
public bool UV2Changed;
public bool UVChanged;
public Vector2[] UVs;
public bool VertChanged;

public Vector3[] Vertices;

protected Mesh _mesh;
protected GameObject _gameObject;
protected int _indexTotal;
protected int _indexUsed;
Expand All @@ -40,6 +38,8 @@ internal class VertexPool
protected int _vertexTotal;
protected int _vertexUsed;

private bool _canTick;

public VertexPool(Material material, SFTrail owner)
{
_vertexTotal = _vertexUsed = 0;
Expand All @@ -48,18 +48,18 @@ public VertexPool(Material material, SFTrail owner)
CreateMeshObj(owner, material);
_material = material;
InitArrays();
IndiceChanged = ColorChanged = UVChanged = UV2Changed = VertChanged = true;
IndiceChanged = ColorChanged = UVChanged = true;
}

public void RecalculateBounds()
{
MyMesh.RecalculateBounds();
_mesh.RecalculateBounds();
}


public void SetMeshObjectActive(bool flag)
{
if (_meshFilter == null)
if (!_meshFilter)
{
return;
}
Expand All @@ -81,7 +81,9 @@ private void CreateMeshObj(SFTrail owner, Material material)
MeshRenderer.sharedMaterial = material;
MeshRenderer.sortingLayerName = _owner.SortingLayerName;
MeshRenderer.sortingOrder = _owner.SortingOrder;
_meshFilter.sharedMesh = new Mesh();
_meshFilter.sharedMesh = _mesh = new Mesh();

_canTick = true;
}

public void Destroy()
Expand Down Expand Up @@ -152,36 +154,32 @@ public void EnlargeArrays(int count, int icount)
IndiceChanged = true;
ColorChanged = true;
UVChanged = true;
VertChanged = true;
UV2Changed = true;
}

public void LateUpdate()
{
if (MyMesh == null)
{
if (!_canTick)
return;
}

if (_vertCountChanged)
{
MyMesh.Clear();
_mesh.Clear();
}

MyMesh.vertices = Vertices;
_mesh.vertices = Vertices;
if (UVChanged)
{
MyMesh.uv = UVs;
_mesh.uv = UVs;
}

if (ColorChanged)
{
MyMesh.colors = Colors;
_mesh.colors = Colors;
}

if (IndiceChanged)
{
MyMesh.triangles = Indices;
_mesh.triangles = Indices;
}

ElapsedTime += Time.deltaTime;
Expand All @@ -200,8 +198,6 @@ public void LateUpdate()
IndiceChanged = false;
ColorChanged = false;
UVChanged = false;
UV2Changed = false;
VertChanged = false;
}

public class VertexSegment
Expand Down
Loading

0 comments on commit e3b3e31

Please sign in to comment.