The more computations and processing your shaderA program that runs on the GPU. More info
See in Glossary code needs to do, the more it will impact the performance of your game. For example, supporting color per material is nice to make a shader more flexible, but if you always leave that color set to white then useless computations are performed for each vertex or pixelThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary rendered on screen.
The frequency of computations will also impact the performance of your game. Usually there are many more pixels rendered (and subsequently more pixel shader executions) than there are vertices (vertex shader executions), and more vertices than objects being rendered. Where possible, move computations out of the pixel shader code into the the vertex shaderA program that runs on each vertex of a 3D model when the model is being rendered. More info
See in Glossary code, or move them out of shaders completely and set the values in a script.
Surface ShadersA streamlined way of writing shaders for the Built-in Render Pipeline. More info
See in Glossary are great for writing shaders that interact with lighting. However, their default options are tuned to cover a broad number of general cases. Tweak these for specific situations to make shaders run faster or at least be smaller:
approxview
directive for shaders that use view direction (i.e. Specular) makes the view direction normalized per vertex instead of per pixel. This is approximate, but often good enough.halfasview
for Specular shader types is even faster. The half-vector (halfway between lighting direction and view vector) is computed and normalized per vertex, and the lighting function receives the half-vector as a parameter instead of the view vector.noforwardadd
makes a shader fully support one-directional light in Forward renderingA rendering path that renders each object in one or more passes, depending on lights that affect the object. Lights themselves are also treated differently by Forward Rendering, depending on their settings and intensity. More infonoambient
disables ambient lighting and spherical harmonics lights on a shader. This can make performance slightly faster.When writing shaders in Cg/HLSL, there are three basic number types: float
, half
and fixed
(see Data Types and Precision).
For good performance, always use the lowest precision that is possible. This is especially important on mobile platforms like iOSApple’s mobile operating system. More info
See in Glossary and Android. Good rules of thumb are:
float
precision.half
precision. Increase only if necessary.fixed
precision.In practice, exactly which number type you should use for depends on the platform and the GPU. Generally speaking:
float
precision, so float/half/fixed
end up being exactly the same underneath. This can make testing difficult, as it’s harder to see if half/fixed precision is really enough, so always test your shaders on the target device for accurate results.half
precision support. This is usually faster, and uses less power to do calculations.Fixed
precision is generally only useful for older mobile GPUs. Most modern GPUs (the ones that can run OpenGL ES 3 or Metal) internally treat fixed
and half
precision exactly the same.See Data Types and Precision for more details.
The fixed-function AlphaTest - or its programmable equivalent, clip()
- has different performance characteristics on different platforms:
On some platforms (mostly mobile GPUs found in iOS and Android devices), using ColorMask to leave out some channels (e.g. ColorMask RGB
) can be resource-intensive, so only use it if really necessary.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.