Ejemplo n.º 1
0
        public override bool PrepareCapture()
        {
            if (_capturing)
            {
                return(false);
            }

            if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated"))
            {
                Debug.LogError("[AVProMovieCapture] OpenGL not yet supported for CaptureFromCamera360 component, please use Direct3D11 instead. You may need to switch your build platform to Windows.");
                return(false);
            }

            // Setup material
            _pixelFormat = NativePlugin.PixelFormat.RGBA32;
            _isTopDown   = true;
            if (_camera == null)
            {
                _camera = this.GetComponent <Camera>();
            }
            if (_camera == null)
            {
                Debug.LogError("[AVProMovieCapture] No camera assigned to CaptureFromCamera360");
                return(false);
            }

            // Resolution
            int finalWidth  = Mathf.FloorToInt(_camera.pixelRect.width);
            int finalHeight = Mathf.FloorToInt(_camera.pixelRect.height);

            if (_renderResolution == Resolution.Custom)
            {
                finalWidth  = (int)_renderSize.x;
                finalHeight = (int)_renderSize.y;
            }
            else if (_renderResolution != Resolution.Original)
            {
                GetResolution(_renderResolution, ref finalWidth, ref finalHeight);
            }

            // Setup rendering a different render target if we're overriding resolution or anti-aliasing
            //if (_renderResolution != Resolution.Original || _renderAntiAliasing != QualitySettings.antiAliasing)
            {
                int aaLevel = GetCameraAntiAliasingLevel(_camera);

                CubemapRenderMethod cubemapRenderMethod = GetCubemapRenderingMethod();
                Debug.Log("[AVProMovieCapture] Using cubemap render method: " + cubemapRenderMethod.ToString());

                if (!Mathf.IsPowerOfTwo(_cubemapResolution))
                {
                    _cubemapResolution = Mathf.ClosestPowerOfTwo(_cubemapResolution);
                    Debug.LogWarning("[AVProMovieCapture] Cubemap must be power-of-2 dimensions, resizing to closest = " + _cubemapResolution);
                }

                // Create the final render target
                _targetNativePointer = System.IntPtr.Zero;
                if (_finalTarget != null)
                {
                    _finalTarget.DiscardContents();
                    if (_finalTarget.width != finalWidth || _finalTarget.height != finalHeight)
                    {
                        RenderTexture.ReleaseTemporary(_finalTarget);
                        _finalTarget = null;
                    }
                }
                if (_finalTarget == null)
                {
                    _finalTarget      = RenderTexture.GetTemporary(finalWidth, finalHeight, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default, 1);
                    _finalTarget.name = "[AVProMovieCapture] 360 Final Target";
                }

                // Create the per-face render target (only when need to support GUI rendering)
                if (_faceTarget != null)
                {
                    _faceTarget.DiscardContents();
                    if (_faceTarget.width != _cubemapResolution || _faceTarget.height != _cubemapResolution || _faceTarget.depth != _cubemapDepth || aaLevel != _faceTarget.antiAliasing)
                    {
                        RenderTexture.Destroy(_faceTarget);
                        _faceTarget = null;
                    }
                }
                if (cubemapRenderMethod == CubemapRenderMethod.Manual)
                {
                    if (_faceTarget == null)
                    {
                        _faceTarget                  = new RenderTexture(_cubemapResolution, _cubemapResolution, _cubemapDepth, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
                        _faceTarget.name             = "[AVProMovieCapture] 360 Face Target";
                        _faceTarget.isPowerOfTwo     = true;
                        _faceTarget.wrapMode         = TextureWrapMode.Clamp;
                        _faceTarget.filterMode       = FilterMode.Bilinear;
                        _faceTarget.autoGenerateMips = false;
                        _faceTarget.antiAliasing     = aaLevel;
                    }

                    _cubemapToEquirectangularMaterial.SetFloat(_propFlipX, 0.0f);
                }
                else
                {
                    // Unity's RenderToCubemap result needs flipping
                    _cubemapToEquirectangularMaterial.SetFloat(_propFlipX, 1.0f);
                }

                _cubemapToEquirectangularMaterial.DisableKeyword("USE_ROTATION");
                _cubemapToEquirectangularMaterial.DisableKeyword("STEREOPACK_TOP");
                _cubemapToEquirectangularMaterial.DisableKeyword("STEREOPACK_BOTTOM");
                _cubemapToEquirectangularMaterial.DisableKeyword("STEREOPACK_LEFT");
                _cubemapToEquirectangularMaterial.DisableKeyword("STEREOPACK_RIGHT");

                if (_render180Degrees)
                {
                    _cubemapToEquirectangularMaterial.DisableKeyword("LAYOUT_EQUIRECT360");
                    _cubemapToEquirectangularMaterial.EnableKeyword("LAYOUT_EQUIRECT180");
                }
                else
                {
                    _cubemapToEquirectangularMaterial.DisableKeyword("LAYOUT_EQUIRECT180");
                    _cubemapToEquirectangularMaterial.EnableKeyword("LAYOUT_EQUIRECT360");
                }

                // Create the cube render target
                int cubeDepth = 0;
                if (cubemapRenderMethod != CubemapRenderMethod.Manual)
                {
                    cubeDepth = _cubemapDepth;
                }
                int cubeAA = 1;
                if (cubemapRenderMethod != CubemapRenderMethod.Manual)
                {
                    cubeAA = aaLevel;
                }
                if (_cubeTarget != null)
                {
                    _cubeTarget.DiscardContents();
                    if (_cubeTarget.width != _cubemapResolution || _cubeTarget.height != _cubemapResolution || _cubeTarget.depth != cubeDepth || cubeAA != _cubeTarget.antiAliasing)
                    {
                        RenderTexture.Destroy(_cubeTarget);
                        _cubeTarget = null;
                    }
                }
                if (_cubeTarget == null)
                {
                    _cubeTarget              = new RenderTexture(_cubemapResolution, _cubemapResolution, cubeDepth, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
                    _cubeTarget.name         = "[AVProMovieCapture] 360 Cube Target";
                    _cubeTarget.isPowerOfTwo = true;

#if AVPRO_MOVIECAPTURE_RENDERTEXTUREDIMENSIONS_54
                    _cubeTarget.dimension = UnityEngine.Rendering.TextureDimension.Cube;
#else
                    _cubeTarget.isCubemap = true;
#endif

                    _cubeTarget.useMipMap        = false;
                    _cubeTarget.autoGenerateMips = false;
                    _cubeTarget.antiAliasing     = cubeAA;
                    _cubeTarget.wrapMode         = TextureWrapMode.Clamp;
                    _cubeTarget.filterMode       = FilterMode.Bilinear;
                }

                if (_useMotionBlur)
                {
                    _motionBlurCameras    = new Camera[1];
                    _motionBlurCameras[0] = _camera;
                }
            }

            SelectRecordingResolution(finalWidth, finalHeight);

            GenerateFilename();

            return(base.PrepareCapture());
        }
Ejemplo n.º 2
0
        private void UpdateTexture()
        {
            // In Direct3D the RT can be flipped vertically

            /*if (source.texelSize.y < 0)
             * {
             *
             * }*/

            //_cubeCamera.transform.position = _camera.transform.position;
            //_cubeCamera.transform.rotation = _camera.transform.rotation;

            Camera camera = _camera;

            _cubeTarget.DiscardContents();
            _finalTarget.DiscardContents();

            CubemapRenderMethod cubemapRenderMethod = GetCubemapRenderingMethod();

            if (_stereoRendering == StereoPacking.None)
            {
                if (cubemapRenderMethod == CubemapRenderMethod.Unity)
                {
                                        #if AVPRO_MOVIECAPTURE_UNITYBUG_RENDERTOCUBEMAP_56
                    RenderTexture prev = camera.targetTexture;
                                        #endif
                    // Note: Camera.RenderToCubemap() doesn't support camera rotation
                    camera.RenderToCubemap(_cubeTarget, 63);

                                        #if AVPRO_MOVIECAPTURE_UNITYBUG_RENDERTOCUBEMAP_56
                    // NOTE: We need this to clean up the state in at least Unity 5.6.0 - 5.6.1p1
                    camera.targetTexture = prev;
                                        #endif
                }
                else if (cubemapRenderMethod == CubemapRenderMethod.Manual)
                {
                    RenderCameraToCubemap(camera, _cubeTarget);
                }
                RenderCubemapToEquiRect(_cubeTarget, _finalTarget, false, Quaternion.identity, true);
            }
            else
            {
                                #if AVPRO_MOVIECAPTURE_UNITY_STEREOCUBEMAP_RENDER
                if (cubemapRenderMethod == CubemapRenderMethod.Unity2018)
                {
                    //Left eye
                    camera.stereoSeparation = _ipd;
                    camera.RenderToCubemap(_cubeTarget, 63, Camera.MonoOrStereoscopicEye.Left);
                    RenderCubemapToEquiRect(_cubeTarget, _finalTarget, false, camera.transform.rotation, true);

                    // Right eye
                    _cubeTarget.DiscardContents();
                    camera.RenderToCubemap(_cubeTarget, 63, Camera.MonoOrStereoscopicEye.Right);
                    RenderCubemapToEquiRect(_cubeTarget, _finalTarget, false, camera.transform.rotation, false);
                }
                else
                                #endif
                if (cubemapRenderMethod == CubemapRenderMethod.Manual)
                {
                    // Save camera state
                    Vector3 cameraPosition = camera.transform.localPosition;

                    // Left eye
                    camera.transform.Translate(new Vector3(-_ipd / 2f, 0f, 0f), Space.Self);
                    RenderCameraToCubemap(camera, _cubeTarget);
                    RenderCubemapToEquiRect(_cubeTarget, _finalTarget, false, Quaternion.identity, true);

                    // Right eye
                    camera.transform.localPosition = cameraPosition;
                    camera.transform.Translate(new Vector3(_ipd / 2f, 0f, 0f), Space.Self);
                    RenderCameraToCubemap(camera, _cubeTarget);
                    RenderCubemapToEquiRect(_cubeTarget, _finalTarget, false, Quaternion.identity, false);

                    // Restore camera state
                    camera.transform.localPosition = cameraPosition;
                }
            }
        }