void addCommonCameraComponents(GameObject camGO) { Camera cam = camGO.GetComponent <Camera>(); if (!cam) { cam = Undo.AddComponent <Camera>(camGO); } GUILayer guiLayer = camGO.GetComponent <GUILayer>(); if (!guiLayer) { guiLayer = Undo.AddComponent <GUILayer>(camGO); } FlareLayer flareLayer = camGO.GetComponent <FlareLayer>(); if (!flareLayer) { flareLayer = Undo.AddComponent <FlareLayer>(camGO); } AudioListener al = camGO.GetComponent <AudioListener>(); if (!al) { al = Undo.AddComponent <AudioListener>(camGO); } }
protected void CreateFungusSceneManager() { // open & verify a path where we can save the SceneManager string path = GetFolderPath("SceneManager"); // make sure we got a valud path if (path == "") { return; } // Create the SceneManager Scene sceneManagerScene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single); // make sure the scene we created was valid if (!sceneManagerScene.IsValid()) { Debug.LogError("Invalid Scene. Could not create scene '" + path + "'."); return; } // get all the cameras Camera[] cameras = GameObject.FindObjectsOfType <Camera>(); foreach (Camera camera in cameras) { // if this is our scene's camera if (camera == Camera.main) { // change the values camera.orthographic = true; camera.clearFlags = CameraClearFlags.SolidColor; camera.cullingMask = 0; camera.depth = -99; camera.backgroundColor = Color.grey; camera.useOcclusionCulling = false; camera.allowHDR = false; camera.allowMSAA = false; FlareLayer flareLayer = camera.gameObject.GetComponent <FlareLayer>(); DestroyImmediate(flareLayer); camera.name = "Manager Camera"; } } // remove the lights Light[] lights = GameObject.FindObjectsOfType <Light>(); for (int i = lights.Length - 1; i >= 0; --i) { DestroyImmediate(lights[i].gameObject); } // try to save if (!EditorSceneManager.SaveScene(sceneManagerScene, path + "/SceneManager.unity", false)) { Debug.LogWarning("Couldn't create FungusSceneManager"); } }
private void OnEnable() { go = (target as Camera).gameObject; //remove the FlareLayer component FlareLayer fl = go.GetComponent <FlareLayer>(); if (fl != null) { DestroyImmediate(fl); } }
/// <summary> /// Selects gradients for fog & ground (horizon of procedural sky) based on isOvercast. Disables sun flare if /// overcast, and sets various settings for sky before starting SetAmbient coroutine. /// </summary> /// <param name="isOverCast"></param> public void Init(bool isOverCast) { Debug.Log("in AmbientFogLightController init"); if (isOverCast) { FogGradient = PresetContainer.Instance.fogOver; if (FogGradient == null) { Debug.Log("FogGradient is null (overcast)"); } else { Debug.Log("FogGradient != null (overcast)"); } HorizonGradient = PresetContainer.Instance.colorOver; AtmosphereCurve = PresetContainer.Instance.atmosphereOver; SkyMat.SetFloat("_Exposure", .3f); //##TODO - add to presets SunFlare.enabled = false; } else { FogGradient = PresetContainer.Instance.fogBase; if (FogGradient == null) { Debug.Log("FogGradient is null"); } else { Debug.Log("FogGradient != null"); } HorizonGradient = PresetContainer.Instance.colorBase; AtmosphereCurve = PresetContainer.Instance.atmosphereBase; SkyMat.SetFloat("_Exposure", .5f); } SetSkyObjectSize(); flareLayer = this.transform.Find("SkyCam").gameObject.GetComponent <FlareLayer>(); _skyTint = PresetContainer.Instance.skyTint; SkyMat.SetColor("_SkyTint", _skyTint); _atmsphrOffset = PresetContainer.Instance.atmsphrOffset; }
void Start() { if (!isLocalPlayer) { AudioListener listener = GetComponentInChildren <AudioListener>(); GUILayer gui = GetComponentInChildren <GUILayer>(); FlareLayer flare = GetComponentInChildren <FlareLayer>(); listener.enabled = false; gui.enabled = false; flare.enabled = false; return; } if (Camera.main) { Camera.main.gameObject.SetActive(false); } playerCamera = GetComponentInChildren <Camera>(); playerCamera.enabled = true; }
void Awake() { if (!PlayerPrefs.HasKey("LensFlare")) { PlayerPrefs.SetInt("LensFlare", (HR_HighwayRacerProperties.Instance._defaultFlares == true ? 1 : 0)); } if (!PlayerPrefs.HasKey("Bloom")) { PlayerPrefs.SetInt("Bloom", (HR_HighwayRacerProperties.Instance._defaultBloom == true ? 1 : 0)); } if (!PlayerPrefs.HasKey("HQLights")) { PlayerPrefs.SetInt("HQLights", (HR_HighwayRacerProperties.Instance._defaultHQLights == true ? 1 : 0)); } bloom = GetComponent <BloomOptimized>(); flareLayer = GameObject.FindObjectOfType <FlareLayer>(); Check(); }
protected void CreateFungusSceneManager() { // open & verify a path where we can save the SceneManager string path = GetFolderPath("SceneManager"); // make sure we got a valid path if (path == "") { return; } // Create the SceneManager Scene sceneManagerScene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single); // make sure the scene we created was valid if (!sceneManagerScene.IsValid()) { Debug.LogError("Invalid Scene. Could not create scene '" + path + "'."); return; } // get all the cameras Camera[] cameras = GameObject.FindObjectsOfType <Camera>(); foreach (Camera camera in cameras) { // if this is our scene's camera if (camera == Camera.main) { // change the values camera.orthographic = true; camera.clearFlags = CameraClearFlags.SolidColor; camera.cullingMask = 0; camera.depth = -99; camera.backgroundColor = Color.grey; camera.useOcclusionCulling = false; camera.allowHDR = false; camera.allowMSAA = false; FlareLayer flareLayer = camera.gameObject.GetComponent <FlareLayer>(); DestroyImmediate(flareLayer); camera.name = "ManagerCamera"; } } // remove the lights Light[] lights = GameObject.FindObjectsOfType <Light>(); for (int i = lights.Length - 1; i >= 0; --i) { //lights[i].gameObject.SetActive(false); DestroyImmediate(lights[i].gameObject); } // add prefabs to scene // add the SceneManager prefab GameObject sceneManagerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/FungusManager/SceneManager/Prefabs/SceneManager.prefab", typeof(GameObject)); if (sceneManagerPrefab == null) { Debug.LogError("Couldn't load SceneManager prefab"); return; } GameObject sceneManagerGameObject = PrefabUtility.InstantiatePrefab(sceneManagerPrefab, sceneManagerScene) as GameObject; // disconnect this object from the prefab (in package folder) that created it PrefabUtility.DisconnectPrefabInstance(sceneManagerGameObject); // add the flowcharts empty object //GameObject flowchartsPrefab = Resources.Load<GameObject>("SceneManager/Prefabs/Flowcharts"); GameObject flowchartsPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/FungusManager/SceneManager/Prefabs/Flowcharts.prefab", typeof(GameObject)); if (flowchartsPrefab == null) { Debug.LogError("Couldn't load Flowcharts prefab"); return; } GameObject flowchartsGameObject = PrefabUtility.InstantiatePrefab(flowchartsPrefab, sceneManagerScene) as GameObject; flowchartsGameObject.transform.SetSiblingIndex(3); // disconnect this object from the prefab (in package folder) that created it PrefabUtility.DisconnectPrefabInstance(flowchartsGameObject); // add an empty Flowchart from Fungus GameObject flowchartPrefab = Resources.Load <GameObject>("Prefabs/Flowchart"); if (flowchartPrefab == null) { Debug.LogError("Couldn't load Fungus Flowchart prefab"); return; } GameObject flowchartGameObject = PrefabUtility.InstantiatePrefab(flowchartPrefab, sceneManagerScene) as GameObject; PrefabUtility.DisconnectPrefabInstance(flowchartGameObject); flowchartGameObject.name = "SceneManagement"; // attach this flowchart to the flowcharts GameObject flowchartGameObject.transform.parent = flowchartsGameObject.transform; // find the default block in Flowchart Block defaultBlock = flowchartGameObject.GetComponent <Block>(); defaultBlock.BlockName = "Start"; // by default, add a 'Start' scene to this Flowchart RequestManagedScene requestManagedScene = flowchartGameObject.AddComponent <RequestManagedScene>(); requestManagedScene.sceneName = "Start"; // get the flowchart script defaultBlock.CommandList.Add(requestManagedScene); // try to save if (!EditorSceneManager.SaveScene(sceneManagerScene, path + "/SceneManager.unity", false)) { Debug.LogWarning("Couldn't create FungusSceneManager"); return; } // add this new scene to the build settings ClearBuildSettings(); SaveSceneToBuildSettings(sceneManagerScene, true); // if the SceneManager is loaded, make it active SetSceneToActive(sceneManagerScene); MoveSceneToTop(sceneManagerScene); if (bakeLightingManually) { BakeAllScenes(); } CheckScenes(); }
private static Object ReadAsset(AssetStream stream, AssetInfo assetInfo, long offset, int size) { Object asset = null; switch (assetInfo.ClassID) { case ClassIDType.GameObject: asset = new GameObject(assetInfo); break; case ClassIDType.Transform: asset = new Transform(assetInfo); break; case ClassIDType.Camera: asset = new Camera(assetInfo); break; case ClassIDType.Material: asset = new Material(assetInfo); break; case ClassIDType.MeshRenderer: asset = new MeshRenderer(assetInfo); break; case ClassIDType.Texture2D: asset = new Texture2D(assetInfo); break; case ClassIDType.MeshFilter: asset = new MeshFilter(assetInfo); break; case ClassIDType.Mesh: asset = new Mesh(assetInfo); break; case ClassIDType.Shader: asset = new Shader(assetInfo); break; case ClassIDType.TextAsset: asset = new TextAsset(assetInfo); break; case ClassIDType.Rigidbody2D: asset = new Rigidbody2D(assetInfo); break; case ClassIDType.Rigidbody: asset = new Rigidbody(assetInfo); break; case ClassIDType.CircleCollider2D: asset = new CircleCollider2D(assetInfo); break; case ClassIDType.PolygonCollider2D: asset = new PolygonCollider2D(assetInfo); break; case ClassIDType.BoxCollider2D: asset = new BoxCollider2D(assetInfo); break; case ClassIDType.PhysicsMaterial2D: asset = new PhysicMaterial(assetInfo); break; case ClassIDType.MeshCollider: asset = new MeshCollider(assetInfo); break; case ClassIDType.BoxCollider: asset = new BoxCollider(assetInfo); break; case ClassIDType.SpriteCollider2D: asset = new CompositeCollider2D(assetInfo); break; case ClassIDType.EdgeCollider2D: asset = new EdgeCollider2D(assetInfo); break; case ClassIDType.CapsuleCollider2D: asset = new CapsuleCollider2D(assetInfo); break; case ClassIDType.AnimationClip: asset = new AnimationClip(assetInfo); break; case ClassIDType.AudioListener: asset = new AudioListener(assetInfo); break; case ClassIDType.AudioSource: asset = new AudioSource(assetInfo); break; case ClassIDType.AudioClip: asset = new AudioClip(assetInfo); break; case ClassIDType.RenderTexture: asset = new RenderTexture(assetInfo); break; case ClassIDType.Cubemap: asset = new Cubemap(assetInfo); break; case ClassIDType.Avatar: asset = new Avatar(assetInfo); break; case ClassIDType.AnimatorController: asset = new AnimatorController(assetInfo); break; case ClassIDType.GUILayer: asset = new GUILayer(assetInfo); break; case ClassIDType.Animator: asset = new Animator(assetInfo); break; case ClassIDType.Light: asset = new Light(assetInfo); break; case ClassIDType.Animation: asset = new Animation(assetInfo); break; case ClassIDType.MonoScript: asset = new MonoScript(assetInfo); break; case ClassIDType.NewAnimationTrack: asset = new NewAnimationTrack(assetInfo); break; case ClassIDType.FlareLayer: asset = new FlareLayer(assetInfo); break; case ClassIDType.Font: asset = new Font(assetInfo); break; case ClassIDType.PhysicMaterial: asset = new PhysicMaterial(assetInfo); break; case ClassIDType.SphereCollider: asset = new SphereCollider(assetInfo); break; case ClassIDType.CapsuleCollider: asset = new CapsuleCollider(assetInfo); break; case ClassIDType.SkinnedMeshRenderer: asset = new SkinnedMeshRenderer(assetInfo); break; case ClassIDType.BuildSettings: asset = new BuildSettings(assetInfo); break; case ClassIDType.AssetBundle: asset = new AssetBundle(assetInfo); break; case ClassIDType.WheelCollider: asset = new WheelCollider(assetInfo); break; case ClassIDType.MovieTexture: asset = new MovieTexture(assetInfo); break; case ClassIDType.TerrainCollider: asset = new TerrainCollider(assetInfo); break; case ClassIDType.TerrainData: asset = new TerrainData(assetInfo); break; case ClassIDType.ParticleSystem: asset = new ParticleSystem(assetInfo); break; case ClassIDType.ParticleSystemRenderer: asset = new ParticleSystemRenderer(assetInfo); break; case ClassIDType.SpriteRenderer: asset = new SpriteRenderer(assetInfo); break; case ClassIDType.Sprite: asset = new Sprite(assetInfo); break; case ClassIDType.Terrain: asset = new Terrain(assetInfo); break; case ClassIDType.AnimatorOverrideController: asset = new AnimatorOverrideController(assetInfo); break; case ClassIDType.CanvasRenderer: asset = new CanvasRenderer(assetInfo); break; case ClassIDType.Canvas: asset = new Canvas(assetInfo); break; case ClassIDType.RectTransform: asset = new RectTransform(assetInfo); break; case ClassIDType.SpriteAtlas: asset = new SpriteAtlas(assetInfo); break; default: return(null); } stream.BaseStream.Position = offset; if (Config.IsGenerateGUIDByContent) { byte[] data = stream.ReadBytes(size); asset.Read(data); using (MD5 md5 = MD5.Create()) { byte[] md5Hash = md5.ComputeHash(data); assetInfo.GUID = new UtinyGUID(md5Hash); } } else { stream.AlignPosition = offset; asset.Read(stream); long read = stream.BaseStream.Position - offset; if (read != size) { throw new Exception($"Read {read} but expected {size} for object {asset.GetType().Name}"); } } return(asset); }
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); }
private void Start() { if (GameData.Instance.testServer || GameData.IsHeadlessServer) { Debug.Log("Turn off lightsystem as this is a server"); 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" FlareLayer 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; 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; //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.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(_camera.pixelWidth, _camera.pixelHeight, 0, _texFormat); _screenBlitTempTex.filterMode = FilterMode.Point; LightCamera.orthographic = _camera.orthographic; if (EnableNormalMapping) { _lightSourcesTexture = new RenderTexture(_camera.pixelWidth, _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; Point2 upsampledObstacleSize = _extendedLightTextureSize * (LightObstaclesAntialiasing ? 2 : 1); _obstaclesUpsampledTexture = new RenderTexture(upsampledObstacleSize.x, upsampledObstacleSize.y, 0, _texFormat); if (AffectOnlyThisCamera) { _renderTargetTexture = new RenderTexture(_camera.pixelWidth, _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); }
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); }
// Use this for initialization void Start() { flare = GetComponent <FlareLayer>(); }