Unity shader tutorial-Lesson 1: Process for writing shader and application shader, unityshader

Source: Internet
Author: User

Unity shader tutorial-Lesson 1: Process for writing shader and application shader, unityshader


I haven't updated my blog for more than half a year because I have partnered with several friends to plan a website (98jy.net ). Now I have to leave it empty to write something. The first few series may have to contribute elsewhere. However, the copyright of the article is still mine, which can still be put here for the convenience of csdn's friends.

Original post address: http://98jy.net/article/16

This is the first lesson of our Unity shader tutorial. In this lesson, you will learn how to use a program to compile a colorator that can be used in Unity: the diffuse shadow. The content of this lesson mainly familiarizes us with the process of creating shader and applying shader.

  1. Install Unity 4.6 or later
  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 multiple times
Note: 1. after creating the ground, select the object (in the Hierarchy panel) and clear the position of the corresponding transform to 0.2. after being created, all sphere objects are the same as those on the ground, and position is partially cleared. Drag the ball to a certain distance and place it on the ground. You can see the whole part of the ball.
After the above work is done, we get the following picture:

Formal work
  • On the Project Panel of the editor, right-click Create under the Panel handle, and select Shader from the pop-up menu.
  • Change the name of the created shader file to myDiffuse.
  • Double-click the myDiffuse file to enable MonoDevelop to run and open it. You can edit
  • In the open 'mydiffuse' file, select all the code and press delete to clear the code.
  • Copy the following code snippet to the file content.
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"}
  • Then save our file. Go back to the Unity Editor
  • Create a Material on the Project panel and rename it myMaterial.
  • Select myMaterial. On the Inspector panel, click the Open menu in the shader column, and select 'mydiffuse' from the drop-down menu'
  • Drag the material of myMaterial to an object in Hierarchy, which can be sphere or plane.
  • Select the object in Hierarchy that we just dragged into myMaterial. On the Inspector panel, make sure that the 'use Light probes' section of the 'mesh renderer' section contains 'mymaterial'

This process is a step for creating a new shader and applying our shader to an object through material. In the future, each new shader can use an object in the scene through similar steps.

After applying the shader above to an object, we can find that the object is exactly the same as the original. This is because the shader function we wrote is exactly the same as the default shader of unity. That is to say, although we didn't specify related shader for it when creating an object, unity will ensure that it carries a default one. In 3d applications, each object must have a corresponding shader for final rendering (the term used to draw objects ). When you do not choose it, the 3d engine itself will help you select a default one.

However, we also noticed that shader can be applied to objects only by using material (material) as a media, rather than directly bound to objects. This is why we have to create a material after creating the shader, then select our shader from the material, and then attach the material to the object. Therefore, for an object, it has material attributes, and shader is only a component or part of the material. We need to figure it out first.

This is the content of our first lesson. Familiarize yourself with the entire shader production and application process. In the following course, we will add elements based on This shader to change the color and some richer functions of objects.

Related Article

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.