Пример #1
0
        private void SetupCamera(CamSettings settings)
        {
            var aspect = settings.aspect;

            zoomLevel = Mathf.Max(0.05f, Mathf.Abs(zoomLevel)) * Math.Sign(zoomLevel);
            // "Physical" pixel render size
            Vector2 screenRenderSize = GetScreenRenderSize();

            // Pixel render size
            int[] pixelRenderSize = GetRenderTextureSize(screenRenderSize, settings.aspect);

            float targetAspect = (float)pixelRenderSize[0] / (float)pixelRenderSize[1];

            cam.aspect = targetAspect;

            if (cam.orthographic)
            {
                // Orthographic camera needs to use screen size when calculating quad offset
                screenRenderSize = new Vector2(Screen.width, Screen.height);

                // Set the camera's size, according to pixel size
                float targetHeight = pixelRenderSize[1];
                // Use pixel density to convert to world units
                targetHeight /= pixelsPerUnit;
                targetHeight /= 2f;
                // Change orthographic size so camera is sized to world unit
                cam.orthographicSize = targetHeight;
            }

            // Find the settings to be used for drawing the GL quad
            CalculateQuad(screenRenderSize, pixelRenderSize);

            // Important to release current render texture
            cam.targetTexture = null;
            fallbackMaterial.SetTexture("_MainTex", null);
            if (advancedSettings != null && advancedSettings.cameraMaterial != null)
            {
                advancedSettings.cameraMaterial.SetTexture("_MainTex", null);
            }
            if (renderTexture != null)
            {
                renderTexture.Release();
            }

            // Create new render texture
            Vector2 renderSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) / settings.downsample;

            int[] actualRenderSize = GetRenderTextureSize(renderSize, Vector2.one);
            renderTexture = new RenderTexture(actualRenderSize[0], actualRenderSize[1], 0)
            {
                useMipMap  = true,
                filterMode = FilterMode.Point,
                wrapMode   = TextureWrapMode.Clamp
            };
            // Make main camera render into it
            cam.targetTexture = renderTexture;

            // Set render texture as _MainTex on materials
            fallbackMaterial.SetTexture("_MainTex", renderTexture);
            if (advancedSettings != null)
            {
                CameraMaterial = advancedSettings.cameraMaterial;
            }

            lastSettings = settings;

            cam.Render();
            camDraw.DrawQuad();
        }
Пример #2
0
        protected void SetupCamera()
        {
            var aspect = AspectStretch;

            zoomLevel = Mathf.Max(0.05f, Mathf.Abs(zoomLevel)) * Math.Sign(zoomLevel);
            // "Physical" render size
            Vector2 screenRenderSize = GetScreenRenderSize();

            // Pixel render size
            int[] pixelRenderSize = GetRenderTextureSize(screenRenderSize, aspect);

            // Find the settings to be used for drawing the GL quad
            Vector2 pixelSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) * zoomLevel;

            if (cam.orthographic)
            {
                // Orthographic camera needs to use screen size when calculating quad offset
                screenRenderSize = new Vector2(Screen.width, Screen.height);

                // Set the camera's size, according to pixel size
                float targetHeight = pixelRenderSize[1];
                // Use pixel density to convert to world units
                targetHeight /= pixelsPerUnit;
                targetHeight /= 2f;
                // Change orthographic size so camera is sized to world unit
                cam.orthographicSize = targetHeight;
            }

            quadOffset    = pixelSize - screenRenderSize;
            quadOffset   /= 2;
            quadOffset.x /= Screen.width;
            quadOffset.y /= Screen.height;

            // Set camera aspect ratio, since pixel aspect ratio will be different from current aspect
            float targetAspect = (float)pixelRenderSize[0] / (float)pixelRenderSize[1];

            cam.aspect = targetAspect;

            // Important to release current render texture
            cam.targetTexture = null;
            fallbackMaterial.SetTexture("_MainTex", null);
            if (advancedSettings != null && advancedSettings.cameraMaterial != null)
            {
                advancedSettings.cameraMaterial.SetTexture("_MainTex", null);
            }
            if (renderTexture != null)
            {
                renderTexture.Release();
            }

            // Create new render texture
            renderTexture = new RenderTexture(pixelRenderSize[0], pixelRenderSize[1], 0)
            {
                useMipMap  = true,
                filterMode = FilterMode.Point,
                wrapMode   = TextureWrapMode.Clamp
            };
            // Make main camera render into it
            cam.targetTexture = renderTexture;

            // Set render texture as _MainTex on materials
            fallbackMaterial.SetTexture("_MainTex", renderTexture);
            if (advancedSettings != null)
            {
                CameraMaterial = advancedSettings.cameraMaterial;
            }

            lastSettings = new CamSettings(aspect, zoomLevel, cam.fieldOfView, cam.orthographic);

            cam.Render();
            camDraw.DrawQuad();
        }