Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recording video with custom resolution and custom viewport #5

Open
julapy opened this issue Nov 1, 2017 · 9 comments
Open

recording video with custom resolution and custom viewport #5

julapy opened this issue Nov 1, 2017 · 9 comments

Comments

@julapy
Copy link

julapy commented Nov 1, 2017

hey there,
have been working with your plugin and finding it very clear and easy to follow with your documentation in the code. really good job :)

im currently trying to record a video at 640 x 640 resolution, only a section of the screen, not the whole thing. please see reference image below.

ive done this previously using other frameworks by first capturing the entire screen into a framebuffer (render texture in unity) and then rendering that into a smaller 640 x 640 framebuffer by scaling and positioning the first texture in the new 640 x 640 viewport.

i can see this being done to some extent in iVidCapProVideo.cs class, but from what i can see it only performs scaling into the render texture, but does not support cropping.

would be a nice feature for iVidCapPro if you could point me to how this could be done.

cheers,
L.

iphone-6-wireframe

@ThisNetWorks
Copy link
Collaborator

Are you recording on a 3d canvas, or game object? An alternative solution to what you're proposing might be to put a second camera in the scene and record from that at the resolution you want

@julapy
Copy link
Author

julapy commented Nov 1, 2017

hmm yeah good point... but wouldn't that require rendering the scene twice? which is something im trying to avoid...

@Akevlam
Copy link
Collaborator

Akevlam commented Nov 1, 2017

Glad you're finding the code docs to be helpful!

It seems to me that what you want to achieve might be possible by adjusting the scale and offset passed to Graphics.Blit in iVidCapProVideo.cs. I'm not in a position to test that right now, but you might try hardcoding some values in that call on line 203 and see what happens. If that works, I'd suggest a new method in the file, something like SetCaptureRegion, that enables those values to be set.

Another alternative, though it would get considerably more messy, would be to do it on the native plugin side. I'm pretty sure the embedded GPUImage code includes a Crop filter that could be pressed into service.

@julapy
Copy link
Author

julapy commented Nov 1, 2017

yeah ok, ill try tweaking some values inside iVidCapProVideo.cs

@julapy
Copy link
Author

julapy commented Nov 3, 2017

after some testing it appears the below code for changing the viewport, doesn't do anything.
even if i comment out the viewport change completely, the source render texture is always scaled to fit the rt render texture.

GL.PushMatrix();
GL.LoadPixelMatrix();
GL.Viewport(captureRect);
			
Graphics.Blit (source, rt);
			
GL.PopMatrix();

@Akevlam
Copy link
Collaborator

Akevlam commented Nov 4, 2017

Did you alter the Viewport Rect on the capture camera? It is possible the operation of Graphics.Blit has changed since this was implemented. Sounds like investigation is needed in order to determine if this is still needed.

I was referring to a different possible approach to achieving your goal. As of Unity 2017, Graphics.Blit now accepts scale and offset parameters. Like this:

Graphics.Blit(source, rt, new Vector2(0.5f, 0.5f), new Vector2(0.25f, 0.25f));

Some experimentation leads me to believe you may be able to achieve the effect you want by the proper setting of these parameters.

@julapy
Copy link
Author

julapy commented Nov 5, 2017

hey @Akevlam,
for this project we're using 5.6.3 so that new Blit function is not an option unfortunately.
but i have been using Graphics.Blit(source, rt, material)
and using a shader to resample from the screen render texture, to a custom sized render texture.
seems to be working... just need to clean it up.
do you have another repo for the unity cs code so i can add a pull request?

@julapy
Copy link
Author

julapy commented Nov 5, 2017

here is the shader im using for resampling the screen render texture into a custom sized render texture.


Shader "Custom/ividcappro" {

	Properties {
		_MainTex ("", 2D) = "white" {}
		_CornerX1 ("CornerX1", Range(0,1)) = 0.0
		_CornerX2 ("CornerX2", Range(0,1)) = 1.0
		_CornerY1 ("CornerY1", Range(0,1)) = 0.0
		_CornerY2 ("CornerY2", Range(0,1)) = 0.5
	}
 
	SubShader {
 
		ZTest Always Cull Off ZWrite Off Fog { Mode Off } //Rendering settings
 
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc" 
			//we include "UnityCG.cginc" to use the appdata_img struct
    
			struct v2f {
				float4 pos : POSITION;
				half2 uv : TEXCOORD0;
			};
   
			v2f vert (appdata_img v){
   				v2f o;
   				o.pos = UnityObjectToClipPos (v.vertex);
   				o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
   				return o; 
  			}
    
  			sampler2D _MainTex; //Reference in Pass is necessary to let us use this variable in shaders
  			float _CornerX1;
  			float _CornerX2;
  			float _CornerY1;
  			float _CornerY2;
    
			fixed4 frag (v2f i) : COLOR {

				float tx = lerp(_CornerX1, _CornerX2, i.uv.x);
				float ty = lerp(1.0 - _CornerY1, 1.0 - _CornerY2, 1.0 - i.uv.y);
				fixed4 col = tex2D(_MainTex, fixed2(tx, ty));
				return col;
			}
  			ENDCG
		}
	} 
	FallBack "Diffuse"
}

inside iVidCapProVideo Awake() im adding:

mat = new Material( Shader.Find("Custom/ividcappro") );

and passing in the captureRect as normalized screen coordinates

float cornerX1 = captureRect.x;
float cornerY1 = captureRect.y;
float cornerX2 = captureRect.x + captureRect.width;
float cornerY2 = captureRect.y + captureRect.height;

mat.SetFloat("_CornerX1", cornerX1);
mat.SetFloat("_CornerY1", cornerY1);
mat.SetFloat("_CornerX2", cornerX2);
mat.SetFloat("_CornerY2", cornerY2);

Graphics.Blit (source, rt, mat);

@ThisNetWorks
Copy link
Collaborator

Glad to hear you sorted it.

I haven't split them out into seperate repo's at this stage - the c# source is in this repo at
https://github.com/ThisNetWorks/iVidCapPro/tree/master/iVidCapProUnityProject

so if you were to make your changes to that code, and put a pull request in that would be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants