Rotate a RenderTexture via Shader

Hi, i have a shader that implement the anaglyph stereoscopy starting from two renderTexture. Now i need to keep rotate the output image of given angle, provided by the gyroscope of a mobile device, to let the screen image be always oriented by the user view point. Is that possible to achieve this? Here the code:

Shader "Colour Balanced Anaglyph Parametric" {
Properties {
   _LeftTex ("Left (RGB)", RECT) = "white" {}
   _RightTex ("Right (RGB)", RECT) = "white" {}
   _Balance_Left_R         ("Balance Left R", Vector) = (0, 0, 0, 0)
   _Balance_Left_G         ("Balance Left G", Vector) = (0, 0, 0, 0)
   _Balance_Left_B         ("Balance Left B", Vector) = (0, 0, 0, 0)
  
   _Balance_Right_R     ("Balance Right R", Vector) = (0, 0, 0, 0)
   _Balance_Right_G        ("Balance Right G", Vector) = (0, 0, 0, 0)
   _Balance_Right_B        ("Balance Right B", Vector) = (0, 0, 0, 0)
}

SubShader {
   Pass {
      ZTest Always Cull Off ZWrite Off
      Fog { Mode off }

      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #pragma fragmentoption ARB_precision_hint_fastest
      #include "UnityCG.cginc"
     
      uniform sampler2D _LeftTex;
      uniform sampler2D _RightTex;
     
      uniform float4 _Balance_Left_R;
      uniform float4 _Balance_Left_G;
      uniform float4 _Balance_Left_B;
     
      uniform float4 _Balance_Right_R;
      uniform float4 _Balance_Right_G;
      uniform float4 _Balance_Right_B;
     
      struct v2f {
         float4 pos : POSITION;
         float2 uv : TEXCOORD0;
      };
     
      v2f vert( appdata_img v )
      {
         v2f o;
         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
         float2 uv = MultiplyUV( UNITY_MATRIX_TEXTURE0, v.texcoord );
         o.uv = uv;
         return o;
      }
     
      half4 frag (v2f i) : COLOR
      {
         float ra, ga, ba, r1, g1, b1, r2, g2, b2;
         float4 texL = tex2D(_LeftTex, i.uv);
         float4 texR = tex2D(_RightTex, i.uv);
         float4 texRGB;
        
         r1    =    texL.r * _Balance_Left_R[0] + texL.g * _Balance_Left_R[1] + texL.b * _Balance_Left_R[2];
         g1    =    texL.r * _Balance_Left_G[0] + texL.g * _Balance_Left_G[1] + texL.b * _Balance_Left_G[2];
         b1    =    texL.r * _Balance_Left_B[0] + texL.g * _Balance_Left_B[1] + texL.b * _Balance_Left_B[2];
        
         r2    =    texR.r * _Balance_Right_R[0] + texR.g * _Balance_Right_R[1] + texR.b * _Balance_Right_R[2];
         g2    =    texR.r * _Balance_Right_G[0] + texR.g * _Balance_Right_G[1] + texR.b * _Balance_Right_G[2];
         b2    =    texR.r * _Balance_Right_B[0] + texR.g * _Balance_Right_B[1] + texR.b * _Balance_Right_B[2];
       
        
         texRGB = float4(r1+r2,g1+g2,b1+b2,1);
         return texRGB;
      }
      ENDCG
   }
}  
   Fallback off
}

Sorry for bad english

For one thing this shader can be more optimized. Instead of:

r1    =    texL.r * _Balance_Left_R[0] + texL.g * _Balance_Left_R[1] + texL.b * _Balance_Left_R[2];

Use:

r1    =    dot(texL.rgb, _Balance_Left_R.rgb);

Rotating a texture in a shader is quite easy, but in this case I don’t see why you would. Instead, just rotate the camera’s for the left and right eye based on the gyroscope. Then the RenderTextures for the left and right eyes should fit perfectly and all this shader needs to do is a simple compose.

Oh, one more optimization. Combine r1, g1 and b1 into a float3 rgb1, so you can add rgb1 and rgb2 in one addition. And make texL and texR float3’s too.

1 Like

Thanks for the reply. The reason i cant rotate the two cameras is that they have a custom oblique frustum, this for achieve an anamorphic projection. Viewing the screen in a fixed position in real world make an illusion of geometry protruding from the screen. If i rotate the camera, then also the fixed view point rotate and the illusion break.

If you rotate the RenderTextures, they won’t fit the screen and the illusion will also break. No matter what the projection matrix is, you have to adjust the camera matrix to mimic the two eyes you’re virtually looking through. I also wonder what you mean with anamorphic projection, because it actually means an adjusted aspect ratio, which has no need for a custom oblique frustum.

oh, i see. So i cant achieve my goal. For the anamorphic projection a example can be found here: Using Projection Matrix to Create Holographic Effect - Unity Engine - Unity Discussions

After some thinking, now i get what you mean about the camera matrix. Thank you so much!