Unity3D game development from zero single row (9), unity3d Game Development
Abstract
What we want to learn today is some Shader examples, from simple to difficult. Let's go.
A large wave of examples
Use the project used in the previous article. Click to download
Red crab
Test1.shader
Shader "Custom/Test1" { SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float4 color : COLOR; }; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = 1; } ENDCG } Fallback "Diffuse" }
O. Albedo = 1; indicates that the output color is white, and the direction light is switched to Red. After calculation by the lambert illumination model
Crab with normal map
Shader "Custom/Test2" { Properties { _MainTex ("Texture", 2D) = "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; }; sampler2D _MainTex; sampler2D _BumpMap; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); } ENDCG } Fallback "Diffuse" }
In comparison, there is no normal texture on the left and a normal texture on the right.
Crab running from the sword spirit
Let's take a look at Jian lingfeng.
It feels like there are a lot of highlights (don't look blind, hello !), This is also a specialized term called Rim Lighting. Our crab can also, hum ~
Shader "Custom/Test3" { Properties { _MainTex ("Texture", 2D) = "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0) _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0 } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; float3 viewDir; }; sampler2D _MainTex; sampler2D _BumpMap; float4 _RimColor; float _RimPower; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal)); o.Emission = _RimColor.rgb * pow (rim, _RimPower); } ENDCG } Fallback "Diffuse" }
Rendering results (the model accuracy is a little low. Let's take a look)
In brief, the principle is mainly used to calculate the edge illumination. First, the angle between the line of sight and the normal is used to find the edge of the model, and then the intensity of the emitted light is controlled based on the distance.
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), IN.worldNormal));o.Emission = _RimColor.rgb * pow (rim, _RimPower);
IN. viewDir is the current angle vector, and IN. worldNormal is the object's normal. Dot is the dot product of the calculated angle of view and normal. It is equal to the cos value of the angle between the angle of view and normal. The value of Cos is 1--cos and it becomes 0-1, the maximum value is reached when the angle is 90 degrees, which is used to simulate the intensity of side light (the most powerful part of light with the angle of view 90 degrees is the edge light)
Using a pow function (rim's _ rimPower power) to enlarge the change rate of this value can enhance the shiny edge effect.
Fat crab
The principle of this effect is very simple, that is, moving the vertex position along the normal direction to a certain distance.
Shader "Custom/Test4" { Properties { _MainTex ("Texture", 2D) = "white" {} _Amount ("Extrusion Amount", Range(-1,1)) = 0.5 } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert vertex:vert struct Input { float2 uv_MainTex; }; float _Amount; void vert (inout appdata_full v) { v.vertex.xyz += v.normal * _Amount; } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
It's fat, but... How is it strange to draw frames? It is actually a mesh problem. The triangle surface of the model extracted from the game may not be good. For example, the extracted mesh 9 is not closed. Let's just change it!
Import the model from Dota2's official website, and give it the same shader. The result is as follows.
Gao fushuai instantly becomes stupid and cute! 0. The cost is to convert the Realistic Style model into the Q version style.
Vertex modifier function
You can also add vetex shader to the surface shader.
The method is to add the vertex: xxx field during the declaration, for example
#pragma surface surf Lambert vertex:vert
Then define the vert function. The vertex shading process is performed before the surface function. The following example is to render the normal (overlay the original color ).
Shader "Custom/test5" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert vertex:vert struct Input { float2 uv_MainTex; float3 customColor; }; void vert (inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); o.customColor = abs(v.normal); } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Albedo *= IN.customColor; } ENDCG } Fallback "Diffuse" }
Rendering result
Previously, Rim lighting can be computed in vertex shading.
Final Color Modifier
This is like fregment shader, which is the last stage of pipeline.
The defined method is similar to that of vertex,
#pragma surface surf Lambert finalcolor:mycolor
Then define the mycolor function. The mycolor function is executed after the surf function is executed.
Shader "Custom/test6" { Properties { _MainTex ("Texture", 2D) = "white" {} _ColorTint ("Tint", Color) = (1.0, 0.6, 0.6, 1.0) } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert finalcolor:mycolor struct Input { float2 uv_MainTex; }; fixed4 _ColorTint; void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) { color *= _ColorTint; } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
Rendering Effect
Reference
Surface Shader Examples-http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html
Questions about unity3D Game Development
I have been familiar with virtools and unity, and I feel that the engine has its own advantages. As long as I master all of them, I personally feel that unity is in line with my style. Not very gorgeous, but all game functions can be implemented. There are a lot of resources available for download in the unity store, and it is also very convenient to make 2d games.
You can also export your own game to your mobile phone. If you are narcissistic, you will feel satisfied. You can combine art and programming very well. There are very few talents in this field. It is not difficult to learn about unity. As long as you complete several game production examples with video, you can achieve most of the functions. To be proficient, you must study it.
There are many cracked versions of unity on the Internet, and 3.0 is enough. The cracked version is still stable. Occasional problems.
I feel that about 5000 of my laptop can meet the requirements, and I need to use a desktop computer to make a precision model.
For beginners, click game development. By the way, click unity3D.
If you really want to do this, you have to start from scratch and from the most basic. There are several videos on the Internet that seem good after learning U3D for two months. In fact, they are all plug-ins. This method seems to be fast but basically useless, just to satisfy vanity, what can a scenario without interaction be considered?
I started from scratch, and now it's almost half a year. The most difficult thing I feel is programming, because I have never touched it before, and it's a little basic for Model Materials. Most of the time is spent on learning programming, because this aspect cannot be bypassed. Although there is a plug-in playMaker that can play games without programming experience, it doesn't feel good, but if you don't want to learn programming, you can try it.
For programming, unity often uses Javascript and C #. Although javascript is easy to learn, Unity's javascript is not a standard, but its unique UnityScript, but now there is no systematic UnityScript teaching material, so it is not easy for me to learn. However, I am still reading UnityScript, because I learned it from the beginning and want to learn it and then change it to C #. C # is recommended if you want to learn it, but I don't know if unity's c # is the same as standard c #. You can make a decision in this regard.
If untiy is suitable for personal development, you can watch the video of kotoyev. There is a comparison between unity and several other engines, among which there is a well-known engine such as unreal.
If you say that it has been made in two years, it still feels reliable, but in the first year, you should stick to the foundation. Even if the foundation is ready, do not rush to make large games or try some games.
One of the reasons for using unity is that I have not compiled a map in the Warcraft map editor, but since the editor can make something as classic as dota, can't unity be used? By the way, I know dota but I have never played it. Cheng Hai has been playing it for a while. Compared with dota, I prefer other Warcraft games on the haofang platform, for example, the fire of passers-by in the past, the war of rabbits and sheep, and so on. The Battle of warcraft also has a high level, such as the bloody mission, which can be called the highest in China, while the Open-mode reverse-flow battle can be played as an online game.
In addition, not all things need to be done by yourself. For example, in Outfit7 development, the first model used was bought from a website. If a plug-in is very helpful for game production, you may wish to use it. However, we recommend that you start from the basics and use the plug-in to improve efficiency.
Currently, the active forum in China is the unity holy book.
Other sub-sections, such as tiandihui and cocochina, are not very active and unity3d8 is also good. However, many resources need to be downloaded at a higher level.
It is only a transformation for whether a character is 2D or 3D. The so-called 2D is a series of rendered images. Most of these images are rendered using 3D models, but unity is not suitable for 2D, only plug-ins are available.
Because it is self-taught, there is no systematic thing to tell you, you can only say that this is a very difficult way, do it after making a decision!
It's a bit messy.