Skip to content

Commit

Permalink
Small changes and improvements.
Browse files Browse the repository at this point in the history
- Added Initial Radius to CrawlingSwarm.
- Changed to create a clone of a given material before using it.
  • Loading branch information
keijiro committed Jun 7, 2017
1 parent a2912ee commit 049bc32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
14 changes: 8 additions & 6 deletions Assets/Swarm/CrawlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ void Start()
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);

if (_props == null)
{
// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
_props.SetFloat("_UniqueID", Random.value);
}
// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
_props.SetFloat("_UniqueID", Random.value);

// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";
}

void OnDestroy()
Expand All @@ -183,6 +184,7 @@ void OnDestroy()
_positionBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
}

void Update()
Expand Down
6 changes: 6 additions & 0 deletions Assets/Swarm/FloatingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ void Start()
_props = new MaterialPropertyBlock();
_props.SetFloat("_UniqueID", Random.value);

// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";

_noiseOffset = Vector3.one * _randomSeed;
}

Expand All @@ -241,6 +245,7 @@ void OnDestroy()
_velocityBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
}

void Update()
Expand Down Expand Up @@ -309,6 +314,7 @@ void Update()
_drawArgsBuffer, 0, _props
);

// Move the noise field.
_noiseOffset += Vector3.one * _noiseMotion * delta;
}

Expand Down
26 changes: 13 additions & 13 deletions Assets/Swarm/SwirlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace Swarm
{
[ExecuteInEditMode]
public sealed class SwirlingSwarm : MonoBehaviour
{
#region Instancing properties
Expand Down Expand Up @@ -128,7 +127,7 @@ void OnValidate()
_noiseFrequency = Mathf.Max(0, _noiseFrequency);
}

void OnEnable()
void Start()
{
// Initialize the indirect draw args buffer.
_drawArgsBuffer = new ComputeBuffer(
Expand All @@ -144,30 +143,28 @@ void OnEnable()
_tangentBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);
_normalBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);

if (_props == null)
{
// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
_props.SetFloat("_UniqueID", Random.value);
}
// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
_props.SetFloat("_UniqueID", Random.value);

// Clone the given material before using.
_material = new Material(_material);
_material.name += " (cloned)";

_noiseOffset = Vector3.one * _randomSeed;
}

void OnDisable()
void OnDestroy()
{
_drawArgsBuffer.Release();
_positionBuffer.Release();
_tangentBuffer.Release();
_normalBuffer.Release();
Destroy(_material);
}

void Update()
{
// Move the noise field.
if (Application.isPlaying)
_noiseOffset += _noiseMotion * Time.deltaTime;

// Invoke the update compute kernel.
var kernel = _compute.FindKernel("SwirlingUpdate");

Expand Down Expand Up @@ -215,6 +212,9 @@ void Update()
new Bounds(transform.position, transform.lossyScale * 5),
_drawArgsBuffer, 0, _props
);

// Move the noise field.
_noiseOffset += _noiseMotion * Time.deltaTime;
}

#endregion
Expand Down

0 comments on commit 049bc32

Please sign in to comment.