| | 1 | | using DCL.Camera; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Builder |
| | 7 | | { |
| | 8 | | public class ScreenshotCameraController : IScreenshotCameraController |
| | 9 | | { |
| | 10 | | private IContext context; |
| | 11 | | private UnityEngine.Camera screenshotCamera; |
| | 12 | | internal IFreeCameraMovement freeCameraMovement; |
| | 13 | |
|
| | 14 | | public void Init(IContext context) |
| | 15 | | { |
| 7 | 16 | | this.context = context; |
| 7 | 17 | | GameObject screenshotCameraGameObject = new GameObject("BuilderScreenshotCamera"); |
| 7 | 18 | | screenshotCamera = screenshotCameraGameObject.AddComponent<UnityEngine.Camera>(); |
| 7 | 19 | | screenshotCamera.depth = -9999; |
| | 20 | |
|
| 7 | 21 | | screenshotCamera.gameObject.SetActive(false); |
| | 22 | |
|
| 7 | 23 | | if (context.sceneReferences.cameraController != null) |
| | 24 | | { |
| 0 | 25 | | var cameraController = context.sceneReferences.cameraController.GetComponent<Camera.CameraController>(); |
| | 26 | | // We assign the same culling configuration as the camera |
| 0 | 27 | | var biwCulling = BIWUtils.GetBIWCulling(cameraController.GetCulling()); |
| 0 | 28 | | screenshotCamera.cullingMask = biwCulling; |
| | 29 | |
|
| 0 | 30 | | if(cameraController.TryGetCameraStateByType<FreeCameraMovement>(out CameraStateBase cameraState)) |
| 0 | 31 | | freeCameraMovement = (FreeCameraMovement) cameraState; |
| | 32 | | } |
| 7 | 33 | | } |
| | 34 | |
|
| 4 | 35 | | public void Dispose() { GameObject.Destroy(screenshotCamera.gameObject); } |
| | 36 | |
|
| | 37 | | public void TakeSceneAerialScreenshot(IParcelScene parcelScene, IScreenshotCameraController.OnSnapshotsReady onS |
| | 38 | | { |
| 0 | 39 | | Vector3 pointToLookAt = BIWUtils.CalculateUnityMiddlePoint(parcelScene); |
| 0 | 40 | | float heightPosition = context.editorContext.godModeDynamicVariablesAsset.aerialScreenshotHeight * Mathf.Sqr |
| 0 | 41 | | Vector3 cameraPosition = pointToLookAt + Vector3.up * heightPosition; |
| | 42 | |
|
| 0 | 43 | | TakeSceneScreenshot(cameraPosition, pointToLookAt, BIWSettings.AERIAL_SCREENSHOT_WIDTH, BIWSettings.AERIAL_S |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public void TakeSceneScreenshot(IScreenshotCameraController.OnSnapshotsReady onSuccess) |
| | 47 | | { |
| 1 | 48 | | screenshotCamera.transform.position = freeCameraMovement.GetCameraPosition; |
| 1 | 49 | | screenshotCamera.transform.rotation = freeCameraMovement.gameObject.transform.rotation; |
| | 50 | |
|
| 1 | 51 | | TakeScreenshot(onSuccess, BIWSettings.SCENE_SNAPSHOT_WIDTH_RES, BIWSettings.SCENE_SNAPSHOT_HEIGHT_RES); |
| 1 | 52 | | } |
| | 53 | |
|
| 2 | 54 | | public void TakeSceneScreenshot(Vector3 camPosition, Vector3 pointToLookAt, IScreenshotCameraController.OnSnapsh |
| | 55 | |
|
| | 56 | | public void TakeSceneScreenshot(Vector3 camPosition, Vector3 pointToLookAt, int width, int height, IScreenshotCa |
| | 57 | | { |
| 1 | 58 | | screenshotCamera.transform.position = camPosition; |
| 1 | 59 | | screenshotCamera.transform.LookAt(pointToLookAt); |
| 1 | 60 | | TakeScreenshot(onSuccess, width, height); |
| 1 | 61 | | } |
| | 62 | |
|
| | 63 | | private void TakeScreenshot(IScreenshotCameraController.OnSnapshotsReady callback, int width, int height) |
| | 64 | | { |
| 2 | 65 | | if (UnityEngine.Camera.main == null) |
| 2 | 66 | | return; |
| | 67 | |
|
| | 68 | | //We deselect the entities to take better photos |
| 0 | 69 | | context.editorContext.entityHandler.DeselectEntities(); |
| | 70 | |
|
| 0 | 71 | | screenshotCamera.gameObject.SetActive(true); |
| | 72 | |
|
| 0 | 73 | | var current = screenshotCamera.targetTexture; |
| 0 | 74 | | screenshotCamera.targetTexture = null; |
| | 75 | |
|
| 0 | 76 | | Texture2D sceneScreenshot = ScreenshotFromCamera(width, height); |
| 0 | 77 | | screenshotCamera.targetTexture = current; |
| 0 | 78 | | screenshotCamera.gameObject.SetActive(false); |
| | 79 | |
|
| 0 | 80 | | callback?.Invoke(sceneScreenshot); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private Texture2D ScreenshotFromCamera(int width, int height) |
| | 84 | | { |
| 0 | 85 | | RenderTexture rt = new RenderTexture(width, height, 32); |
| 0 | 86 | | screenshotCamera.targetTexture = rt; |
| 0 | 87 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 0 | 88 | | screenshotCamera.Render(); |
| 0 | 89 | | RenderTexture.active = rt; |
| 0 | 90 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 0 | 91 | | screenShot.Apply(); |
| 0 | 92 | | return screenShot; |
| | 93 | | } |
| | 94 | | } |
| | 95 | | } |