Unity Shader Tutorial-First lesson: Writing shader and applying shader processes

Source: Internet
Author: User

This is the first lesson of our Unity Shader tutorial, and in this lesson you will learn how to use a program to write a shader that can be used in unity: the diffuse shader. This lesson focuses on the process of creating shader and applying shader.

Preparatory work:
    1. Install Unity version 4.6 or more
    2. Create a new project
    3. Menu gameobject | 3D Object | Plane creates a plane, as our ground
    4. Menu gameobject | 3D Object | Sphere creates a ball, repeatedly creating multiple
Note: 1. After creating the ground, select the object (in the hierarchy panel) and clear the position portion of the corresponding transform into 0.2. All sphere are created with the same ground objects as above, and the position part is zeroed out. Then drag the ball to a certain distance, and put it on the ground, you can see the whole part of the ball so far.
The above work is done and we get this picture:



Official work
    • on the project panel of the editor, right-click on create under the panel handle, select shader
    • double-click Mydiffuse This file, let MonoDevelop run and open it, we can edit
    • Open ' Mydiffuse ' file, all of the code is selected, press DELETE to clear the
shader" Cookbookshaders/basicdiffuse "{Properties{_maintex (" Base (RGB) ", 2D) =" White "{}}subshader{tags {" rendertype "=" Opaque "}lod 200cgprogram#pragma surface surf lambertsampler2d _maintex;struct Input{float2 Uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} Endcg}fallback "Diffuse"} 
    • And then save this file for us. Back to the Unity editor
    • Create a material by creating on the project panel and rename it to Mymaterial
    • Select Mymaterial. Corresponding to the Inspector panel, Shader field Point open menu, drop-down bar select ' Mydiffuse '
    • Drag the mymaterial material to an object in the hierarchy, either sphere or plane
    • Select the object in hierarchy that we just dragged into the mymaterial, and on the corresponding Inspector panel make sure to see the ' Use light probes ' part of the ' Mesh Renderer ' section below ' mymaterial '


This process is a step to create a new shader and apply our shader to the object through material. Each new shader can be used in a similar step to the object in the scene.


When the above shader is applied to an object, we can find that the representation of the object is identical to the original. The reason for this is that the first shader feature we wrote was identical to the unity default shader. In other words, while we are creating the object, we do not assign it the relevant shader, but unity will ensure that it takes a default. In 3d applications, each object must have a corresponding shader to complete the final rendering (the term of the object is drawn). The 3d engine itself will help you choose a default when you are not deliberately choosing.


However, we also note that shader can only be applied to objects by material (material) as a medium, not directly to objects. This is why we have to create a material after creating the shader, and then choose our shader from material and then bind the material to the object. So, for an object, it has material properties, and shader is just a component or part of the material. We need to figure this out first.


This is the first lesson of the content, familiar with the entire shader production and application process. In the following lesson, we will add elements to this shader, allowing objects to change color and some richer functions.


98 Education (98jy.net) original articles, reproduced please specify the original address, otherwise as infringement!

Unity Shader Tutorial-First lesson: Writing shader and applying shader processes

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.