/// <summary> /// Use leiaState to render final picture /// </summary> public void RenderImage(LeiaCamera camera) { if (enabled) { _leiaState.DrawImage(camera, Decorators); } }
/// <summary> /// Updates the views by applying leiaState UpdateViews method and sets renderTextures from texturePool. /// </summary> public void UpdateViews(LeiaCamera camera) { if (enabled) { this.Debug("UpdateViews"); _leiaState.UpdateViews(camera); } }
public void setLeiaCamera(LeiaCamera leiaCamera) { // we may still end up populating a static collecton of Cameras here. // when users use ShareRenderTextures script, exactly one Camera (deepest Camera) needs to have clear flag = root cam's clear flag // other cameras need to have Clear flag = depth only myCam = leiaCamera.GetComponentInChildren <LeiaRenderCamera>().GetComponent <Camera>(); _leiaCamera = leiaCamera; }
public static void DrawCameraBounds(LeiaCamera controller, GizmoType gizmoType) { #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0_0 || UNITY_5_0_1 GizmoType notSelected = GizmoType.NotSelected; #elif UNITY_5_0_2 || UNITY_5_0_3 || UNITY_5_0_4 GizmoType notSelected = GizmoType.NotInSelectionHierarchy; #else GizmoType notSelected = GizmoType.NonSelected; #endif if ((gizmoType & notSelected) != 0 && controller.DrawCameraBounds == false) { return; } var camera = controller.GetComponent <Camera>(); if (camera == null) { return; } DisplayConfig displayConfig; if (Application.isPlaying) { displayConfig = LeiaDisplay.Instance.GetDisplayConfig(); } else { displayConfig = Object.FindObjectOfType <LeiaDisplay>().GetDisplayConfig(); } LeiaCameraData leiaCameraData = LeiaCameraUtils.ComputeLeiaCamera( camera, controller.ConvergenceDistance, controller.BaselineScaling, displayConfig); LeiaBoundsData leiaBoundsData = LeiaCameraUtils.ComputeLeiaBounds(camera, leiaCameraData, controller.ConvergenceDistance, controller.CameraShift, displayConfig); if (((gizmoType & notSelected) != 0 && controller.DrawCameraBounds) || (gizmoType & GizmoType.Selected) != 0) { // draw convergence plane in editor play mode Handles.DrawSolidRectangleWithOutline(leiaBoundsData.screen, _leiaScreenPlaneColor, _leiaScreenWireColor); } if (((gizmoType & notSelected) != 0 && controller.DrawCameraBounds) || (gizmoType & GizmoType.Selected) != 0) { // draw frustum outline in white Handles.DrawSolidRectangleWithOutline(leiaBoundsData.north, _leiaBoundsPlaneColor, _leiaBoundsWireColor); Handles.DrawSolidRectangleWithOutline(leiaBoundsData.south, _leiaBoundsPlaneColor, _leiaBoundsWireColor); Handles.DrawSolidRectangleWithOutline(leiaBoundsData.east, _leiaBoundsPlaneColor, _leiaBoundsWireColor); Handles.DrawSolidRectangleWithOutline(leiaBoundsData.west, _leiaBoundsPlaneColor, _leiaBoundsWireColor); Handles.DrawSolidRectangleWithOutline(leiaBoundsData.top, _leiaBoundsPlaneColor, _leiaBoundsWireColor); Handles.DrawSolidRectangleWithOutline(leiaBoundsData.bottom, _leiaBoundsPlaneColor, _leiaBoundsWireColor); } }
/// <summary> /// Performs a raycast from the given LeiaCamera /// </summary> /// <param name="leiaCam">A LeiaCamera with a Camera component and Transform</param> /// <param name="position">A screenPosition</param> /// <returns>A ray from the camera's world position, that passes through the screenPosition</returns> public static Ray ScreenPointToRay(LeiaCamera leiaCam, Vector3 screenPosition) { Camera cam = leiaCam.Camera; bool prev_state = cam.enabled; cam.enabled = true; Ray r = cam.ScreenPointToRay(screenPosition); cam.enabled = prev_state; return(r); }
public void Start() { _LeiaCam = GetComponent <LeiaCamera>(); if (_LeiaCam == null) { return; } _targetCollisionLayerMask = 1 << _targetCollisionLayer; _targetConvergenceDistance = _LeiaCam.ConvergenceDistance; }
public void RestoreEffects() { this.Debug("RestoreEffects"); _effects.Clear(); if (_leiaCamera == null) { _leiaCamera = GetComponent <LeiaCamera>(); } RemoveEffectsFromLeiaViews(); }
void Start() { leiaCam = GetComponent <LeiaCamera>(); shift = Vector2.zero; #if !UNITY_2017_1_OR_NEWER RemoveParallaxCondition(true, LogLevel.Error, "Removing ParallaxEffect.cs component: Requires Unity version 2017.1 or newer"); #endif RemoveParallaxCondition(leiaCam == null, LogLevel.Error, string.Format("{0} {1}", "Removing ParallaxEffect.cs component: could not find LeiaCamera attached to gameObject named ", gameObject.name)); #if !UNITY_EDITOR RemoveParallaxCondition(Application.platform != RuntimePlatform.Android, LogLevel.Warning, "Removing ParallaxEffect.cs component: Runtime platform is not Android"); RemoveParallaxCondition(!SystemInfo.supportsGyroscope, LogLevel.Warning, "Removing ParallaxEffect.cs component: Device does not support Gyroscope"); #endif }
public void ForceUpdate() { this.Debug("ForceUpdate"); if (_leiaCamera == null) { _leiaCamera = GetComponent <LeiaCamera>(); } if (LeiaDisplay.InstanceIsNull) { return; } IsEffectsChanged(); CopyEffectsToLeiaViews(); }
public virtual void DrawImage(LeiaCamera camera, LeiaStateDecorators decorators) { if (_material == null) { this.Trace("Creating material"); _material = CreateMaterial(decorators.AlphaBlending); } if (template_renderTexture == null) { template_renderTexture = new RenderTexture(camera.Camera.pixelWidth, camera.Camera.pixelHeight, 0) { name = "interlaced" }; } _material.SetFloat("_viewRectX", camera.Camera.rect.x); _material.SetFloat("_viewRectY", camera.Camera.rect.y); _material.SetFloat("_viewRectW", camera.Camera.rect.width); _material.SetFloat("_viewRectH", camera.Camera.rect.height); if (_viewsHigh * _viewsWide > 16) { throw new NotSupportedException(SeparateTilesNotSupported); } for (int i = 0; i < _displayConfig.NumViews.x; i++) { int viewIndex = (int)(i * (1f / _displayConfig.NumViews.x) * _displayConfig.UserNumViews.x); _material.SetTexture("_texture_" + i, camera.GetView(viewIndex).TargetTexture); } // all templates run this line // Square and Slanted use it to interlace using an interlacing _material. // Abstract uses it to copy data from _texture_0 to template_renderTexture because _material is TWO_DIM shader, i.e. a simple pixel-copy shader Graphics.Blit(Texture2D.whiteTexture, template_renderTexture, _material); // Square and Slanted perform additional blits to screen in their override DrawImage classes // Square and Slanted are excluded from this line because their _material.name is not OpaqueShaderName or TransparentShaderName if (_material != null && !string.IsNullOrEmpty(_material.name) && (_material.name.Equals(TwoDimLeiaStateTemplate.OpaqueShaderName) || _material.name.Equals(TwoDimLeiaStateTemplate.TransparentShaderName))) { // AbstractLeiaStateTemplate uses this line to copy 2D view data from template_renderTexture to screen Graphics.Blit(template_renderTexture, Camera.current.activeTexture); } }
public void ProjectOntoZDP() { Transform t = null; LeiaCamera ideal_lc = null; if (Camera.main != null && Camera.main.GetComponent <LeiaCamera>() != null) { t = Camera.main.transform; ideal_lc = Camera.main.GetComponent <LeiaCamera>(); } else if (FindObjectOfType <LeiaCamera>() != null) { t = FindObjectOfType <LeiaCamera>().transform; ideal_lc = t.GetComponent <LeiaCamera>(); } else if (FindObjectOfType <Camera>() != null) { t = FindObjectOfType <Camera>().transform; } else { #if UNITY_EDITOR Debug.LogWarning("No LeiaCamera or Camera in scene"); #else LogUtil.Debug("No LeiaCamera or Camera in scene."); #endif } if (ideal_lc != null) { Vector3 error = ideal_lc.transform.position + ideal_lc.ConvergenceDistance * ideal_lc.transform.forward - transform.position; Vector3 BF = Vector3.Project(error, ideal_lc.transform.forward); transform.position = transform.position + BF; transform.rotation = ideal_lc.transform.rotation; } else if (t != null) { Vector3 error = t.position + 10.0f * t.forward - transform.position; Vector3 BF = Vector3.Project(error, t.forward); transform.position = t.position + BF; transform.rotation = t.rotation; } }
public CameraCalculatedParams(LeiaCamera properties, DisplayConfig displayConfig) { ScreenHalfHeight = properties.ConvergenceDistance * Mathf.Tan(properties.FieldOfView * Mathf.PI / 360.0f); ScreenHalfWidth = displayConfig.UserAspectRatio * ScreenHalfHeight; float f = (displayConfig.ViewResolution.y / displayConfig.ResolutionScale) / 2f / Mathf.Tan(properties.FieldOfView * Mathf.PI / 360f); EmissionRescalingFactor = displayConfig.SystemDisparityPixels * properties.BaselineScaling * properties.ConvergenceDistance / f; /*Debug.Log ("CameraCalculatedParams" + " UserPanel.x: " + displayConfig.UserPanelResolution.x + " UserPanel.y: " + displayConfig.UserPanelResolution.y + " UserView.x: " + displayConfig.UserViewResolution.x + " UserView.y: " + displayConfig.UserViewResolution.y + " Screen.width: " + Screen.width + " Screen.height: " + Screen.height + " properties.FieldOfView: " + properties.FieldOfView + " ScreenHalfWidth: " + ScreenHalfWidth + " ScreenHalfHeight: " + ScreenHalfHeight + " baseline: " + EmissionRescalingFactor);*/ }
public override void UpdateViews(LeiaCamera leiaCamera) { _displayConfig.UserNumViews = new XyPair <int>(1, 1); base.UpdateViews(leiaCamera); float near = Mathf.Max(1.0e-5f, leiaCamera.NearClipPlane); float far = Mathf.Max(near, leiaCamera.FarClipPlane); var cam = leiaCamera.GetView(0); if (cam.IsCameraNull) { return; } cam.Camera.ResetProjectionMatrix(); cam.Position = new Vector3(0.0f, 0.0f, 0.0f); cam.NearClipPlane = near; cam.FarClipPlane = far; }
public void Update() { if (_leiaCamera == null) { _leiaCamera = GetComponent <LeiaCamera>(); if (_leiaCamera == null) { DestroyScript(this); return; } } if (LeiaDisplay.InstanceIsNull) { return; } if (IsEffectsChanged()) { CopyEffectsToLeiaViews(); StartCoroutine(CheckProjectionMatrixReset()); } }
public virtual void UpdateViews(LeiaCamera leiaCamera) { this.Debug("UpdateViews"); if (_viewsWide != _displayConfig.UserNumViews.x) { SetViewCount(_displayConfig.UserNumViews.x, _viewsHigh); UpdateEmissionPattern(LeiaDisplay.Instance.Decorators); } leiaCamera.SetViewCount(_viewsWide * _viewsHigh); int width, height; GetTileSize(out width, out height); int id = 0; for (int ny = 0; ny < _viewsHigh; ny++) { for (int nx = 0; nx < _viewsWide; nx++) { int viewId = ny * _viewsWide + nx; var view = leiaCamera.GetView(viewId); if (view.IsCameraNull) { continue; } string viewIdStr = string.Format("view_{0}_{1}", nx, ny); view.SetTextureParams(width, height, viewIdStr); view.ViewIndexX = nx; view.ViewIndexY = ny; view.AttachLeiaMediaCommandBuffersForIndex(id); view.ViewIndex = id++; } } }
private static void OnDrawLeiaBounds(LeiaCamera controller, GizmoType gizmoType) { LeiaCameraBounds.DrawCameraBounds(controller, gizmoType); }
/// <summary> /// Sets up /// recording object(s), /// recording framerate, /// folders /// </summary> public void BeginRecording() { // only allow recording in edit mode #if !UNITY_EDITOR return; #endif #pragma warning disable CS0162 // Suppress unreachable code warning // in all cases: regulate the passage of time // Time.captureFramerate fixes Update() calls such that Update() effectively only gets called // after enough simulated playback time has passed to warrant a new frame // forcing captureFramerate = 60 ensures that the correct video framerate AND duration are made // without Unity time at 60, 24 fps recordings are 2.5x longer than they should be Time.captureFramerate = frameRate; isActive = true; // in all cases, we will need RenderTextures from LeiaCamera if (leia_cam == null) { leia_cam = transform.GetComponent <LeiaCamera>(); // if LeiaCamera has clear flag "Solid color" in PNG format with a weak alpha, background pixels will be dimmed by alpha if ((leia_cam.Camera.clearFlags == CameraClearFlags.Color || leia_cam.Camera.clearFlags == CameraClearFlags.SolidColor) && recordingFormat.ToString().Equals("png") && leia_cam.Camera.backgroundColor.a < 1.0f ) { LogUtil.Log(LogLevel.Warning, "When recording in format {0} from {1} with clear flag {2} and background {3}:\n\tBackground pixels will be dimmed by alpha channel of color {3}", recordingFormat, leia_cam, leia_cam.Camera.clearFlags, leia_cam.Camera.backgroundColor); } } if (leia_cam != null && leia_cam.GetView(0) != null && leia_cam.GetView(0).TargetTexture != null) { RenderTexture view_prime = leia_cam.GetView(0).TargetTexture; cols = Mathf.FloorToInt(Mathf.Sqrt(leia_cam.GetViewCount())); rows = (cols == 0 ? 0 : leia_cam.GetViewCount() / cols); record_w = view_prime.width * cols; record_h = view_prime.height * rows; views = new RenderTexture[leia_cam.GetViewCount()]; for (int i = 0; i < leia_cam.GetViewCount(); i++) { views[i] = leia_cam.GetView(i).TargetTexture; } } System.DateTime currTime = System.DateTime.Now; folderPath = Path.Combine(Application.streamingAssetsPath, string.Format("{0:D3}_{1:D2}_{2:D2}_{3:D2}", currTime.DayOfYear, currTime.Hour, currTime.Minute, currTime.Second)); Directory.CreateDirectory(folderPath); // if png/jpg // no additional behavior // if mp4 #if UNITY_EDITOR && UNITY_2017_3_OR_NEWER if (recordingFormat.ToString().Equals("mp4")) { VideoTrackAttributes videoAttr = new VideoTrackAttributes() { frameRate = new MediaRational(frameRate), width = (uint)record_w, height = (uint)record_h, includeAlpha = false }; string vid_name = string.Format("recording_{0}x{1}.{2}", cols, rows, recordingFormat.ToString()); encoder = new MediaEncoder(Path.Combine(folderPath, vid_name), videoAttr); } #endif #pragma warning restore CS0162 // Suppress unreachable code warning }
public override void OnInspectorGUI() { if (_controller == null) { _controller = (LeiaCamera)target; } if (!_controller.enabled) { return; } EditorGUI.BeginChangeCheck(); // allow multi-object editing // display object properties in sequence when multiple objects selected for (int i = 0; i < targets.Length; i++) { _controller = (LeiaCamera)targets[i]; if (targets.Length > 1) { EditorGUILayout.Separator(); EditorGUILayout.ObjectField(_controller, typeof(LeiaCamera), true); } UndoableInputFieldUtils.ImmediateFloatField(() => _controller.ConvergenceDistance, v => _controller.ConvergenceDistance = v, ConvergenceDistanceFieldLabel, _controller); UndoableInputFieldUtils.ImmediateFloatField(() => _controller.BaselineScaling, v => _controller.BaselineScaling = v, BaselineScalingFieldLabel, _controller); UndoableInputFieldUtils.BoolField(() => _controller.DrawCameraBounds, v => _controller.DrawCameraBounds = v, DrawCameraBoundsFieldLabel, _controller); EditorGUILayout.Separator(); if (EditorApplication.isPlaying) { if (!LeiaDisplay.InstanceIsNull) { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(UpdateEffectsButtonLabel)) { _controller.UpdateEffects(); } EditorGUILayout.HelpBox(UpdateEffectsHelpText, MessageType.Info); EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox(NoEffectsControllerHelpText, MessageType.Info); } } if (targets.Length > 1) { EditorGUILayout.Separator(); } } if (EditorGUI.EndChangeCheck()) { foreach (Object obj in targets) { EditorUtility.SetDirty(obj); } } }
public override void DrawImage(LeiaCamera camera, LeiaStateDecorators decorators) { base.DrawImage(camera, decorators); Graphics.Blit(template_renderTexture, Camera.current.activeTexture, _sharpening); }
public static Ray ScreenPointToRay(LeiaCamera leiaCam, Vector2 position) { return(ScreenPointToRay(leiaCam, (Vector3)position)); }
public override void UpdateViews(LeiaCamera leiaCamera) { base.UpdateViews(leiaCamera); var calculated = new CameraCalculatedParams(leiaCamera, _displayConfig); var near = Mathf.Max(1.0e-5f, leiaCamera.NearClipPlane); var far = Mathf.Max(near, leiaCamera.FarClipPlane); var halfDeltaX = calculated.ScreenHalfWidth; var halfDeltaY = calculated.ScreenHalfHeight; var deltaZ = far - near; Matrix4x4 m = Matrix4x4.zero; float posx, posy; for (int ny = 0; ny < _viewsHigh; ny++) { for (int nx = 0; nx < _viewsWide; nx++) { var viewId = ny * _viewsWide + nx; var view = leiaCamera.GetView(viewId); if (view.IsCameraNull) { continue; } if (leiaCamera.Camera.orthographic) { // catch case where camera's orthgraphicSize = 0. // In Unity perspective camera cannot have FOV 0 but orthographic can have size 0 float orthoSize = Mathf.Max(1E-5f, leiaCamera.Camera.orthographicSize); float halfSizeX = orthoSize * _displayConfig.UserAspectRatio; float halfSizeY = orthoSize; int tileWidth, tileHeight; GetTileSize(out tileWidth, out tileHeight); float baseline = 2.0f * halfSizeX * leiaCamera.BaselineScaling * _displayConfig.SystemDisparityPixels * leiaCamera.ConvergenceDistance / (tileWidth / _displayConfig.ResolutionScale); posx = GetEmissionX(nx, ny) * baseline + leiaCamera.CameraShift.x; posy = GetEmissionY(nx, ny) * baseline + leiaCamera.CameraShift.y; // row 0 m [0, 0] = 1.0f / halfSizeX; m [0, 1] = 0.0f; m [0, 2] = -posx / (halfSizeX * leiaCamera.ConvergenceDistance); m [0, 3] = 0.0f; // row 1 m [1, 0] = 0.0f; m [1, 1] = 1.0f / halfSizeY; m [1, 2] = -posy / (halfSizeY * leiaCamera.ConvergenceDistance); m [1, 3] = 0.0f; // row 2 m [2, 0] = 0.0f; m [2, 1] = 0.0f; m [2, 2] = -2.0f / deltaZ; m [2, 3] = -(far + near) / deltaZ; // row 3 m [3, 0] = 0.0f; m [3, 1] = 0.0f; m [3, 2] = 0.0f; m [3, 3] = 1.0f; } else // perspective { posx = calculated.EmissionRescalingFactor * (GetEmissionX(nx, ny) + leiaCamera.CameraShift.x); posy = calculated.EmissionRescalingFactor * (GetEmissionY(nx, ny) + leiaCamera.CameraShift.y); // row 0 m[0, 0] = leiaCamera.ConvergenceDistance / halfDeltaX; m[0, 1] = 0.0f; m[0, 2] = -posx / halfDeltaX; m[0, 3] = 0.0f; // row 1 m[1, 0] = 0.0f; m[1, 1] = leiaCamera.ConvergenceDistance / halfDeltaY; m[1, 2] = -posy / halfDeltaY; m[1, 3] = 0.0f; // row 2 m[2, 0] = 0.0f; m[2, 1] = 0.0f; m[2, 2] = -(far + near) / deltaZ; m[2, 3] = -2.0f * far * near / deltaZ; // row 3 m[3, 0] = 0.0f; m[3, 1] = 0.0f; m[3, 2] = -1.0f; m[3, 3] = 0.0f; } view.Position = new Vector3(posx, posy, 0); view.Matrix = m; view.NearClipPlane = near; view.FarClipPlane = far; UpdateSharpeningParameters(); } } }