void Update() { FogOfWarTeam fow = FogOfWarTeam.GetTeam(team); if (fow == null) { Debug.LogWarning("There is no Fog Of War team for team #" + team.ToString()); return; } bool visible = fow.GetFogValue(_transform.position) < minFogStrength * 255; if (_renderer != null) { _renderer.enabled = visible; } if (_graphic != null) { _graphic.enabled = visible; } if (_canvas != null) { _canvas.enabled = visible; } }
void UpdatePlayerMovement() { Vector2 playerpos = Vector2.MoveTowards(playerTransform.position, _targetPosition, Time.deltaTime * movementSpeed);; playerTransform.position = playerpos; if (cameraTransform != null) { cameraTransform.position = new Vector3(playerpos.x, playerpos.y, -10); } if (Input.GetKeyDown(KeyCode.Space)) { FogOfWarTeam.GetTeam(0).SetAll(255); } if (Vector2.Distance(playerpos, _targetPosition) < 0.01f) { Vector2 dir = Vector2.zero; if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { ++dir.y; } else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { ++dir.x; } else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { --dir.y; } else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { --dir.x; } if (dir.sqrMagnitude > 0.1f) { RaycastHit2D hit = Physics2D.Raycast(_targetPosition, dir, 1); if (hit.collider == null) { _targetPosition += dir; } } } playerAnimator.movement = (_targetPosition - playerpos).normalized; }
void Update() { bool isinfog = FogOfWarTeam.GetTeam(team).GetFogValue(_transform.position) < (byte)(minFogStrength * 255); if (_isInFog == isinfog) { return; } _isInFog = !_isInFog; if (_isInFog) { onFogEnter.Invoke(); } else { onFogExit.Invoke(); } }
public void Render() { FogOfWarTeam fow = FogOfWarTeam.GetTeam(team); if (fow == null) { Debug.LogWarning("No FogOfWar team found: " + team.ToString()); return; } if (fow.finalFogTexture == null) { return; } if (_ids == null) { _ids = new FoWIDs(); } #if UNITY_2019_3_OR_NEWER FogOfWarClearFog clearfog = null; camera.TryGetComponent(out clearfog); #else FogOfWarClearFog clearfog = camera.GetComponent <FogOfWarClearFog>(); #endif if (clearfog != null && clearfog.targetCamera?.targetTexture != null) { fogColorTexture = clearfog.targetCamera.targetTexture; fogTextureScreenSpace = true; } if ((camera.depthTextureMode & DepthTextureMode.Depth) == 0) { camera.depthTextureMode |= DepthTextureMode.Depth; } fow.finalFogTexture.filterMode = pointFiltering ? FilterMode.Point : FilterMode.Bilinear; SetTexture(_ids.fogTex, fow.finalFogTexture); SetVector(_ids.fogTextureSize, fow.mapResolution.ToFloat()); SetFloat(_ids.mapSize, fow.mapSize); SetVector(_ids.mapOffset, fow.mapOffset); SetColor(_ids.mainFogColor, fogColor); SetMatrix(_ids.inverseView, camera.cameraToWorldMatrix); SetMatrix(_ids.inverseProj, camera.projectionMatrix.inverse); SetFloat(_ids.outsideFogStrength, outsideFogStrength); SetVector(_ids.cameraWorldPosition, camera.transform.position); SetFloat(_ids.stereoSeparation, camera.stereoSeparation); // orthographic is treated very differently in the shader, so we have to make sure it executes the right code SetKeyword("CAMERA_PERSPECTIVE", !camera.orthographic); SetKeyword("CAMERA_ORTHOGRAPHIC", camera.orthographic); // which plane will the fog be rendered to? SetKeyword("PLANE_XY", fow.plane == FogOfWarPlane.XY); SetKeyword("PLANE_YZ", fow.plane == FogOfWarPlane.YZ); SetKeyword("PLANE_XZ", fow.plane == FogOfWarPlane.XZ); SetKeyword("FOG_COLORED", fogColorTexture == null); SetKeyword("FOG_TEXTURED_WORLD", fogColorTexture != null && !fogTextureScreenSpace); SetKeyword("FOG_TEXTURED_SCREEN", fogColorTexture != null && fogTextureScreenSpace); if (fogColorTexture != null) { SetTexture(_ids.fogColorTex, fogColorTexture); SetVector(_ids.fogColorTexScale, new Vector2(fogColorTextureScale, fogColorTextureHeight)); } SetKeyword("FOGFARPLANE", fogFarPlane); BlitToScreen(); }
void LateUpdate() { FogOfWarTeam fow = FogOfWarTeam.GetTeam(team); if (fow == null) { return; } // setup texture if (_texture == null || _texture.width != fow.mapResolution.x || _texture.height != fow.mapResolution.y) { if (_texture != null) { Destroy(_texture); } _texture = new Texture2D(fow.mapResolution.x, fow.mapResolution.y, TextureFormat.ARGB32, false, false); _texture.name = "FogOfWarMinimap"; _fogValues = new byte[fow.mapResolution.x * fow.mapResolution.y]; _pixels = new Color32[fow.mapResolution.x * fow.mapResolution.y]; GetComponent <RawImage>().texture = _texture; if (aspectRatioFitter != null) { aspectRatioFitter.aspectRatio = (float)fow.mapResolution.x / fow.mapResolution.y; } } // fog fow.GetTotalFogValues(ref _fogValues); for (int i = 0; i < _fogValues.Length; ++i) { int r = (fogColor.r - nonFogColor.r) * _fogValues[i] / 255 + nonFogColor.r; int g = (fogColor.g - nonFogColor.g) * _fogValues[i] / 255 + nonFogColor.g; int b = (fogColor.b - nonFogColor.b) * _fogValues[i] / 255 + nonFogColor.b; int a = (fogColor.a - nonFogColor.a) * _fogValues[i] / 255 + nonFogColor.a; _pixels[i] = new Color32((byte)r, (byte)g, (byte)b, (byte)a); } // units byte opponentminvisibility = (byte)(opponentMinFogStrength * 255); for (int i = 0; i < FogOfWarUnit.registeredUnits.Count; ++i) { FogOfWarUnit unit = FogOfWarUnit.registeredUnits[i]; if (unit.team == team) { DrawIconOnMap(fow, unit.transform.position, unitColor); } else { DrawIconOnMap(fow, unit.transform.position, enemyColor, opponentminvisibility); } } // camera if (camera != null) { DrawIconOnMap(fow, camera.transform.position, cameraColor); } // apply to texture _texture.SetPixels32(_pixels); _texture.Apply(false, false); }