GPU Lightning Weapons

This commit is contained in:
Reinier Vens 2021-02-05 03:21:15 +00:00
commit 8d10e33db0
26 changed files with 1163 additions and 876 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -5,12 +6,16 @@ using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Mathematics;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
using static Unity.Mathematics.math;
using static Unity.Mathematics.noise;
using Random = UnityEngine.Random;
public class LightningCompute : MonoBehaviour
{
public Camera Camera;
[Header("Bolt Properties")]
public Vector3 StartPosition;
public Vector3 EndPosition;
@ -22,7 +27,19 @@ public class LightningCompute : MonoBehaviour
public float NoiseFrequency;
public float NoiseGain;
public float NoiseLacunarity;
public Camera Camera;
public bool FixedEndpoint;
public bool Animate;
[Header("Animation Properties")]
public float LeaderWidth;
public float LeaderDuration;
public float LeaderIntensity;
public float PulseDuration;
public float PulseIntensity;
public float PulseWidth;
public AnimationCurve PulseTrunkIntensityCurve;
public AnimationCurve PulseBranchIntensityCurve;
public AnimationCurve PulseWidthCurve;
[Header("Buffer Sizes")]
public int TrunkNodeCount;
@ -36,6 +53,9 @@ public class LightningCompute : MonoBehaviour
public bool DebugDrawNodes;
public bool DebugDrawVertices;
public Action OnLeaderComplete;
public Action OnPulseComplete;
private Transform _cameraTransform;
private ComputeBuffer _nodeBuffer;
@ -48,6 +68,12 @@ public class LightningCompute : MonoBehaviour
private const int GROUP_SIZE = 64;
private int _updateKernel;
private int _createWidthKernel;
private float _animationStartTime;
private float _time;
private bool _leaderComplete;
private bool _pulseComplete;
private bool _initialized;
private Material _materialInstance;
private int NodeBufferSize => TrunkNodeCount + BranchCount * BranchNodeCount;
private int VertexBufferSize => NodeBufferSize * 2;
@ -78,14 +104,21 @@ public class LightningCompute : MonoBehaviour
void Start()
{
Initialize();
_materialInstance = Instantiate(RenderMaterial);
}
void Initialize()
{
if (_initialized) return;
_initialized = true;
if (!Camera)
Camera = Camera.main;
_cameraTransform = Camera.transform;
_updateKernel = ComputeShader.FindKernel("UpdateNodes");
_createWidthKernel = ComputeShader.FindKernel("CreateWidth");
InitBuffers();
}
void InitBuffers()
{
_nodeBuffer = new ComputeBuffer(NodeBufferSize, Marshal.SizeOf(typeof(Node)));
var nodeList = new List<Node>();
for (int i = 0; i < TrunkNodeCount; i++)
@ -113,17 +146,6 @@ public class LightningCompute : MonoBehaviour
endPos = EndPosition,
startTime = 0
};
for (int i = 0; i < BranchCount; i++)
{
_branchEndpoints[i] = Random.insideUnitSphere * BranchLength;
var parent = nodeList[(int)((float) i / BranchCount * TrunkNodeCount)];
_bolts[i+1] = new Bolt
{
startPos = parent.pos,
endPos = parent.pos + _branchEndpoints[i],
startTime = parent.time
};
}
_boltBuffer.SetData(_bolts);
@ -159,6 +181,24 @@ public class LightningCompute : MonoBehaviour
_indexBuffer = new ComputeBuffer(indexData.Length, Marshal.SizeOf(typeof(uint))); // 1 node to 2 triangles(6vertexs)
_indexBuffer.SetData(indexData);
}
public void StartAnimation()
{
Initialize();
_animationStartTime = Time.time;
_time = Random.value * 1000;
_leaderComplete = false;
_pulseComplete = false;
for (int i = 0; i < BranchCount; i++)
{
_branchEndpoints[i] = Random.insideUnitSphere * BranchLength;
_bolts[i+1] = new Bolt
{
startTime = Random.value * .8f
};
}
}
float fbm(float2 p)
{
@ -183,23 +223,51 @@ public class LightningCompute : MonoBehaviour
void Update()
{
var time = Time.time * MorphSpeed;
_time += Time.deltaTime * MorphSpeed;
if(Animate)
{
var leaderLerp = (Time.time - _animationStartTime) / LeaderDuration;
if (leaderLerp > 1 && !_leaderComplete)
{
OnLeaderComplete?.Invoke();
_leaderComplete = true;
}
var pulseLerp = (Time.time - _animationStartTime - LeaderDuration) / PulseDuration;
if (pulseLerp > 1 && !_pulseComplete)
{
OnPulseComplete?.Invoke();
_pulseComplete = true;
}
ComputeShader.SetFloat("_StartWidth", leaderLerp < 1 ? LeaderWidth : StartWidth * PulseWidthCurve.Evaluate(pulseLerp)*PulseWidth);
ComputeShader.SetFloat("_EndWidth", leaderLerp < 1 ? LeaderWidth : EndWidth * PulseWidthCurve.Evaluate(pulseLerp)*PulseWidth);
_materialInstance.SetFloat("_LeaderProgress", leaderLerp);
_materialInstance.SetFloat("_BranchIntensity", leaderLerp < 1 ? LeaderIntensity : PulseBranchIntensityCurve.Evaluate(pulseLerp) * PulseIntensity);
_materialInstance.SetFloat("_Intensity", leaderLerp < 1 ? LeaderIntensity : PulseTrunkIntensityCurve.Evaluate(pulseLerp) * PulseIntensity);
}
else
{
ComputeShader.SetFloat("_StartWidth", StartWidth);
ComputeShader.SetFloat("_EndWidth", EndWidth);
}
var diff = EndPosition - StartPosition;
var dist = diff.magnitude;
_bolts[0].startPos = StartPosition;
_bolts[0].startOffset = -(Vector3) fbm3(float2(0, time));
_bolts[0].startOffset = -(Vector3) fbm3(float2(0, _time));
_bolts[0].endPos = EndPosition;
_bolts[0].endOffset = -(Vector3) fbm3(float2(dist, time));
_bolts[0].endOffset = FixedEndpoint ? -(Vector3) fbm3(float2(dist, _time)) : Vector3.zero;
for (int i = 0; i < BranchCount; i++)
{
var root = Vector3.Lerp(StartPosition, EndPosition, _bolts[i + 1].startTime);
var rootDist = _bolts[i + 1].startTime * dist;
_bolts[i + 1].startPos =
root + (Vector3) fbm3(float2(rootDist, time)) +
root + (Vector3) fbm3(float2(rootDist, _time)) +
_bolts[0].startOffset * (max(.5f - _bolts[i + 1].startTime, 0) * 2) +
_bolts[0].endOffset * (max(_bolts[i + 1].startTime - .5f, 0) * 2);
_bolts[i + 1].startOffset = -(Vector3) fbm3(float2(0, time + (i + 1) * 10));
_bolts[i + 1].startOffset = -(Vector3) fbm3(float2(0, _time + (i + 1) * 10));
_bolts[i + 1].endPos = root + _branchEndpoints[i];
}
@ -208,7 +276,7 @@ public class LightningCompute : MonoBehaviour
ComputeShader.SetBuffer(_updateKernel, "_NodeBuffer", _nodeBuffer);
ComputeShader.SetBuffer(_updateKernel, "_BoltBuffer", _boltBuffer);
ComputeShader.SetFloat("_Time", time);
ComputeShader.SetFloat("_Time", _time);
ComputeShader.SetFloat("_NoiseAmplitude", NoiseAmplitude);
ComputeShader.SetFloat("_NoiseFrequency", NoiseFrequency);
ComputeShader.SetFloat("_NoiseGain", NoiseGain);
@ -219,8 +287,6 @@ public class LightningCompute : MonoBehaviour
ComputeShader.SetInt("_TrunkNodeCount", TrunkNodeCount);
ComputeShader.SetInt("_BranchNodeCount", BranchNodeCount);
ComputeShader.SetVector("_CameraPos", _cameraTransform.position);
ComputeShader.SetFloat("_StartWidth", StartWidth);
ComputeShader.SetFloat("_EndWidth", EndWidth);
ComputeShader.SetBuffer(_createWidthKernel, "_NodeBuffer", _nodeBuffer);
ComputeShader.SetBuffer(_createWidthKernel, "_BoltBuffer", _boltBuffer);
@ -228,10 +294,10 @@ public class LightningCompute : MonoBehaviour
ComputeShader.Dispatch(_createWidthKernel, NodeBufferSize/GROUP_SIZE, 1, 1);
RenderMaterial.SetBuffer("_IndexBuffer", _indexBuffer);
RenderMaterial.SetBuffer("_VertexBuffer", _vertexBuffer);
_materialInstance.SetBuffer("_IndexBuffer", _indexBuffer);
_materialInstance.SetBuffer("_VertexBuffer", _vertexBuffer);
Graphics.DrawProcedural(
RenderMaterial,
_materialInstance,
new Bounds((EndPosition + StartPosition) / 2, (EndPosition - StartPosition).magnitude * Vector3.one),
MeshTopology.Triangles,
IndexBufferSize,
@ -241,6 +307,9 @@ public class LightningCompute : MonoBehaviour
ShadowCastingMode.Off,
false,
0);
//
// if(pulseLerp > 2)
// StartAnimation();
}
#region cleanup