如何在Unity中创建可以显示由许多小图像组成的纹理的着色器

时间:2019-09-20 13:33:20

标签: c# unity3d shader shaderlab

所以我想做的是从SQL表中加载卫星图像,并将它们包裹在一个球体上,以创建一个地球仪。我知道我已经加载了覆盖的图像,只是不确定如何使着色器以正确的方向显示图像。

我去了Unity论坛,并从Unity文档中签出了this code

使用链接的着色器代码和我在论坛上获得的帮助,这是我最终得到的代码:

Properties
    {
        _MainTexArray("Tex", 2DArray) = "" {}
        _SliceRange("Slices", Range(0,32)) = 6
        _UVScale("UVScale", Float) = 1
        _COLUMNS("Columns", Range(0, 5)) = 1
        _ROWS("Rows", Range(0, 5)) = 1
        _CELLS("Cells", Range(0, 32)) = 16
    }
        SubShader
        {
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                // texture arrays are not available everywhere,
                // only compile shader on platforms where they are
                #pragma require 2darray

                #include "UnityCG.cginc"

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

                float _SliceRange;
                float _UVScale;

                v2f vert(float4 vertex : POSITION)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(vertex);
                    o.uv.xy = (vertex.xy + 0.5) * _UVScale;
                    o.uv.z = (vertex.z + 0.5) * _SliceRange;
                    return o;
                }
                float _COLUMNS; //Columns and rows only go between 0 and 1
                float _ROWS;
                float _CELLS;
                UNITY_DECLARE_TEX2DARRAY(_MainTexArray);        
                half4 frag(v2f i) : SV_Target
                {
                    float3 uv = float3(i.uv.x * _CELLS, i.uv.y * _CELLS, 0);
                    uv.z = floor(i.uv.x / _COLUMNS) * floor(i.uv.y / _ROWS);
                    return UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, uv / _CELLS); 
                }
                ENDCG
            }
        }

使用我的资料看起来像这样: distorted texture on plane and sphere

这是我用来加载SQL映像的代码:

textures = new Texture2D[size];
            for (int x = 0; x <= 7; x++) 
            {
                for (int y = 0; y <= 3; y++)
                {
                    textures[count] = tiler.Read(x, y, 2); //The z determines the zoom level, so I wouldn't want them all loaded at once
                    if (textures[count] != null) TextureScale.Bilinear(textures[count], 256, 256);
                    count++;
                }
            }


        texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, true, true);
        texArr.filterMode = FilterMode.Bilinear;
        texArr.wrapMode = TextureWrapMode.Repeat;
        for (int i = 0; i < textures.Length; i++)
        {
            if (textures[i] == null) continue;
            texArr.SetPixels(textures[i].GetPixels(0), i, 0);
        }    
        texArr.Apply();
        mat.SetTexture("_MainTexArray", texArr);

在SQL表中,x和y确定图块的位置,而z确定缩放级别。我现在只使用一个缩放级别。

很抱歉,无法链接整个着色器类,但是我对着色器并不十分了解,所以我不太清楚问题出在哪里。

1 个答案:

答案 0 :(得分:1)

如果您可以索引照片阵列,从而有效地对地球进行等角投影,则可以尝试使用Farfarer from the Unity forums复制并稍稍修改如下的着色器代码的修改形式:

Shader "Custom/Equirectangular" {
    Properties{
        _MainTexArray("Tex", 2DArray) = "" {}
        _COLUMNS("Columns", Int) = 2
        _ROWS("Rows", Int) = 2
    }

        SubShader{
            Pass {
                Tags {"LightMode" = "Always"}

                CGPROGRAM
                    #pragma vertex vert
                    #pragma fragment frag
                    #pragma require 2darray

                    #include "UnityCG.cginc"

                    struct appdata {
                       float4 vertex : POSITION;
                       float3 normal : NORMAL;
                    };

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

                    v2f vert(appdata v)
                    {
                        v2f o;
                        o.pos = UnityObjectToClipPos(v.vertex);
                        o.normal = v.normal;
                        return o;
                    }

                    UNITY_DECLARE_TEX2DARRAY(_MainTexArray);

                    int _ROWS;
                    int _COLUMNS;

                    #define PI 3.141592653589793

                    inline float2 RadialCoords(float3 a_coords)
                    {
                        float3 a_coords_n = normalize(a_coords);
                        float lon = atan2(a_coords_n.z, a_coords_n.x);
                        float lat = acos(a_coords_n.y);
                        float2 sphereCoords = float2(lon, lat) * (1.0 / PI);
                        return float2(sphereCoords.x * 0.5 + 0.5, 1 - sphereCoords.y);
                    }

                    float4 frag(v2f IN) : COLOR
                    {
                        float2 equiUV = RadialCoords(IN.normal);

                        float2 texIndex;
                        float2 uvInTex = modf(equiUV * float2(_COLUMNS,_ROWS), texIndex);

                        int flatTexIndex = texIndex.x * _ROWS + texIndex.y;

                        return UNITY_SAMPLE_TEX2DARRAY(_MainTexArray,
                                float3(uvInTex, flatTexIndex));
                    }
                ENDCG
            }
        }
    FallBack "VertexLit"
}

您还需要使用

texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, false, true);

代替

texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, true, false); 

如果我将此脚本附加到球体上,它将对我有用:

Material myMat;
public List<Texture2D> texes;

IEnumerator Start()
{
    yield return null;
    myMat = GetComponent<Renderer>().material;

    Texture2DArray texArr = new Texture2DArray(256, 256, 9, 
            TextureFormat.RGBA32, false, true);
    texArr.filterMode = FilterMode.Bilinear;
    texArr.wrapMode = TextureWrapMode.Clamp;

    for (int i = 0 ; i < texes.Count ; i++)
    {
        texArr.SetPixels(texes[i].GetPixels(), i, 0);
    }

    texArr.Apply();

    myMat.SetTexture("_MainTexArray", texArr);
}

,然后在texes中依次添加以下纹理:

0:bottom left at index 0

1:center left at index 1

2:top left at index 2

3:bottom center at index 3

4:center at index 4

5:top center at index 5

6:bottom right at index 6

7:center right at index 7

8:top right at index 8

并将“行和列”设置为3,它会产生不错的结果:

seamless wrapped texture array

如果启用了双线性过滤,则纹理的边界处仍会出现一些伪像。但是必须将这些伪像放大到相当近的距离才能看到。由于缺乏用于双线性滤波的相邻像素,它们大多以不正确的混合或缺失像素的形式出现:

enter image description here

当然,此示例未正确平铺,因此沿一条经线存在明显的接缝:

only seam where texture doesn't repeat

由于此设置需要球体的法线,因此该方法仅适用于法线近似于球体法线的事物。因此,例如,它将无法在飞机上正确渲染。