Exemplo n.º 1
0
        private void Start()
        {
            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 (enableNormalMapping && !_camera.orthographic)
            {
                Debug.LogError("Normal mapping is not supported with perspective camera.");
                enableNormalMapping = false;
            }

            FlareLayer flare = GetComponent <FlareLayer>();

            if (flare != null && flare.enabled)
            {
                Debug.Log("Disabling FlareLayer since it's null.");
                flare.enabled = false;
            }

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

            float lightPixelsPerUnityMeter = LightPixelsPerUnityMeter;

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

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

                float rawSmallCamHeight = 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;
                    //var gameCamHalfFov = _camera.fieldOfView*Mathf.Deg2Rad/2f;
                    int   texHeight = Mathf.RoundToInt(lightCamSize / lightPixelSize);
                    float texWidth  = texHeight * _camera.aspect;
                    _extendedLightTextureSize = Point2.Round(new Vector2(texWidth, texHeight));
                }
                {
                    float lightCamHalfFov = _camera.fieldOfView * Mathf.Deg2Rad / 2f;
                    float lightCamSize    = Mathf.Tan(lightCamHalfFov) * lightObstaclesDistance * 2;
                    //LightCamera.orthographicSize = lightCamSize/2f;

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

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

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

            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.");
            }

            if (_extendedLightTextureSize.x < 4 || _extendedLightTextureSize.y < 4 ||
                _smallLightTextureSize.x < 4 || _smallLightTextureSize.y < 4)
            {
                Debug.LogError("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);

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

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

            _obstaclesPostProcessor = new ObstacleCameraPostProcessor();

            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);
        }