简体   繁体   中英

Set normal map at runtime in Unity

I have a trouble setting normal map via script in unity. I have a normal map and I want to assign it to a material of the target object.

renderer.material = new Material(oldMaterial);    
renderer.material.SetTexture("_BumpMap", normalTexture);

This line works for assigning the texture. But the target object's normal map is not updated until I open inspector and click material component. I can update albedo texture by using this technique and it works.
Is there a function to force material update its properties ? Any ideas?

尝试添加一个:

renderer.material.shaderKeywords = new string[1]{"_NORMALMAP"};

According to Unity's Docs, you first need to EnableKeyword : _NORMALMAP . Then, the normal map you provided can be displayed. For more explanation, you can see the following code or this website .

//Fetch the Renderer from the GameObject
renderer = GetComponent<Renderer> ();

//Make sure to enable the Keywords
renderer.material.EnableKeyword ("_NORMALMAP");

//Set the Normal map using the Texture
renderer.material.SetTexture("_BumpMap", normalTexture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM