5,337
edits
No edit summary |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
== Shaders == | == Shaders == | ||
Unity | Unity shaders are written in [[HLSL]]. | ||
Unity supports the standard vertex, geometry, | Unity supports the standard vertex, geometry, fragment shader pipeline.<br> | ||
They also have their own variation of surface shaders. | They also have their own variation of fragment shaders called surface shaders which automatically handle lighting.<br> | ||
Compute | Compute shaders are useful for doing parallel computation on the GPU.<br> | ||
Results from | Results from compute shaders can be used on the graphical shaders without being copied back to the CPU.<br> | ||
=== Compute Shaders === | === Compute Shaders === | ||
To use a compute shader, add a < | To use a compute shader, add a <code>ComputeShader</code> reference to your C# script. | ||
To copy data to and from the GPU, use a < | To copy data to and from the GPU, use a <code>ComputeBuffer</code>. | ||
You can copy standard floats arrays as well as Unity Vector2, Vector3, and Vector4 structs. | You can copy standard floats arrays as well as Unity Vector2, Vector3, and Vector4 structs. | ||
<!-- | <!-- | ||
Line 21: | Line 21: | ||
</syntaxhighlight > | </syntaxhighlight > | ||
--> | --> | ||
== Optimization == | |||
=== Loading Files === | |||
All loading of streaming assets should be done in a background thread. See [https://wiki.davidl.me/view/C_Sharp#Multithreading C# Multithreading] for more details on multithreading.<br> | |||
I've found that Unity's job system doesn't perform as well as C#'s ThreadPool when stressed with thousands of small tasks so I recommend using C# APIs over Unity APIs whenever possible.<br> | |||
[https://wiki.davidl.me/view/C_Sharp#Multithreading Ciela Spike's Thread Ninja] may be used to run coroutines in the background when you only need a single, but complex, task.<br> | |||
Much of Unity's API such as <code>new Mesh()</code> and <code>new Texture2D()</code> cannot be called from background threads. I suggest caching textures as <code>byte[]</code> or <code>Color32[]</code> which can then be loaded with [https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html <code>Texture2D.LoadRawImageData()</code>]. Similarly, you can cache a Mesh by caching a struct of vertices, triangles, normals, and tangents. | |||
==References== | |||
* [https://docs.unity3d.com/Manual/index.html Unity User Manual] | |||
* [https://www.udemy.com/course/the-ultimate-guide-to-game-development-with-unity/ Basic Game Dev Course (Paid)] |