Unity 6.3
0 онлайн 2 гостей 3 в системе
Вход

Material.SetTexture

Declaration

public void SetTexture(string name, Texture value);
public void SetTexture(int nameID, Texture value);
public void SetTexture(string name, RenderTexture value, Rendering.RenderTextureSubElement element);
public void SetTexture(int nameID, RenderTexture value, Rendering.RenderTextureSubElement element);

Параметры

Параметр Описание
nameID Имя свойства ID, используйте Shader.PropertyToID, чтобы получить его.
имя Имя свойства, e.g. "_MainTex".
значение Текстура для установки.
элемент Необязательный параметр, который определяет тип данных, которые должны быть установлены из RenderTexture.

Описание

Задает именованную текстуру.

Многие шейдеры используют более одной текстуры. Использование SetTexture для изменения текстуры (идентифицируемой по имени свойства шейдера или уникальному имени свойства) ID).

При настройке текстур на материалах с помощью Standard Shader, вы должны знать, что вам может потребоваться использовать EnableKeyword для включения возможностей шейдера, которые ранее не использовались. Для более подробной информации, читайте Доступ к материалам через скрипт.

Общие имена текстур, используемые UnityВстроенные шейдеры:
"_MainTex" является основной диффузной текстурой. К ней также можно подключиться через mainTexture свойства.
"_BumpMap" это карта нормалей.

Свойства шейдера также показывают некоторые ключевые слова, необходимые для установки текстуры материала. Чтобы увидеть это, перейдите к вашему материалу и щелкните правой кнопкой мыши Шейдер раскрывающееся меню в верхней части. Далее, выберите Выберите шейдер.

Указание `RenderTextureSubElement`, вы можете указать, какой тип данных устанавливать из RenderTextureВозможны следующие варианты: RenderTextureSubElement.Color, RenderTextureSubElement.Depth, и RenderTextureSubElement.Stencil.

Дополнительные ресурсы: mainTexture свойство, GetTexture, Shader.PropertyToID, Свойства в шейдерных программах, RenderTextureSubElement.

//Attach this script to your GameObject (make sure it has a Renderer component)
//Click on the GameObject. Attach your own Textures in the GameObject’s Inspector.

//This script takes your GameObject’s material and changes its Normal Map, Albedo, and Metallic properties to the Textures you attach in the GameObject’s Inspector. This happens when you enter Play Mode

using UnityEngine;

public class Example : MonoBehaviour {

//Set these Textures in the Inspector public Texture m_MainTexture, m_Normal, m_Metal; Renderer m_Renderer;

// Use this for initialization void Start () { //Fetch the Renderer from the GameObject m_Renderer = GetComponent<Renderer> ();

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

//Set the Texture you assign in the Inspector as the main texture (Or Albedo) m_Renderer.material.SetTexture("_MainTex", m_MainTexture); //Set the Normal map using the Texture you assign in the Inspector m_Renderer.material.SetTexture("_BumpMap", m_Normal); //Set the Metallic Texture as a Texture you assign in the Inspector m_Renderer.material.SetTexture ("_MetallicGlossMap", m_Metal); } }