示例#1
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 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, 0, 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"));

            _lightBlockerReplacementShader = Shader.Find(@"Light2D/Internal/LightBlockerReplacementShader");

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

            _obstaclesPostProcessor = new ObstacleCameraPostPorcessor();

            LoopAmbientLight(100);
        }
示例#2
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();
        }
示例#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;
            }
            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);
        }
示例#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);
        }