Shader Programming Learning Note (iv) organization of--unity Shader (Shaderlab)

Source: Internet
Author: User

Organization of Unity Shader

Unity Shader's form

  Unity's Official Handbook says unity Shader has three different authoring scenarios, three of which are surface shaders, vertex and fragment shaders, and fixed function shaders. As you can see from the previous notes, there are two main parts of the programmable graphics pipeline that can be written shader: vertex shader and fragment shader, but unity also has surface shaders and fixed function Shaders

For fixed function shaders, the meaning of the surface is to understand the fixing pipeline shader, before the programmable pipeline hardware appears, many of the light flow calculation will be placed in the hardware for processing, we regard the fixed pipeline function as corresponding to the fixed pipeline hardware operation, This shader function is very conservative, such as enabling simple lighting, simple texture sampling, and so on, for the vast majority of hardware now can be well supported.

Surface shaders is the recommended and encouraged shader in unity, and when you create a shader in Unity (Unity5), the default code is to use surface shaders. So why do you have a shader like surface shaders? What does it have to do with vertex and fragment shaders? We can understand that the programmable graphics pipeline can be identified as two shader programs: vertex shader and fragment shader, as for surface shader in vertex shader and fragment Shader a wrapper above, the unity engine will eventually compile the surface shaders code into vertex and fragment shaders that can be identified and invoked by the hardware.

Unity's official recommendation is to read the basic concepts of Shaderlab syntax before learning shader, and then read about surface shaders or vertex and fragment shaders. Fixed function shaders can only be written using shaderlab syntax, while surface shaders and vertex and fragment shaders are not limited to shderlab syntax, CG/HLSL is available, It can even be written in GLSL, a piece of code embedded in Shaderlab.

SHADERLAB Basic Structure

  Shaderlab is a program that unity has customized specifically to write shader. Using Shaderlab, the three shader mentioned above can be written in the same format, which will not lead to different syntax for writing different shader. In the previous article, we made an image of the relationship between shader, materials, and textures, such as: material is the object to be used eventually, shader is a program about how to process and process, it is a kind of procedure fragment, as for the map, color and so on they belong to the material of this processing solution. So when you want to build a shader, to use it in the material used, both need to have an algorithm, but also need to add some raw materials, first to understand the main structure of Shaderlab, the following is the basic structure of Shaderlab:

" name " {    [Properties]    subshaders    [FallBack]}

  The properties function allows you to define the required parameters for the material in the Unity Material (material) inspector, such as colors, decals, parameters, etc., which are raw materials and used by shader programs.

The Subshader function is a shader program fragment written specifically for GPU rendering, with or without at least one subshader in one shader, or multiple subshader, but only one of them can be selected at execution time. So why allow multiple subshader to exist at the same time? The reason is that an algorithm or an instruction in the shader may not support the current hardware, such as a shader program has multiple Subshader, when the shader program is executed, the first to detect the current hardware can be intact to support the first Subshader, If the first subshader can be well supported by the current hardware, then the current subshader is used, and if not, then the next subshader is detected. In writing the shader, if it is not certain that the current subshader in some old hardware is well supported, you can write a second subshader to adapt to these old, slightly worse graphics hardware, of course, you can continue to write a third, fourth, fifth, etc. The Subshader function to be written is simplified in order and the operation instruction is simple in order to satisfy more hardware support. If you write a subshader that doesn't support your current hardware, you'll need to use Shaderlab's "FallBack" functionality.

Fallback means to roll back. If the current hardware is unable to support all subshader, unity will roll back the current shading and roll back to the shader specified by fallback, so the fallback instructions follow generally the system comes with a relatively simple, can be performed by the vast majority of hardware shader. Here are a few examples of Unity's official built-in shader, with more specific information available in the Official Unity Handbook.

Unity build-in Shader

    • unlit. This is just a texture,not affected by any lighting. ( not illuminated.) This is just a texture, not affected by any light )

  This is the simplest shader in unity and is very efficient to perform, and the shader is often used in the UI system.

    • vertexlit. ( vertex illumination )

  The shader has the ability to render light on vertices.

    • diffuse. ( Diffuse reflection )

  Diffuse reflection is also a form of illumination, but it does not only perform light calculations on vertices, but also light calculations in the fragment program.

    • Normal Mapped. This was a bit more expensive than diffuse:it adds more texture (normal map), and a couple of shader instructions. ( normal map, More expensive than diffuse: one or more textures (normal map) and several shader structures are added

  Normal mapping technology is a more traditional graphics rendering technology, through a poster for sampling calculation, but this map is not a common map, but a map of the storage of normal vector, the data sampled from this image as a practice vector, and then the light calculation, the main purpose is when a few models of the number of patch vertices, When the composition of the patch is not much detail, in order to express rich details, you can use normal map to compensate for such details, the use of normal map shader is very high.

    • specular. This adds specular highlight calculation. ( high light. Added special high-light calculation )

  Specular refers to specular high light reflection, mainly used to simulate smooth objects, such as metal, glass and so on.

    • Normal Mapped Specular. Again,this is a bit more expensive than specular. ( high-gloss normal map. A little more expensive than high light )

  The shader combines the above two points with both normal and high-gloss displays.

    • Parallax Normal mapped. This adds parallax normal-mapping calculation. ( parallax Normal map. Added parallax normal map calculation )
    • Parallax Normal Mapped specular. This adds both Poarallax normal-mapping and Specluar highlight calculation. ( parallax High-light normal map. Added parallax normal map and specular highlight calculation )

  Parallax is the visual difference, the comparison of image interpretation is when the left eye and the right eye respectively to see the same object, the two eyes see the result is not the same, it is because of the left eye and the right eye has a distance of about three centimeters, people see the world is three-dimensional, three-dimensional.
So what is the function of parallax normal mapping? The reason is that when the normal map map mapping, is to better represent the details of the object, the normal data stored in the texture, but the texture is immutable, when the object is rendered, will get a better result, showing the details of the object bump, so the normal map in some textbooks also called bump mapping, But when the object with normal map is rotated, it is supposed that some places because of the angle of view deviation is not see its concave-convex shape or to see the bump feeling will be more intense, because the normal map is a fixed map, will not achieve such an effect, and parallax normal map of the role of compensating for such defects.

Shader Programming Learning Note (iv) organization of--unity Shader (Shaderlab)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.