Skip to content

Commit

Permalink
small improvements and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jun 4, 2017
1 parent 9152ed9 commit bd71719
Show file tree
Hide file tree
Showing 14 changed files with 327 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ float3 PickSamplePoint(float2 uv, float index)
// Uniformaly distributed points on a unit sphere http://goo.gl/X2F1Ho
#if defined(FIX_SAMPLING_PATTERN)
float gn = GradientNoise(uv * _Downsample);
float u = frac(UVRandom(0.0, index) + gn) * 2.0 - 1.0;
float theta = (UVRandom(1.0, index) + gn) * UNITY_PI_2;
// FIXEME: This was added to avoid a NVIDIA driver issue.
// vvvvvvvvvvvv
float u = frac(UVRandom(0.0, index + uv.x * 1e-10) + gn) * 2.0 - 1.0;
float theta = (UVRandom(1.0, index + uv.x * 1e-10) + gn) * UNITY_PI_2;
#else
float u = UVRandom(uv.x + _Time.x, uv.y + index) * 2.0 - 1.0;
float theta = UVRandom(-uv.x - _Time.x, uv.y + index) * UNITY_PI_2;
Expand Down
104 changes: 55 additions & 49 deletions Assets/Swarm/CrawlingSwarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@

namespace Swarm
{
// Distance-field constrained swarm
public sealed class CrawlingSwarm : MonoBehaviour
{
#region Basic settings
#region Instancing properties

[SerializeField] int _instanceCount = 1000;

public int instanceCount {
get { return _instanceCount; }
}

#endregion

#region Render settings

[SerializeField] TubeTemplate _template;

public TubeTemplate template {
Expand All @@ -35,22 +30,9 @@ public float radius {
set { _radius = value; }
}

[SerializeField] Material _material;

public Material material {
get { return _material; }
}

[SerializeField] CosineGradient _gradient;

public CosineGradient gradient {
get { return _gradient; }
set { _gradient = value; }
}

#endregion

#region Dynamics settings
#region Dynamics properties

[SerializeField] float _speed = 0.75f;

Expand Down Expand Up @@ -88,6 +70,23 @@ public float noiseMotion {

#endregion

#region Material properties

[SerializeField] Material _material;

public Material material {
get { return _material; }
}

[SerializeField] CosineGradient _gradient;

public CosineGradient gradient {
get { return _gradient; }
set { _gradient = value; }
}

#endregion

#region Hidden attributes

[SerializeField, HideInInspector] ComputeShader _compute;
Expand Down Expand Up @@ -119,6 +118,10 @@ public float noiseMotion {
void OnValidate()
{
_instanceCount = Mathf.Max(kThreadCount, _instanceCount);
_radius = Mathf.Max(0, _radius);
_speed = Mathf.Max(0, _speed);
_noiseFrequency = Mathf.Max(0, _noiseFrequency);
_noiseSpread = Mathf.Max(0, _noiseSpread);
}

void Start()
Expand All @@ -140,35 +143,20 @@ void Start()

// Initialize the position buffer.
var kernel = _compute.FindKernel("CrawlingInit");
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.SetTexture(kernel, "DFVolume", _volume.texture);
_compute.SetInt("InstanceCount", InstanceCount);
_compute.SetInt("HistoryLength", HistoryLength);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);

// Initialize the update kernel.
kernel = _compute.FindKernel("CrawlingUpdate");
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetTexture(kernel, "DFVolume", _volume.texture);

// Initialize the reconstruction kernel.
kernel = _compute.FindKernel("CrawlingReconstruct");
_compute.SetBuffer(kernel, "PositionBufferRO", _positionBuffer);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);
_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);

// Initialize the mateiral.
_material.SetBuffer("_PositionBuffer", _positionBuffer);
_material.SetBuffer("_TangentBuffer", _tangentBuffer);
_material.SetBuffer("_NormalBuffer", _normalBuffer);
_material.SetInt("_InstanceCount", InstanceCount);
_material.SetInt("_HistoryLength", HistoryLength);

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

void OnDestroy()
Expand All @@ -190,34 +178,52 @@ void Update()
var offset1 = InstanceCount * ((_frameCount + 1) % HistoryLength);
var offset2 = InstanceCount * ((_frameCount + 2) % HistoryLength);

// Update the parameters for the compute kernels.
// Invoke the update compute kernel.
var kernel = _compute.FindKernel("CrawlingUpdate");

_compute.SetInt("IndexOffset0", offset0);
_compute.SetInt("IndexOffset1", offset1);
_compute.SetInt("IndexOffset2", offset2);

_compute.SetFloat("Speed", _speed * delta);
_compute.SetFloat("NoiseFrequency", _noiseFrequency);
_compute.SetFloat("NoiseSpread", _noiseSpread / InstanceCount);
_compute.SetFloat("NoiseOffset", Time.time * _noiseMotion);

// Update the position buffer.
var kernel = _compute.FindKernel("CrawlingUpdate");
_compute.SetTexture(kernel, "DFVolume", _volume.texture);
_compute.SetBuffer(kernel, "PositionBuffer", _positionBuffer);

_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);

// Reconstruct tangent/normal vectors.
// Invoke the reconstruction kernel.
kernel = _compute.FindKernel("CrawlingReconstruct");

_compute.SetBuffer(kernel, "PositionBufferRO", _positionBuffer);
_compute.SetBuffer(kernel, "TangentBuffer", _tangentBuffer);
_compute.SetBuffer(kernel, "NormalBuffer", _normalBuffer);

_compute.Dispatch(kernel, ThreadGroupCount, 1, 1);
}

// Draw the meshes with instancing.
_material.SetInt("_IndexOffset", _frameCount + 3);
// Draw the mesh with instancing.
_material.SetFloat("_Radius", _radius);

_material.SetVector("_GradientA", _gradient.coeffsA);
_material.SetVector("_GradientB", _gradient.coeffsB);
_material.SetVector("_GradientC", _gradient.coeffsC2);
_material.SetVector("_GradientD", _gradient.coeffsD2);

_material.SetMatrix("_LocalToWorld", transform.localToWorldMatrix);
_material.SetMatrix("_WorldToLocal", transform.worldToLocalMatrix);

_material.SetBuffer("_PositionBuffer", _positionBuffer);
_material.SetBuffer("_TangentBuffer", _tangentBuffer);
_material.SetBuffer("_NormalBuffer", _normalBuffer);

_material.SetInt("_IndexOffset", _frameCount + 3);
_material.SetInt("_InstanceCount", InstanceCount);
_material.SetInt("_HistoryLength", HistoryLength);

Graphics.DrawMeshInstancedIndirect(
_template.mesh, 0, _material,
new Bounds(Vector3.zero, Vector3.one * 1.5f),
Expand Down
4 changes: 2 additions & 2 deletions Assets/Swarm/CrawlingSwarm.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 24 additions & 16 deletions Assets/Swarm/Editor/CrawlingSwarmEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,62 @@ namespace Swarm
public class CrawlingSwarmEditor : Editor
{
SerializedProperty _instanceCount;

SerializedProperty _template;
SerializedProperty _radius;
SerializedProperty _material;
SerializedProperty _gradient;

SerializedProperty _volume;
SerializedProperty _speed;
SerializedProperty _noiseFrequency;
SerializedProperty _noiseSpread;
SerializedProperty _noiseMotion;

SerializedProperty _material;
SerializedProperty _gradient;

static class Labels
{
public static GUIContent frequency = new GUIContent("Frequency");
public static GUIContent spread = new GUIContent("Spread");
public static GUIContent motion = new GUIContent("Motion");
}

void OnEnable()
{
_instanceCount = serializedObject.FindProperty("_instanceCount");

_template = serializedObject.FindProperty("_template");
_radius = serializedObject.FindProperty("_radius");
_material = serializedObject.FindProperty("_material");
_gradient = serializedObject.FindProperty("_gradient");

_volume = serializedObject.FindProperty("_volume");
_speed = serializedObject.FindProperty("_speed");
_noiseFrequency = serializedObject.FindProperty("_noiseFrequency");
_noiseSpread = serializedObject.FindProperty("_noiseSpread");
_noiseMotion = serializedObject.FindProperty("_noiseMotion");

_material = serializedObject.FindProperty("_material");
_gradient = serializedObject.FindProperty("_gradient");
}

public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(_instanceCount);

EditorGUILayout.Space();

EditorGUILayout.PropertyField(_template);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_radius);
EditorGUILayout.PropertyField(_material);
EditorGUILayout.PropertyField(_gradient);

EditorGUILayout.Space();
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_speed);
EditorGUILayout.PropertyField(_volume);
EditorGUILayout.PropertyField(_noiseFrequency);
EditorGUILayout.PropertyField(_noiseSpread);
EditorGUILayout.PropertyField(_noiseMotion);
EditorGUILayout.LabelField("Noise Field");
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_noiseFrequency, Labels.frequency);
EditorGUILayout.PropertyField(_noiseSpread, Labels.spread);
EditorGUILayout.PropertyField(_noiseMotion, Labels.motion);
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_material);
EditorGUILayout.PropertyField(_gradient);

serializedObject.ApplyModifiedProperties();
}
Expand Down
3 changes: 2 additions & 1 deletion Assets/Swarm/Editor/Default Tube Material.mat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Material:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Default Tube Material
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 4800000, guid: 1b66724e3dcef3c498c92b24b8cd0fbd, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -65,6 +65,7 @@ Material:
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Smoothness: 0
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
Expand Down
42 changes: 42 additions & 0 deletions Assets/Swarm/Editor/Default Volume Data.asset

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Assets/Swarm/Editor/Default Volume Data.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 25 additions & 18 deletions Assets/Swarm/Editor/SwirlingSwarmEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,58 @@ namespace Swarm
public class SwirlingSwarmEditor : Editor
{
SerializedProperty _instanceCount;

SerializedProperty _template;
SerializedProperty _radius;
SerializedProperty _material;
SerializedProperty _gradient;
SerializedProperty _length;

SerializedProperty _spread;
SerializedProperty _length;
SerializedProperty _noiseFrequency;
SerializedProperty _noiseMotion;

SerializedProperty _material;
SerializedProperty _gradient;

static class Labels
{
public static GUIContent frequency = new GUIContent("Frequency");
public static GUIContent motion = new GUIContent("Motion");
}

void OnEnable()
{
_instanceCount = serializedObject.FindProperty("_instanceCount");

_template = serializedObject.FindProperty("_template");
_radius = serializedObject.FindProperty("_radius");
_material = serializedObject.FindProperty("_material");
_gradient = serializedObject.FindProperty("_gradient");
_length = serializedObject.FindProperty("_length");

_spread = serializedObject.FindProperty("_spread");
_length = serializedObject.FindProperty("_length");
_noiseFrequency = serializedObject.FindProperty("_noiseFrequency");
_noiseMotion = serializedObject.FindProperty("_noiseMotion");

_material = serializedObject.FindProperty("_material");
_gradient = serializedObject.FindProperty("_gradient");
}

public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(_instanceCount);

EditorGUILayout.Space();

EditorGUILayout.PropertyField(_template);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_radius);
EditorGUILayout.PropertyField(_material);
EditorGUILayout.PropertyField(_gradient);

EditorGUILayout.Space();
EditorGUILayout.PropertyField(_length);
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_spread);
EditorGUILayout.PropertyField(_length);
EditorGUILayout.PropertyField(_noiseFrequency);
EditorGUILayout.PropertyField(_noiseMotion);
EditorGUILayout.LabelField("Noise Field");
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_noiseFrequency, Labels.frequency);
EditorGUILayout.PropertyField(_noiseMotion, Labels.motion);
EditorGUI.indentLevel--;

EditorGUILayout.PropertyField(_material);
EditorGUILayout.PropertyField(_gradient);

serializedObject.ApplyModifiedProperties();
}
Expand Down
Loading

0 comments on commit bd71719

Please sign in to comment.