1

如何在使用Texture2DArray的时候开启Mipmap效果

 2 years ago
source link: https://blog.uwa4d.com/archives/TechSharing_280.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1)如何在使用Texture2DArray的时候开启Mipmap效果
​2)CharacterController移动碰撞问题
3)一个关于Shader下坐标系转换的问题
4)加载配置内存过大问题
5)关于UnityWebRequest的手机端Timeout无效的问题


这是第280篇UWA技术知识分享的推送。今天我们继续为大家精选了若干和开发、优化相关的问题,建议阅读时间10分钟,认真读完必有收获。

UWA 问答社区:answer.uwa4d.com
UWA QQ群2:793972859(原群已满员)

Texture

Q:使用Texture2DArray的时候发现没有Mipmap效果,想请问下怎么让Mipmap生效,原始的纹理都是开启Mipmap的。

A1:在创建Texture2DArray时需要注意倒数第二个参数,设为True就表示开启Mipmap。
参考文档:Texture2DArray Constructor

该问答由UWA提供

A2:具体代码可参考:

Texture2DArray mTex2DArray = new Texture2DArray(Textures[0].width, Textures[0].height, Textures.Length, TextureFormat.RGBA32, true, false);
for (int index = 0; index < Textures.Length; index++)
{
    for (int m = 0; m < Textures[index].mipmapCount; m++)
    {
        Graphics.CopyTexture(Textures[index], 0, m, mTex2DArray, index, m);
    }
}
AssetDatabase.CreateAsset(mTex2DArray, "Assets/Texture2DArray/Assets/AlbedoArrayWithMipmap.asset");

倒数第二个参数为True时,会生成Mipmap。

效果如下:

1.png

不生成Mipmap:

2.png

感谢一直有点困的仓鼠@UWA问答社区提供了回答,欢迎大家转至社区交流:
https://answer.uwa4d.com/question/5ab7afd60b827e2c0bfdd18f


Physics

Q:我们项目使用CharacterController时碰到一个问题:第一人称角色去碰撞第三人称角色,出现的现象是会把第三人称角色撞动。

第一人称角色使用CharacterController组件,第三人称角色使用CapsuleCollider和RigidBody:

实验一:游戏运行中,拖一个第三人称角色到场景,场景中第一人称角色是撞不动第三人称角色。经过猜测,跟网络同步相关。

实验二:两个客户端C1和C2,客户端C1中的第一人称角色A1去撞第三人称角色B1,A1的位置经服务器同步到客户端C2,此时客户端C2中的第一人称角色B1,每帧更新时,调用Controller.Move方法,猜测引擎底层检测到碰撞,改变了B1的位置。

这时客户端C2中的第一人称角色B1位置经服务器同步到客户端C1,所以客户端C1中A1看到B1被撞动了。

因为Controller.Move函数只接受一个参数Motion,这个参数是一帧的移动增量的意思。人物站着不动和别人碰撞,传的参数Motion基本一致,区分不开。

上层传的参数差异不大的情况下,Controller.Move导致的行为不一致。暂时没好的解决方式。

A:第一人称角色使用CharacterController组件,第三人称角色使用CapsuleCollider和RigidBody。

第三人称角色使用RigidBody的,不要直接用tranform.position直接赋值,要使用trans.GetComponent().MovePosition(destination)。Unity官方推荐使用RigidBody的MovePosition方法来移动。

CharacterController底层会有碰撞回弹机制,使用RigidBody的MovePosition方法能忽略物理碰撞。

感谢题主无面者1986@UWA问答社区提供了回答,欢迎大家转至社区交流:
https://answer.uwa4d.com/question/61b1b169d8413e18eb40fc30


Shader

Q:最近在看ColinLeung的ScreenSpaceDecal使用深度图做贴花的Shader发现一个问题:在Shader中需要计算摄像机坐标系到顶点的射线,因为在Unity中ViewSpace是右手坐标系,而ObjectSpace和WorldSpace都是左手。正常右手转左手是通过取反xy或取反z。但ColinLeung处理是xyz都取反。这个怎么理解呢?

     VertexPositionInputs vertexPositionInput = GetVertexPositionInputs(input.positionOS);
                o.positionCS = vertexPositionInput.positionCS;

                // regular unity fog
#if _UnityFogEnable
                o.cameraPosOSAndFogFactor.a = ComputeFogFactor(o.positionCS.z);
#else
                o.cameraPosOSAndFogFactor.a = 0;
#endif

                // prepare depth texture's screen space UV
                o.screenPos = ComputeScreenPos(o.positionCS);

                // get "camera to vertex" ray in View space
                float3 viewRay = vertexPositionInput.positionVS;

                // [important note]
                //=========================================================
                // "viewRay z division" must do in the fragment shader, not vertex shader! (due to rasteriazation varying interpolation's perspective correction)
                // We skip the "viewRay z division" in vertex shader for now, and store the division value into varying o.viewRayOS.w first, 
                // we will do the division later when we enter fragment shader
                // viewRay /= viewRay.z; //skip the "viewRay z division" in vertex shader for now
                o.viewRayOS.w = viewRay.z;//store the division value to varying o.viewRayOS.w
                //=========================================================

                // unity's camera space is right hand coord(negativeZ pointing into screen), we want positive z ray in fragment shader, so negate it
                viewRay *= -1;

                // it is ok to write very expensive code in decal's vertex shader, 
                // it is just a unity cube(4*6 vertices) per decal only, won't affect GPU performance at all.
                float4x4 ViewToObjectMatrix = mul(UNITY_MATRIX_I_M, UNITY_MATRIX_I_V);

                // transform everything to object space(decal space) in vertex shader first, so we can skip all matrix mul() in fragment shader
                o.viewRayOS.xyz = mul((float3x3)ViewToObjectMatrix, viewRay);
                o.cameraPosOSAndFogFactor.xyz = mul(ViewToObjectMatrix, float4(0,0,0,1)).xyz; // hard code 0 or 1 can enable many compiler optimization

                return o;

完整Shader:ScreenSpaceDecal.shader

A:这里其实是想得到Object Space下,viewRay的反方向,也就是从顶点指向相机的向量。其实跟左右手坐标系关系不大,原始viewRay=vertexPositionInput.positionVS,是相机坐标系下顶点坐标(也就是相机坐标系下从相机指向顶点的向量),所以乘上-1之后,即为相机坐标系下从顶点指向相机的向量,在转换回Object Space下就是需要的viewRayOS了。

也可以这么得到o.viewRayOS.xyz:
o.viewRayOS.xyz = mul(UNITY_MATRIX_I_M, float4(GetCameraPositionWS(), 1)) - input.positionOS;

感谢范世青@UWA问答社区提供了回答,欢迎大家转至社区交流:
https://answer.uwa4d.com/question/61bc3780d8413e18eb4f4c2a


Config

Q:配置表太多占用内存过大时,除了采用Sqlite,还有什么好的解决办法?

A1:推荐轻量二进制方案,支持Lua,支持整表加载或者单条数据:
https://github.com/SihaoLiang/TableFramework

感谢梁思豪@UWA问答社区提供了回答

A2:配置二进制化后,在C++层用Mmap来读取。

感谢黄程@UWA问答社区提供了回答,欢迎大家转至社区交流:
https://answer.uwa4d.com/question/5fc8254010a17c6c2b09d48c


Android

Q:通过UnityWebRequest创建Bundle下载请求,我设置Timeout=10,Editor上断网很快就会提示request.isHttpError,设置20、10秒之后提示,设置30、20秒之后提示,但我在手机上Android/iOS无论Timeout设置多少,只要一断网,立马就会提示request.isHttpError,这是不是Bug?

Unity版本2020.3
https://forum.unity.com/threads/unitywebrequest-is-ignoring-timeout-on-ios.712271/
https://forum.unity.com/threads/unitywebrequest-timeout.539822/
好像确实无效,不知道HttpWebRequest是否有效?

A1:MSDN做了说明,这个不包含DNS的时间,解决方案里面也有:《HttpWebRequest.Timeout 属性》

感谢那个小伙-474465@UWA问答社区提供了回答,欢迎大家转至社区交流:
https://answer.uwa4d.com/question/61bbee91d8413e18eb4edaf0

20211220
更多精彩问题等你回答~

封面图来源于网络


今天的分享就到这里。当然,生有涯而知无涯。在漫漫的开发周期中,您看到的这些问题也许都只是冰山一角,我们早已在UWA问答网站上准备了更多的技术话题等你一起来探索和分享。欢迎热爱进步的你加入,也许你的方法恰能解别人的燃眉之急;而他山之“石”,也能攻你之“玉”。

官网:www.uwa4d.com
官方技术博客:blog.uwa4d.com
官方问答社区:answer.uwa4d.com
UWA学堂:edu.uwa4d.com
官方技术QQ群:793972859(原群已满员)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK