Skip to content

Commit

Permalink
Added the trim parameter and the reset button.
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jun 10, 2017
1 parent 049bc32 commit e5383b6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
41 changes: 29 additions & 12 deletions Assets/Swarm/CrawlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public float radius {
set { _radius = value; }
}

[SerializeField, Range(0, 1)] float _trim = 1;

public float trim {
get { return _trim; }
set { _trim = value; }
}

#endregion

#region Dynamics properties
Expand Down Expand Up @@ -110,7 +117,7 @@ public int randomSeed {

#endregion

#region Private fields
#region Private members

ComputeBuffer _drawArgsBuffer;
ComputeBuffer _positionBuffer;
Expand All @@ -130,6 +137,25 @@ public int randomSeed {

#endregion

#region Public Methods

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);
}

#endregion

#region MonoBehaviour functions

void OnValidate()
Expand Down Expand Up @@ -157,17 +183,7 @@ void Start()
_tangentBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);
_normalBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);

// Initialize the compute buffers.
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);
ResetPositions();

// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
Expand Down Expand Up @@ -243,6 +259,7 @@ void Update()
_material.SetInt("_IndexOffset", _frameCount + 3);
_material.SetInt("_InstanceCount", InstanceCount);
_material.SetInt("_HistoryLength", HistoryLength);
_material.SetInt("_IndexLimit", (int)(_trim * HistoryLength));

Graphics.DrawMeshInstancedIndirect(
_template.mesh, 0, _material,
Expand Down
6 changes: 6 additions & 0 deletions Assets/Swarm/Editor/CrawlingSwarmEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CrawlingSwarmEditor : Editor
SerializedProperty _instanceCount;
SerializedProperty _template;
SerializedProperty _radius;
SerializedProperty _trim;

SerializedProperty _speed;
SerializedProperty _volume;
Expand All @@ -38,6 +39,7 @@ void OnEnable()
_instanceCount = serializedObject.FindProperty("_instanceCount");
_template = serializedObject.FindProperty("_template");
_radius = serializedObject.FindProperty("_radius");
_trim = serializedObject.FindProperty("_trim");

_speed = serializedObject.FindProperty("_speed");
_volume = serializedObject.FindProperty("_volume");
Expand All @@ -60,6 +62,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_template);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_radius);
EditorGUILayout.PropertyField(_trim);
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_speed);
Expand All @@ -78,6 +81,9 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_randomSeed);

serializedObject.ApplyModifiedProperties();

if (Application.isPlaying && GUILayout.Button("Reset"))
foreach (CrawlingSwarm cs in targets) cs.ResetPositions();
}
}
}
6 changes: 6 additions & 0 deletions Assets/Swarm/Editor/FloatingSwarmEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class FloatingSwarmEditor : Editor
SerializedProperty _instanceCount;
SerializedProperty _template;
SerializedProperty _radius;
SerializedProperty _trim;

SerializedProperty _attractor;
SerializedProperty _attractorPosition;
Expand Down Expand Up @@ -53,6 +54,7 @@ void OnEnable()
_instanceCount = serializedObject.FindProperty("_instanceCount");
_template = serializedObject.FindProperty("_template");
_radius = serializedObject.FindProperty("_radius");
_trim = serializedObject.FindProperty("_trim");

_attractor = serializedObject.FindProperty("_attractor");
_attractorPosition = serializedObject.FindProperty("_attractorPosition");
Expand Down Expand Up @@ -83,6 +85,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_template);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_radius);
EditorGUILayout.PropertyField(_trim);
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_attractor);
Expand Down Expand Up @@ -110,6 +113,9 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_randomSeed);

serializedObject.ApplyModifiedProperties();

if (Application.isPlaying && GUILayout.Button("Reset"))
foreach (FloatingSwarm fs in targets) fs.ResetPositions();
}
}
}
39 changes: 28 additions & 11 deletions Assets/Swarm/FloatingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public float radius {
set { _radius = value; }
}

[SerializeField, Range(0, 1)] float _trim = 1;

public float trim {
get { return _trim; }
set { _trim = value; }
}

#endregion

#region Dynamics and attractor properteis
Expand Down Expand Up @@ -156,7 +163,7 @@ public int randomSeed {

#endregion

#region Private fields
#region Private members

ComputeBuffer _drawArgsBuffer;
ComputeBuffer _positionBuffer;
Expand Down Expand Up @@ -185,6 +192,24 @@ Vector4 AttractorVector {

#endregion

#region Public Methods

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);
}

#endregion

#region MonoBehaviour functions

void OnValidate()
Expand Down Expand Up @@ -216,16 +241,7 @@ void Start()
_tangentBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);
_normalBuffer = new ComputeBuffer(HistoryLength * InstanceCount, 16);

// 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);
ResetPositions();

// This property block is used only for avoiding an instancing bug.
_props = new MaterialPropertyBlock();
Expand Down Expand Up @@ -307,6 +323,7 @@ void Update()

_material.SetInt("_InstanceCount", InstanceCount);
_material.SetInt("_HistoryLength", HistoryLength);
_material.SetInt("_IndexLimit", (int)(_trim * HistoryLength));

Graphics.DrawMeshInstancedIndirect(
_template.mesh, 0, _material,
Expand Down
3 changes: 2 additions & 1 deletion Assets/Swarm/Shader/Tube.shader
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Shader "Swarm/Tube"
uint _InstanceCount;
uint _HistoryLength;
uint _IndexOffset;
uint _IndexLimit;

#endif

Expand All @@ -61,7 +62,7 @@ Shader "Swarm/Tube"
float phi = v.vertex.x; // Angle in slice
float cap = v.vertex.y; // -1:head, +1:tail
float seg = v.vertex.z; // Segment index
uint iseg = (uint)seg;
uint iseg = min((uint)seg, _IndexLimit);

// Parameter along the curve (used for coloring).
float param = seg / _HistoryLength;
Expand Down
1 change: 1 addition & 0 deletions Assets/Swarm/SwirlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void Update()

_material.SetInt("_InstanceCount", InstanceCount);
_material.SetInt("_HistoryLength", HistoryLength);
_material.SetInt("_IndexLimit", HistoryLength);

Graphics.DrawMeshInstancedIndirect(
_template.mesh, 0, _material,
Expand Down

0 comments on commit e5383b6

Please sign in to comment.