Skip to content

Instantly share code, notes, and snippets.

@zzzz465
Created May 25, 2019 13:49
Show Gist options
  • Save zzzz465/00a80c2009e9db1e963ae9d7a3350bf9 to your computer and use it in GitHub Desktop.
Save zzzz465/00a80c2009e9db1e963ae9d7a3350bf9 to your computer and use it in GitHub Desktop.
a
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Xft
{
// Token: 0x020004D7 RID: 1239
public class XWeaponTrail : MonoBehaviour
{
// Token: 0x17000426 RID: 1062
// (get) Token: 0x060016E9 RID: 5865 RVA: 0x000527EC File Offset: 0x000509EC
public Vector3 curHeadPos
{
get
{
return (this._pointStart.position + this._pointEnd.position) / 2f;
}
}
// Token: 0x17000427 RID: 1063
// (get) Token: 0x060016EA RID: 5866 RVA: 0x00052813 File Offset: 0x00050A13
protected virtual Color color
{
get
{
return this._color;
}
}
// Token: 0x060016EB RID: 5867 RVA: 0x0005281C File Offset: 0x00050A1C
public virtual void Start()
{
this._elemPool = new XWeaponTrail.ElementPool(this._maxFrame);
this._trailWidth = (this._pointStart.position - this._pointEnd.position).magnitude;
this._trailRenderer = UnityEngine.Object.Instantiate<XWeaponTrailRenderer>(this._trailRendererPrefab, Vector3.zero, Quaternion.identity);
this._vertexPool = new VertexPool(this._trailRenderer.mesh);
this._vertexSegment = this._vertexPool.GetVertices(this._granularity * 3, (this._granularity - 1) * 12);
this.UpdateIndices();
}
// Token: 0x060016EC RID: 5868 RVA: 0x000528BE File Offset: 0x00050ABE
public virtual void OnEnable()
{
this._frameNum = 0;
}
// Token: 0x060016ED RID: 5869 RVA: 0x000528C7 File Offset: 0x00050AC7
public virtual void OnDisable()
{
if (this._trailRenderer)
{
this._trailRenderer.enabled = false;
}
}
// Token: 0x060016EE RID: 5870 RVA: 0x000528E4 File Offset: 0x00050AE4
public virtual void LateUpdate()
{
this._frameNum++;
if (this._frameNum == this._skipFirstFrames + 1)
{
if (this._trailRenderer)
{
this._trailRenderer.enabled = true;
}
this._spline.Clear();
for (int i = 0; i < this._maxFrame; i++)
{
this._spline.AddControlPoint(this.curHeadPos, this._pointStart.position - this._pointEnd.position);
}
this._snapshotList.Clear();
this._snapshotList.Add(new XWeaponTrail.Element(this._pointStart.position, this._pointEnd.position));
this._snapshotList.Add(new XWeaponTrail.Element(this._pointStart.position, this._pointEnd.position));
}
else if (this._frameNum < this._skipFirstFrames + 1)
{
return;
}
this.UpdateHeadElem();
this.RecordCurElem();
this.RefreshSpline();
this.UpdateVertex();
this._vertexPool.ManualUpdate(Time.deltaTime);
}
// Token: 0x060016EF RID: 5871 RVA: 0x00052A06 File Offset: 0x00050C06
public virtual void OnDestroy()
{
if (this._trailRenderer != null)
{
UnityEngine.Object.Destroy(this._trailRenderer.gameObject);
}
}
// Token: 0x060016F0 RID: 5872 RVA: 0x00052A28 File Offset: 0x00050C28
public virtual void OnDrawGizmosSelected()
{
if (this._pointEnd == null || this._pointStart == null)
{
return;
}
float magnitude = (this._pointStart.position - this._pointEnd.position).magnitude;
if (magnitude < Mathf.Epsilon)
{
return;
}
Gizmos.color = Color.red;
Gizmos.DrawSphere(this._pointStart.position, magnitude * 0.04f);
Gizmos.color = Color.blue;
Gizmos.DrawSphere(this._pointEnd.position, magnitude * 0.04f);
}
// Token: 0x060016F1 RID: 5873 RVA: 0x00052AC4 File Offset: 0x00050CC4
public virtual void RefreshSpline()
{
for (int i = 0; i < this._snapshotList.Count; i++)
{
this._spline.ControlPoints[i].Position = this._snapshotList[i].pos;
this._spline.ControlPoints[i].Normal = this._snapshotList[i].pointEnd - this._snapshotList[i].pointStart;
}
this._spline.RefreshSpline();
}
// Token: 0x060016F2 RID: 5874 RVA: 0x00052B58 File Offset: 0x00050D58
public virtual void UpdateVertex()
{
VertexPool pool = this._vertexSegment.Pool;
Color color = this.color;
for (int i = 0; i < this._granularity; i++)
{
int num = this._vertexSegment.VertStart + i * 3;
float num2 = (float)i / (float)this._granularity;
float tl = num2;
Vector2 zero = Vector2.zero;
Vector3 vector = this._spline.InterpolateByLen(tl);
Vector3 vector2 = this._spline.InterpolateNormalByLen(tl);
Vector3 vector3 = vector + vector2.normalized * this._trailWidth * 0.5f;
Vector3 vector4 = vector - vector2.normalized * this._trailWidth * 0.5f;
pool.vertices[num] = vector3;
pool.colors[num] = color;
zero.x = 0f;
zero.y = num2;
pool.uvs[num] = zero;
pool.vertices[num + 1] = vector;
pool.colors[num + 1] = color;
zero.x = 0.5f;
zero.y = num2;
pool.uvs[num + 1] = zero;
pool.vertices[num + 2] = vector4;
pool.colors[num + 2] = color;
zero.x = 1f;
zero.y = num2;
pool.uvs[num + 2] = zero;
}
this._vertexSegment.Pool.uvChanged = true;
this._vertexSegment.Pool.vertChanged = true;
this._vertexSegment.Pool.colorChanged = true;
}
// Token: 0x060016F3 RID: 5875 RVA: 0x00052D18 File Offset: 0x00050F18
public virtual void UpdateIndices()
{
VertexPool pool = this._vertexSegment.Pool;
for (int i = 0; i < this._granularity - 1; i++)
{
int num = this._vertexSegment.VertStart + i * 3;
int num2 = this._vertexSegment.VertStart + (i + 1) * 3;
int num3 = this._vertexSegment.IndexStart + i * 12;
pool.indices[num3] = num2;
pool.indices[num3 + 1] = num2 + 1;
pool.indices[num3 + 2] = num;
pool.indices[num3 + 3] = num2 + 1;
pool.indices[num3 + 4] = num + 1;
pool.indices[num3 + 5] = num;
pool.indices[num3 + 6] = num2 + 1;
pool.indices[num3 + 7] = num2 + 2;
pool.indices[num3 + 8] = num + 1;
pool.indices[num3 + 9] = num2 + 2;
pool.indices[num3 + 10] = num + 2;
pool.indices[num3 + 11] = num + 1;
}
pool.indiceChanged = true;
}
// Token: 0x060016F4 RID: 5876 RVA: 0x00052E28 File Offset: 0x00051028
public virtual void UpdateHeadElem()
{
this._snapshotList[0].pointStart = this._pointStart.position;
this._snapshotList[0].pointEnd = this._pointEnd.position;
}
// Token: 0x060016F5 RID: 5877 RVA: 0x00052E64 File Offset: 0x00051064
public virtual void RecordCurElem()
{
XWeaponTrail.Element element = this._elemPool.Get();
element.pointStart = this._pointStart.position;
element.pointEnd = this._pointEnd.position;
if (this._snapshotList.Count < this._maxFrame)
{
this._snapshotList.Insert(1, element);
return;
}
this._elemPool.Release(this._snapshotList[this._snapshotList.Count - 1]);
this._snapshotList.RemoveAt(this._snapshotList.Count - 1);
this._snapshotList.Insert(1, element);
}
// Token: 0x040015C8 RID: 5576
[SerializeField]
protected XWeaponTrailRenderer _trailRendererPrefab;
// Token: 0x040015C9 RID: 5577
[SerializeField]
protected Transform _pointStart;
// Token: 0x040015CA RID: 5578
[SerializeField]
protected Transform _pointEnd;
// Token: 0x040015CB RID: 5579
[SerializeField]
protected int _maxFrame = 20;
// Token: 0x040015CC RID: 5580
[SerializeField]
protected int _granularity = 60;
// Token: 0x040015CD RID: 5581
[SerializeField]
protected Color _color = Color.white;
// Token: 0x040015CE RID: 5582
[SerializeField]
protected int _skipFirstFrames = 4;
// Token: 0x040015CF RID: 5583
protected float _trailWidth;
// Token: 0x040015D0 RID: 5584
protected List<XWeaponTrail.Element> _snapshotList = new List<XWeaponTrail.Element>();
// Token: 0x040015D1 RID: 5585
protected XWeaponTrail.ElementPool _elemPool;
// Token: 0x040015D2 RID: 5586
protected Spline _spline = new Spline();
// Token: 0x040015D3 RID: 5587
protected VertexPool _vertexPool;
// Token: 0x040015D4 RID: 5588
protected VertexPool.VertexSegment _vertexSegment;
// Token: 0x040015D5 RID: 5589
protected XWeaponTrailRenderer _trailRenderer;
// Token: 0x040015D6 RID: 5590
protected int _frameNum;
// Token: 0x020004D8 RID: 1240
public class Element
{
// Token: 0x17000428 RID: 1064
// (get) Token: 0x060016F7 RID: 5879 RVA: 0x00052F48 File Offset: 0x00051148
public Vector3 pos
{
get
{
return (this.pointStart + this.pointEnd) * 0.5f;
}
}
// Token: 0x060016F8 RID: 5880 RVA: 0x00052F65 File Offset: 0x00051165
public Element(Vector3 start, Vector3 end)
{
this.pointStart = start;
this.pointEnd = end;
}
// Token: 0x060016F9 RID: 5881 RVA: 0x000026D8 File Offset: 0x000008D8
public Element()
{
}
// Token: 0x040015D7 RID: 5591
public Vector3 pointStart;
// Token: 0x040015D8 RID: 5592
public Vector3 pointEnd;
}
// Token: 0x020004D9 RID: 1241
public class ElementPool
{
// Token: 0x17000429 RID: 1065
// (get) Token: 0x060016FA RID: 5882 RVA: 0x00052F7B File Offset: 0x0005117B
// (set) Token: 0x060016FB RID: 5883 RVA: 0x00052F83 File Offset: 0x00051183
public int CountAll { get; private set; }
// Token: 0x1700042A RID: 1066
// (get) Token: 0x060016FC RID: 5884 RVA: 0x00052F8C File Offset: 0x0005118C
public int CountActive
{
get
{
return this.CountAll - this.CountInactive;
}
}
// Token: 0x1700042B RID: 1067
// (get) Token: 0x060016FD RID: 5885 RVA: 0x00052F9B File Offset: 0x0005119B
public int CountInactive
{
get
{
return this._stack.Count;
}
}
// Token: 0x060016FE RID: 5886 RVA: 0x00052FA8 File Offset: 0x000511A8
public ElementPool(int preCount)
{
for (int i = 0; i < preCount; i++)
{
XWeaponTrail.Element item = new XWeaponTrail.Element();
this._stack.Push(item);
int countAll = this.CountAll;
this.CountAll = countAll + 1;
}
}
// Token: 0x060016FF RID: 5887 RVA: 0x00052FF4 File Offset: 0x000511F4
public virtual XWeaponTrail.Element Get()
{
XWeaponTrail.Element result;
if (this._stack.Count == 0)
{
result = new XWeaponTrail.Element();
int countAll = this.CountAll;
this.CountAll = countAll + 1;
}
else
{
result = this._stack.Pop();
}
return result;
}
// Token: 0x06001700 RID: 5888 RVA: 0x00053033 File Offset: 0x00051233
public virtual void Release(XWeaponTrail.Element element)
{
if (this._stack.Count > 0 && this._stack.Peek() == element)
{
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
}
this._stack.Push(element);
}
// Token: 0x040015D9 RID: 5593
protected readonly Stack<XWeaponTrail.Element> _stack = new Stack<XWeaponTrail.Element>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment