2

I'm trying to do a custom skybox for 360 images which has 2 textures with a crossfade and I need it to respond to Rotation value like the Unity Skybox. I just need the same slider but I´m not getting any luck, I'm complete new on shaders.

Here is the code I have until now

Shader "Custom/fundido"
{
   Properties {

     _Blend ("Blend", Range (0, 1) ) = 0.0
     _Rotation ("Rotation", Range(0, 360)) = 0
     _BaseTexture ("Cubemap (HDR)", Cube) = "grey" {}
     _OverlayTexture ("Cubemap2 (HDR)", Cube) = "grey" {}

 }

SubShader {

 Tags { "Queue"="Background" "RenderType"="Background" 
"PreviewType"="Skybox" }
     
Pass {

         SetTexture[_BaseTexture]
         SetTexture[_OverlayTexture] {
         ConstantColor (0,0,0, [_Blend]) 
         combine texture Lerp(constant) previous
         }
     }
 }
 }

The _Blend works perfect for the crossfade I just need to add the Rotation listener.

Thanks so much!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Have a look on this link : https://forum.unity.com/threads/rotate-a-skybox.130639/ , But instead of changing the rotation via script, you will change it with your slider _Rotation properties. – Tengku Fathullah Sep 14 '17 at 03:43
  • Hi Tengku! Thanks for being so quick. I tried with that post and the trick of the two cameras with some modifications works but give me some axis weird movements when I attach de gyroscope control to the main camera. As you say the best method would be use my slider but it is not connected to nothing because I don't know how to attach it to the shader. And that's what I'm trying to do, add my slider to the shader I have for the crossfade. Thankas :) – Gabriel Cerra Sep 14 '17 at 10:47
  • Hi i just arrive home. I manage to create the shader and it works for me, after some cleaning i will post you an answer. – Tengku Fathullah Sep 14 '17 at 15:51

1 Answers1

3

Here is the shader. You can shift the day cycle through script. https://docs.unity3d.com/ScriptReference/Material.SetFloat.html

Shader "TFTM/Skybox2CubeBlend" {
Properties {
    _Blend ("Blend", Range (0, 1) ) = 0.0
    _Rotation ("Rotation", Range(0, 360)) = 0
    _Tex ("Cubemap   (HDR)", Cube) = "grey" {}
    _OverlayTex ("CubemapOverlay (HDR)", Cube) = "grey" {}
}

SubShader {
    Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    Cull Off ZWrite Off

    Pass {

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0

        #include "UnityCG.cginc"

        samplerCUBE _Tex;
        samplerCUBE _OverlayTex;
        half4 _Tex_HDR;
        half4 _Tint;
        half _Exposure;
        float _Rotation;
        float _Blend;

        float3 RotateAroundYInDegrees (float3 vertex, float degrees)
        {
            float alpha = degrees * UNITY_PI / 180.0;
            float sina, cosa;
            sincos(alpha, sina, cosa);
            float2x2 m = float2x2(cosa, -sina, sina, cosa);
            return float3(mul(m, vertex.xz), vertex.y).xzy;
        }

        struct appdata_t {
            float4 vertex : POSITION;
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            float3 texcoord : TEXCOORD0;
        };

        v2f vert (appdata_t v)
        {
            v2f o;
            float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
            o.vertex = UnityObjectToClipPos(rotated);
            o.texcoord = v.vertex.xyz;
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            half4 tex = texCUBE (_Tex, i.texcoord);
            half4 tex2 = texCUBE (_OverlayTex, i.texcoord);
            float4 env = lerp( tex, tex2, _Blend );

            half3 c = DecodeHDR (env, _Tex_HDR);

            return half4(c, 1);
        }
        ENDCG 
    }
}   
Fallback Off

}
Tengku Fathullah
  • 1,279
  • 1
  • 18
  • 43
  • Wow!!! Amazing thank you so much! Do you know a good documentation place to start learning shaders? I want to understand them for future problems. And really thanks again :D – Gabriel Cerra Sep 14 '17 at 17:52
  • Personally I really like Alan Zucconi shaders book. http://www.alanzucconi.com/books/ – Tengku Fathullah Sep 14 '17 at 18:01
  • Thanks Tengku hope I can return the favor. :D – Gabriel Cerra Sep 14 '17 at 18:25
  • Sure, have a look on my github there is few shader that i created. Might be helpful for you. https://github.com/TengkuFathullah , and you can vote the answer as useful so it will be in top list. Give me shout if you need more good reference. – Tengku Fathullah Sep 14 '17 at 18:30