This series focuses on Unity Shaders and Effects Cookbook (thanks to the author of the book), plus a bit of personal understanding or outreach.
Here are all the illustrations of this book. Here is the code and resources required for this book (you can also download it from the website, of course).
========================================== Split Line ==========================================
write in front
in the previous article, we learned some techniques to initially optimize the shader. This time, we learn more techniques to achieve a more complex shader:normal-mapped specular Shader. These techniques include: Two new variables halfasview or approxview using the illumination function , reduce the number of maps used, and better compress the map.
preparatory work
- Create a new scene and a sphere, adding a parallel light.
- Create a new shader and material that you can name as Mobileshader.
- Assign shader to material and give material to the sphere.
Implement
- first, or modify < Span style= "color: #3363a4;" >properties block. In this section we need a diffuse map whose alpha channel value corresponds to the smoothness of the Pixel (Gloss), and a normal map, and a slider bar for the specular index.
properties {_diffuse ("Base (RGB) specular Amount (A)", 2D) = "White" { }_normalmap ("Normal Map", 2D) = "Bump" {}_specintensity ("specular Width", Range (0.01, 1)) = 0.5}
explanation : There has been no thorough understanding of the calculation details of the variables in Unity Surfaceoutput. Explain it in more detail here. surfaceoutput can be found in this article, as follows:
Pre name= "code" class= "plain" >struct surfaceoutput {half3 Albedo; The reflectivity of the pixel reflects the base color of the pixel half3 Normal; The normal direction of the pixel is half3 emission; The self-luminous color of the pixel makes it possible for the object itself to emit light half specular even without illumination; The pixel's high-light index half Gloss; The high light smoothness of the pixel, the higher the value, the clearer the reflection, the more blurred half Alpha; The opacity of the pixel};
- # pragma declaration. This controls the switch on surface shader properties, making the shader more efficient or inefficient:
cgprogram#pragma Surface Surf Mobileblinnphong exclude_path:prepass nolightmap noforwardadd halfasview
explanation : Ignore delayed illumination, do not support illumination mapping, accept only one single parallel light source as a pixel-by-source. Finally, use halfasview declaration tells Unity that We use a half vector between the light direction and the viewing direction to replace the true observation direction viewdir to calculate the illumination function. This will speed up the processing time of the shader because it is calculated based on the vertex-by-pixel rather than by pixels. Although the result is approximate, it is sufficient for the mobile platform.
- Establish and link the variables in the Properties block. Unlike before, this time we use fixed to get the value of the Highlight index slider:
sampler2d _diffuse;sampler2d _normalmap;fixed _specintensity;
- Gets the UV coordinates of the map. As mentioned in the previous article, in order to save variable space, we only use one UV value:
struct Input {half2 uv_diffuse;};
- Since we have added a new variable to the declaration, we can use the new parameter in the Illumination function:
Inline Fixed4 Lightingmobileblinnphong (surfaceoutput s, fixed3 Lightdir, Fixed3 halfdir, fixed atten) {fixed diff = max (0 , Dot (S.normal, lightdir)), Fixed NH = max (0, Dot (s.normal, halfdir)), Fixed spec = POW (NH, s.specular *) * S.GLOSS; Fixed4 C;c.rgb = (S.albedo * _lightcolor0.rgb * diff + _lightcolor0.rgb * spec) * (Atten * 2); c.a = 0.0;return c;}
- Finally, we perform the calculation of the pixel color in the surf function:
void Surf (Input in, InOut surfaceoutput o) {fixed4 Diffusetex = tex2d (_diffuse, in.uv_diffuse); O. Albedo = Diffusetex.rgb;o. Gloss = Diffusetex.a;o. Alpha = 0.0;o. Specular = _specintensity;o. Normal = Unpacknormal (tex2d (_normalmap, In.uv_diffuse));}
Finally, the results are as follows:
explain
we conclude by summarizing all the techniques used: Optimizing variable types, sharing UV coordinates, reducing the number of lights processed, allowing shader to work only on specific renderers, using approximations instead of exact values, and reducing or compressing maps.