125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Artngame.TEM
|
|
{
|
|
public class Lightning : MonoBehaviour
|
|
{
|
|
public GameObject lineRenderer;
|
|
|
|
private List<Segment> lightningSegments = new List<Segment>();
|
|
|
|
private List<GameObject> lineRenderers = new List<GameObject>();
|
|
|
|
private Vector3 startPos;
|
|
|
|
private Vector3 endPos;
|
|
|
|
public int generations = 4;
|
|
|
|
public float maximumOffset = 60f;
|
|
|
|
public float splitProbability = 0.8f;
|
|
|
|
public float width = 0.8f;
|
|
|
|
public float smallWidth = 0.3f;
|
|
|
|
public float maxLightingHeight = 100f;
|
|
|
|
public float maxLightingLength = 100f;
|
|
|
|
public bool useMouseLeftEffect;
|
|
|
|
private void Start()
|
|
{
|
|
startPos = base.transform.position + new Vector3(0f, Random.Range(0f, maxLightingHeight), 0f);
|
|
endPos = startPos - base.transform.up * (230f + Random.Range(0f, maxLightingLength));
|
|
GenerateLightning(startPos, endPos, generations, maximumOffset, splitProbability);
|
|
Object.Destroy(base.gameObject, 0.5f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetMouseButton(0) && useMouseLeftEffect)
|
|
{
|
|
for (int i = 0; i < lineRenderers.Count; i++)
|
|
{
|
|
Object.Destroy(lineRenderers[i]);
|
|
}
|
|
GenerateLightning(startPos, endPos, generations, maximumOffset, splitProbability);
|
|
}
|
|
}
|
|
|
|
private void GenerateLightning(Vector3 startPos, Vector3 endPos, int generations, float offsetAmount, float splitProb)
|
|
{
|
|
lightningSegments.Clear();
|
|
List<Segment> list = new List<Segment>();
|
|
lightningSegments.Add(new Segment(startPos, endPos, isBranch: false));
|
|
Vector3 rhs = Camera.main.transform.position - (startPos + endPos) / 2f;
|
|
for (int i = 0; i < generations; i++)
|
|
{
|
|
for (int j = 0; j < lightningSegments.Count; j++)
|
|
{
|
|
Vector3 start = lightningSegments[j].start;
|
|
Vector3 end = lightningSegments[j].end;
|
|
Vector3 vector = (start + end) / 2f;
|
|
Vector3 normalized = Vector3.Cross(vector - start, rhs).normalized;
|
|
vector += normalized * Random.Range(0f - offsetAmount, offsetAmount);
|
|
list.Add(new Segment(start, vector, lightningSegments[j].isBranch));
|
|
list.Add(new Segment(vector, end, lightningSegments[j].isBranch));
|
|
if (Random.Range(0f, 1f) < splitProb)
|
|
{
|
|
float num = Random.Range(-30f, 30f);
|
|
num = ((!(num < 0f)) ? (num + 20f) : (num - 20f));
|
|
Vector3 vector2 = RotatePointAroundPivot(end, vector, new Vector3(num, 0f, 0f));
|
|
Vector3 normalized2 = (vector2 - vector).normalized;
|
|
vector2 = vector + normalized2 * (vector2 - vector).magnitude * 0.6f;
|
|
list.Add(new Segment(vector, vector2, isBranch: true));
|
|
}
|
|
}
|
|
offsetAmount /= 2f;
|
|
lightningSegments.Clear();
|
|
for (int k = 0; k < list.Count; k++)
|
|
{
|
|
lightningSegments.Add(list[k]);
|
|
}
|
|
list.Clear();
|
|
}
|
|
AddToLineRenderer();
|
|
}
|
|
|
|
private Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
|
|
{
|
|
Vector3 vector = point - pivot;
|
|
vector = Quaternion.Euler(angles) * vector;
|
|
point = vector + pivot;
|
|
return point;
|
|
}
|
|
|
|
private void AddToLineRenderer()
|
|
{
|
|
lineRenderers.Clear();
|
|
for (int i = 0; i < lightningSegments.Count; i++)
|
|
{
|
|
GameObject gameObject = Object.Instantiate(lineRenderer);
|
|
gameObject.transform.parent = base.transform;
|
|
lineRenderers.Add(gameObject);
|
|
LineRenderer component = gameObject.GetComponent<LineRenderer>();
|
|
component.SetPosition(0, lightningSegments[i].start);
|
|
component.SetPosition(1, lightningSegments[i].end);
|
|
if (lightningSegments[i].isBranch)
|
|
{
|
|
component.startWidth = smallWidth;
|
|
component.endWidth = smallWidth;
|
|
}
|
|
else
|
|
{
|
|
component.startWidth = width;
|
|
component.endWidth = width;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|