似乎是在Unity5.4中开始支持GPU Instacing,但如果要比较好的使用推荐用unity5.6版本,因为这几个版本一直在改。

这里测试也是使用unity5.6.2进行测试

在5.6的版本里,Instancing不再是一个单独的shader,而是一个开关。

如果使用Instancing功能需要开启,否则调用相关接口时会报错

默认情况下,多个一样的模型会被动态批次合并优化掉,动态批次合并有很多种情况不可用,其中一种就是镜像的情况。

这里用镜像后的实例模型和GPU Instancing做比较

注意,在Unity5.6.2或者之后的版本中,只要材质球勾选Instancing,即自动开启并使用GPU Instancing。

GPU Instancing大致代码如下(用Graphics一次性调用减少了层级对象的创建开销):

void Update()
{var meshRenderer = template.GetComponent<MeshRenderer>();var meshFilter = template.GetComponent<MeshFilter>();var mesh = meshFilter.sharedMesh;var matrices = new Matrix4x4[instanceCount];for (int i = 0; i < matrices.Length; i++){var position = Random.insideUnitSphere * range;var rotation = Quaternion.LookRotation(Random.insideUnitSphere);var scale = Vector3.one * Random.Range(-2f, 2f);var matrix = Matrix4x4.TRS(position, rotation, scale);matrices[i] = matrix;}Graphics.DrawMeshInstanced(mesh, 0, meshRenderer.sharedMaterial, matrices);
}

是实时随机的位置,会看见只有13个Batches.

常规实例化测试脚本(挂了正弦运动脚本,注意镜像反转,使其无法动态批次合并):

for (int i = 0; i < instanceCount; i++)
{var instancedTemplate = Instantiate(template);instancedTemplate.transform.position = Random.insideUnitSphere * range;instancedTemplate.transform.forward = Random.insideUnitSphere;instancedTemplate.transform.localScale = Vector3.one * Random.Range(-2f, 2f);
}

大概在1020个Batches

另外我还打了个APK包测了下,居然能在我的红米3S上跑。这就有点厉害了

那么GPU Instacing其实也有一些限制的,比如不支持蒙皮网格等(不过资源商店有一个Animation Instacing: 链接)

(补充:Unity官方开源了一个Animation Instacing: https://blogs.unity3d.com/cn/2018/04/16/animation-instancing-instancing-for-skinnedmeshrenderer/)

这些支持信息在官网的页面都有罗列 https://docs.unity3d.com/Manual/GPUInstancing.html

硬件需求:

GPU Instancing is available on the following platforms and APIs:

  • DirectX 11 and DirectX 12 on Windows

  • OpenGL Core 4.1+/ES3.0+ on Windows, macOS, Linux, iOS and Android

  • Metal on macOS and iOS

  • Vulkan on Windows and Android

  • PlayStation 4 and Xbox One

  • WebGL (requires WebGL 2.0 API)

模块间的需求(没找到原版的帖子,翻译版摘抄一段):

下列情况不能使用Instancing:

  • 使用Lightmap的物体
  • 受不同Light Probe / Reflection Probe影响的物体
  • 使用包含多个Pass的Shader的物体,只有第一个Pass可以Instancing前向渲染时,受多个光源影响的物体只有Base Pass可以instancing,Add Passes不行

另外,默认的DrawMeshInstanced有1024实例数的限制

需要DrawMeshInstancedIndirect,而这个接口依赖ComputerShader,一些平台不支持。

然后再测一下GPU Instanced Indirect,也就是DrawMeshInstancedIndirect这个接口

似乎是借助ComputerShader实现超过1024数量的Instancing

下图为3万个Cube:

代码和shader我做了点修改,大致如下:

void Update()
{// Update starting position bufferif (cachedInstanceCount != instanceCount) UpdateBuffers();for (int i = 0; i < mPositions.Length; i++){float angle = Random.Range(0.0f, Mathf.PI * 2.0f);float distance = Random.Range(20.0f, 100.0f);float height = Random.Range(-2.0f, 2.0f);float size = Random.Range(0.05f, 0.25f);mPositions[i] = new Vector4(Mathf.Sin(angle) * distance, height, Mathf.Cos(angle) * distance, size);}positionBuffer.SetData(mPositions);instanceMaterial.SetBuffer("positionBuffer", positionBuffer);// Render
    Graphics.DrawMeshInstancedIndirect(instanceMesh,0,instanceMaterial,new Bounds(Vector3.zero, new Vector3(100.0f, 100.0f, 100.0f)),argsBuffer);
}

shader需要额外定制,这点比较蛋疼。如果换成standard读不到positionBuffer这种结构。

DrawMeshInstancedIndirect的具体使用案例,可以参考这两个链接:

https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html

https://github.com/tiiago11/Unity-InstancedIndirectExamples

补充测试工程地址: https://gitee.com/Hont/GPUInstancingTest

(unity2017.4)

扩展阅读:

Geometry instancing

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

Unity中的批处理优化与GPU Instancing

http://forum.china.unity3d.com/thread-22714-1-3.html

转载于:https://www.cnblogs.com/hont/p/7143626.html

Unity GPU Instancing的使用尝试相关推荐

  1. unity gpu instancing

    Unity gpu instancing unity可以自动合并相同的material对象渲染 将对应shader enable instancing的选项勾上 本文说明一下直接调用unity api ...

  2. 使用GPU Instancing屏幕花屏问题

    1)使用GPU Instancing屏幕花屏问题 2)如何优化AssetBundle大小 3)如何使用GPU Skinning提升性能 4)iOS上Shader里tex2D采样偏移的问题 5)如何管理 ...

  3. 【Unity游戏开发】静态、动态合批与GPU Instancing

    https://zhuanlan.zhihu.com/p/356211912 前言 动态合批与静态合批其本质是对将多次绘制请求,在允许的条件下进行合并处理,减少cpu对gpu绘制请求的次数,达到提高性 ...

  4. Unity的GPU Instancing

    Unity的GPU Instancing GPU Instancing可以用来批量绘制大量相同几何结构相同材质的物体,以降低绘制所需的batches.要想在Unity中使用,首先需要至少在shader ...

  5. unity SRP Batcher与GPU instancing使用情况

    SRP Batcher更合适大量不同的物体, 比如材质上用了不同的贴图.参数.等等,只要shader变种不变,即使不同材质也能合并: GPU instancing 必须同材质同参数,只是可以自定义ma ...

  6. 每天执行一次批处理_关于静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析...

    静态批处理[1] 定义 标明为 Static 的静态物件,如果在使用相同材质球的条件下,在Build(项目打包)的时候Unity会自动地提取这些共享材质的静态模型的Vertex buffer和Inde ...

  7. 闲云野鹤:吃鸡(一)之场景制作:使用GPU instancing方式制作刷草插件

    用GPU instancing方式制作刷草插件(unity版本8.2.2) 先上最终效果图(欢迎加我qq交流:358641634): 十种草 混刷生成比较自然的场景(带阴影.风力.草可见距离可调) 插 ...

  8. unity Gpu Instance

    参考网址: https://blog.csdn.net/leonwei/article/details/73274808 https://docs.unity3d.com/ScriptReferenc ...

  9. Unity3D GPU Instancing测试

    GPU instancing 很早就支持手机了(Android只支持Opengl ES 3.0),最近在调研这个就对它测试了一下. 如果是不动的物体勾选static静态合并批次(40-50帧率) 自定 ...

最新文章

  1. 《父亲家书》选:给初为人师的儿子
  2. python求投影距离_python实现高斯投影正反算方式
  3. 全球通信云市场爆发增长,RTC 技术普惠还有多远
  4. geotrellis使用(三十)使用geotrellis读取PostGIS空间数据
  5. 抓取html的内容,js获取网页选中内容(包含html代码)
  6. Java如何解决乱码问题
  7. 自定义Hibernate Validator规则注解
  8. 1.1.29 加入项目符号后换行文字未对齐
  9. 一般柱子与柱子的距离_97讲珠宝展柜制作厂家谈谈珠宝展柜一般的尺寸是多少...
  10. CentOS 6.3下Apache+SVN部署Web版本同步
  11. 文本居于图片左侧html,CSS实现图片与文本的居中对齐的常见方式
  12. 学习笔记_vnpy实战培训day04
  13. git统计每个人的代码行数_程序员实用工具,推荐一款代码统计神器GitStats
  14. xp等系统的登陆的密码清除方法
  15. session 分布式处理-----https://segmentfault.com/a/1190000013447750?utm_source=tag-newest
  16. 1个平方大概多少立杆_普通水泥地面一平方大概要用多少地板漆
  17. PMP证书考试试题有多少
  18. SVPWM算法理解(一)——基本原理
  19. android自定义素材拼图,美图秀秀Android新版 拼图排版秀北爱
  20. JAVA光头之路(一)--环境变量

热门文章

  1. 算法分析与设计 —— 贪心算法「活动安排」「背包问题」「哈夫曼编码」
  2. TensorFlow2.0(七)--基础API使用
  3. 带你了解FPGA(1)--一些需要了解的概念
  4. matlab 鼠标自定义选中图像的任意区域
  5. java模拟摇摆小球程序代码_Android实现左右摆动的球体动画效果
  6. ubuntu更改屏幕界面大小和中文
  7. js控制scss的变量_web前端:js如何操作sass里的变量及calc 使用sass变量
  8. java 获取网卡信息_java 使用 java.net.InterfaceAddress 获取网卡信息
  9. 西门子smartclient怎么用_Smart Client学习体会(一) Smart Client介绍
  10. ios上input框上边有阴影