From 8b94c0d1ac89d33eb7d21590fc633baf32f380bd Mon Sep 17 00:00:00 2001 From: Keijiro Takahashi Date: Sun, 11 Jun 2017 18:05:12 +0900 Subject: [PATCH] Fixed asset management --- Assets/Swarm/CrawlingSwarm.cs | 37 +++++++++++++++++++-------------- Assets/Swarm/FloatingSwarm.cs | 37 +++++++++++++++++++-------------- Assets/Swarm/Shader/Tube.shader | 2 +- Assets/Swarm/SwirlingSwarm.cs | 12 ++++++----- 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/Assets/Swarm/CrawlingSwarm.cs b/Assets/Swarm/CrawlingSwarm.cs index 62bf91b..fc8fab8 100644 --- a/Assets/Swarm/CrawlingSwarm.cs +++ b/Assets/Swarm/CrawlingSwarm.cs @@ -123,6 +123,7 @@ public int randomSeed { ComputeBuffer _positionBuffer; ComputeBuffer _tangentBuffer; ComputeBuffer _normalBuffer; + bool _materialCloned; MaterialPropertyBlock _props; int _frameCount; @@ -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 @@ -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() diff --git a/Assets/Swarm/FloatingSwarm.cs b/Assets/Swarm/FloatingSwarm.cs index a95de7b..0915917 100644 --- a/Assets/Swarm/FloatingSwarm.cs +++ b/Assets/Swarm/FloatingSwarm.cs @@ -170,6 +170,7 @@ public int randomSeed { ComputeBuffer _velocityBuffer; ComputeBuffer _tangentBuffer; ComputeBuffer _normalBuffer; + bool _materialCloned; MaterialPropertyBlock _props; Vector3 _noiseOffset; @@ -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 @@ -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() diff --git a/Assets/Swarm/Shader/Tube.shader b/Assets/Swarm/Shader/Tube.shader index 5dc9942..5db11bf 100644 --- a/Assets/Swarm/Shader/Tube.shader +++ b/Assets/Swarm/Shader/Tube.shader @@ -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 diff --git a/Assets/Swarm/SwirlingSwarm.cs b/Assets/Swarm/SwirlingSwarm.cs index 0869651..6f581f1 100644 --- a/Assets/Swarm/SwirlingSwarm.cs +++ b/Assets/Swarm/SwirlingSwarm.cs @@ -102,6 +102,7 @@ public int randomSeed { ComputeBuffer _positionBuffer; ComputeBuffer _tangentBuffer; ComputeBuffer _normalBuffer; + bool _materialCloned; MaterialPropertyBlock _props; Vector3 _noiseOffset; @@ -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()