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

How to script skybox shader to combine rotation(xyz) and fading FX

Discussion in 'Shaders' started by Sam_Space, Nov 19, 2021.

  1. Sam_Space

    Sam_Space

    Joined:
    Jun 28, 2019
    Posts:
    16
    [SOLVED] SEE BELOW-
    I am currently working on creating a skybox shader that fades between two cubemaps and creating the ability to rotate the skybox on either the X, Y and Z axes.

    I have created two separate shader scripts. One script has rotation fx to spin the cubemap in any direction and the other can fade between two cubemap skyboxes. I am having a problem trying to combine both shaders into one. I am not super confident in shader scripting, but I imagined this problem may seem simple to solve by someone who knows what they are doing.

    I figured that if both fx are combined then each cubemap needs to follow the same rotation while they fade. I would also like the ability to add a third, fourth and fifth cubemap to the shader. I will post the skybox fade script and the rotation script. Mind you, I found these scripts online, I didn't write them, but I am trying to find a way to combine them.
     

    Attached Files:

    Last edited: Nov 22, 2021
  2. Sam_Space

    Sam_Space

    Joined:
    Jun 28, 2019
    Posts:
    16
    [Solved] So I ended up finding a solution to combining both shaders to make a fading skybox that also rotates on all 3 axes. If anyone is interested and finds this, I hope it helps you.

    Code (CSharp):
    1. Shader "Skybox/Blend and Rotation" {
    2.     Properties{
    3.         _Tint("Tint Color", Color) = (.5, .5, .5, .5)
    4.  
    5.         [Gamma] _Exposure("Exposure", Range(0, 8)) = 1.0
    6.         // Increase the rotation property to X, Y, Z
    7.         _RotationX("Rotation X", Range(0, 720)) = 0
    8.         _RotationY("Rotation Y", Range(0, 720)) = 0
    9.         _RotationZ("Rotation Z", Range(0, 720)) = 0
    10.         [NoScaleOffset] _CubeTex1("Cubemap 1 (HDR)", Cube) = "grey" {}
    11.         [NoScaleOffset] _CubeTex2("Cubemap 2 (HDR)", Cube) = "grey" {}
    12.         [NoScaleOffset] _CubeTex3("Cubemap 3 (HDR)", Cube) = "grey" {}
    13.         [NoScaleOffset] _CubeTex4("Cubemap 4 (HDR)", Cube) = "grey" {}
    14.  
    15.         _Blend1("Blend1", Range(0.0,1.0)) = 0.5
    16.         _Blend2("Blend2", Range(0.0,1.0)) = 0.5
    17.         _Blend3("Blend3", Range(0.0,1.0)) = 0.5
    18.  
    19.     }
    20.         //SECTION 1
    21.         SubShader{
    22.             Tags {"Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox"}
    23.             Cull Off ZWrite Off
    24.             Pass {
    25.                 CGPROGRAM
    26.                 #pragma vertex vert
    27.                 #pragma fragment frag
    28.                 #pragma target 2.0
    29.                 #include "UnityCG.cginc"
    30.                 samplerCUBE _CubeTex1;
    31.                 samplerCUBE _CubeTex2;
    32.                 samplerCUBE _CubeTex3;
    33.                 samplerCUBE _CubeTex4;
    34.  
    35.                 half4 _CubeTex1_HDR;
    36.                 half4 _CubeTex2_HDR;
    37.                 half4 _CubeTex3_HDR;
    38.                 half4 _CubeTex4_HDR;
    39.  
    40.                 half4 _Tint;
    41.                 half _Exposure;
    42.                 // Increase the rotation angle variable to 3 according to the property
    43.                 float _RotationX;
    44.                 float _RotationY;
    45.                 float _RotationZ;
    46.  
    47.                 // BLEND S*** TOGETHER WOO
    48.                 float _Blend1;
    49.                 float _Blend2;
    50.                 float _Blend3;
    51.  
    52.                 // renamed rotation function, change argument from float to float3
    53.                 float3 RotateAroundZXYInDegrees(float3 vertex, float3 degrees)
    54.                 {
    55.                     // Calculate sine and cosine for each of the three components ...
    56.                     float3 alpha = degrees * UNITY_PI / 180.0;
    57.                     float3 sina, cosa;
    58.                     sincos(alpha, sina, cosa);
    59.                     // Create a rotation matrix around each axis ...
    60.                     float3x3 rx = float3x3 (
    61.                             1.0, 0.0, 0.0,
    62.                             0.0, cosa.x, -sina.x,
    63.                             0.0, sina.x, cosa.x);
    64.                     float3x3 ry = float3x3 (
    65.                          cosa.y, 0.0, sina.y,
    66.                             0.0, 1.0, 0.0,
    67.                         -sina.y, 0.0, cosa.y);
    68.                     float3x3 rz = float3x3 (
    69.                          cosa.z, -sina.z, 0.0,
    70.                          sina.z, cosa.z, 0.0,
    71.                             0.0, 0.0, 1.0);
    72.                     // Composite rotation in order of Z axis, X axis, Y axis ...
    73.                     float3x3 m = mul(ry, mul(rx, rz));
    74.                     // apply rotation and return
    75.                     return mul(m, vertex);
    76.                 }
    77.                 struct appdata_t {
    78.                     float4 vertex: POSITION;
    79.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    80.                 };
    81.                 struct v2f {
    82.                     float4 vertex: SV_POSITION;
    83.                     float3 texcoord: TEXCOORD0;
    84.                     UNITY_VERTEX_OUTPUT_STEREO
    85.                 };
    86.                 v2f vert(appdata_t v)
    87.                 {
    88.                     v2f o;
    89.                     UNITY_SETUP_INSTANCE_ID(v);
    90.                     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    91.                     // give X, Y, Z rotation angles to the rotation function
    92.                     float3 rotated = RotateAroundZXYInDegrees(v.vertex, float3 (_RotationX, _RotationY, _RotationZ));
    93.                     o.vertex = UnityObjectToClipPos(rotated);
    94.                     o.texcoord = v.vertex.xyz;
    95.                     return o;
    96.                 }
    97.                 fixed4 frag(v2f i) : SV_Target
    98.                 {
    99.                     half4 sampledCubeTex1 = texCUBE(_CubeTex1, i.texcoord);
    100.                     half4 sampledCubeTex2 = texCUBE(_CubeTex2, i.texcoord);
    101.                     half4 sampledCubeTex3 = texCUBE(_CubeTex3, i.texcoord);
    102.                     half4 sampledCubeTex4 = texCUBE(_CubeTex4, i.texcoord);
    103.  
    104.  
    105.                     //sampledCubeTex1.xyz = DecodeHDR(sampledCubeTex1, _CubeTex1_HDR);
    106.                     //sampledCubeTex2.xyz = DecodeHDR(sampledCubeTex2, _CubeTex2_HDR);
    107.                     //sampledCubeTex2.xyz = DecodeHDR(sampledCubeTex3, _CubeTex3_HDR);
    108.  
    109.                     //half3 c = DecodeHDR(sampledCubeTex1, _CubeTex1_HDR);
    110.                     //c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
    111.                     //c *= _Exposure;
    112.  
    113.                     half3 blendResult = sampledCubeTex1.xyz + sampledCubeTex2.xyz * _Blend1 + (sampledCubeTex3.xyz * _Blend2) + (sampledCubeTex4.xyz * _Blend3);
    114.                     blendResult *= _Tint.rgb * _Exposure * unity_ColorSpaceDouble.rgb;
    115.  
    116.                     return half4 (blendResult, 1);
    117.                 }
    118.                 ENDCG
    119.             }
    120.            
    121.    
    122.         }
    123.        
    124.         Fallback off
    125. }
     
  3. AnthonyZornig

    AnthonyZornig

    Joined:
    Jul 13, 2023
    Posts:
    1
    Hi Sam_Space,
    thank you! I searched the last 3 hours for any scritps like yours and on top, I started Unity two days ago and never coded C# before.
    Question: I would like to integrate a

    "public float RotateSpeed = 0.2f;
    void Update()
    {
    RenderSettings.skybox.SetFloat ("_Rotation", Time.time * RotateSpeed);
    }"

    Any idea how to resolve that ? I assume that this just needs two or three more lines? I want a rotating MilkyWay in a night scene…
    Any help appreciated!

    Anyways… this script already helped me a lot !
    Cheers, Anthony