Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mobile Skybox shader for rotation

Discussion in 'Shaders' started by Suduckgames, Apr 9, 2018.

  1. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Hi, I am new to the shader development so I apologize if something is not well explained

    I wanted to have a skybox that rotate on X axis, as unity skybox default shader rotate it on the Y axis I decided to use it as a template, and eliminate the HDR features. (The idea was making it looks similar to the normal mobile/sdkybvox shader)

    Everything works great, however I want to understand the differences bewteen

    Code (CSharp):
    1.  Pass {
    2.         SetTexture [_BackTex]  { combine texture }
    3.     }
    and

    Code (CSharp):
    1. Pass{
    2.         CGPROGRAM
    3. #pragma vertex vert
    4. #pragma fragment frag
    5. #pragma target 2.0
    6.         sampler2D _BackTex;
    7.     float4 frag(v2f i) : SV_Target{ return  tex2D(_BackTex, i.texcoord); }
    8.         ENDCG
    9.     }

    Also, some guide how to improve this shader or optimizations it will be appreciated

    full code of the shader

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Mobile/RotationSkybox" {
    4.     Properties{
    5.  
    6.  
    7.         _Rotation("Rotation", float) = 0
    8.         [NoScaleOffset] _FrontTex("Front [+Z]", 2D) = "grey" {}
    9.     [NoScaleOffset] _BackTex("Back [-Z]", 2D) = "grey" {}
    10.     [NoScaleOffset] _LeftTex("Left [+X]", 2D) = "grey" {}
    11.     [NoScaleOffset] _RightTex("Right [-X]", 2D) = "grey" {}
    12.     [NoScaleOffset] _UpTex("Up [+Y]", 2D) = "grey" {}
    13.     [NoScaleOffset] _DownTex("Down [-Y]", 2D) = "grey" {}
    14.     }
    15.  
    16.         SubShader{
    17.         Tags{ "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" }
    18.         Cull Off ZWrite Off
    19.  
    20.         CGINCLUDE
    21. #include "UnityCG.cginc"
    22.  
    23.  
    24.  
    25.     float _Rotation;
    26.  
    27.     float3 RotateAroundYInDegrees(float3 vertex, float degrees)
    28.     {
    29.         float alpha = degrees * UNITY_PI / 180.0;
    30.         float sina, cosa;
    31.         sincos(alpha, sina, cosa);
    32.         float2x2 m = float2x2(cosa, -sina, sina, cosa);
    33.         return float3(vertex.x,mul(m, vertex.yz)).xzy;
    34.     }
    35.  
    36.     struct appdata_t {
    37.         float4 vertex : POSITION;
    38.         float2 texcoord : TEXCOORD0;
    39.    
    40.     };
    41.     struct v2f {
    42.         float4 vertex : SV_POSITION;
    43.         float2 texcoord : TEXCOORD0;
    44.    
    45.     };
    46.     v2f vert(appdata_t v)
    47.     {
    48.         v2f o;  
    49.         float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
    50.         o.vertex = UnityObjectToClipPos(rotated);
    51.         o.texcoord = v.texcoord;
    52.         return o;
    53.     }
    54.     float4 skybox_frag(v2f i, sampler2D smp, float4 smpDecode)
    55.     {
    56.         float4 tex = tex2D(smp, i.texcoord);
    57.         return tex;
    58.     }
    59.     ENDCG
    60.  
    61.         Pass{
    62.         CGPROGRAM
    63. #pragma vertex vert
    64. #pragma fragment frag
    65. #pragma target 2.0
    66.         sampler2D _FrontTex;
    67.     float4 frag(v2f i) : SV_Target{return  tex2D(_FrontTex, i.texcoord);
    68.     }
    69.         ENDCG
    70.     }
    71.         Pass{
    72.         CGPROGRAM
    73. #pragma vertex vert
    74. #pragma fragment frag
    75. #pragma target 2.0
    76.         sampler2D _BackTex;
    77.     float4 frag(v2f i) : SV_Target{ return  tex2D(_BackTex, i.texcoord); }
    78.         ENDCG
    79.     }
    80.         Pass{
    81.         CGPROGRAM
    82. #pragma vertex vert
    83. #pragma fragment frag
    84. #pragma target 2.0
    85.         sampler2D _LeftTex;
    86.     float4 frag(v2f i) : SV_Target{ return  tex2D(_LeftTex, i.texcoord); }
    87.         ENDCG
    88.     }
    89.         Pass{
    90.         CGPROGRAM
    91. #pragma vertex vert
    92. #pragma fragment frag
    93. #pragma target 2.0
    94.         sampler2D _RightTex;
    95.     float4 frag(v2f i) : SV_Target{ return  tex2D(_RightTex, i.texcoord); }
    96.         ENDCG
    97.     }
    98.         Pass{
    99.         CGPROGRAM
    100. #pragma vertex vert
    101. #pragma fragment frag
    102. #pragma target 2.0
    103.         sampler2D _UpTex;
    104.     float4 frag(v2f i) : SV_Target{ return  tex2D(_UpTex, i.texcoord); }
    105.         ENDCG
    106.     }
    107.         Pass{
    108.         CGPROGRAM
    109. #pragma vertex vert
    110. #pragma fragment frag
    111. #pragma target 2.0
    112.         sampler2D _DownTex;
    113.     float4 frag(v2f i) : SV_Target{ return  tex2D(_DownTex, i.texcoord); }
    114.         ENDCG
    115.     }
    116.     }
    117. }
    118.  
     
  2. Sylafrs

    Sylafrs

    Joined:
    Jun 25, 2013
    Posts:
    53
    Plop :)

    I used your code and the Euler3x3 function from GitHub "Keijiro/ShurikenPlus" I found to implement eulerAngles instead of one axis:

    https://github.com/keijiro/ShurikenPlus/blob/master/Assets/ShurikenPlus/Shaders/Common.hlsl

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. // Simplified Skybox shader. Differences from regular Skybox one:
    4. // - no tint color
    5.  
    6. Shader "Virtuallyz/Mobile/Skybox" {
    7. Properties {
    8.     _Rotation("Rotation", Vector) = (0, 0, 0, 0)
    9.     _FrontTex ("Front (+Z)", 2D) = "white" {}
    10.     _BackTex ("Back (-Z)", 2D) = "white" {}
    11.     _LeftTex ("Left (+X)", 2D) = "white" {}
    12.     _RightTex ("Right (-X)", 2D) = "white" {}
    13.     _UpTex ("Up (+Y)", 2D) = "white" {}
    14.     _DownTex ("Down (-Y)", 2D) = "white" {}
    15. }
    16.  
    17. SubShader {
    18.     Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    19.     Cull Off ZWrite Off Fog { Mode Off }
    20.  
    21.         CGINCLUDE
    22.  
    23.     #include "UnityCG.cginc"
    24.     float3 _Rotation;
    25.  
    26.     float3x3 Euler3x3(float3 v)
    27.     {
    28.         float sx, cx;
    29.         float sy, cy;
    30.         float sz, cz;
    31.  
    32.         sincos(v.x, sx, cx);
    33.         sincos(v.y, sy, cy);
    34.         sincos(v.z, sz, cz);
    35.  
    36.         float3 row1 = float3(sx*sy*sz + cy*cz, sx*sy*cz - cy*sz, cx*sy);
    37.         float3 row3 = float3(sx*cy*sz - sy*cz, sx*cy*cz + sy*sz, cx*cy);
    38.         float3 row2 = float3(cx*sz, cx*cz, -sx);
    39.  
    40.         return float3x3(row1, row2, row3);
    41.     }
    42.  
    43.     float3 RotateAroundInDegrees(float3 vertex, float3 degrees)
    44.     {
    45.         return mul(Euler3x3(_Rotation / 360), vertex);
    46.     }
    47.  
    48.     struct appdata_t {
    49.         float4 vertex : POSITION;
    50.         float2 texcoord : TEXCOORD0;
    51.  
    52.     };
    53.     struct v2f {
    54.         float4 vertex : SV_POSITION;
    55.         float2 texcoord : TEXCOORD0;
    56.  
    57.     };
    58.     v2f vert(appdata_t v)
    59.     {
    60.         v2f o;
    61.         float3 rotated = RotateAroundInDegrees(v.vertex, _Rotation);
    62.      
    63.         o.vertex = UnityObjectToClipPos(rotated);
    64.         o.texcoord = v.texcoord;
    65.         return o;
    66.     }
    67.     float4 skybox_frag(v2f i, sampler2D smp, float4 smpDecode)
    68.     {
    69.         float4 tex = tex2D(smp, i.texcoord);
    70.         return tex;
    71.     }
    72.     ENDCG
    73.         Pass{
    74.         CGPROGRAM
    75. #pragma vertex vert
    76. #pragma fragment frag
    77. #pragma target 2.0
    78.         sampler2D _FrontTex;
    79.     float4 frag(v2f i) : SV_Target{return  tex2D(_FrontTex, i.texcoord);
    80.     }
    81.         ENDCG
    82.     }
    83.         Pass{
    84.         CGPROGRAM
    85. #pragma vertex vert
    86. #pragma fragment frag
    87. #pragma target 2.0
    88.         sampler2D _BackTex;
    89.     float4 frag(v2f i) : SV_Target{ return  tex2D(_BackTex, i.texcoord); }
    90.         ENDCG
    91.     }
    92.         Pass{
    93.         CGPROGRAM
    94. #pragma vertex vert
    95. #pragma fragment frag
    96. #pragma target 2.0
    97.         sampler2D _LeftTex;
    98.     float4 frag(v2f i) : SV_Target{ return  tex2D(_LeftTex, i.texcoord); }
    99.         ENDCG
    100.     }
    101.         Pass{
    102.         CGPROGRAM
    103. #pragma vertex vert
    104. #pragma fragment frag
    105. #pragma target 2.0
    106.         sampler2D _RightTex;
    107.     float4 frag(v2f i) : SV_Target{ return  tex2D(_RightTex, i.texcoord); }
    108.         ENDCG
    109.     }
    110.         Pass{
    111.         CGPROGRAM
    112. #pragma vertex vert
    113. #pragma fragment frag
    114. #pragma target 2.0
    115.         sampler2D _UpTex;
    116.     float4 frag(v2f i) : SV_Target{ return  tex2D(_UpTex, i.texcoord); }
    117.         ENDCG
    118.     }
    119.         Pass{
    120.         CGPROGRAM
    121. #pragma vertex vert
    122. #pragma fragment frag
    123. #pragma target 2.0
    124.         sampler2D _DownTex;
    125.     float4 frag(v2f i) : SV_Target{ return  tex2D(_DownTex, i.texcoord); }
    126.         ENDCG
    127.     }
    128.  
    129. }
    130. }
    131.  
    Thanks for sharing :)