Exemplo n.º 1
0
        private void Start()
        {
            if (GameData.Instance.testServer || GameData.IsHeadlessServer)
            {
                Debug.Log("Turn off lightsystem as this is a server");
                this.enabled = false;
            }
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Shader.SetGlobalTexture("_ObstacleTex", Texture2D.whiteTexture);
                return;
            }
#endif

            if (LightCamera == null)
            {
                Debug.LogError(
                    "Lighting Camera in LightingSystem is null. Please, select Lighting Camera camera for lighting to work.");
                enabled = false;
                return;
            }
            if (LightOverlayMaterial == null)
            {
                Debug.LogError(
                    "LightOverlayMaterial in LightingSystem is null. Please, select LightOverlayMaterial camera for lighting to work.");
                enabled = false;
                return;
            }
            if (AffectOnlyThisCamera && _camera.targetTexture != null)
            {
                Debug.LogError("\"Affect Only This Camera\" will not work if camera.targetTexture is set.");
                AffectOnlyThisCamera = false;
            }

            _camera = GetComponent <Camera>();

            if (EnableNormalMapping && !_camera.orthographic)
            {
                Debug.LogError("Normal mapping is not supported with perspective camera.");
                EnableNormalMapping = false;
            }

            // if both FlareLayer component and AffectOnlyThisCamera setting is enabled
            // Unity will print an error "Flare renderer to update not found"
            var flare = GetComponent <FlareLayer>();
            if (flare != null && flare.enabled)
            {
                Debug.Log("Disabling FlareLayer since AffectOnlyThisCamera setting is checked.");
                flare.enabled = false;
            }

            if (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf))
            {
                HDR = false;
            }
            _texFormat = HDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            var lightPixelsPerUnityMeter = LightPixelsPerUnityMeter;

            _halfTexelOffest = SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 9");

            InitTK2D();

            if (_camera.orthographic)
            {
                var rawCamHeight = (_camera.orthographicSize + LightCameraSizeAdd) * 2f;
                var rawCamWidth  = (_camera.orthographicSize * _camera.aspect + LightCameraSizeAdd) * 2f;

                _extendedLightTextureSize = new Point2(
                    Mathf.RoundToInt(rawCamWidth * lightPixelsPerUnityMeter),
                    Mathf.RoundToInt(rawCamHeight * lightPixelsPerUnityMeter));

                var rawSmallCamHeight = _camera.orthographicSize * 2f * lightPixelsPerUnityMeter;
                _smallLightTextureSize = new Point2(
                    Mathf.RoundToInt(rawSmallCamHeight * _camera.aspect),
                    Mathf.RoundToInt(rawSmallCamHeight));
            }
            else
            {
                {
                    var lightCamHalfFov = (_camera.fieldOfView + LightCameraFovAdd) * Mathf.Deg2Rad / 2f;
                    var lightCamSize    = Mathf.Tan(lightCamHalfFov) * LightObstaclesDistance * 2;
                    //var gameCamHalfFov = _camera.fieldOfView*Mathf.Deg2Rad/2f;
                    var texHeight = Mathf.RoundToInt(lightCamSize / LightPixelSize);
                    var texWidth  = texHeight * _camera.aspect;
                    _extendedLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
                }
                {
                    var lightCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                    var lightCamSize    = Mathf.Tan(lightCamHalfFov) * LightObstaclesDistance * 2;
                    //LightCamera.orthographicSize = lightCamSize/2f;

                    var gameCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                    var gameCamSize    = Mathf.Tan(gameCamHalfFov) * LightObstaclesDistance * 2;
                    _camera.orthographicSize = gameCamSize / 2f;

                    var texHeight = Mathf.RoundToInt(lightCamSize / LightPixelSize);
                    var texWidth  = texHeight * _camera.aspect;
                    _smallLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
                }
            }

            if (_extendedLightTextureSize.x % 2 != 0)
            {
                _extendedLightTextureSize.x++;
            }
            if (_extendedLightTextureSize.y % 2 != 0)
            {
                _extendedLightTextureSize.y++;
            }

            if (_extendedLightTextureSize.x > 1024 || _extendedLightTextureSize.y > 1024 ||
                _smallLightTextureSize.x > 1024 || _smallLightTextureSize.y > 1024)
            {
                Debug.LogError("LightPixelSize is too small. That might have a performance impact.");
                return;
            }

            if (_extendedLightTextureSize.x < 4 || _extendedLightTextureSize.y < 4 ||
                _smallLightTextureSize.x < 4 || _smallLightTextureSize.y < 4)
            {
                Debug.LogError("LightPixelSize is too big. Lighting may not work correctly.");
                return;
            }

            _screenBlitTempTex            = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight, 0, _texFormat);
            _screenBlitTempTex.filterMode = FilterMode.Point;

            LightCamera.orthographic = _camera.orthographic;

            if (EnableNormalMapping)
            {
                _lightSourcesTexture = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight,
                                                         0, _texFormat);
                _lightSourcesTexture.filterMode = FilterMode.Point;
            }
            else
            {
                _lightSourcesTexture = new RenderTexture(_smallLightTextureSize.x, _smallLightTextureSize.y,
                                                         0, _texFormat);
                _lightSourcesTexture.filterMode = LightTexturesFilterMode;
            }

            _obstaclesTexture = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y,
                                                  0, _texFormat);
            _ambientTexture = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y,
                                                0, _texFormat);

            _ambientTexture.filterMode = LightTexturesFilterMode;

            var upsampledObstacleSize = _extendedLightTextureSize * (LightObstaclesAntialiasing ? 2 : 1);
            _obstaclesUpsampledTexture = new RenderTexture(
                upsampledObstacleSize.x, upsampledObstacleSize.y, 0, _texFormat);

            if (AffectOnlyThisCamera)
            {
                _renderTargetTexture            = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight, -2, RenderTextureFormat.ARGB32);
                _renderTargetTexture.filterMode = FilterMode.Point;
                _camera.targetTexture           = _renderTargetTexture;
                _camera.clearFlags      = CameraClearFlags.SolidColor;
                _camera.backgroundColor = Color.clear;
            }

            _alphaBlendedMaterial = new Material(Shader.Find("Light2D/Internal/Alpha Blended"));

            if (XZPlane)
            {
                Shader.EnableKeyword("LIGHT2D_XZ_PLANE");
            }
            else
            {
                Shader.DisableKeyword("LIGHT2D_XZ_PLANE");
            }

            _obstaclesPostProcessor = new ObstacleCameraPostPorcessor();

            LoopAmbientLight(100);
        }
Exemplo n.º 2
0
        private void Start()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Shader.SetGlobalTexture("_ObstacleTex", Texture2D.whiteTexture);

                return;
            }
#endif

            #region Setting Validation

            if (lightCamera == null)
            {
                Debug.LogError("Lighting Camera in LightingSystem is null. Please, select Lighting Camera camera for lighting to work.");
                enabled = false;

                return;
            }

            if (lightOverlayMaterial == null)
            {
                Debug.LogError("LightOverlayMaterial in LightingSystem is null. Please, select LightOverlayMaterial camera for lighting to work.");
                enabled = false;

                return;
            }

            if (affectOnlyThisCamera && _camera.targetTexture != null)
            {
                Debug.LogError("\"Affect Only This Camera\" will not work if camera.targetTexture is set.");
                affectOnlyThisCamera = false;
            }

            _camera = GetComponent <Camera>();

            if (enableNormalMapping && !_camera.orthographic)
            {
                Debug.LogError("Normal mapping is not supported with perspective camera.");
                enableNormalMapping = false;
            }

            // if both FlareLayer component and AffectOnlyThisCamera setting is enabled
            // Unity will print an error "Flare renderer to update not found"
            FlareLayer flare = GetComponent <FlareLayer>();

            if (flare != null && flare.enabled)
            {
                Debug.Log("Disabling FlareLayer since AffectOnlyThisCamera setting is checked.");
                flare.enabled = false;
            }

            #endregion

            hdr        = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf);
            _texFormat = hdr ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            float lightPixelsPerUnityMeter = LightPixelsPerUnityMeter;

            _halfTexelOffest = SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 9");

            InitTk2D();

            if (_camera.orthographic)
            {
                float rawCamHeight = (_camera.orthographicSize + lightCameraSizeAdd) * 2f;
                float rawCamWidth  = (_camera.orthographicSize * _camera.aspect + lightCameraSizeAdd) * 2f;

                _extendedLightTextureSize = new Point2(Mathf.RoundToInt(rawCamWidth * lightPixelsPerUnityMeter), Mathf.RoundToInt(rawCamHeight * lightPixelsPerUnityMeter));

                float rawSmallCamHeight = _camera.orthographicSize * 2f * lightPixelsPerUnityMeter;
                _smallLightTextureSize = new Point2(Mathf.RoundToInt(rawSmallCamHeight * _camera.aspect), Mathf.RoundToInt(rawSmallCamHeight));
            }
            else
            {
                float lightCamHalfFov = (_camera.fieldOfView + lightCameraFovAdd) * Mathf.Deg2Rad / 2f;
                float lightCamSize    = Mathf.Tan(lightCamHalfFov) * lightObstaclesDistance * 2;

                float texHeight = Mathf.Round(lightCamSize / lightPixelSize);
                float texWidth  = texHeight * _camera.aspect;

                _extendedLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));

                //Now without a second scope for this segment
                lightCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                lightCamSize    = Mathf.Tan(lightCamHalfFov) * lightObstaclesDistance * 2;

                //LightCamera.orthographicSize = lightCamSize/2f;

                //TODO: gameCam and lightCam have exactly the same values, need to look at this further
                float gameCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                float gameCamSize    = Mathf.Tan(gameCamHalfFov) * lightObstaclesDistance * 2;
                _camera.orthographicSize = gameCamSize / 2f;

                texHeight = Mathf.Round(lightCamSize / lightPixelSize);
                texWidth  = texHeight * _camera.aspect;

                _smallLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
            }

            //Make the light texture size even
            if (_extendedLightTextureSize.x % 2 != 0)
            {
                _extendedLightTextureSize.x++;
            }
            if (_extendedLightTextureSize.y % 2 != 0)
            {
                _extendedLightTextureSize.y++;
            }

            if (_extendedLightTextureSize.x > 1024 || _extendedLightTextureSize.y > 1024 ||
                _smallLightTextureSize.x > 1024 || _smallLightTextureSize.y > 1024)
            {
                Debug.LogWarning("LightPixelSize is too small. That might have a performance impact.");
            }

            if (_extendedLightTextureSize.x < 4 || _extendedLightTextureSize.y < 4 ||
                _smallLightTextureSize.x < 4 || _smallLightTextureSize.y < 4)
            {
                Debug.LogWarning("LightPixelSize is too big. Lighting may not work correctly.");
            }

            _screenBlitTempTex = new RenderTexture(_camera.pixelWidth, _camera.pixelHeight, 0, _texFormat)
            {
                filterMode = FilterMode.Point
            };

            lightCamera.orthographic = _camera.orthographic;

            if (enableNormalMapping)
            {
                _lightSourcesTexture = new RenderTexture(_camera.pixelWidth, _camera.pixelHeight, 0, _texFormat)
                {
                    filterMode = FilterMode.Point
                }
            }
            ;
            else
            {
                _lightSourcesTexture = new RenderTexture(_smallLightTextureSize.x, _smallLightTextureSize.y, 0, _texFormat)
                {
                    filterMode = lightTexturesFilterMode
                }
            };

            _obstaclesTexture = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y, 0, _texFormat);
            _ambientTexture   = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y, 0, _texFormat)
            {
                filterMode = lightTexturesFilterMode
            };

            Point2 upsampledObstacleSize = _extendedLightTextureSize * (lightObstaclesAntialiasing ? 2 : 1);
            _obstaclesUpsampledTexture = new RenderTexture(upsampledObstacleSize.x, upsampledObstacleSize.y, 0, _texFormat);

            if (affectOnlyThisCamera)
            {
                //TODO: I don't understand why this doesn't also use _texFormat. Further investigation necessary
                _renderTargetTexture = new RenderTexture(_camera.pixelWidth, _camera.pixelHeight, 0, RenderTextureFormat.ARGB32)
                {
                    filterMode = FilterMode.Point
                };
                _camera.targetTexture   = _renderTargetTexture;
                _camera.clearFlags      = CameraClearFlags.SolidColor;
                _camera.backgroundColor = Color.clear;
            }

            _alphaBlendedMaterial = new Material(Shader.Find("Light2D/Internal/Alpha Blended"));

            if (xzPlane)
            {
                Shader.EnableKeyword("LIGHT2D_XZ_PLANE");
            }
            else
            {
                Shader.DisableKeyword("LIGHT2D_XZ_PLANE");
            }

            _obstaclesPostProcessor = new ObstacleCameraPostProcessor();

            LoopAmbientLight(100);
        }
Exemplo n.º 3
0
        private void Start()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Shader.SetGlobalTexture("_ObstacleTex", Texture2D.whiteTexture);
                return;
            }
#endif

            if (LightCamera == null)
            {
                Debug.LogError(
                    "Lighting Camera in LightingSystem is null. Please, select Lighting Camera camera for lighting to work.");
                enabled = false;
                return;
            }
            if (LightOverlayMaterial == null)
            {
                Debug.LogError(
                    "LightOverlayMaterial in LightingSystem is null. Please, select LightOverlayMaterial camera for lighting to work.");
                enabled = false;
                return;
            }

            _camera = GetComponent <Camera>();

            if (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf))
            {
                HDR = false;
            }
            _texFormat = HDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            var lightPixelsPerUnityMeter = LightPixelsPerUnityMeter;

            InitTK2D();

            if (_camera.orthographic)
            {
                var rawCamHeight = (_camera.orthographicSize + LightCameraSizeAdd) * 2f;
                var rawCamWidth  = (_camera.orthographicSize * _camera.aspect + LightCameraSizeAdd) * 2f;

                _lightTextureSize = new Point2(
                    Mathf.RoundToInt(rawCamWidth * lightPixelsPerUnityMeter),
                    Mathf.RoundToInt(rawCamHeight * lightPixelsPerUnityMeter));
            }
            else
            {
                var lightCamHalfFov = (_camera.fieldOfView + LightCameraFovAdd) * Mathf.Deg2Rad / 2f;
                var lightCamSize    = Mathf.Tan(lightCamHalfFov) * LightObstaclesDistance * 2;
                LightCamera.orthographicSize = lightCamSize / 2f;

                var gameCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                var gameCamSize    = Mathf.Tan(gameCamHalfFov) * LightObstaclesDistance * 2;
                _camera.orthographicSize = gameCamSize / 2f;

                var texHeight = Mathf.RoundToInt(lightCamSize / LightPixelSize);
                var texWidth  = texHeight * _camera.aspect;
                _lightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
            }

            if (_lightTextureSize.x % 2 != 0)
            {
                _lightTextureSize.x++;
            }
            if (_lightTextureSize.y % 2 != 0)
            {
                _lightTextureSize.y++;
            }

            var obstacleTextureSize = _lightTextureSize * (LightObstaclesAntialiasing ? 2 : 1);

            _screenBlitTempTex = new RenderTexture(Screen.width, Screen.height, 0, _texFormat);

            LightCamera.orthographicSize = _lightTextureSize.y / (2f * lightPixelsPerUnityMeter);
            LightCamera.fieldOfView      = _camera.fieldOfView + LightCameraFovAdd;
            LightCamera.orthographic     = _camera.orthographic;

            _lightSourcesTexture = new RenderTexture(_lightTextureSize.x, _lightTextureSize.y,
                                                     0, _texFormat);
            _obstaclesTexture = new RenderTexture(obstacleTextureSize.x, obstacleTextureSize.y,
                                                  0, _texFormat);
            _ambientTexture = new RenderTexture(_lightTextureSize.x, _lightTextureSize.y,
                                                0, _texFormat);

            if (LightObstaclesAntialiasing)
            {
                _obstaclesDownsampledTexture = new RenderTexture(_lightTextureSize.x, _lightTextureSize.y,
                                                                 0, _texFormat);
            }

            LightCamera.aspect = _lightTextureSize.x / (float)_lightTextureSize.y;

            _obstaclesPostProcessor = new ObstacleCameraPostPorcessor();
        }
Exemplo n.º 4
0
        private void Start()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Shader.SetGlobalTexture("_ObstacleTex", Texture2D.whiteTexture);
                return;
            }
#endif

            if (LightCamera == null)
            {
                Debug.LogError(
                    "Lighting Camera in LightingSystem is null. Please, select Lighting Camera camera for lighting to work.");
                enabled = false;
                return;
            }
            if (LightOverlayMaterial == null)
            {
                Debug.LogError(
                    "LightOverlayMaterial in LightingSystem is null. Please, select LightOverlayMaterial camera for lighting to work.");
                enabled = false;
                return;
            }
            if (AffectOnlyThisCamera && _camera.targetTexture != null)
            {
                Debug.LogError("\"Affect Only This Camera\" will not work if camera.targetTexture is set.");
                AffectOnlyThisCamera = false;
            }

            _camera = GetComponent <Camera>();

            if (EnableNormalMapping && !_camera.orthographic)
            {
                Debug.LogError("Normal mapping is not supported with perspective camera.");
                EnableNormalMapping = false;
            }

            if (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf))
            {
                HDR = false;
            }
            _texFormat = HDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            var lightPixelsPerUnityMeter = LightPixelsPerUnityMeter;

            //_halfTexelOffest = SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 9");

            InitTK2D();

            if (_camera.orthographic)
            {
                var rawCamHeight = (_camera.orthographicSize + LightCameraSizeAdd) * 2f;
                var rawCamWidth  = (_camera.orthographicSize * _camera.aspect + LightCameraSizeAdd) * 2f;

                _extendedLightTextureSize = new Point2(
                    Mathf.RoundToInt(rawCamWidth * lightPixelsPerUnityMeter),
                    Mathf.RoundToInt(rawCamHeight * lightPixelsPerUnityMeter));

                var rawSmallCamHeight = _camera.orthographicSize * 2f * lightPixelsPerUnityMeter;
                _smallLightTextureSize = new Point2(
                    Mathf.RoundToInt(rawSmallCamHeight * _camera.aspect),
                    Mathf.RoundToInt(rawSmallCamHeight));
            }
            else
            {
                {
                    var lightCamHalfFov = (_camera.fieldOfView + LightCameraFovAdd) * Mathf.Deg2Rad / 2f;
                    var lightCamSize    = Mathf.Tan(lightCamHalfFov) * LightObstaclesDistance * 2;
                    //var gameCamHalfFov = _camera.fieldOfView*Mathf.Deg2Rad/2f;
                    var texHeight = Mathf.RoundToInt(lightCamSize / LightPixelSize);
                    var texWidth  = texHeight * _camera.aspect;
                    _extendedLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
                }
                {
                    var lightCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                    var lightCamSize    = Mathf.Tan(lightCamHalfFov) * LightObstaclesDistance * 2;
                    //LightCamera.orthographicSize = lightCamSize/2f;

                    var gameCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                    var gameCamSize    = Mathf.Tan(gameCamHalfFov) * LightObstaclesDistance * 2;
                    _camera.orthographicSize = gameCamSize / 2f;

                    var texHeight = Mathf.RoundToInt(lightCamSize / LightPixelSize);
                    var texWidth  = texHeight * _camera.aspect;
                    _smallLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
                }
            }

            if (_extendedLightTextureSize.x % 2 != 0)
            {
                _extendedLightTextureSize.x++;
            }
            if (_extendedLightTextureSize.y % 2 != 0)
            {
                _extendedLightTextureSize.y++;
            }

            if (_extendedLightTextureSize.x > 1024 || _extendedLightTextureSize.y > 1024 ||
                _smallLightTextureSize.x > 1024 || _smallLightTextureSize.y > 1024)
            {
                Debug.LogError("LightPixelSize is too small. Turning off lighting system.");
                enabled = false;
                return;
            }

            if (_extendedLightTextureSize.x < 4 || _extendedLightTextureSize.y < 4 ||
                _smallLightTextureSize.x < 4 || _smallLightTextureSize.y < 4)
            {
                Debug.LogError("LightPixelSize is too big. Turning off lighting system.");
                enabled = false;
                return;
            }

            _screenBlitTempTex            = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight, 0, _texFormat);
            _screenBlitTempTex.filterMode = FilterMode.Point;

            LightCamera.orthographic = _camera.orthographic;

            if (EnableNormalMapping)
            {
                _lightSourcesTexture = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight,
                                                         0, _texFormat);
                _lightSourcesTexture.filterMode = FilterMode.Point;
            }
            else
            {
                _lightSourcesTexture = new RenderTexture(_smallLightTextureSize.x, _smallLightTextureSize.y,
                                                         0, _texFormat);
                _lightSourcesTexture.filterMode = LightTexturesFilterMode;
            }

            _obstaclesTexture = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y,
                                                  0, _texFormat);
            _ambientTexture = new RenderTexture(_extendedLightTextureSize.x, _extendedLightTextureSize.y,
                                                0, _texFormat);

            _ambientTexture.filterMode = LightTexturesFilterMode;

            var upsampledObstacleSize = _extendedLightTextureSize * (LightObstaclesAntialiasing ? 2 : 1);
            _obstaclesUpsampledTexture = new RenderTexture(
                upsampledObstacleSize.x, upsampledObstacleSize.y, 0, _texFormat);

            if (AffectOnlyThisCamera)
            {
                _renderTargetTexture            = new RenderTexture((int)_camera.pixelWidth, (int)_camera.pixelHeight, 0, RenderTextureFormat.ARGB32);
                _renderTargetTexture.filterMode = FilterMode.Point;
            }

            _obstaclesPostProcessor = new ObstacleCameraPostPorcessor();

            LoopAmbientLight(100);
        }