Lightning
Created Lightning Gun effect (Uses LineRenderer, very slow) Created GPU Lightning renderer (very fast, still needs integration)
This commit is contained in:
parent
3a9ee2ccf7
commit
bb297bf84c
100 changed files with 7105 additions and 13 deletions
175
Assets/Plugins/GPUTrail/Scripts/GPUTrailBase.cs
Normal file
175
Assets/Plugins/GPUTrail/Scripts/GPUTrailBase.cs
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public abstract class GPUTrailBase : MonoBehaviour
|
||||
{
|
||||
#region TypeDefine
|
||||
|
||||
public struct Node
|
||||
{
|
||||
public Vector3 pos;
|
||||
public float time;
|
||||
public Color color;
|
||||
}
|
||||
|
||||
public struct Vertex
|
||||
{
|
||||
public Vector3 pos;
|
||||
public Vector2 uv;
|
||||
public Color color;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public ComputeShader Cs;
|
||||
public Material MaterialShared;
|
||||
public float Life = 10f;
|
||||
public float InputPerSec = 60f;
|
||||
public int InputNumMax = 5;
|
||||
public float MINNodeDistance = 0.1f;
|
||||
public float StartWidth = 1f;
|
||||
public float EndWidth = 1f;
|
||||
public bool Draw = true;
|
||||
|
||||
protected int nodeNumPerTrail;
|
||||
|
||||
protected ComputeBuffer nodeBuffer;
|
||||
protected ComputeBuffer vertexBuffer;
|
||||
protected ComputeBuffer indexBuffer;
|
||||
|
||||
protected Material material;
|
||||
|
||||
protected abstract int TrailNumMax { get; }
|
||||
public int NodeBufferSize { get { return TrailNumMax * nodeNumPerTrail; } }
|
||||
public int VertexBufferSize { get { return NodeBufferSize * 2; } }
|
||||
|
||||
public int VertexNumPerTrail { get { return nodeNumPerTrail * 2; } }
|
||||
public int IndexNumPerTrail { get { return (nodeNumPerTrail - 1) * 6; } }
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
ReleaseBuffer();
|
||||
|
||||
nodeNumPerTrail = Mathf.CeilToInt(Life * InputPerSec);
|
||||
InitBuffer();
|
||||
|
||||
material = new Material(MaterialShared);
|
||||
}
|
||||
|
||||
|
||||
protected virtual void InitBuffer()
|
||||
{
|
||||
nodeBuffer = new ComputeBuffer(NodeBufferSize, Marshal.SizeOf(typeof(Node)));
|
||||
nodeBuffer.SetData(Enumerable.Repeat(default(Node), nodeBuffer.count).ToArray());
|
||||
|
||||
vertexBuffer = new ComputeBuffer(VertexBufferSize, Marshal.SizeOf(typeof(Vertex))); // 1 node to 2 vtx(left,right)
|
||||
vertexBuffer.SetData(Enumerable.Repeat(default(Vertex), vertexBuffer.count).ToArray());
|
||||
|
||||
// 各Nodeの最後と次のNodeの最初はポリゴンを繋がないので-1
|
||||
var indexData = new int[IndexNumPerTrail];
|
||||
var iidx = 0;
|
||||
for (var iNode = 0; iNode < nodeNumPerTrail - 1; ++iNode)
|
||||
{
|
||||
var offset = +iNode * 2;
|
||||
indexData[iidx++] = 0 + offset;
|
||||
indexData[iidx++] = 1 + offset;
|
||||
indexData[iidx++] = 2 + offset;
|
||||
indexData[iidx++] = 2 + offset;
|
||||
indexData[iidx++] = 1 + offset;
|
||||
indexData[iidx++] = 3 + offset;
|
||||
}
|
||||
|
||||
indexBuffer = new ComputeBuffer(indexData.Length, Marshal.SizeOf(typeof(uint))); // 1 node to 2 triangles(6vertexs)
|
||||
indexBuffer.SetData(indexData);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected virtual void ReleaseBuffer()
|
||||
{
|
||||
new[] { nodeBuffer, vertexBuffer, indexBuffer }
|
||||
.Where(b => b != null)
|
||||
.ToList().ForEach(buffer =>
|
||||
{
|
||||
buffer.Release();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected virtual bool IsCameraOrthographic { get { return Camera.main.orthographic; } }
|
||||
protected virtual Vector3 ToOrthographicCameraDir { get { return -Camera.main.transform.forward; } }
|
||||
protected virtual Vector3 CameraPos { get { return Camera.main.transform.position; } }
|
||||
|
||||
protected void SetCommonParameterForCs()
|
||||
{
|
||||
_SetCommonParameterForCS(Cs);
|
||||
}
|
||||
protected void _SetCommonParameterForCS(ComputeShader cs)
|
||||
{
|
||||
cs.SetInt("_TrailNum", TrailNumMax);
|
||||
cs.SetInt("_NodeNumPerTrail", nodeNumPerTrail);
|
||||
|
||||
cs.SetInt("_InputNodeNum", Mathf.Min(InputNumMax, Mathf.FloorToInt(_inputNumCurrent)));
|
||||
//cs.SetInt("_LerpType", (int)_lerpType);
|
||||
cs.SetFloat("_MinNodeDistance", MINNodeDistance);
|
||||
cs.SetFloat("_Time", Time.time);
|
||||
cs.SetFloat("_Life", Life);
|
||||
|
||||
cs.SetVector("_ToCameraDir", IsCameraOrthographic ? ToOrthographicCameraDir : Vector3.zero);
|
||||
cs.SetVector("_CameraPos", CameraPos);
|
||||
cs.SetFloat("_StartWidth", StartWidth);
|
||||
cs.SetFloat("_EndWidth", EndWidth);
|
||||
}
|
||||
|
||||
float _inputNumCurrent;
|
||||
protected virtual void LateUpdate()
|
||||
{
|
||||
_inputNumCurrent = Time.deltaTime * InputPerSec + (_inputNumCurrent - Mathf.Floor(_inputNumCurrent)); // continue under dicimal
|
||||
UpdateVertex();
|
||||
}
|
||||
|
||||
protected abstract void UpdateVertex();
|
||||
|
||||
|
||||
void OnRenderObject()
|
||||
{
|
||||
if (Draw)
|
||||
{
|
||||
if ((Camera.current.cullingMask & (1 << gameObject.layer)) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OnRenderObjectInternal();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected virtual void SetMaterialParam() { }
|
||||
protected virtual void SetCommonMaterialParam()
|
||||
{
|
||||
SetMaterialParam();
|
||||
material.SetInt("_VertexNumPerTrail", VertexNumPerTrail);
|
||||
material.SetBuffer("_IndexBuffer", indexBuffer);
|
||||
material.SetBuffer("_VertexBuffer", vertexBuffer);
|
||||
}
|
||||
|
||||
protected virtual void OnRenderObjectInternal()
|
||||
{
|
||||
SetCommonMaterialParam();
|
||||
|
||||
material.DisableKeyword("GPUTRAIL_TRAIL_INDEX_ON");
|
||||
material.SetPass(0);
|
||||
|
||||
Graphics.DrawProceduralNow(MeshTopology.Triangles, indexBuffer.count, TrailNumMax);
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
ReleaseBuffer();
|
||||
|
||||
Destroy(material);
|
||||
}
|
||||
}
|
||||
14
Assets/Plugins/GPUTrail/Scripts/GPUTrailBase.cs.meta
Normal file
14
Assets/Plugins/GPUTrail/Scripts/GPUTrailBase.cs.meta
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 57deac542dd2b2a40bde9a026090d417
|
||||
timeCreated: 1485156582
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- cs: {instanceID: 0}
|
||||
- _material: {fileID: 2100000, guid: c1c458d860a40bc4f836b8625223124f, type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
26
Assets/Plugins/GPUTrail/Scripts/GPUTrailSpline.cs
Normal file
26
Assets/Plugins/GPUTrail/Scripts/GPUTrailSpline.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class GPUTrailSpline : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// start~end をつなぐ曲線上の点をtに応じて求める
|
||||
/// http://t-pot.com/program/2_3rdcurve/index.html
|
||||
/// </summary>
|
||||
/// <param name="t">0f~1f</param>
|
||||
/// <param name="prev"></param>
|
||||
/// <param name="start"></param>
|
||||
/// <param name="end"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 CatmullRom(float t, Vector3 prev, Vector3 start, Vector3 end)
|
||||
{
|
||||
var t2 = t * t;
|
||||
|
||||
return
|
||||
0.5f * (
|
||||
t2 * (prev - 2f * start + end)
|
||||
+ t * (-prev + end)
|
||||
)
|
||||
+ start;
|
||||
}
|
||||
}
|
||||
12
Assets/Plugins/GPUTrail/Scripts/GPUTrailSpline.cs.meta
Normal file
12
Assets/Plugins/GPUTrail/Scripts/GPUTrailSpline.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 45d00a08e3faffd4a953ac1e71b719b4
|
||||
timeCreated: 1484652959
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue