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, IAssetBundleLoader, IDependOnSplineCore { [Content] public SplineCore MySplineCore { get; set; } [Content] private MeshFilter m_MeshFilter; [Content] private MeshRenderer m_MyMeshRenderer; [Header("LineRenderer.Material")] [Content] public string LinesAssetBundlePath; private static Material m_StaticCacheDefaultMaterial; protected static Material StaticCacheDefaultMaterial { get { if (m_StaticCacheDefaultMaterial == null) m_StaticCacheDefaultMaterial = Resources.Load("Line/Default"); return m_StaticCacheDefaultMaterial; } } [Content] public Material MyDefaultMaterial; public override void ResetEnterGameStatus() { base.ResetEnterGameStatus(); MyMeshRenderer.material = MyDefaultMaterial; } public MeshFilter MyMeshFilter { get { if (m_MeshFilter == null) m_MeshFilter = this.GetOrAddComponent(); return m_MeshFilter; } } public MeshRenderer MyMeshRenderer { get { if (m_MyMeshRenderer == null) m_MyMeshRenderer = this.GetOrAddComponent(); return m_MyMeshRenderer; } } 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 UnloadScript() { if (string.IsNullOrEmpty(LinesAssetBundlePath) == false) yield return this.UnloadAssetBundle(LinesAssetBundlePath); LinesAssetBundlePath = ""; yield return base.UnloadScript(); } /// /// 新增 /// /// 插值时间 /// /// /// 可取值为30种缓动曲线 [Convention.RScript.Variable.Attr.Method] public void Add(float time, float from, float to, MathExtension.EaseCurveType curveType) { ManualAddEntry(time, new(from, to), curveType); } [Convention.RScript.Variable.Attr.Method] public void LoadSpline(string path) { this.LoadSplineTool(path); } [Convention.RScript.Variable.Attr.Method] public void LoadSpline(SplineCore spline) { this.MySplineCore = spline; } /// /// 加载对应ab包并加载指定材质 /// /// /// [Convention.RScript.Variable.Attr.Method] public void LoadMaterial(string ab, string material) { MyMeshRenderer.enabled = true; LinesAssetBundlePath = ab; this.LoadAssetBundle(ab, x => { MyDefaultMaterial = x.LoadAsset(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: BasicSplineRenderer where TMeshGenerator : MeshGenerator { [Content] public TMeshGenerator MyMeshGenerator; protected override IEnumerator DoSomethingDuringApplyScript() { yield return base.DoSomethingDuringApplyScript(); MyMeshGenerator = this.GetOrAddComponent(); 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; } /// /// 设置材质UV映射 /// /// Clip, UniformClip, Clamp, UniformClamp [Convention.RScript.Variable.Attr.Method] public void SetUVMode(MeshGenerator.UVMode mode) { MyUVMode = 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); } } }