Unity shadersdiffuse shading--Create a basic surface Shader

Source: Internet
Author: User

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 ==========================================


This time the work is very simple, we are mainly to understand the unity shaders basic work principle.



get ready to work create a new unity Project in the assets directory with a new folder: Shaders creates a new folder under the Assets directory: Materials
implementation under the Shaders folder, right-click to create a new shader, named Basicdiffuse, and open it; Under the Materials folder, make a material, And using the Basicdiffuse.shader we created earlier, create a sphere and drag the material you created earlier onto it to see the effect; You can also add a texture to material.

After explaining the opening of the Basicdiffuse.shader, we can see that unity has actually written a lot of code for us:

Shader "Custom/basicdiffuse" {
	Properties {
		_maintex ("Base (RGB)", 2D) = "White" {}
	}
	subshader {
		Tags {"Rendertype" = "Opaque"}
		LOD
		
		cgprogram
		#pragma surface surf Lambert sampler2d _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"
}

The first line shows the path of the shader in Unity, which appears in the drop-down list when you select a material shader, and can be changed at any time, and the last line indicates that when this shader fails to run in the current environment, Unity's own diffuse Shader will be called by default, and the rest will be covered in the following chapters.

Unity uses CG to achieve this, it encapsulates these implementation details, provides us with a component-based method of writing shader, such as adjusting the image of the UV coordinates, matrix conversion, etc. it helps you do it. Previously, we needed to rewrite the code for some basic functionality to create a shader from scratch. As your experience becomes richer, you will naturally want to understand how unity handles the work of the image Processing Unit (GPU). If you want to know how unity calls CG, you can see it in Unity's installation directory unity4\editor\data\cgincludes. In some of the sections that follow.

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.