c# - 如何在 Unity 中基于相机视口将纹理应用于材质
问题描述
所以这篇文章或多或少是我最后一个问题的延续,所以如果你迷路了,请检查那个链接。
基本上,当用户进一步放大相机时,我需要加载应用于地球上材料的更高质量的图像。就目前而言,我根据缩放级别一次加载所有图像,这在较低的缩放级别上没有得到优化,而在较高的缩放级别上是不可能的,因为 texture2darray 只能容纳 2048 个图像。
我需要知道的是,我怎样才能只加载相机正在查看的图像,而不担心屏幕外的图像?
这可能是一个可笑的具体问题,所以我理解我是否在这里吠错了树,但我想我会把它扔在那里,看看是否有人有任何见解。
这是我目前的平铺课程:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TilerHelper : MonoBehaviour
{
MapTiler tiler = new MapTiler("G:/s2cloudless_4326_v1.0.0.sqlite"); //Function that loads from the sql table
public Material mat; //The material
Texture2DArray texArr; //The array that will be made into the final texture
public Texture2D[] textures; //The images loaded from the sql table
int size = 2048; //A texture2darray can only hold 2048 textures
int count = 0; //Iterator for loading images
int zoom = 1; //Determines which zoom level to load from the sql table
int lastZoom = 1; //Determines whether the zoom level has moved
CameraOrbit cam; //The main camera
int initX = 3; //The x value of the image at zoom level 1
int initY = 1;//The y value of the image at zoom level 1
// Start is called before the first frame update
void Start()
{
SetTiles(3, 1, 1, 4, 2); //This loads the first set of images at max distance when the program starts
cam = Camera.main.gameObject.GetComponent<CameraOrbit>();
}
// Update is called once per frame
void Update()
{
//These different zoom levels are based on the camera distance from the earth (large earth, so large numbers)
if (cam.distance <= 90000) zoom = 12;
else if (cam.distance <= 100000) zoom = 11;
else if (cam.distance <= 110000) zoom = 10;
else if (cam.distance <= 130000) zoom = 9;
else if (cam.distance <= 150000) zoom = 8;
else if (cam.distance <= 170000) zoom = 7;
else if (cam.distance <= 190000) zoom = 6;
else if (cam.distance <= 210000) zoom = 5;
else if (cam.distance <= 230000) zoom = 4;
else if (cam.distance <= 250000) zoom = 3;
else if (cam.distance <= 270000) zoom = 2;
else if (cam.distance >= 270000) zoom = 1;
//If the camera has gone to a new zoom level...
if(lastZoom != zoom)
{
if (zoom == 1) SetTiles(initX, initY, zoom, 4, 2); //Set it to 1 manually, since the formula won't work for it
else
{
//This formula will load the correct images in the correct places regardless of zoom level
int counter = 1;
int resultX = initX;
int resultY = initY;
while(counter < zoom)
{
resultX = resultX * 2 + 1;
resultY = resultY * 2 + 1;
counter++;
}
SetTiles(resultX, resultY, zoom, (int)Mathf.Pow(2, zoom + 1), (int)Mathf.Pow(2, zoom));
}
lastZoom = zoom; //Update last zoom
}
}
//The method that actually places the images
void SetTiles(int x, int y, int z, int columns, int rows)
{
textures = new Texture2D[size]; //The array to hold all the textures
//Load and place all the images according to passed x, y, and zoom level
for (int i = 0; i <= x; i++)
{
for (int j = 0; j <= y; j++)
{
textures[count] = tiler.Read(i, j, z); //The z determines the zoom level, so I wouldn't want them all loaded at once
count++;
}
}
count = 0; //Reset the counter
//Instantiate the texture2darray
texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, false, true);
texArr.filterMode = FilterMode.Bilinear;
texArr.wrapMode = TextureWrapMode.Clamp;
//Set the texture2darray to contain all images loaded
for (int i = 0; i < textures.Length; i++)
{
if (textures[i] == null) continue;
texArr.SetPixels(textures[i].GetPixels(), i, 0);
}
//Apply the texture and set appropriate material values
texArr.Apply();
mat.SetTexture("_MainTexArray", texArr);
Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, -90, 0), Vector3.one);
mat.SetMatrix("_Matrix", matrix);
mat.SetInt("_COLUMNS", columns);
mat.SetInt("_ROWS", rows);
}
}
这是我的着色器代码
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/Tiling"
{
Properties
{
_MainTexArray("Tex", 2DArray) = "" {}
_COLUMNS("Columns", Int) = 8
_ROWS("Rows", Int) = 4
}
SubShader
{
Pass{
Tags {"RenderType" = "Opaque"}
Lighting Off
ZWrite Off
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;
};
float4x4 _Matrix;
v2f vert(appdata v, float3 normal : TEXCOORD0)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.normal = mul(v.normal, _Matrix);
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);
}
float _UVClamp;
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
}
}
}
谢谢你。
解决方案
尝试解决有趣的问题,而无需遍历大量图块或像素。这是一个部分答案,可能会帮助您找到解决方案。
这里有两个主要问题:
- 查找哪些“瓷砖”对相机可见。
- 将这些图块加载到着色器中并让它正确索引它们。
查找哪些“图块”对相机可见
我能想到的最好的主意是尝试找到一种方法,将相机的球体视图投影到等角投影上可见点的位置。
您可以通过多次ViewportPointToRay
和碰撞检查对此进行采样,但这会不准确,尤其是在相机可以看到球体“周围”并且光线不与那里的球体碰撞的情况下。
对此可能有一种公式化的方法,但我不确定该怎么做。
将这些图块加载到着色器中并让它正确索引它们。
如果您可以确定需要发送哪些 equirectangular 瓷砖,这是更容易的部分。如果您可以绘制一个围绕相机可以看到的所有点水平环绕的矩形,那么您可以发送部分或完全在该“加载矩形”内的图块。
您还需要发送矩形的起点和终点。因此着色器可以计算如何将整个地图texIndex
坐标更改为该图块存储在“加载矩形”中的位置。
基本上,着色器中的这些行可能是相同的:
float2 equiUV = RadialCoords(IN.normal);
float2 texIndex;
float2 uvInTex = modf(equiUV * float2(_COLUMNS, _ROWS), texIndex);
...
return UNITY_SAMPLE_TEX2DARRAY(_MainTexArray,
float3(uvInTex, flatTexIndex));
这条线必须以某种方式改变,以考虑加载矩形参数以及它如何水平环绕瓷砖地图:
int flatTexIndex = texIndex.x * _ROWS + texIndex.y;
抱歉含糊不清,尤其是第一部分。祝你好运。
推荐阅读
- c# - 检查 DateTime + TimeSpan 之间的重叠
- matlab - 极度压缩信号的去噪
- reactjs - 使用 react-router v4 升级实现 ConnectedRouter 的问题
- python-xarray - 用于多维坐标的 xarray 数据集 swap_dims
- haskell - Yesod 插入后返回整个实体?
- r - 通过R中的变量在散点图上着色点
- javascript - 通过节点或电子找出哪个网络接口是哪个
- sql - sql:创建调用函数的约束时出错
- javascript - eval() 函数中的节点 process.env 变量给出不同的结果
- python - 在 python 中表示数据结构