/// <summary> /// Updates the fog chunk states. The chunk will update its buffer and texture once at the start. /// It will then be in the idle state and check for revealers within its bounds moving. /// If a revealer within its bounds changes position, it will then update the buffer and texture. /// This avoids needless buffer and texture updating when nothing in the fog of war chunk has changed. /// </summary> protected void Update() { if (!chunkActive) { return; } if (clampBlendFactor) { if (textureBlendTime > 0f) { blendFactor = Mathf.Clamp01(blendFactor + Time.deltaTime / textureBlendTime); } else { blendFactor = 1f; } } //will it blend? if (fogState == MangoFogState.Blending) { float time = Time.time; if (nextUpdate < time) { nextUpdate = time + updateFrequency; fogState = MangoFogState.NeedUpdate; } } //the buffer was updated, update the texture else if (fogState != MangoFogState.NeedUpdate) { UpdateTexture(); } }
/// <summary> /// Update the specified texture with the new color buffer. /// </summary> protected void UpdateTexture() { if (!chunkActive) { return; } if (fogTexture == null) { // Native ARGB format is the fastest as it involves no data conversion fogTexture = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false); fogTexture.wrapMode = TextureWrapMode.Clamp; fogTexture.SetPixels32(buffer0); fogTexture.filterMode = MangoFogInstance.Instance.fogFilterMode; fogTexture.Apply(); fogState = MangoFogState.Blending; } else if (fogState == MangoFogState.UpdateTexture) { fogTexture.SetPixels32(buffer0); fogTexture.Apply(); blendFactor = 0f; fogState = MangoFogState.Blending; } //let it blend until the chunk needs to be updated again //changeStates[chunkID] = 0; }
public void StartChunk() { //buffer update at the start changeStates[chunkID] = 1; fogState = MangoFogState.Blending; //enables the system to start updating chunkActive = true; // creates a new thread for this chunk fogThread = new Thread(() => UpdateThread(chunkID)); doThread = true; fogThread.Start(); }
/// <summary> /// If it's time to update, do so now. /// </summary> protected void UpdateThread(int chunkID) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); int cid = chunkID; while (doThread) { //make sure the buffer needs updating and revealer changes were made if (fogState == MangoFogState.NeedUpdate) { sw.Reset(); sw.Start(); UpdateBuffer(); sw.Stop(); elapsed = 0.001f * (float)sw.ElapsedMilliseconds; //buffer was updated, update the texture fogState = MangoFogState.UpdateTexture; } Thread.Sleep(1); } #if UNITY_EDITOR Debug.Log("The thread has exited."); #endif }