unity-game-engine - Unity自定义天空盒就像Unity天空盒一样

标签 unity-game-engine shader cg skybox 360-panorama

我正在尝试为 360 度图像制作一个自定义天空盒,它有 2 个带有交叉淡入淡出的纹理,我需要它像 Unity 天空盒一样响应旋转值。我只需要相同的 slider ,但我没有得到任何运气,我对着色器完全陌生。

这是我到目前为止的代码

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 非常适合交叉淡入淡出,我只需要添加旋转监听器。

非常感谢!

最佳答案

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

}

关于unity-game-engine - Unity自定义天空盒就像Unity天空盒一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204527/

相关文章:

unity-game-engine - 将 RigidBody2D 旋转与 Sprite 分开

opengl - GLSL - 奇怪的语法错误 "<"

algorithm - 如何模糊片段着色器的结果?

c# - MLAgents Agent 的训练并没有进步

unity-game-engine - 在 Unity 2018 中忽略 'Packages' 文件夹是否安全?

c# - 协程停止工作

c++ - GLSL 构建错误

opengl-es - OpenGL 片段着色器如何知道要在纹理中采样哪个像素?

shader - 如何修改/置换 Cg 片段着色器中的像素位置?

c# - 在Windows 10下使用SharpDX 2.6.3