亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Unity 中創建一個可以顯示由許多小圖像組成的紋理的著色器

如何在 Unity 中創建一個可以顯示由許多小圖像組成的紋理的著色器

C#
HUX布斯 2023-12-17 17:01:32
所以我想做的是從 SQL 表加載衛星圖像并將它們包裹在一個球體周圍以創建一個地球儀。我知道我已經加載了所覆蓋的圖像,我只是不確定如何使我的著色器以正確的方向顯示圖像。我訪問了 Unity 論壇并從 Unity 文檔中查看了此代碼。使用鏈接的著色器代碼和我在論壇上收到的幫助,這是我最終得到的代碼: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            }        }
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

如果您可以對照片數組進行索引,以便有效地獲得地球的等距柱狀投影,則可以嘗試使用著色器代碼的修改形式Farfarer Unity論壇復制并稍作修改如下:

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中按順序添加這些紋理:

https://img1.sycdn.imooc.com/657eb92b0001bb2702871094.jpg

https://img1.sycdn.imooc.com/657eb93d00010f2e02861085.jpg

https://img1.sycdn.imooc.com/657eb946000152d102870273.jpg

并將行和列設置為 3,它會產生不錯的結果:

https://img1.sycdn.imooc.com/657eb95f0001600b23451289.jpg

如果啟用雙線性過濾,紋理邊界處仍然存在一些偽影。但這些文物必須放大得很近才能看到。由于雙線性過濾缺乏相鄰像素,它們大多顯示為混合不當或丟失像素:

https://img1.sycdn.imooc.com/657eb97000012c0e23401291.jpg

當然,這個示例沒有正確平鋪,因此沿著一條經線有明顯的接縫:

https://img1.sycdn.imooc.com/657eb9800001e48923421287.jpg

由于此設置需要來自球體的法線,因此這只適用于法線接近球體的物體。因此,例如,它無法在平面上正確渲染。



查看完整回答
反對 回復 2023-12-17
  • 1 回答
  • 0 關注
  • 191 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號