Skip to main content
deleted 4 characters in body
Source Link
    Shader "Skybox/LatLong"
{
    Properties
    {
        [NoScaleOffset] _MainTexture ("Main Texture (LatLong)", 2D) = "white" {}
    }
    SubShader 
    {
        Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
        Cull Off ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTexture;

            struct appdata_t
            {
                float4 vertex : POSITION;
            };

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

            v2f vert (appdata_t v)
            {
                v2f OUT;
                OUT.pos = UnityObjectToClipPos(v.vertex);

                // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
                float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
                OUT.vertex = -eyeRay;

                return OUT;
            }

            float2half2 GetLatLongCoords(float3 normalizedViewDir)
            {
                floathalf lon = atan2(-normalizedViewDir.x, -normalizedViewDir.z);
                floathalf lat = asin(-normalizedViewDir.y);

                float2half2 uv;

                uv.x = (lon / (2.0 * UNITY_PI)) + 0.5;

                uv.y = ((lat / UNITY_PI) + 0.5);
                uv.y = saturate((uv.y - 0.5) / 0.5);

                return uv;
            }

            half4 frag (v2f IN) : SV_Target
            {
                half3 viewDir = normalize(IN.vertex.xyz);

                half2 coords = GetLatLongCoords(viewDir);

                half4 col = tex2Dlod(_MainTexture, half4(coords, 2, 0));

                return col;
            }

            ENDCG
        }
    }
}
    Shader "Skybox/LatLong"
{
    Properties
    {
        [NoScaleOffset] _MainTexture ("Main Texture (LatLong)", 2D) = "white" {}
    }
    SubShader 
    {
        Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
        Cull Off ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTexture;

            struct appdata_t
            {
                float4 vertex : POSITION;
            };

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

            v2f vert (appdata_t v)
            {
                v2f OUT;
                OUT.pos = UnityObjectToClipPos(v.vertex);

                // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
                float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
                OUT.vertex = -eyeRay;

                return OUT;
            }

            float2 GetLatLongCoords(float3 normalizedViewDir)
            {
                float lon = atan2(-normalizedViewDir.x, -normalizedViewDir.z);
                float lat = asin(-normalizedViewDir.y);

                float2 uv;

                uv.x = (lon / (2.0 * UNITY_PI)) + 0.5;

                uv.y = ((lat / UNITY_PI) + 0.5);
                uv.y = saturate((uv.y - 0.5) / 0.5);

                return uv;
            }

            half4 frag (v2f IN) : SV_Target
            {
                half3 viewDir = normalize(IN.vertex.xyz);

                half2 coords = GetLatLongCoords(viewDir);

                half4 col = tex2Dlod(_MainTexture, half4(coords, 2, 0));

                return col;
            }

            ENDCG
        }
    }
}
    Shader "Skybox/LatLong"
{
    Properties
    {
        [NoScaleOffset] _MainTexture ("Main Texture (LatLong)", 2D) = "white" {}
    }
    SubShader 
    {
        Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
        Cull Off ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTexture;

            struct appdata_t
            {
                float4 vertex : POSITION;
            };

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

            v2f vert (appdata_t v)
            {
                v2f OUT;
                OUT.pos = UnityObjectToClipPos(v.vertex);

                // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
                float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
                OUT.vertex = -eyeRay;

                return OUT;
            }

            half2 GetLatLongCoords(float3 normalizedViewDir)
            {
                half lon = atan2(-normalizedViewDir.x, -normalizedViewDir.z);
                half lat = asin(-normalizedViewDir.y);

                half2 uv;

                uv.x = (lon / (2.0 * UNITY_PI)) + 0.5;

                uv.y = ((lat / UNITY_PI) + 0.5);
                uv.y = saturate((uv.y - 0.5) / 0.5);

                return uv;
            }

            half4 frag (v2f IN) : SV_Target
            {
                half3 viewDir = normalize(IN.vertex.xyz);

                half2 coords = GetLatLongCoords(viewDir);

                half4 col = tex2Dlod(_MainTexture, half4(coords, 2, 0));

                return col;
            }

            ENDCG
        }
    }
}
Source Link

Mipmaps and LOD Behavior in Skybox Shaders

  1. Is it beneficial to use Generate Mipmaps for a texture used in a skybox shader?

I need to use tex2Dlod to fix the edge seams issue, and I want to use the highest LOD I can reach in order to get the lowest resolution.

  1. What is the highest LOD for textures with a resolution of 1024?

  2. What will happen if the resolution changes to 512 but the LOD value is not changed?

  3. How will tex2Dlod behave if Generate Mipmaps is disabled in the texture settings?

enter image description here

Used shader:

    Shader "Skybox/LatLong"
{
    Properties
    {
        [NoScaleOffset] _MainTexture ("Main Texture (LatLong)", 2D) = "white" {}
    }
    SubShader 
    {
        Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
        Cull Off ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTexture;

            struct appdata_t
            {
                float4 vertex : POSITION;
            };

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

            v2f vert (appdata_t v)
            {
                v2f OUT;
                OUT.pos = UnityObjectToClipPos(v.vertex);

                // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
                float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
                OUT.vertex = -eyeRay;

                return OUT;
            }

            float2 GetLatLongCoords(float3 normalizedViewDir)
            {
                float lon = atan2(-normalizedViewDir.x, -normalizedViewDir.z);
                float lat = asin(-normalizedViewDir.y);

                float2 uv;

                uv.x = (lon / (2.0 * UNITY_PI)) + 0.5;

                uv.y = ((lat / UNITY_PI) + 0.5);
                uv.y = saturate((uv.y - 0.5) / 0.5);

                return uv;
            }

            half4 frag (v2f IN) : SV_Target
            {
                half3 viewDir = normalize(IN.vertex.xyz);

                half2 coords = GetLatLongCoords(viewDir);

                half4 col = tex2Dlod(_MainTexture, half4(coords, 2, 0));

                return col;
            }

            ENDCG
        }
    }
}