Search Unity

Help with shader to undistort webcam image

Discussion in 'Shaders' started by doktor3d, Oct 4, 2013.

  1. doktor3d

    doktor3d

    Joined:
    May 7, 2013
    Posts:
    9
    I'm quite new to shaders and I'm having some trouble implementing this Radial Undistortion example from GPU Gems. The shader uses the lens distortion coefficients generated during camera calibration to correct geometric distortion in real-time.

    I'm working from the Fisheye example, which distorts a scene camera's view to simulate a fisheye lens.

    The shader that I have written is as follows:
    Code (csharp):
    1. Shader "Custom/CameraUndistort" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.             ZTest Always Cull Off ZWrite Off
    10.             Fog { Mode off }
    11.  
    12. CGPROGRAM
    13. #pragma vertex vert
    14. #pragma fragment frag
    15. #pragma fragmentoption ARB_precision_hint_fastest
    16.  
    17. #include "UnityCG.cginc"
    18.    
    19.     struct v2f {
    20.         float4 pos : POSITION;
    21.         float2 uv : TEXCOORD0;
    22.     };
    23.    
    24.     uniform sampler2D _MainTex;
    25.    
    26.     v2f vert( appdata_img v )
    27.     {
    28.         v2f o;
    29.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    30.         o.uv = v.texcoord.xy;
    31.         return o;
    32.     }
    33.    
    34.     float4 frag(v2f i) : COLOR
    35.     {
    36.         float2 coords = i.uv;
    37.         float4 pos = i.pos;
    38.        
    39.         // known constants for the particular lens and sensor
    40.         float f = 368.28488;  // focal length
    41.         float ox = 147.54834; // principal point, x axis
    42.         float oy = 126.01673; // principal point, y axis
    43.         float k1 = 0.4142;    // constant for radial distortion correction
    44.         float k2 = 0.40348;
    45.         float2 xy = (coords - float2(ox, oy))/float2(f,f);
    46.        
    47.         float r = sqrt(dot(xy,xy));
    48.         float r2 = r * r;
    49.         float r4 = r2 * r2;
    50.         float coeff = (k1 * r2 + k2 * r4);
    51.        
    52.         // add the calculated offsets to the current texture coordinates
    53.         xy = ((xy + xy * coeff.xx) * f.xx) + float2(ox, oy);
    54.        
    55.         // look up the texture at the corrected coordinates and output the color
    56.         return texRECT(_MainTex, xy);      
    57.     }
    58.    
    59.     ENDCG
    60.    
    61.         }
    62.     }
    63.  
    64. Fallback off
    65.    
    66. }
    And my associated script attached to the camera is as follows:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. [RequireComponent (typeof(Camera))]
    6. [AddComponentMenu ("Image Effects/CameraUndistort")]
    7.  
    8. public class CameraUndistort : PostEffectsBase {
    9.    
    10.     public Shader CameraUndistortShader = null;
    11.     public Material CameraUndistortMaterial = null;
    12.    
    13.     bool CheckResources (){
    14.         CheckSupport (false);
    15.         CameraUndistortMaterial = CheckShaderAndCreateMaterial(CameraUndistortShader,CameraUndistortMaterial);
    16.         if(!isSupported)
    17.             ReportAutoDisable ();
    18.         return isSupported;        
    19.     }
    20.    
    21.     void OnRenderImage (RenderTexture source, RenderTexture destination){      
    22.         if(CheckResources()==false) {
    23.             Graphics.Blit (source, destination);
    24.             return;
    25.         }
    26.                
    27.         Graphics.Blit (source, destination, CameraUndistortMaterial);  
    28.     }
    29. }
    At the moment the result I get is my scene camera showing completely white. I've tried a number of alterations to both the shader and the script, but I'm obviously doing some things wrong. I may not be connecting the shader output with the material on the script or something like that. Any help would be much appreciated.
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Check your pm, i sent you the solution.
     
  3. danielpiressm

    danielpiressm

    Joined:
    Mar 26, 2014
    Posts:
    1
    I think im having the same problem. Could u send me the solution as well?

    Thanks :)
     
  4. marisonina

    marisonina

    Joined:
    Aug 4, 2014
    Posts:
    1
    I am also struggling with this issue. Could you help me out as well, or point me to the right direction, please? Thank You.
     
  5. Zamahra

    Zamahra

    Joined:
    Sep 18, 2014
    Posts:
    1
    I've got a shader which is a little different to this script, but I also have the same problems. This shader renders a WebcamTexture and ever
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    The shader is a part of my "TOZ Image Effects" pack which is available in the assetstore.
     
    Last edited: Sep 7, 2018
  7. liangyw

    liangyw

    Joined:
    Sep 29, 2016
    Posts:
    7
    I tried to find your pack from the asset store. it seems no longer available. Can you please let me know where to find it? Thank you!
     
  8. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    It is one of the effects in my TOZ Image Effects pack.
     
  9. liangyw

    liangyw

    Joined:
    Sep 29, 2016
    Posts:
    7
    Thank you for your reply! your package is very cool! I am wondering if you have shaders for the following segmentation effect, the segmentation depends on different material. If you have it, please let me know and I am willing to pay for it. Thank you! e.g.
     
  10. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I am not sure if i understand segmentation effect from the video, it just renders every object (group of objects) with different colors. And if thats the thing you need, its a simple shader. But if im missing something else here, let me know through private message and ill help.