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
|
|
@ -0,0 +1,37 @@
|
|||
using UnityEngine;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public class GPUTrailIndirectCullingSample : GPUTrailIndirectCulling
|
||||
{
|
||||
public GPUTrailIndirectSampleParticle _particle;
|
||||
|
||||
protected override int TrailNumMax
|
||||
{
|
||||
get
|
||||
{
|
||||
return _particle._particleNum;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
_particle.Init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected override bool UpdateInputBuffer()
|
||||
{
|
||||
_particle.UpdateInputBuffer(_inputBuffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void ReleaseBuffer()
|
||||
{
|
||||
base.ReleaseBuffer();
|
||||
|
||||
_particle.ReleaseBuffer();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 976dc38227ec0784fb630ff2d0b49f26
|
||||
timeCreated: 1485156615
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- cs: {fileID: 7200000, guid: 5e0b0e8f0f658774f9dca7b0695eacd4, type: 3}
|
||||
- _material: {fileID: 2100000, guid: c1c458d860a40bc4f836b8625223124f, type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
using UnityEngine;
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
|
||||
public class GPUTrailIndirectSample : GPUTrailIndirect
|
||||
{
|
||||
public GPUTrailIndirectSampleParticle _particle;
|
||||
|
||||
protected override int TrailNumMax
|
||||
{
|
||||
get
|
||||
{
|
||||
return _particle._particleNum;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
_particle.Init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected override bool UpdateInputBuffer()
|
||||
{
|
||||
_particle.UpdateInputBuffer(_inputBuffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void ReleaseBuffer()
|
||||
{
|
||||
base.ReleaseBuffer();
|
||||
|
||||
_particle.ReleaseBuffer();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 665b0a84258e7534787071e45a68c201
|
||||
timeCreated: 1485429859
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- _cs: {fileID: 7200000, guid: 5e0b0e8f0f658774f9dca7b0695eacd4, type: 3}
|
||||
- _material: {fileID: 2100000, guid: c1c458d860a40bc4f836b8625223124f, type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
|
||||
using UnityEngine;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class GPUTrailIndirectSampleParticle
|
||||
{
|
||||
struct Particle
|
||||
{
|
||||
public Vector3 velocity;
|
||||
public Vector3 pos;
|
||||
}
|
||||
|
||||
ComputeBuffer _particleBuffer;
|
||||
|
||||
|
||||
public ComputeShader _particleCS;
|
||||
public int _particleNum = 1000;
|
||||
public float _startSpeed = 1f;
|
||||
public float _forceRate = 0.01f;
|
||||
public float _damping = 0.99f;
|
||||
public float _gravity = 0.01f;
|
||||
public Vector3 _bounds;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
_particleBuffer = new ComputeBuffer(_particleNum, Marshal.SizeOf(typeof(Particle)));
|
||||
|
||||
var data = new Particle[_particleBuffer.count];
|
||||
for (var i = 0; i < data.Length; ++i)
|
||||
{
|
||||
data[i] = new Particle
|
||||
{
|
||||
pos = -Vector3.up * _bounds.y * 0.5f,
|
||||
velocity = Vector3.Scale(Random.insideUnitSphere, Vector3.up * _startSpeed)
|
||||
};
|
||||
}
|
||||
|
||||
_particleBuffer.SetData(data);
|
||||
}
|
||||
|
||||
const int NUM_THREAD_X = 8;
|
||||
public void UpdateInputBuffer(ComputeBuffer inputBuffer)
|
||||
{
|
||||
_particleCS.SetFloat("_Time", Time.time);
|
||||
_particleCS.SetFloat("_ForceRate", _forceRate);
|
||||
_particleCS.SetFloat("_Damping", _damping);
|
||||
_particleCS.SetFloat("_Gravity", _gravity);
|
||||
_particleCS.SetFloats("_Bounds", _bounds.x, _bounds.y, _bounds.z);
|
||||
|
||||
|
||||
var kernel = _particleCS.FindKernel("CSMain");
|
||||
_particleCS.SetBuffer(kernel, "_ParticleBuffer", _particleBuffer);
|
||||
_particleCS.SetBuffer(kernel, "_InputBuffer", inputBuffer);
|
||||
|
||||
_particleCS.Dispatch(kernel, Mathf.CeilToInt((float)_particleBuffer.count / NUM_THREAD_X), 1, 1);
|
||||
}
|
||||
|
||||
public void ReleaseBuffer()
|
||||
{
|
||||
if (_particleBuffer != null) _particleBuffer.Release();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a3c656aeaa021ad47b3436f65d3d8ad8
|
||||
timeCreated: 1485156615
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- cs: {fileID: 7200000, guid: 5e0b0e8f0f658774f9dca7b0695eacd4, type: 3}
|
||||
- _material: {fileID: 2100000, guid: c1c458d860a40bc4f836b8625223124f, type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
#pragma kernel CSMain
|
||||
|
||||
#define NUM_THREAD_X 16
|
||||
|
||||
struct Particle
|
||||
{
|
||||
float3 velocity;
|
||||
float3 pos;
|
||||
};
|
||||
|
||||
float _Time;
|
||||
float _ForceRate;
|
||||
float _Damping;
|
||||
float _Gravity;
|
||||
float3 _Bounds;
|
||||
RWStructuredBuffer<Particle> _ParticleBuffer;
|
||||
RWStructuredBuffer<float3> _InputBuffer;
|
||||
|
||||
float rand(float2 co){
|
||||
return frac(sin(dot(co.xy ,float2(12.9898,78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
[numthreads(NUM_THREAD_X,1,1)]
|
||||
void CSMain (uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
uint idx = id.x;
|
||||
|
||||
Particle particle = _ParticleBuffer[idx];
|
||||
float3 force = float3 (
|
||||
rand(float2(idx, _Time )) - 0.5,
|
||||
rand(float2(idx, _Time*2)) - 0.5,
|
||||
rand(float2(idx, _Time*3)) - 0.5
|
||||
);
|
||||
|
||||
float3 velocity = particle.velocity * _Damping + force * _ForceRate;
|
||||
|
||||
velocity.y -= _Gravity;
|
||||
|
||||
float3 pos = particle.pos + velocity;
|
||||
|
||||
float3 overPlus = pos - _Bounds;
|
||||
if ( overPlus.x > 0) { pos.x = _Bounds.x; velocity.x *= -0.95;}
|
||||
if ( overPlus.y > 0) { pos.y = _Bounds.y; velocity.y *= -0.95;}
|
||||
if ( overPlus.z > 0) { pos.z = _Bounds.z; velocity.z *= -0.95;}
|
||||
|
||||
float3 overMinus = pos + _Bounds;
|
||||
if ( overMinus.x < 0){ pos.x = -_Bounds.x; velocity.x *= -0.95;}
|
||||
if ( overMinus.y < 0){ pos.y = -_Bounds.y; velocity.y *= -0.95;}
|
||||
if ( overMinus.z < 0){ pos.z = -_Bounds.z; velocity.z *= -0.95;}
|
||||
|
||||
particle.pos = pos;
|
||||
particle.velocity = velocity;
|
||||
_ParticleBuffer[idx] = particle;
|
||||
|
||||
_InputBuffer[idx] = particle.pos;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 16daaa733f6a1194394beb0d2f0d6fb9
|
||||
timeCreated: 1484821669
|
||||
licenseType: Pro
|
||||
ComputeShaderImporter:
|
||||
currentAPIMask: 4
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue