# Using Material Data in Shaders > **Notice:** The sections below cover advanced manual material development: material definitions, inheritance, and rendering integration. **The recommended way to create materials is through the visual, node-based [Material Editor](../../../content/materials/index.md), which provides an approachable workflow without writing material and shader code by hand.** A material describes inputs such as colors, textures, and feature switches. Shader code uses those inputs to calculate surface properties or an image. The connection between a declaration in a material file and a name in shader code determines which values and resources the shader receives. The examples below use source-authored [ULON base materials](../../../code/formats/materials_formats/ulon_base_material_format.md) with embedded [UUSL](../../../code/uusl/index.md) code. UUSL provides the Engine's shader syntax and macros for writing code across supported graphics APIs. The focus here is how material declarations become shader inputs. For the Editor workflow and a complete surface example, start with [Creating a Base Material](../../../code/materials_shaders/creating_base_material/index.md). That example already uses the same naming and conditional compilation rules explained here. ## Values, Resources, and Shader Variants Material settings reach rendering in different ways. A color supplies a value, a texture supplies a resource, and a state can select different compiled shader code. | Declaration | Use in a shader | |---|---| | Parameter, such as Color or Slider | A typed uniform value. Different material instances can supply different values to the same shader code. | | Texture, such as Texture2D | A bound texture resource that shader code can sample. | | State | Preprocessor definitions that select a shader variant. They can include or exclude calculations and resource declarations. | | Option | A predefined rendering setting, such as blending or face culling. Options can also provide shader definitions; they are not ordinary uniform parameters. | Changing a parameter value does not require a new shader variant. Changing a state used by shader definitions can select a different variant, which the Engine compiles or retrieves from its cache as needed. Use a parameter for a changing numeric value and a state when the feature changes which code should be compiled. See the [Parameters](../../../code/formats/materials_formats/ulon_materials/parameters.md), [Textures](../../../code/formats/materials_formats/ulon_materials/textures.md), [States](../../../code/formats/materials_formats/ulon_materials/states.md), and [Options](../../../code/formats/materials_formats/ulon_materials/options.md) references for the available declarations and arguments. ## Connecting Declarations to Calculations The following base adds a tint, a brightness multiplier, and an optional texture to the standard [Mesh](../../../code/materials_shaders/abstract_materials/mesh.md) implementation. To try it, create an opaque mesh base named *tinted_surface* through *Asset Browser → Create Code → Base Material → Mesh → Opaque*, then replace the generated file's contents with this example. ```glsl BaseMaterial { Group "Surface Inputs" { Color tint = [0.7 0.8 1 1] Slider brightness = 1 State use_texture = false Texture2D albedo = "core/textures/common/white.texture" if[use_texture] } Shader vertex_struct = #{ INIT_BASE_DATA #} Shader fragment = #{ float3 color = var_tint.rgb * var_brightness; #ifdef STATE_USE_TEXTURE color *= TEXTURE(tex_albedo, DATA_UV.xy).rgb; #endif OUT_FRAG_ALBEDO = color; OUT_FRAG_ROUGHNESS = 0.6f; #} } ``` The root explicitly selects the *var* and *tex* prefixes. Mesh already supplies these prefixes, but writing them here makes the naming rule visible. The Engine generates declarations for *var_tint*, *var_brightness*, and, when enabled, *tex_albedo*. Do not add matching uniform or texture declarations to this example. The two Shader entries are fragments incorporated into Mesh's complete programs. Mesh supplies the entry points and [passes](../../../code/formats/materials_formats/ulon_materials/passes.md). It also supplies the meaning of *INIT_BASE_DATA*, *DATA_UV*, and *OUT_FRAG_ALBEDO*; these names belong to the Mesh interface rather than to every UUSL shader. The calculation assigns an albedo and roughness, not a final lit pixel color. Mesh writes the surface data required by the rendering pipeline. Lighting and the scene environment affect the displayed result. To check the inputs, follow the [child creation and assignment steps](../../../code/materials_shaders/creating_base_material/index.md#editor) with this base, *Samples::tinted_surface*, and a lit UV mesh. Change *Tint* and reduce *Brightness* to darken the surface. Enable *Use Texture* and assign an [imported nonwhite texture](../../../editor2/assets_workflow/assets_create_import.md#import) to *Albedo*. Its colors should multiply the tint. Enabling the initial white texture alone leaves the result unchanged. ## Parameter Names and Types A parameter has a material name and a shader name. For *Color tint*, the material name is *tint*. With *var_prefix=var*, its shader name is *var_tint*. The *title="Tint"* argument sets the Editor label and does not change either technical name. Unless a declaration specifies its own *shader_name*, the shader name is the prefix followed by an underscore and the material name. With an empty prefix, the material name is used directly. The *namespace* argument identifies the material and does not add a namespace to uniform names. The declaration type determines the shader value type. Editor controls such as color pickers and sliders do not become separate shader-language types: | ULON declaration type | Shader type | |---|---| | Color | float4 | | Slider or Float | float | | SliderInt or Int | int | | Float2, Float3, or Float4 | float2, float3, or float4, respectively | In the example, *var_tint.rgb* reads three components of a float4, and *var_brightness* is a float. Changing their values on a child material changes the inputs supplied when that material is rendered. It does not replace the shader calculation. For ordinary ULON parameters, *shared=true* makes the parameter available to shaders, and *auto_init=true* generates its declaration in the material constant buffer. Both are enabled by default. The Engine supplies current parameter values by their shader names. Setting *shared=false* excludes the parameter from shader inputs. With *shared=true*, setting *auto_init=false* disables only the generated declaration. Declare the matching name and type in shader code. The Engine still supplies the parameter's value. ## Texture Names and Sampling A texture declaration selects a resource type and an initial resource. With *texture_prefix=tex*, *Texture2D albedo* becomes *tex_albedo* in shader code. As with parameters, an explicit *shader_name* overrides the generated name. Shared textures with automatic initialization enabled receive the appropriate UUSL declaration and, unless specified explicitly, a binding slot. Refer to the resource by its shader name, rather than inferring a slot from the declaration's position in the material file. In the example, sample the conditional texture inside the enabled branch: ```glsl #ifdef STATE_USE_TEXTURE float3 sampled_color = TEXTURE(tex_albedo, DATA_UV.xy).rgb; #endif ``` Here *TEXTURE()* samples the 2D texture, and *DATA_UV.xy* supplies Mesh's first UV set. The texture declaration does not choose coordinates by itself: the shader calculation does. Other resource types and sampling operations are described in [Textures and Buffers](../../../code/uusl/textures.md). Changing the texture assigned to a material changes the bound resource. Changing the declared resource type, for example from Texture2D to Texture3D, also changes the shader interface and requires matching sampling code. ## State Definitions and Conditional Code For a shared toggle state using automatic definitions, the Engine supplies a numeric *GET_STATE_* definition and, when the value is nonzero, a *STATE_* marker. Names are converted to uppercase. | use_texture value | Definitions | |---|---| | false | GET_STATE_USE_TEXTURE=0; STATE_USE_TEXTURE is absent | | true | GET_STATE_USE_TEXTURE=1; STATE_USE_TEXTURE is present | Either of these tests selects the enabled branch: ```glsl #ifdef STATE_USE_TEXTURE // Code for the enabled feature. #endif #if GET_STATE_USE_TEXTURE // Code for the enabled feature. #endif ``` Do not use *#ifdef GET_STATE_USE_TEXTURE* to test whether the toggle is enabled: the numeric definition also exists when its value is zero. A preprocessor condition selects code before shader compilation; it is not a runtime branch evaluated for each pixel. Switch states also provide a marker for the selected item, named *STATE__*. Integer states expose their numeric value through *GET_STATE_*. A state's explicit *shader_name* replaces the standard naming scheme with a single numeric definition of that name. See [States](../../../code/formats/materials_formats/ulon_materials/states.md) for the full rules, including pass restrictions and disabling sharing. The texture declaration in the example has the material [condition](../../../code/formats/materials_formats/ulon_materials/conditions.md) *if[use_texture]*. Its generated shader declaration is conditional too. The matching *#ifdef STATE_USE_TEXTURE* around sampling prevents shader code from referring to a resource that is absent in the disabled variant. Hiding a control in the Editor is not a substitute for guarding its use in shader code. ## Data Supplied by the Engine and the Abstract Material Not every name used in shader code comes from a material declaration. The renderer supplies data such as time, camera transforms, and viewport information; these built-in inputs are listed in [UUSL Parameters](../../../code/uusl/parameters.md). Their use depends on the shader stage and rendering context. An abstract material can additionally provide geometry inputs, helper functions, and output variables. In the example, Mesh passes UV coordinates to the fragment stage and accepts albedo and roughness outputs. Consult the chosen [abstract material](../../../code/materials_shaders/abstract_materials/index.md) for this contract: changing the foundation can change the available inputs and expected outputs. The separately documented [Surface and Material Parameters](../../../code/uusl/custom_parameters.md) use custom parameter layouts. They are a different mechanism from ordinary ULON parameters such as *Color tint*; a generated *var_* name does not refer to a custom layout slot. ## Reading Shaders with Explicit Declarations Existing materials may declare constant buffers and texture bindings directly in shader source. This is common in [XML base materials](../../../code/formats/materials_formats/base_material_format.md); ULON also permits explicit declarations when automatic initialization is disabled. When writing a complete ULON shader program, include *core/materials/shaders/api/common.h*, directly or through a higher-level Engine shader header, to insert the generated material declarations. Mesh already includes the required header, so the example's extension fragments do not need an additional include. UUSL's [Types and Keywords](../../../code/uusl/types.md) reference describes *CBUFFER* and *UNIFORM*, while [Textures and Buffers](../../../code/uusl/textures.md) describes texture initialization macros. A declaration in shader source still needs the correct name, type, and binding to receive the intended data. Avoid adding a second declaration for an input already generated by the ULON loader. When diagnosing an unknown shader name, check the material declaration, its prefix or explicit shader name, sharing and initialization settings, conditions, and pass restrictions. For geometry inputs and output names, check the abstract material instead.