bool IsProjectionReset()
        {
#if !UNITY_EDITOR
            return(false);
#else
            if (_effects.Count < 1)
            {
                return(false);
            }
            if (Mathf.Approximately(_leiaCamera.BaselineScaling, 0))
            {
                return(false);
            }

            if (LeiaDisplay.Instance.ActualLightfieldMode == LeiaDisplay.LightfieldMode.Off)
            {
                return(false);
            }

            for (int i = 0; i < _leiaCamera.GetViewCount(); i++)
            {
                //m02 only appears to be 0 when the projection matrix has been reset
                if (!Mathf.Approximately(_leiaCamera.GetView(i).Matrix.m02, 0))
                {
                    return(false);
                }
            }

            return(true);
#endif
        }
        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);
            }
        }
Exemplo n.º 3
0
        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 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++;
                }
            }
        }
        /// <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
        }
Exemplo n.º 6
0
        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();
                }
            }
        }