Unity Shader Tutorial-Lesson Three: Practice! Same shader, multiple material.

Source: Internet
Author: User
Tags pow

This article starting address: HTTP://98JY.NET/ARTICLE/18

More and more timely articles please about 98 education


In this class we begin to use the properties knowledge we have learned to change the code, using a shader to implement several different spheres:


Steps:


    • Open shader file using MonoDevelop
    • Ensure that the properties inside are the following content


_emissivecolor ("emissive color", color) = (1, 1, 1, 1) _ambientcolor ("Ambient color", color) = (1, 1, 1, 1) _somevalue ("C Oefficient ", Range (0, 10)) = 2.5
    • Locate the #pragma surface surf Lambert line in your code, and enter the following three lines of code after entering another line


FLOAT4 _emissivecolor;float4 _ambientcolor;float _somevalue;


    • Clear the original in this place


Sampler2d _maintex;


    • Find the method void Surf (Input in, InOut surfaceoutput O), and change the contents to


FLOAT4 C = Pow ((_emissivecolor + _ambientcolor), _somevalue), O. Albedo = C.rgb;o. Alpha = C.A;


To make sure that the final content you made is the same as mine, here is the final shader code


shader" Mydiffuse "{properties{_emissivecolor (" emissive color ", color) = (1, 1, 1, 1 ) _ambientcolor ("Ambient color", color) = (1, 1, 1, 1) _somevalue ("coefficient", Range (0, ten)) = 2.5}subshader{tags {"Ren Dertype "=" Opaque "}lod 200cgprogram#pragma surface Surf Lambertfloat4 _emissivecolor;float4 _ambientcolor;float _ Somevalue;struct input{float2 Uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {float4 c = Pow ((_emissivecolor + _ambientcolor), _somevalue), O. Albedo = C.rgb;o. Alpha = C.A;} Endcg}fallback "Diffuse"} 



Here we can save the shader. Back in Unity.


After returning to unity, select the material named Mydiffuse, then press ctrl+d on the keyboard to copy the material and rename it to MyDiffuse2. The same way, copy out MyDiffuse3, MyDiffuse4. This way, we can make a different four material using the same shader.


In the scene panel, if you do not fill 4 sphere, you can add it now. Then the four balls were dragged into our mydiffuse, MyDiffuse2, MyDiffuse3, MyDiffuse4 and several material.


If you select any of the material, you can select emissive color or ambient color on inspector, or you can change the value by dragging the slider bar, and you can see that the objects in the scene will change depending on your changes. At the same time, although we have only written a shader for 4 objects, changing an object's material does not affect objects that use a different material. In other words, the value in our attribute is actually present in material. When we change the relevant values, unity will store the values in the material corresponding file, and at run time, as the parameters required by the shader to send to shader. As a result, although we wrote only one shader, the object's performance was different because the parameters were sent ( for those who are curious, the appendix to this lesson describes how to open the material file and see what is stored ).


Explanation of the code we changed above

When you declare a new property value in the properties block, you also give shader a way to let the user adjust the internal values through the Inspector panel. Each variable will store the results of the last user fix. However, if the code needs to use these values in the subshader{} block, you must declare a variable of the same name in this block, which links our values on the inspector to the values of the last code to form a whole.


Once you declare these variables in the subshader{} block, you can use these variables in the surf () method. In the Surf () method, the Pow () method we used is a method provided by the Nvidia CG language, and unity shader can use the method in CG, which is similar to the POW in other languages, and is "the specified power to return a specified number ."


Appendix:

Unity provides a way to save some files as text, which we can use to view the contents of the material file.

Steps:


    • Menu Edit | Project Settings | Editor, the following panel will open



    • In the asset serialization of the panel, Mode points open, select Force Text
    • Wait for the progress bar to complete the task
    • Find our Mydiffuse material (material) file (in. mat) at the end of the hard drive, open with the software that can open the text file (write, Ultraedit, notepad++)
    • See what's inside, I'm here.



The color values we choose can be seen in the highlighted areas of the figure.


Unity Shader Tutorial-Lesson Three: Practice! Same shader, multiple material.

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.