Skip to content

Commit

Permalink
Fixed to reset the simulation state properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Sep 9, 2016
1 parent 589c0a0 commit 7005b4e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Assets/Kvant/SprayMV/Shaders/Kernels.shader
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Shader "Hidden/Kvant/SprayMV/Kernels"
float3 _SpinParams; // spin*2, speed-to-spin*2, randomness
float2 _NoiseParams; // freq, amp
float3 _NoiseOffset;
float4 _Config; // throttle, random seed, dT, time
float4 _Config; // throttle, dT, time

// Particle generator functions
float4 NewParticlePosition(float2 uv)
{
float t = _Config.w;
float t = _Config.z;

// Random position
float3 p = float3(UVRandom(uv, t), UVRandom(uv, t + 1), UVRandom(uv, t + 2));
Expand Down Expand Up @@ -130,7 +130,7 @@ Shader "Hidden/Kvant/SprayMV/Kernels"
float3 v = tex2D(_VelocityBuffer, i.uv).xyz;

// Decaying
float dt = _Config.z;
float dt = _Config.y;
p.w -= lerp(_LifeParams.x, _LifeParams.y, UVRandom(i.uv, 12)) * dt;

if (p.w > -0.5)
Expand Down Expand Up @@ -158,7 +158,7 @@ Shader "Hidden/Kvant/SprayMV/Kernels"
v *= _Acceleration.w; // dt is pre-applied in script

// Constant acceleration
float dt = _Config.z;
float dt = _Config.y;
v += _Acceleration.xyz * dt;

// Acceleration by turbulent noise
Expand All @@ -183,7 +183,7 @@ Shader "Hidden/Kvant/SprayMV/Kernels"
float3 v = tex2D(_VelocityBuffer, i.uv).xyz;

// Delta angle
float dt = _Config.z;
float dt = _Config.y;
float theta = (_SpinParams.x + length(v) * _SpinParams.y) * dt;

// Randomness
Expand Down
46 changes: 25 additions & 21 deletions Assets/Kvant/SprayMV/SprayMV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public void RequestReconfigurationFromEditor()
RenderTexture _rotationBuffer2;

// Variables for simulation
float _time;
Vector3 _noiseOffset;

// Custom properties applied to the mesh renderer.
Expand Down Expand Up @@ -282,8 +283,6 @@ void SetUpTemporaryObjects()
_material.hideFlags = HideFlags.HideAndDontSave;
}

_material.SetFloat("_RandomSeed", _randomSeed);

if (_positionBuffer1 == null) _positionBuffer1 = CreateSimulationBuffer();
if (_positionBuffer2 == null) _positionBuffer2 = CreateSimulationBuffer();
if (_velocityBuffer1 == null) _velocityBuffer1 = CreateSimulationBuffer();
Expand All @@ -307,6 +306,9 @@ void ReleaseTemporaryObjects()
// Reset the simulation state.
void ResetSimulationState()
{
_time = 0;
_noiseOffset = Vector3.zero;

UpdateSimulationParameters(0);

Graphics.Blit(null, _positionBuffer2, _material, 0);
Expand Down Expand Up @@ -350,15 +352,15 @@ void UpdateSimulationParameters(float dt)
m.SetVector("_NoiseParams", new Vector2(_noiseFrequency, _noiseAmplitude));

// Move the noise field backward in the direction of the
// acceleration vector, or simply pull up when no acceleration.
if (_acceleration == Vector3.zero)
_noiseOffset += Vector3.up * _noiseMotion * dt;
else
_noiseOffset += _acceleration.normalized * _noiseMotion * dt;

// acceleration vector, or simply pull up if no acceleration is set.
var noiseDir = (_acceleration == Vector3.zero) ? Vector3.up : _acceleration.normalized;
_noiseOffset += noiseDir * _noiseMotion * dt;
m.SetVector("_NoiseOffset", _noiseOffset);

m.SetVector("_Config", new Vector4(_throttle, _randomSeed, dt, Time.time));
_time += dt;
m.SetVector("_Config", new Vector3(_throttle, dt, _time));

m.SetFloat("_RandomSeed", _randomSeed);
}

// Invoke the simulation kernels.
Expand Down Expand Up @@ -451,24 +453,26 @@ void LateUpdate()

if (_reconfigured)
{
// - Initialize temporary objects at the first frame.
// - Re-initialize temporary objects on configuration changes.
// Initialize temporary objects at the first frame,
// and re-initialize them on configuration changes.
ReleaseTemporaryObjects();
SetUpTemporaryObjects();

// Reset the simulation state.
ResetSimulationState();

// Editor: do warmup before the first frame.
if (!Application.isPlaying)
for (var i = 0; i < 24; i++)
InvokeSimulationKernels(1.0f / 24);

_reconfigured = false;
}

// Advance simulation time.
if (Application.isPlaying) InvokeSimulationKernels(Time.deltaTime);
if (Application.isPlaying)
{
// Advance simulation time.
InvokeSimulationKernels(Time.deltaTime);
}
else
{
// Editor: Simulate 1 second from the initial state.
ResetSimulationState();
for (var i = 0; i < 24; i++)
InvokeSimulationKernels(1.0f / 24);
}

// Update external components (mesh filter and renderer).
UpdateMeshFilter();
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/Test.unity
path: Assets/Test/Test.unity

0 comments on commit 7005b4e

Please sign in to comment.