Skip to content

Commit

Permalink
Fixed asset management
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jun 11, 2017
1 parent e5383b6 commit 8b94c0d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
37 changes: 21 additions & 16 deletions Assets/Swarm/CrawlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public int randomSeed {
ComputeBuffer _positionBuffer;
ComputeBuffer _tangentBuffer;
ComputeBuffer _normalBuffer;
bool _materialCloned;
MaterialPropertyBlock _props;
int _frameCount;

Expand All @@ -141,17 +142,20 @@ public int randomSeed {

public void ResetPositions()
{
// Invoke the initialization kernel.
var kernel = _compute.FindKernel("CrawlingInit");
_compute.SetInt("InstanceCount", InstanceCount);
_compute.SetInt("HistoryLength", HistoryLength);
_compute.SetFloat("RandomSeed", _randomSeed);
_compute.SetFloat("InitialSpread", _initialSpread);
_compute.SetTexture(kernel, "DFVolume", _volume.texture);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);
if (_positionBuffer != null)
{
// Invoke the initialization kernel.
var kernel = _compute.FindKernel("CrawlingInit");
_compute.SetInt("InstanceCount", InstanceCount);
_compute.SetInt("HistoryLength", HistoryLength);
_compute.SetFloat("RandomSeed", _randomSeed);
_compute.SetFloat("InitialSpread", _initialSpread);
_compute.SetTexture(kernel, "DFVolume", _volume.texture);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);
}
}

#endregion
Expand Down Expand Up @@ -192,15 +196,16 @@ void Start()
// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";
_materialCloned = true;
}

void OnDestroy()
{
_drawArgsBuffer.Release();
_positionBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
if (_drawArgsBuffer != null) _drawArgsBuffer.Release();
if (_positionBuffer != null) _positionBuffer.Release();
if (_tangentBuffer != null) _tangentBuffer.Release();
if (_normalBuffer != null) _normalBuffer.Release();
if (_materialCloned) Destroy(_material);
}

void Update()
Expand Down
37 changes: 21 additions & 16 deletions Assets/Swarm/FloatingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public int randomSeed {
ComputeBuffer _velocityBuffer;
ComputeBuffer _tangentBuffer;
ComputeBuffer _normalBuffer;
bool _materialCloned;
MaterialPropertyBlock _props;
Vector3 _noiseOffset;

Expand All @@ -196,16 +197,19 @@ Vector4 AttractorVector {

public void ResetPositions()
{
// Invoke the initialization kernel.
var kernel = _compute.FindKernel("FloatingInit");
_compute.SetInt("InstanceCount", InstanceCount);
_compute.SetInt("HistoryLength", HistoryLength);
_compute.SetVector("Attractor", AttractorVector);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "VelocityBuffer", _velocityBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);
if (_positionBuffer != null)
{
// Invoke the initialization kernel.
var kernel = _compute.FindKernel("FloatingInit");
_compute.SetInt("InstanceCount", InstanceCount);
_compute.SetInt("HistoryLength", HistoryLength);
_compute.SetVector("Attractor", AttractorVector);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "VelocityBuffer", _velocityBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);
}
}

#endregion
Expand Down Expand Up @@ -250,18 +254,19 @@ void Start()
// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";
_materialCloned = true;

_noiseOffset = Vector3.one * _randomSeed;
}

void OnDestroy()
{
_drawArgsBuffer.Release();
_positionBuffer.Release();
_velocityBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
if (_drawArgsBuffer != null) _drawArgsBuffer.Release();
if (_positionBuffer != null) _positionBuffer.Release();
if (_velocityBuffer != null) _velocityBuffer.Release();
if (_tangentBuffer != null) _tangentBuffer.Release();
if (_normalBuffer != null) _normalBuffer.Release();
if (_materialCloned) Destroy(_material);
}

void Update()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Swarm/Shader/Tube.shader
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Shader "Swarm/Tube"

CGPROGRAM

#pragma surface surf Standard vertex:vert addshadow
#pragma surface surf Standard vertex:vert addshadow nolightmap
#pragma instancing_options procedural:setup

struct Input
Expand Down
12 changes: 7 additions & 5 deletions Assets/Swarm/SwirlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public int randomSeed {
ComputeBuffer _positionBuffer;
ComputeBuffer _tangentBuffer;
ComputeBuffer _normalBuffer;
bool _materialCloned;
MaterialPropertyBlock _props;
Vector3 _noiseOffset;

Expand Down Expand Up @@ -150,17 +151,18 @@ void Start()
// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";
_materialCloned = true;

_noiseOffset = Vector3.one * _randomSeed;
}

void OnDestroy()
{
_drawArgsBuffer.Release();
_positionBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
if (_drawArgsBuffer != null) _drawArgsBuffer.Release();
if (_positionBuffer != null) _positionBuffer.Release();
if (_tangentBuffer != null) _tangentBuffer.Release();
if (_normalBuffer != null) _normalBuffer.Release();
if (_materialCloned) Destroy(_material);
}

void Update()
Expand Down

0 comments on commit 8b94c0d

Please sign in to comment.