Note: This documentation is for the old 0.2.0 version of A-Frame. Check out the documentation for the current 0.5.0 version
The material component defines the appearance of an entity. The built-in shaders allow us to define properties such as color, opacity, or textures. Custom shaders can be registered to extend the material component to allow for a wide range of visual effects. The geometry component can be defined alongside to provide a shape alongside the appearance to create a complete mesh.
The material component is coupled to shaders. Some of the built-in shading models will provide properties like color or texture to the material component.
Here is an example defining a red material using the default standard shading model:
Here is an example of using a different built-in shading model:
Here is an example of using perhaps a custom shader:
The material component has only a few base properties, but more properties will be available to set uniforms and attributes depending on the schema of the shader applied:
|opacity||Extent of transparency. If the
|transparent||Whether material is transparent. Transparent entities are rendered after non-transparent entities.||false|
|shader||Which shader or shading model to use. Defaults to the built-in standard shading model. Can be set to the built-in flat shading model or to a registered custom shader||standard|
|side||Which sides of the mesh to render. Can be one of
|material-texture-loaded||Texture loaded onto material. Or when the first frame is playing for video textures.|
|material-video-ended||For video textures, emitted when the video has reached its end (may not work with
To set a texture using one of the built-in shading models, specify the
src can be a selector to either an
src can also be an inline URL. Note that this is less performant than going through the asset management system:
Most of the other properties works together with textures. For example, the
color property will act as the base color and be multiplied per-pixel with the texture. Set it to
#fff to maintain the original colors of the texture.
Video and image textures are cached on the A-Frame layer in order not to push redundant textures to the GPU.
Whether the video texture loops or autoplays depends on the video element used to create the texture. If we simply pass a URL instead of creating and passing a video element, then the texture will loop and autoplay by default. To specify otherwise, create a video element in the asset management system and pass the ID:
To control the video playback such as pausing or seeking, we can use the video element to control media playback. For example:
This doesn’t work as well if you are passing an inline URL, in which case a video element will be created internally. To get a handle on the video element, we should define one in
We might want to tile textures rather than having them stretch. The
repeat property can be used to repeat textures in one of the built-in shading models.
A common operation is to set material properties from another component. To do so, we can specify the material as a dependency component, wait for the entity to load, then use
To play with an example of communicating with the material component, check out the Cross-Component Changes example on Codepen.
Transparency and alpha channels are tricky in 3D graphics. If you are having issues where transparent materials in the foreground do not composite correctly over materials in the background, it is probably due to underlying design of the OpenGL compositor (which WebGL is an API for). In an ideal scenario, transparency in A-Frame would “just work”, regardless of where the developer places an image in 3D space, or what order they define the elements in markup. In the current version of A-Frame, however, it is easy to create scenarios where foreground images occlude background images. This creates confusion and unwanted visual defects. To workaround, try changing the order of the entities.