Unity3d 周分享(22期 2019.8.30 )

这篇博客汇总了多个关于Unity3d的技术要点,包括Transform性能优化、使用ParticleSystems MinMaxCurve和MinMaxGradient、UnityNuGet项目、使用乘法代替除法提高性能、精灵字段的可视化编辑器优化、四元数的结构与应用、可寻址资产系统、Android硬件缩放技术、基于Chrome DevTools的Lua调试器、多线程同步I/O和单线程异步I/O的讨论。内容丰富,涵盖了Unity3d开发中的多个重要方面。
摘要由CSDN通过智能技术生成

选自过去1~2周 自己所看到外文内容:https://twitter.com/unity3d 和各种其他博客来源吧        早就有了,抱歉才发! 

1、 Unity Transform 性能优化摘要

https://qiita.com/sator_imaging/items/ff5811885f515a0a4998

由于我有机会在逐帧的基础上处理大量的Transform ,我想总结一下Unity中更新Transform 的方法以及比较和优化性能的方法。

 

这意味着值得考虑是否有数百或更多个Transform 的 .position 或 .rotation / .eulerAngles 。

 

  • Unity 2018.4.0f1(Unity 2018 LTS)
  • Windows 10 64bit @ Intel Core i9-9900K 3.6GHz

 

始终在rotation / eulerAngles中缓存对xyzw的访问

有些数据Transform中是没有缓存的!

vector3.x = 0 和 transform.position.x = 0 看起来访问方式很像。但实际上很大差别。 即使你缓存了Transform (高版本的Unity已经在底层进行了缓存,不需要开发者进行缓存了)

..... = new Vector3(cache.eulerAngles.x, cache.eulerAngles.y, cache.eulerAngles.z);

例如上,如果您访问成员三次,将生成三个Vector3副本。

 

在Transform.position,Transform.rotation,Transform.eulerAngles等中,getter不是在字段中而是在属性中设置,每次访问时都有结构体拷贝。 原因是Unity的Transform 存有的是 local 相关的数据, 如果访问世界坐标下的内容 必须现算!!!!!

localRotation是最快的

四元数比欧拉角更快

本地空间比世界空间更快

.localRotation,比.rotation, .localEulerAngles比.eulerAngles快。

Transform.Rotate()要好于 localEulerAngles + =

调整偏移等很容易,因为Euler的角度更直观,.localEulerAngles += .....但Transform.Rotate()更快。

当数量很大时,会产生相当大的性能影响。

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

 

如果可用,请使用HumanPoseHandler.SetHumanPose()

如果目标是Humanoid,则HumanPoseHandler的SetHumanPose可能更快。

https://github.com/Bizcast/NoitomHi5InertiaToFingerMuscle

摘要

  • 我们尽可能地不处理欧拉角。
  • 将值设置为.eulerAngles / .localEulerAngles 或者访问成员,性能将极度下降。
  • 访问Quaternion / Vector 3的成员是昂贵的,应始终缓存。
    • 缓存Quaternion/ Vector3而不是Transform。

四元数很快

如果添加偏移 = Quaternion * Quaternion是最快的

严禁访问Quaternion / Vector3的xyzw成员

  • 访问.rotation的每个成员而不缓存(4次访问)
    • : .rotation = transform.rotation.xyzw + Time.time;
      • 约200fps
    • 使用缓存后 大约320 fps
      • : .rotation = cachedRotation.xyzw + Time.time;
  • 访问.eulerAngles的每个成员而不进行缓存(3次访问)
    • 伪: .eulerAngles = transform.eulerAngles.xyz + Time.time;
      • 约110fps
    • 使用缓存后 大约190 fps
      • 伪: .eulerAngles = cachedEulerAngles.xyz + Time.time;

 

 

 

 

2、您知道可以在自己的脚本中使用ParticleSystems MinMaxCurve和MinMaxGradient。 它序列化并包含编辑器支持!

 

 

 

 

3、刚刚发布了OSS项目UnityNuGet https://github.com/xoofx/UnityNuGet ...提供服务,通过Unity Package Manager使用作用域注册表将@nuget软件包安装到@ unity3d项目中。 这真的是一个早期的预览。

https://github.com/xoofx/UnityNuGet

 

 

 

 

 

 

 

4、#unitytips 不要忘记100 * 0.5比100/2更快。 可以的话,用乘法代替除法。 (这适用于任何引擎,而不仅仅是Unity。)

https://twitter.com/ffs_nonamesleft/status/1143436080580112384

“注意: 他举得例子不好都是常量计算, 现代编译器都会进行常量展开(编译时直接计算结果了)”

 

还可以 看看 位运算 `100 >> 1` 可能 是最快的!!!

在值不是常数的情况下,iOS上的乘法速度是除法的两倍。在Unity编辑器中,它的速度提高了约20%。

 

 

 

 

 

 

 

 

 

5、 快速脚本只是为了更好地可视化编辑器中的精灵字段。 使用PropertyDrawer将您的精灵显示为缩略图。

将脚本复制到“Assets / Editor”文件夹。

脚本: https://pastebin.com/Y1R7eSA0

建议您在使用drawer 之前添加对hasMultipleDifferentValues的检查,以防止它覆盖在检查器窗口中多选时所选的所有图像

https://pastebin.com/UipmTQKg

 

 

 

 

 

 

 

6、 关于四元数 Quaternion 的结构:

在四元数可视化中找到了一个存储库,我将在本文后面使用它:

https://github.com/uvivagabond/Visualization-of-physics-in-Unity

在Scenes 文件夹中打开名为TestForQuaternions 的场景。

表示旋转的方法有很多种。借助旋转矩阵,四元数和其他表示,可以借助于欧拉角度来表示旋转。

在本系列中,我将重点介绍四元数。我主要关注他们在Unity中的实现和操作。

https://www.youtube.com/watch?v=OBTU0v0MeeQ

参数x ,y 和z 负责旋转轴的方向。

AngleAxis方法()此方法允许您基于给定(绝对)旋转轴和围绕此轴旋转的角度创建四元数。

我们将四元数的乘法视为连续的身体旋转。A * B≠B * A. 四元数从右向左相乘

欧拉角表示围绕全局坐标系的轴的旋转,即围绕全局轴z ,x 和y 的旋转。

 

四元数的可视化:

https://quaternions.online/

几乎没有困难的四元数描述

https://github.com/NickCuso/Tutorials/blob/master/Quaternions.md

来自fspace的四元数描述

https://www.f-sp.com/entry/2017/08/11/194125

Unity Answers(Bunny 83)

https://answers.unity.com/questions/1297214/quaternions-not-exact.html

https://answers.unity.com/questions/1417640/get-local-euler-angles-by-quaternion.html

https://answers.unity.com/questions/1496797/multiplying-quaternions-and-multiplying-quaternion.html?childToView=1497375#comment-1497375

https://answers.unity.com/questions/1551885/is-quaternion-really-do-not-suffer-from-gimbal-loc.html?childToView=1551892#answer-1551892

https://answers.unity.com/questions/1125215/how-to-flip-a-quaternion-to-face-the-opposite-dire.html?childToView=1496615#comment-1496615

https://answers.unity.com/questions/1365419/locking-rotation-world-z-axis-quaternions-are-shav.html

 

数学描述

https://www.vcalc.com/wiki/KurtHeckman/Quaternion+Calculator+Collection

Mateusz Kowalski

https://www.youtube.com/watch?v=ZgOmCYfw6os

其他

https://www.gamasutra.com/view/feature/3278/rotating_objects_using_quaternions.php

https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation

 

 

 

7、 官方博客: 可寻址资产系统

已经宣布 脱离预览版了 ~~~~

https://blogs.unity3d.com/2019/07/15/addressable-asset-system/

这个支持分析工具也很到位: 比如提供引用计数 功能。

  • Play Mode Simulation: assets load directly from disk without requiring a build process, while still providing the ability to debug asset reference counts through the profiler.
  • Fast Incremental Builds: Addressables ships with the newScriptable Build Pipeline that improves the speed and reliability of incremental builds.
  • Editor Hosting Services: A built-in facility for serving Addressable content to local, LAN connected devices provides a fast, convenient method for iterating content on device.

 

 

 

 

8、 Unity3D研究院之使用Android的硬件缩放技术优化执行效率

原理如下 http://android-developers.blogspot.it/2013/09/using-hardware-scaler-for-performance.html

https://developer.nvidia.com/content/android-development-using-hardware-scaler-performance-and-efficiency

https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/HardwareScalerActivity.java

https://forum.unity.com/threads/interesting-article-from-google-on-android-hardware-scaler.202625/

这两个文章可以对 分辨率有更好的理解!!!

https://www.cinemablend.com/games/Xbox-One-Unlikely-Hit-1080p-With-DX12-Says-Witcher-3-Dev-66567.html

https://www.cinemablend.com/games/DirectX-12-May-Help-Xbox-One-Much-According-Developer-64410.html

https://www.cinemablend.com/games/1080p-Hard-Achieve-Xbox-One-Due-Complex-GPU-Says-Xbox-Director-62893.html

根据开发人员的说法,DirectX 12可能无法帮助Xbox One

Witcher 3 Dev表示Xbox One不太可能通过DX12达到1080p

当人们说“PS4比Xbox One强大50%”时,这就是他们所谈论的内容的一部分。PS4的GPU上有更多着色器单元,因此每帧输出的视觉效果比Xbox One更多。这就是为什么游戏将更频繁地在PS4上达到1080p

此外,当Xbox One上的游戏可以达到1080p时,通常它会这样做而牺牲其他正在渲染到屏幕上的东西。简而言之,每一块硬件都具有技术上限,而Xbox One的GPU由于较旧和较低等级的架构而具有如此低的上限,因此难以在游戏中始终如一地达到1080p。

最终,DirectX 12之前曾被提及为更好的CPU流水线指令,而不是检修GPU的运行方式。(还是跟硬件有关呗,这是因为GPU较弱,ESRAM有限)

这就像尝试使用软件优化来使GTX 7800达到2K分辨率。是的,这是可能的,但你必须做出巨大的牺牲才能在屏幕上显示什么来达到分辨率。你不能用软件神奇地升级硬件; 优化不能解决这个问题。

Xbox 360的1040x600分辨率与Xbox One的1408x792输出相差500,736像素。战地4在Xbox One上的720p相比PS4的900p输出相差518,400像素。

 

Unity 4.x 的代码中确实有使用:

 

    // $TODO Make this less complicated ;)
    // This code is unnecessarily complicated, just to cover the fact that it can be called both from
    // native code (through scripts Screen.SetResolution) and from Java (GLThread onSurfaceChanged).
    protected void setScreenSize(final int w, final int h, final boolean low_profile)
    {
        final SurfaceView view = mGlView;
        Rect rect = view.getHolder().getSurfaceFrame();
        if (Log.LOG_INFO)
            Log.Log(Log.INFO, String.format ("setScreenSize: %dx%d (%dx%d / %dx%d)",
                                          w, h, rect.width (), rect.height (),
                                          view.getWidth (), view.getHeight ()));

        // If w/h matches the view, or if w/h is 0/0, we use the layout size (and disable any fixed-size surface holder)
        final boolean reset = view.getWidth() == w && view.getHeight() == h || w == 0 && h == 0;
        // If w/h is -1/-1, we keep whatever was set before (no change to screen resolution)
        final boolean keep = w == -1 && h == -1;
        if (!keep)
        {
            if (reset)
            {
                if (Log.LOG_INFO) Log.Log(Log.INFO, "setScreenSize: setSizeFromLayout()");
                mSurfaceFixedW = 0;
                mSurfaceFixedH = 0;
            }
            else
            {
                if (Log.LOG_INFO) Log.Log(Log.INFO, "setScreenSize: setFixedSize(" + w + ", " + h + ")");
                mSurfaceFixedW = w;
                mSurfaceFixedH = h;
            }
        }
        else
        {
            boolean fixed = (mSurfaceFixedW != 0 || mSurfaceFixedH != 0);
            if (fixed)
            {
                if (Log.LOG_INFO) Log.Log(Log.INFO, "setScreenSize: keeping fixed size " + mSurfaceFixedW + "x" + mSurfaceFixedH);
            }
            else
            {
                if (Log.LOG_INFO) Log.Log(Log.INFO, "setScreenSize: keeping layout size " + view.getWidth() + "x" + view.getHeight());
            }
        }

        runOnUiThread(new Runnable () {
            public void run () {
                if (!keep)
                {
                    if (reset)
                        view.getHolder().setSizeFromLayout();
                    else
                        view.getHolder().setFixedSize(w, h);
                    view.invalidate();
                }
                if (HONEYCOMB_SUPPORT)
                    HONEYCOMB.setSystemUiHidden(UnityPlayer.this, low_profile);
            }
        });
    }

 

 

9、基于 Chrome DevTools 的 Lua 5.3 调试器

https://mare.js.org/zh-cn/?tdsourcetag=s_pcqq_aiomsg

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值