Dynamically set material parameters in unity-code __unity

Source: Internet
Author: User

We know that in unity you can dynamically change the parameters in the material monitor panel in code, such as changing the value size or replacing the texture. Common APIs are: SetColor, SetFloat, Setint, SetTexture.

Today, however, when setting up a standardshader texture map, it is found that the normal map is set but the object in the scene does not have the normal concave and convex effect, so it is necessary to activate the material panel. After repeated examination and the search for information, there are mainly the following two questions:

Texture type
string[] Texture_type = {"_metallicglossmap", "_bumpmap", "_parallaxmap", "_occlusionmap", "_detailmask", " _detailalbedomap "," _detailnormalmap "};

Material need to set the keyword
string[] texture_keyword = {"_metallicglossmap", "_normalmap", "_parallaxmap", "", "_detail_ MULX2 "," _detail_mulx2 "," _detail_mulx2 "};

Normal map import in need to set type Normalmap

Set the type of normal map
if (fileName = = Diffusename + texture_type[1])
{
    Textureimporter importer = (textureimporter) Assetimporter.getatpath (FilePath);
    Importer.texturetype = Textureimportertype.normalmap;
    Importer. Saveandreimport ();
}

Use the standard shader (standardshader) material to set the appropriate keyword to enable

Open the corresponding keyword mat before material.settexture
. Enablekeyword (Texture_keyword[i]);

The following is my own understanding of the document, not necessarily accurate.

The standard shader used by a material is actually a collection of multiple shaders in unity. Because a material shader can not cover all the functions, such as GI, fog, hdr, and other high-energy effects, so unity of the standard shader is divided into different special features of the shader variant (Shader variant). When the Normalmap is assigned to the material, it activates the shader variant that supports normal mapping, and when the parallax map is assigned to the material, it activates the shader variant that supports Parallax mapping. Therefore, if you want to assign a special texture to the material, you need to open the appropriate keywords to activate the corresponding features of the shader variant.

There are several key words that need to be opened specifically:

Key Words characteristic
_normalmap Normal mapping
_alphatest_on For cutout rendering mode
_alphablend_on For fade rendering mode
_alphapremultiply_on For transparent rendering mode
_emission Set self-glow
_parallaxmap Set up parallax mapping
_detail_mulx2 Used to set a second texture channel
_metallicglossmap To set the metal map in the metallic workflow
_specglossmap Set up a high-light map in the specular workflow

Some of the code that sets the material properties is attached below:

<summary>///set the related properties of shader in the material///<para name = "Mat" > need to set material </para>///<para name = "M Eshmat "> The data collection to be passed in shader </para>///<para name =" Fbxname "> Model name, where the main purpose is to get the location of the texture Map folder </para>///& 
    lt;/summary> void Setshader (Material mat, shaderdata data, string fbxname) {//The main map in the default map resource is the name of the material, and the name of the other map is the material name + map type
    string diffusename = Mat.name; Texturefiles is used to record all the picture files in the Texture folder, record their map name and path dictionary<string, string> texturefiles = new dictionary<string
    , string> ();
    Texturepath is the path to the mapping folder of a FBX model that was previously recorded string[] Filespath = Directory.GetFiles (texturepath[fbxname)); foreach (String FilePath in Filespath) {//Texture_ext is a preset picture suffix name that marks the format of the picture (such as. Jpg,.png,.tif, etc.) if (Array.indexo F (Texture_ext, Path.getextension (FilePath))!=-1) {String fileName = Path.getfilenamewithoutextensio
            N (FilePath); if (Filename.indexof (diffusename) = = 0) {textuRefiles[filename] = FilePath;
                Debug.Log (FileName + "," + FilePath); Set the type of normal map if (FileName = = Diffusename + texture_type[1]) {Texturei
                    Mporter importer = (textureimporter) assetimporter.getatpath (FilePath);
                    Importer.texturetype = Textureimportertype.normalmap; Importer.
                Saveandreimport ();
        }}//Set the main map of the material, that is, the albedo map if (Texturefiles.containskey (Diffusename)) {
        Debug.Log ("Maintexture Exist");
    Mat.maintexture = assetdatabase.loadassetatpath<texture> (Texturefiles[diffusename]); //Set other special types of textures for (int i = 0; i < Texture_type. Length; ++i) {if (Texturefiles.containskey (Diffusename + texture_type[i])) {Debug.Log (texture_ty
            Pe[i] + "Exist"); if (Texture_keyword[i]!= "") mat.
  Enablekeyword (Texture_keyword[i]);          Mat. SetTexture (Texture_type[i], assetdatabase.loadassetatpath<texture> (Texturefiles[diffusename + TEXTURE_TYPE[
        I]));
    } Mat.color = Data.color; Mat.
    SetFloat ("_metallic", data.metallic); Mat.
    SetFloat ("_glossiness", data.glossiness); Mat.
SetColor ("_emissioncolor", Data.emissioncolor); }

Reference: https://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html

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.