Unity3d 周分享(19期 2019.6.22 )

这篇博客汇总了近期Unity3d的相关技术更新,包括Mesh API的改进,提供更底层的控制以优化性能,Unity Job-Safe API的进展,以及FFmpeg在Unity中实时视频流的实现。此外,还提到了使用Unity实现2D灯光、GPU分析工具、Shader Graph等新特性,以及Unity的未来路线图。同时分享了多个实用工具和技巧,如使用Editor script提取类型属性以提高编辑器性能,以及在Canvas上绘图的方法等。
摘要由CSDN通过智能技术生成

选自过去1~2周 自己所看到外文内容:https://twitter.com/unity3d 和各种其他博客来源吧 

1、 #UnityTips您知道OnOpenAsset属性吗? 它允许您在项目窗口中双击资源时调用方法。 例如可以用来打开/编辑一些scriptableobject。

 

2、https://www.febucci.com/2018/11/unity-tips-collection/

  1. Multiply Inspector’s editing speed
  2. Built-in shaders
  3. MinMax Attribute
  4. Animation Curves
  5. Un-dock the Preview window
  6. Hierarchy Organization
  7. Particle System Playback time
  8. EditorOnly tag
  9. Assign a shader to a material upon creation
  10. Insert array element in the Inspector
  11. Show the content of multiple folders
  12. Customize Unity’s C# template
  13. Console Window Log Entries
  14. OnValidate
  15. Set Editor Presets as Default
  16. Change the Editor color when it’s in Play Mode
  17. Drag/select scenes in the build settings
  18. Fold the entire Hierarchy
  19. Rich text in Unity’s console
  20. Asset Store search in project window
  21. DisallowMultipleComponent & RequireComponent
  22. Graphy
  23. Drag multiple items in the inspector
  24. Unity Visual Search
  25. NaughtyAttributes
  26. Foldout
  27. Scene Icons
  28. ContextMenu
  29. Inspector Math Expressions
  30. SerializeField & HideInInspector
  31. FormerlySerializedAs
  32. AddComponentMenu
  33. MenuItem

 

 

3、根据你们这么多人的要求, 在@ Unity3d上制作了关于切割网格的教程

观看完整的教程: https://www.youtube.com/watch?v=1UsuZsaUUng&feature=youtu.be

 

4、您是否正在#unity3d中的代码中创建或修改网格? 我们可能会为此计划一些API改进,欢迎提供反馈。 请参阅google doc:Unity 2019.3 Mesh API Improvements https://docs.google.com/document/d/1I225X6jAxWN0cheDz_3gnhje3hWNMxTZq3FZQs5KqPc/edit#heading=h.vyksohcynwk5

和论坛帖子: https://forum.unity.com/threads/feedback-wanted-mesh-scripting-api-improvements.684670/

 

- 索引缓冲区Index buffers:手动指定整个索引缓冲区大小,从NativeArray设置完整或部分数据,手动设置子网格信息(拓扑,索引计数等)的能力。

- 顶点缓冲区Vertex buffers:能够手动指定顶点缓冲区布局(属性数据格式,流分割),从NativeArray设置完整或部分数据。

- 添加到现有SetFoobar网格API的Slice-like(“int start,int length”)重载。

- 添加到现有网格索引API的ushort重载,以避免索引数据的32位< - > 16位转换。

重要很重要的是 NativeArrays的重载目前非常受欢迎。

 

Problem: Unity中的Mesh脚本API是在Unity 1.5(即2006年!)中完成的,并且大部分保持不变。. 后来增加了一些 non-allocating List<T> 函数例如 (Mesh.SetVertices etc.),但整体API方法没有改变。

它主要存在性能问题,额外的内存复制,内存分配以及无法指定更多自定义数据格式:

  • 顶点数据访问基于各个属性 (positions, normals etc.), 即使内部网格可能存储它们也是交错的。
  • 无法查询或指定自定义顶点数据格式 (例如,对某些组件使用半精度float)
  • 索引缓冲区访问全部基于32位索引,即使对于使用16位索引缓冲区的网格也是如此。
  • 无法对顶点/索引缓冲区数据进行部分更新;每次调用都会替换整个数组。
  • 使用基于NativeArray <T>的数据没有很好的互操作性,这在C#Jobs / Burst / DOTS中很常见。

因此,在2019.3中,我们向Mesh类API添加了更多功能,在某种意义上,“更低级别”的API层更直接并且面向显式索引/顶点缓冲区布局和数据更新。

Let us know what you think about this! Here or on the forum thread.

 

Index Buffer API additions索引缓冲区API添加

(when: 本节中的所有内容都在进行中;尚未进行任何alpha版本)

指定索引缓冲区并更新它的数据:

void SetIndexBufferParams(int indexCount, IndexFormat format);
void SetIndexBufferData(
    NativeArray<T> data, int dataStart, int meshBufferStart, int count);

 

以上是用于设置索引缓冲区并使用数据填充/更新它。就像所有现有的Mesh API一样,这适用于索引缓冲区的系统内存表示,例如,如果你做Mesh.GetIndices,将通过SetIndexBufferData完成更新。

当然仅仅指定索引缓冲区是不够的,还必须设置网格“子网格submeshes”,其中基本上包含以下信息:: 边界框bounding box, 面拓扑face topology, 索引缓冲区范围index buffer range, 顶点缓冲区范围vertex buffer range. Mesh类已经包含subMeshCount(默认为1),除此之外:

struct SubMeshDescriptor
{
	AABB bounds;
	MeshTopology topology;
	int indexStart;
	int indexCount;
	int baseVertex;
	int firstVertex;
	int vertexCount;
};
void SetSubMesh(int index, SubMeshDescriptor desc);
SubMeshDescriptor GetSubMesh(int index);

通过上述组合,您可以完全控制索引缓冲区和网格子集信息。 SetIndexBufferParams可能(重新)为缓冲区分配内存,SetIndexBufferData更新它(或它的一部分),数据与内存中的索引缓冲区布局匹配,并且没有格式转换。 SetSubMesh可用于指定索引开始和计数,而无需调整大小或重新填充索引/顶点缓冲区。

 

Vertex Buffer API additions 顶点缓冲区API

(when: 本节中的所有内容都在进行中;尚未进行任何alpha版本)

需要指定顶点数据布局:存在哪些属性,它们的格式,通道数以及哪些属性进入哪些流(一个流中的所有内容都是经典的“交错interleaved”顶点布局;但Unity也几乎可以任意放置属性进入各种流配置)。

enum VertexAttributeFormat
{
	Float32, Float16,
	UNorm8, SNorm8,
	UNorm16, SNorm16,
	UInt8, SInt8,
	UInt16, SInt16,
	UInt32, SInt32,
};
struct VertexAttributeDescriptor
{
	VertexAttribute attribute;
	VertexAttributeFormat format;
	int dimension;
	int stream;
};
VertexAttributeDescriptor[] GetVertexAttributes();
bool                        HasVertexAttribute(VertexAttribute a);
VertexAttributeFormat       GetVertexAttributeFormat(VertexAttribute a);
int                         GetVertexAttributeDimension(VertexAttribute a);
 
bool SystemInfo.SupportsVertexAttributeFormat(VertexAttributeFormat format,
    int dimension);
 
void SetVertexBufferParams(int vertexCount,
    params VertexAttributeDescriptor[] attributes);

以上内容可用于查询网格数据布局(已经可能对GetNativeVertexBufferPtr有用),或者用于显式设置顶点缓冲区的大小和布局。请注意,未指定顶点数据属性的各个偏移量;目前在Unity中,它们总是被隐式计算为以预定义的顺序 (positions, normals, tangents, colors, texture coordinates, blend weights, blend indices)一个接一个地进行计算。

许多API /平台要求顶点组件的大小为4个字节的倍数,因此我们在SetVertexBufferParams级别强制执行此操作。例如: Float16 with dimension=3 is invalid (6 bytes), or UNorm8 with dimension=2 is invalid too.

使用NativeArray <T>源对每个流进行单独调用来更新顶点数据,其中T是必须与顶点数据布局匹配的结构(这可能意味着C#侧的结构可能需要StructLayout(顺序)属性,也许有Pack字段设置)。

void SetVertexBufferData(
    NativeArray<T> data, int dataStart, int meshBufferStart, int count,
    int stream=0);

与SetIndexBufferData一样,这会更新顶点缓冲区或其中一部分的系统内存表示,并且不会执行任何数据转换。

 

Existing old-style API additions and other small things现有的旧式API 和其他小东西

(when: 本节中的所有内容都在2019.3 alpha 4中)

为了保持一致性并且因为它被请求,我们还为现有的“旧”样式Mesh API添加了更多重载。

顶点数据设置器(SetVertices,SetNormals等)具有 “切片式 slice-style” API,其使用所提供的阵列的一部分或列表用于新的顶点数据。所有顶点属性 SetFoo(List<T>) API也得到:

  • SetFoo(T[])
  • SetFoo(T[], int start, int length)
  • SetFoo(List<T>, int start, int length)

类似地,索引数据设置器(SetTriangles和SetIndices)得到切片式重载:

  • SetIndices(int[] indices, int start, int length, …)
  • SetIndices(List<int> indices, int start, int length, …)

处理索引数据的现有函数在没有额外转换的情况下获得了超载以处理16位索引:

  • Add ushort[] and List<ushort> overloads to SetIndices
  • Add ushort[] and List<ushort> overloads to SetTriangles
  • Add List<ushort> overload to GetIndices

对Mesh API各个部分的脚本文档进行了调整,使其更加准确,详细,并在更多地方提及其他相关API。

 

Not Done Yet, But We Are Thinking About It尚未完成,但我们正在思考它

将网格缓冲区用于计算着色器会很好。也许是以下列方式之一:

  • 可选的标志,使网格缓冲区可以计算-writable/readable, 以及从网格中获取ComputeBuffer或GraphicsBuffer的方法,或者
  • 一种从用户提供的ComputeBuffer或GraphicsBuffer对象和一些额外信息创建 “外部external” Mesh的方法。类似于如何创建“外部”纹理。

 

直接NativeArray访问网格顶点/索引缓冲区,没有由SetVertexBufferData / SetIndexBufferData引起的memcpy。这可能与NativeArray Texture2D.GetRawTextureData的工作方式类似。

 

将“Flags”参数添加到SetVertexBufferParams / SetIndexBufferParams。可能包含以下标志:

  • “Dynamic buffer”, replacing Mesh.MarkDynamic
  • “Compute”, see above wrt Compute Shader usage
  • “Keep/convert previous data”, try to convert previously existing data

将“Flags”参数添加到SetVertexBufferData / SetIndexBufferData。可能包含以下标志:

  • “Update bounds”, optional automatic bounds calculation
  • “Check”, to validate if indices are not out of range (always done in old Mesh API)
  • “Update vertex ranges”, optional automatic SubMeshDescriptor vertex range calculation

 

 

5、

Unity 2018.3中的作业安全API

Unity 2019.1中的Job-Safe API

https://jacksondunstan.com/articles/5029

https://jacksondunstan.com/articles/5239

所以对于今天,我们现在有一个小的Unity编辑器脚本,它将为我们生成列表。这是它的作用:

1查找.dllUnity安装目录的Managed子目录中的所有文件

2打开它们作为 Assembly

3扫描所有方法和属性

4检查[NativeMethod]属性是否存在

5调用get给NativeMethodAttribute.IsThreadSafe

6输出所有匹配项Debug.Log和系统剪贴板

2018.3高达301作业安全的API,但很多人都private还是internal ,因此必须通过一个间接调用public API。总而言之,自Unity 2018.2以来,它只是九种方法和属性的扩展。但是,Unity越来越依赖于软件包来提供新功能,例如当前处于预览版的ECS。要使用此脚本检查任何其他库,只需更改paths变量以包含其目录。

Unity 2019.1.3f1运行相同的脚本会生成350个函数的列表:

值得注意的是,其中许多功能都是私有或内部功能。许多人甚至在他们的名字中都有“内部”这个词。虽然这些仍然可以通过反射在技术上调用,但更典型的方法是在公共API中调用某个函数,然后直接或间接地调用私有函数或内部函数。

找到如何调用这59个新的作业安全功能必须根据具体情况进行,但很容易看出引擎整体上变得更加安全。这对于Unity中高性能代码的未来来说是一件好事,特别是现在我们拥有专门用于C#作业的Burst编译器。

 

 

 

 

6、 ffmpeg stream raw video into unity Texture2D

ffmpeg将原始视频流转换为Unity的Texture2D

下载 https://ffmpeg.zeranoe.com/builds/: Architecture Windows 64-bit

客户端代码: https://github.com/unitycoder/ffmpegStreamToUnity

运行Unity项目 。

然后命令行执行 :

 

unity项目,它应该显示接收到的数据。 ffmpeg会捕获桌面的内容 流式传输到Unity材质texture2D

接下来的步骤

- TCP版本可能修复丢失帧/不同步问题

- 使用正确的容器mpegts左右,修复udp遗漏帧等问题,但难以解码mpeg4有效载荷数据

https://unitycoder.com/blog/2019/05/26/ffmpeg-stream-raw-video-into-unity-texture2d/

 

Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能

 

 

 

 

 

7、 为着色器或脚本启用HDR颜色选择器

我需要一些旧的Unlit / Color-shader的HDR颜色,而对于c#脚本,也发现你可以为着色器添加这个属性:

// BEFORE (no hdr 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值