Advertisement
ryanjon2040

[UE4] shadertoy.com/view/wsscD4

Oct 27th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. // North Star Calibration Utilities
  4. // Screen UV to Rectilinear Coefficients
  5. float left_uv_to_rect_x[16]  = { -0.7530364531010308,  0.8806592947908687, -0.8357813137161849,   0.3013989721607643,  0.9991764544369446, -0.2578159567698274,  0.3278667335649757, -0.4602577277109663,  -0.23980700925448195, -0.056891370605734376, -0.1248008903440144,  0.7641381600051023,   0.20935445281014292, -0.06256983016261788,  0.25844580123833516, -0.5098143951663658 };
  6. float left_uv_to_rect_y[16]  = {  0.5612597403791647, -1.1899589356849427,  0.4652815794139322,  -0.2737933233160801,  0.3774305703820774, -0.8110333901413378,  1.2705775357104372, -0.7461290557575936,  -0.19222925521894155,  0.936404121235537,    -1.7109388784623627,  0.9147182510080394,   0.33073407860855586, -1.1463700238163494,   1.4965795269835196,  -0.7090919632511286 };
  7. float right_uv_to_rect_x[16]  = { -0.2117125319456463, -0.432262579698108,   0.41675063901331316, -0.14650788483832153, 1.0941580384494245, -0.30628109185189906, 0.109119134429531,   0.11642874201014344, -0.2761527408488216,  -0.4335709010559027,    0.9626491769528918, -0.5572405188216735,   0.18342869894719088,  0.37981945016058366, -0.8718621504058989,   0.5218968716935535 };
  8. float right_uv_to_rect_y[16]  = {  1.0129568069314265, -2.110976542118192,   1.4108474581893895,  -0.7746290913232183, -0.746419837008027,   1.747642287758405, - 1.5753294007072252,  0.7143402603200871,   0.5607717274125551,  -1.5019493985594772,    1.2539128525783017, -0.42999735712430215, -0.21517910830152714,  0.5965062719847273,  -0.5664205050494074,   0.18545738302854597};
  9.  
  10.  
  11.  
  12. struct Functions
  13. {
  14.     // Evaluate a 2D polynomial from its coefficients
  15.     float polyval2d(float X, float Y, float C[16])
  16.     {
  17.         float X2 = X * X; float X3 = X2 * X;
  18.         float Y2 = Y * Y; float Y3 = Y2 * Y;
  19.         return  (((C[ 0]     ) + (C[ 1]      * Y) + (C[ 2]      * Y2) + (C[ 3]      * Y3)) +
  20.                 ((C[ 4] * X ) + (C[ 5] * X  * Y) + (C[ 6] * X  * Y2) + (C[ 7] * X  * Y3)) +
  21.                 ((C[ 8] * X2) + (C[ 9] * X2 * Y) + (C[10] * X2 * Y2) + (C[11] * X2 * Y3)) +
  22.                 ((C[12] * X3) + (C[13] * X3 * Y) + (C[14] * X3 * Y2) + (C[15] * X3 * Y3)));
  23.     }
  24.  
  25.     // Calculate the direction a ray takes exiting the eye on its way towards this screen UV
  26.     float3 uvToRayDirection(float2 uv, float  left_uv_to_rect_x[16], float  left_uv_to_rect_y[16],
  27.                                float right_uv_to_rect_x[16], float right_uv_to_rect_y[16])
  28.     {
  29.             float2 screenUV = float2(fmod((2.0 * (1.0-uv.x)), 1.0), uv.y); // Calculate the Screen-Space UV
  30.             float xCoeffs[16] = left_uv_to_rect_x; if(uv.x < 0.5) { xCoeffs = right_uv_to_rect_x; }
  31.             float yCoeffs[16] = left_uv_to_rect_y; if(uv.x < 0.5) { yCoeffs = right_uv_to_rect_y; }
  32.             return float3(polyval2d(screenUV.x, screenUV.y, xCoeffs),
  33.                 polyval2d(screenUV.x, screenUV.y, yCoeffs), 1.0);
  34.     }
  35.  
  36.     // Calculate the origin of a ray given a shader UV
  37.     float3 uvToRayOrigin(float2 uv)
  38.     {
  39.             return float3(0.032 * ((uv.x > 0.5) ? -1.0 : 1.0), 0.0, 0.0);
  40.     }
  41.  
  42.     float4 Out()
  43.     {
  44.     // Calculate the ray origin and direction
  45.     float3 ray_origin = uvToRayOrigin(uv);
  46.     float3 ray_dir    = uvToRayDirection(uv, left_uv_to_rect_x,
  47.                                            left_uv_to_rect_y,
  48.                                            right_uv_to_rect_x,
  49.                                            right_uv_to_rect_y);
  50.  
  51.     // Get the point on a virtual plane 15cm away
  52.     float3 plane_pos = ray_origin + ray_dir * 0.15;
  53.    
  54.     // Create a Checkerboard Pattern
  55.     float2 Pos = floor(plane_pos.xy * 100.0);
  56.     float PatternMask = fmod(Pos.x + fmod(Pos.y, 2.0), 2.0);
  57.     return float4(float3(PatternMask, PatternMask, PatternMask),1.0);
  58.     }
  59. };
  60.  
  61. Functions f;
  62. return f.Out();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement