Advertisement
Guest User

Untitled

a guest
May 10th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2.  
  3. Shader "Skybox/Panoramic" {
  4. Properties {
  5.     _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
  6.     [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
  7.     _Rotation ("Rotation", Range(0, 360)) = 0
  8.     [NoScaleOffset] _MainTex ("Spherical  (HDR)", 2D) = "grey" {}
  9.     [KeywordEnum(6 Frames Layout, Latitude Longitude Layout)] _Mapping("Mapping", Float) = 1
  10.     [Enum(360 Degrees, 0, 180 Degrees, 1)] _ImageType("Image Type", Float) = 0
  11.     [Toggle] _MirrorOnBack("Mirror on Back", Float) = 0
  12.     [Enum(None, 0, Side by Side, 1, Over Under, 2)] _Layout("3D Layout", Float) = 0
  13. }
  14.  
  15. SubShader {
  16.     Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
  17.     Cull Off ZWrite Off
  18.  
  19.     Pass {
  20.  
  21.         CGPROGRAM
  22.         #pragma vertex vert
  23.         #pragma fragment frag
  24.         #pragma target 2.0
  25.         #pragma multi_compile __ _MAPPING_6_FRAMES_LAYOUT
  26.  
  27.         #include "UnityCG.cginc"
  28.  
  29.         sampler2D _MainTex;
  30.         float4 _MainTex_TexelSize;
  31.         half4 _MainTex_HDR;
  32.         half4 _Tint;
  33.         half _Exposure;
  34.         float _Rotation;
  35. #ifndef _MAPPING_6_FRAMES_LAYOUT
  36.         bool _MirrorOnBack;
  37.         int _ImageType;
  38.         int _Layout;
  39. #endif
  40.  
  41. #ifndef _MAPPING_6_FRAMES_LAYOUT
  42.         inline float2 ToRadialCoords(float3 coords)
  43.         {
  44.             float3 normalizedCoords = normalize(coords);
  45.             float latitude = acos(normalizedCoords.y);
  46.             float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
  47.             float2 sphereCoords = float2(longitude, latitude) * float2(0.5/UNITY_PI, 1.0/UNITY_PI);
  48.             return float2(0.5,1.0) - sphereCoords;
  49.         }
  50. #endif
  51.  
  52. #ifdef _MAPPING_6_FRAMES_LAYOUT
  53.         inline float2 ToCubeCoords(float3 coords, float3 layout, float4 edgeSize, float4 faceXCoordLayouts, float4 faceYCoordLayouts, float4 faceZCoordLayouts)
  54.         {
  55.             // Determine the primary axis of the normal
  56.             float3 absn = abs(coords);
  57.             float3 absdir = absn > float3(max(absn.y,absn.z), max(absn.x,absn.z), max(absn.x,absn.y)) ? 1 : 0;
  58.             // Convert the normal to a local face texture coord [-1,+1], note that tcAndLen.z==dot(coords,absdir)
  59.             // and thus its sign tells us whether the normal is pointing positive or negative
  60.             float3 tcAndLen = mul(absdir, float3x3(coords.zyx, coords.xzy, float3(-coords.xy,coords.z)));
  61.             tcAndLen.xy /= tcAndLen.z;
  62.             // Flip-flop faces for proper orientation and normalize to [-0.5,+0.5]
  63.             bool2 positiveAndVCross = float2(tcAndLen.z, layout.x) > 0;
  64.             tcAndLen.xy *= (positiveAndVCross[0] ? absdir.yx : (positiveAndVCross[1] ? float2(absdir[2],0) : float2(0,absdir[2]))) - 0.5;
  65.             // Clamp values which are close to the face edges to avoid bleeding/seams (ie. enforce clamp texture wrap mode)
  66.             tcAndLen.xy = clamp(tcAndLen.xy, edgeSize.xy, edgeSize.zw);
  67.             // Scale and offset texture coord to match the proper square in the texture based on layout.
  68.             float4 coordLayout = mul(float4(absdir,0), float4x4(faceXCoordLayouts, faceYCoordLayouts, faceZCoordLayouts, faceZCoordLayouts));
  69.             tcAndLen.xy = (tcAndLen.xy + (positiveAndVCross[0] ? coordLayout.xy : coordLayout.zw)) * layout.yz;
  70.             return tcAndLen.xy;
  71.         }
  72. #endif
  73.  
  74.         float3 RotateAroundYInDegrees (float3 vertex, float degrees)
  75.         {
  76.             float alpha = degrees * UNITY_PI / 180.0;
  77.             float sina, cosa;
  78.             sincos(alpha, sina, cosa);
  79.             float2x2 m = float2x2(cosa, -sina, sina, cosa);
  80.             return float3(mul(m, vertex.xz), vertex.y).xzy;
  81.         }
  82.  
  83.         struct appdata_t {
  84.             float4 vertex : POSITION;
  85.             UNITY_VERTEX_INPUT_INSTANCE_ID
  86.         };
  87.  
  88.         struct v2f {
  89.             float4 vertex : SV_POSITION;
  90.             float3 texcoord : TEXCOORD0;
  91. #ifdef _MAPPING_6_FRAMES_LAYOUT
  92.             float3 layout : TEXCOORD1;
  93.             float4 edgeSize : TEXCOORD2;
  94.             float4 faceXCoordLayouts : TEXCOORD3;
  95.             float4 faceYCoordLayouts : TEXCOORD4;
  96.             float4 faceZCoordLayouts : TEXCOORD5;
  97. #else
  98.             float2 image180ScaleAndCutoff : TEXCOORD1;
  99.             float4 layout3DScaleAndOffset : TEXCOORD2;
  100. #endif
  101.             UNITY_VERTEX_OUTPUT_STEREO
  102.         };
  103.  
  104.         v2f vert (appdata_t v)
  105.         {
  106.             v2f o;
  107.             UNITY_SETUP_INSTANCE_ID(v);
  108.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  109.             float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
  110.             o.vertex = UnityObjectToClipPos(rotated);
  111.             o.texcoord = v.vertex.xyz;
  112. #ifdef _MAPPING_6_FRAMES_LAYOUT
  113.             // layout and edgeSize are solely based on texture dimensions and can thus be precalculated in the vertex shader.
  114.             float sourceAspect = float(_MainTex_TexelSize.z) / float(_MainTex_TexelSize.w);
  115.             // Use the halfway point between the 1:6 and 3:4 aspect ratios of the strip and cross layouts to
  116.             // guess at the correct format.
  117.             bool3 aspectTest =
  118.                 sourceAspect >
  119.                 float3(1.0, 1.0f / 6.0f + (3.0f / 4.0f - 1.0f / 6.0f) / 2.0f, 6.0f / 1.0f + (4.0f / 3.0f - 6.0f / 1.0f) / 2.0f);
  120.             // For a given face layout, the coordinates of the 6 cube faces are fixed: build a compact representation of the
  121.             // coordinates of the center of each face where the first float4 represents the coordinates of the X axis faces,
  122.             // the second the Y, and the third the Z. The first two float componenents (xy) of each float4 represent the face
  123.             // coordinates on the positive axis side of the cube, and the second (zw) the negative.
  124.             // layout.x is a boolean flagging the vertical cross layout (for special handling of flip-flops later)
  125.             // layout.yz contains the inverse of the layout dimensions (ie. the scale factor required to convert from
  126.             // normalized face coords to full texture coordinates)
  127.             if (aspectTest[0]) // horizontal
  128.             {
  129.                 if (aspectTest[2])
  130.                 { // horizontal strip
  131.                     o.faceXCoordLayouts = float4(0.5,0.5,1.5,0.5);
  132.                     o.faceYCoordLayouts = float4(2.5,0.5,3.5,0.5);
  133.                     o.faceZCoordLayouts = float4(4.5,0.5,5.5,0.5);
  134.                     o.layout = float3(-1,1.0/6.0,1.0/1.0);
  135.                 }
  136.                 else
  137.                 { // horizontal cross
  138.                     o.faceXCoordLayouts = float4(2.5,1.5,0.5,1.5);
  139.                     o.faceYCoordLayouts = float4(1.5,2.5,1.5,0.5);
  140.                     o.faceZCoordLayouts = float4(1.5,1.5,3.5,1.5);
  141.                     o.layout = float3(-1,1.0/4.0,1.0/3.0);
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 if (aspectTest[1])
  147.                 { // vertical cross
  148.                     o.faceXCoordLayouts = float4(2.5,2.5,0.5,2.5);
  149.                     o.faceYCoordLayouts = float4(1.5,3.5,1.5,1.5);
  150.                     o.faceZCoordLayouts = float4(1.5,2.5,1.5,0.5);
  151.                     o.layout = float3(1,1.0/3.0,1.0/4.0);
  152.                 }
  153.                 else
  154.                 { // vertical strip
  155.                     o.faceXCoordLayouts = float4(0.5,5.5,0.5,4.5);
  156.                     o.faceYCoordLayouts = float4(0.5,3.5,0.5,2.5);
  157.                     o.faceZCoordLayouts = float4(0.5,1.5,0.5,0.5);
  158.                     o.layout = float3(-1,1.0/1.0,1.0/6.0);
  159.                 }
  160.             }
  161.             // edgeSize specifies the minimum (xy) and maximum (zw) normalized face texture coordinates that will be used for
  162.             // sampling in the texture. Setting these to the effective size of a half pixel horizontally and vertically
  163.             // effectively enforces clamp mode texture wrapping for each individual face.
  164.             o.edgeSize.xy = _MainTex_TexelSize.xy * 0.5 / o.layout.yz - 0.5;
  165.             o.edgeSize.zw = -o.edgeSize.xy;
  166. #else // !_MAPPING_6_FRAMES_LAYOUT
  167.             // Calculate constant horizontal scale and cutoff for 180 (vs 360) image type
  168.             if (_ImageType == 0)  // 360 degree
  169.                 o.image180ScaleAndCutoff = float2(1.0, 1.0);
  170.             else  // 180 degree
  171.                 o.image180ScaleAndCutoff = float2(2.0, _MirrorOnBack ? 1.0 : 0.5);
  172.             // Calculate constant scale and offset for 3D layouts
  173.             if (_Layout == 0) // No 3D layout
  174.                 o.layout3DScaleAndOffset = float4(0,0,1,1);
  175.             else if (_Layout == 1) // Side-by-Side 3D layout
  176.                 o.layout3DScaleAndOffset = float4(unity_StereoEyeIndex,0,0.5,1);
  177.             else // Over-Under 3D layout
  178.                 o.layout3DScaleAndOffset = float4(0, 1-unity_StereoEyeIndex,1,0.5);
  179. #endif
  180.             return o;
  181.         }
  182.  
  183.         fixed4 frag (v2f i) : SV_Target
  184.         {
  185. #ifdef _MAPPING_6_FRAMES_LAYOUT
  186.             float2 tc = ToCubeCoords(i.texcoord, i.layout, i.edgeSize, i.faceXCoordLayouts, i.faceYCoordLayouts, i.faceZCoordLayouts);
  187. #else
  188.             float2 tc = ToRadialCoords(i.texcoord);
  189.             if (tc.x > i.image180ScaleAndCutoff[1])
  190.                 return half4(0,0,0,1);
  191.             tc.x = fmod(tc.x*i.image180ScaleAndCutoff[0], 1);
  192.             tc = (tc + i.layout3DScaleAndOffset.xy) * i.layout3DScaleAndOffset.zw;
  193. #endif
  194.  
  195.             half4 tex = tex2D (_MainTex, tc);
  196.             half3 c = DecodeHDR (tex, _MainTex_HDR);
  197.             c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
  198.             c *= _Exposure;
  199.             return half4(c, 1);
  200.         }
  201.         ENDCG
  202.     }
  203. }
  204.  
  205.  
  206. CustomEditor "SkyboxPanoramicShaderGUI"
  207. Fallback Off
  208.  
  209. }
  210.  
  211. Please find the file also attached to this post.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement