void CompileFinalTexture(ref float timesincelastupdate, bool checkstopwatch) { if (_currentUnitProcessing >= FogOfWarUnit.registeredUnits.Count && (!checkstopwatch || !multithreaded || _threadPool.hasAllFinished)) { _drawer.GetValues(_fogValuesCopy); _currentUnitProcessing = 0; // prepare texture if (fogTexture == null) { fogTexture = new Texture2D(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); fogTexture.wrapMode = TextureWrapMode.Clamp; fogTexture.filterMode = filterMode; } else if (fogTexture.width != mapResolution.x || fogTexture.height != mapResolution.y) { fogTexture.Resize(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); } else { fogTexture.filterMode = filterMode; } fogTexture.LoadRawTextureData(_fogValuesCopy); fogTexture.Apply(); // apply blur _finalFogTexture = _blur.Apply(fogTexture, mapResolution, blurAmount, blurIterations, blurType); // fade in fog _fadeAmount += fadeSpeed * timesincelastupdate; byte fadebytes = (byte)(_fadeAmount * 255); if (fadebytes > 0) { _drawer.Fade((byte)(partialFogAmount * 255), fadebytes); _fadeAmount -= fadebytes / 255.0f; } timesincelastupdate = 0; if (!_isFirstProcessingFrame) { ProcessUnits(checkstopwatch); } _isFirstProcessingFrame = true; } else { _isFirstProcessingFrame = false; } }
void CompileFinalTexture(ref float timesincelastupdate, bool checkstopwatch) { // don't compile until all units have been processed if (_currentUnitProcessing < FogOfWarUnit.registeredUnits.Count || (checkstopwatch && multithreaded && !_threadPool.hasAllFinished)) { return; } onRenderFogTexture.Invoke(); // get the fog values from the drawer // get current values from units (if updateUnits is false, this will retain what it have since the last time updateUnits was true) _drawer.GetValues(_fogValuesCurrent); // fade in fog if (fadeDuration > 0.0001f) { _fadeAmount += timesincelastupdate / fadeDuration; } if (_fadeAmount > 1) { _fadeAmount = 1; } byte fadebytes = (byte)(_fadeAmount * 255); _drawer.Fade(_fogValuesCurrent, _fogValuesTotal, partialFogAmount, fadebytes); _fadeAmount -= fadebytes / 255.0f; if (updateUnits) { _drawer.Clear(255); } // prepare texture if (fogTexture == null) { fogTexture = new Texture2D(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); fogTexture.wrapMode = TextureWrapMode.Clamp; fogTexture.filterMode = filterMode; } else if (fogTexture.width != mapResolution.x || fogTexture.height != mapResolution.y) { fogTexture.Resize(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); } else { fogTexture.filterMode = filterMode; } fogTexture.LoadRawTextureData(_fogValuesTotal); fogTexture.Apply(); // apply blur _finalFogTexture = _blur.Apply(fogTexture, mapResolution, blurAmount, blurIterations, blurType); if (updateUnits) { _currentUnitProcessing = 0; } timesincelastupdate = 0; _isPerformingManualUpdate = false; // manual update has finished }
void Update() { // prepare threads if (multithreaded) { if (_threadPool == null) { _threadPool = new FogOfWarThreadPool(); } threads = Mathf.Clamp(threads, 2, 8); _threadPool.maxThreads = threads; _threadPool.Clean(); } else if (_threadPool != null) { _threadPool.StopAllThreads(); _threadPool = null; } _stopwatch.Reset(); _stopwatch.Start(); // draw shapes ProcessUnits(); // compile final texture _timeSinceLastUpdate += Time.deltaTime; if (_currentUnitProcessing >= FogOfWarUnit.registeredUnits.Count && (!multithreaded || _threadPool.hasAllFinished)) { _drawer.GetValues(_fogValuesCopy); _currentUnitProcessing = 0; // prepare texture if (fogTexture == null) { fogTexture = new Texture2D(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); fogTexture.wrapMode = TextureWrapMode.Clamp; fogTexture.filterMode = filterMode; } else if (fogTexture.width != mapResolution.x || fogTexture.height != mapResolution.y) { fogTexture.Resize(mapResolution.x, mapResolution.y, TextureFormat.Alpha8, false); } else { fogTexture.filterMode = filterMode; } fogTexture.LoadRawTextureData(_fogValuesCopy); fogTexture.Apply(); // apply blur _finalFogTexture = _blur.Apply(fogTexture, mapResolution, blurAmount, blurIterations, blurType); // fade in fog _fadeAmount += fadeSpeed * _timeSinceLastUpdate; byte fadebytes = (byte)(_fadeAmount * 255); if (fadebytes > 0) { _drawer.Fade((byte)(partialFogAmount * 255), fadebytes); _fadeAmount -= fadebytes / 255.0f; } _timeSinceLastUpdate = 0; if (!_isFirstProcessingFrame) { ProcessUnits(); } _isFirstProcessingFrame = true; } else { _isFirstProcessingFrame = false; } _stopwatch.Stop(); }