2

我正在尝试为 360 图像做一个自定义天空盒,它有 2 个带有淡入淡出的纹理,我需要它像 Unity Skybox 一样响应旋转值。我只需要相同的滑块,但我没有运气,我对着色器完全陌生。

这是我到目前为止的代码

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
         }
     }
 }
 }

_Blend 非常适合交叉淡入淡出,我只需要添加 Rotation 监听器。

非常感谢!!!

4

1 回答 1

3

这是着色器。您可以通过脚本改变一天的周期。 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

}
于 2017-09-14T16:12:06.190 回答