180 lines
6.1 KiB
C#
180 lines
6.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using Convention;
|
|
using Dreamteck.Splines;
|
|
using UnityEngine;
|
|
|
|
namespace Demo.Game
|
|
{
|
|
public interface IDependOnSplineRenderer
|
|
{
|
|
|
|
}
|
|
|
|
public static class DependOnSplineRendererUtility
|
|
{
|
|
public static BasicSplineRenderer LoadSplineRendererTool(this IDependOnSplineCore self, string path)
|
|
{
|
|
return self.SharedInterfaceScriptObject.FindWithPath(path, true) as BasicSplineRenderer;
|
|
}
|
|
}
|
|
|
|
public abstract class BasicSplineRenderer : Updatement<SplineClipDuration>, IAssetBundleLoader, IDependOnSplineCore
|
|
{
|
|
[Content] public SplineCore MySplineCore { get; set; }
|
|
[Content] public MeshRenderer MyMeshRenderer;
|
|
[Header("LineRenderer.Material")]
|
|
[Content] public string LinesAssetBundlePath;
|
|
[Content] public AssetBundle LinesAssetBundle;
|
|
[Content] public string LineMaterial;
|
|
[Content] public Material MyLineMaterial;
|
|
|
|
public abstract Vector3 EvaluateClipFromPosition(float time);
|
|
|
|
public abstract Vector3 EvaluateClipToPosition(float time);
|
|
|
|
public abstract SplineSample EvaluateClipFrom(float time);
|
|
|
|
public abstract SplineSample EvaluateClipTo(float time);
|
|
|
|
public override IEnumerator LoadScript(string script)
|
|
{
|
|
yield return base.LoadScript(script);
|
|
// Bind and Init Spline
|
|
var splineGameObject = MySplineCore.gameObject;
|
|
this.GetOrAddComponent<MeshFilter>();
|
|
MyMeshRenderer = this.GetOrAddComponent<MeshRenderer>();
|
|
MyMeshRenderer.enabled = true;
|
|
if (string.IsNullOrEmpty(LinesAssetBundlePath) == false)
|
|
{
|
|
var ir = LinesAssetBundle.LoadAssetAsync<Material>(LineMaterial);
|
|
ir.completed += x =>
|
|
{
|
|
MyLineMaterial = ir.asset as Material;
|
|
};
|
|
yield return ir;
|
|
}
|
|
MyMeshRenderer.material = MyLineMaterial;
|
|
}
|
|
|
|
public override IEnumerator UnloadScript()
|
|
{
|
|
if (string.IsNullOrEmpty(LinesAssetBundlePath) == false)
|
|
yield return this.UnloadAssetBundle(LinesAssetBundlePath);
|
|
LinesAssetBundlePath = "";
|
|
yield return base.UnloadScript();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <param name="time">插值时间</param>
|
|
/// <param name="from"></param>
|
|
/// <param name="to"></param>
|
|
/// <param name="curveType">可取值为30种缓动曲线</param>
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public void Add(string time, string from, string to, string curveType)
|
|
{
|
|
ManualAddEntry(time, new(float.Parse(from), float.Parse(to)), Enum.Parse<MathExtension.EaseCurveType>(curveType));
|
|
}
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public IEnumerator LoadSpline(string path)
|
|
{
|
|
yield return this.LoadSplineTool(path);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 加载对应ab包并加载指定材质
|
|
/// </summary>
|
|
/// <param name="ab"></param>
|
|
/// <param name="material"></param>
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public IEnumerator LoadMaterial(string ab, string material)
|
|
{
|
|
yield return this.LoadAssetBundle(ab, x =>
|
|
{
|
|
LinesAssetBundlePath = ab;
|
|
LinesAssetBundle = x;
|
|
LineMaterial = material;
|
|
});
|
|
}
|
|
|
|
protected override SplineClipDuration Lerp(SplineClipDuration begin, SplineClipDuration end, float t)
|
|
{
|
|
return new(Mathf.Lerp(begin.ClipFrom, end.ClipFrom, t), Mathf.Lerp(begin.ClipTo, end.ClipTo, t));
|
|
}
|
|
}
|
|
|
|
public class BasicSplineRenderer<TMeshGenerator>: BasicSplineRenderer where TMeshGenerator : MeshGenerator
|
|
{
|
|
[Content] public TMeshGenerator MyMeshGenerator;
|
|
|
|
public override IEnumerator LoadScript(string script)
|
|
{
|
|
yield return base.LoadScript(script);
|
|
// Setup Mesh Generater
|
|
MyMeshRenderer.material = MyLineMaterial;
|
|
MyMeshGenerator = this.GetOrAddComponent<TMeshGenerator>();
|
|
MyMeshGenerator.spline = MySplineCore.MySplineComputer;
|
|
SetupMeshGenerator(MyMeshGenerator);
|
|
MyMeshGenerator.RebuildImmediate();
|
|
}
|
|
|
|
#region SetupMeshGenerator
|
|
|
|
[Content] public MeshGenerator.UVMode MyUVMode = default;
|
|
|
|
public virtual void SetupMeshGenerator(TMeshGenerator meshGenerater)
|
|
{
|
|
meshGenerater.uvMode = MyUVMode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置材质UV映射
|
|
/// </summary>
|
|
/// <param name="mode">Clip, UniformClip, Clamp, UniformClamp</param>
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public void SetUVMode(string mode)
|
|
{
|
|
MyUVMode = Enum.Parse<MeshGenerator.UVMode>(mode);
|
|
}
|
|
|
|
#endregion
|
|
|
|
protected override void UpdateData(SplineClipDuration data)
|
|
{
|
|
MyMeshGenerator.SetClipRange(data.ClipFrom, data.ClipTo);
|
|
}
|
|
|
|
protected override void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
|
|
{
|
|
//保护措施
|
|
if (MyMeshGenerator == null)
|
|
return;
|
|
base.UpdateTicks(currentTime, deltaTime, tickType);
|
|
}
|
|
|
|
public override Vector3 EvaluateClipFromPosition(float time)
|
|
{
|
|
return MySplineCore.MySplineComputer.EvaluatePosition(Evaluate(time).ClipFrom);
|
|
}
|
|
|
|
public override Vector3 EvaluateClipToPosition(float time)
|
|
{
|
|
return MySplineCore.MySplineComputer.EvaluatePosition(Evaluate(time).ClipTo);
|
|
}
|
|
|
|
public override SplineSample EvaluateClipFrom(float time)
|
|
{
|
|
return MySplineCore.MySplineComputer.Evaluate(Evaluate(time).ClipFrom);
|
|
}
|
|
|
|
public override SplineSample EvaluateClipTo(float time)
|
|
{
|
|
return MySplineCore.MySplineComputer.Evaluate(Evaluate(time).ClipTo);
|
|
}
|
|
}
|
|
}
|