MRTK2 standard shader

Standard shader examples

MRTK2 Standard shading system utilizes a single, flexible shader that can achieve visuals similar to Unity's Standard Shader, implement Fluent Design System principles, and remain performant on mixed reality devices.

Example scenes

You can find the shader material examples in the MaterialGallery scene under MRTK/Examples/Demos/StandardShader/Scenes/. All materials in this scene are using the MRTK/Standard shader.

Material Gallery

You can find a comparison scene to compare and test the MRTK/Standard shader against the Unity/Standard shader example in the StandardMaterialComparison scene under MRTK/Examples/Demos/StandardShader/Scenes/.

Material Comparison

Architecture

The MRTK/Standard shading system is an "uber shader" that uses Unity's shader program variant feature to auto-generate optimal shader code based on material properties. When a user selects material properties in the material inspector, they only incur performance cost for features they have enabled.

Material inspector

A custom material inspector exists for the MRTK/Standard shader called MixedRealityStandardShaderGUI.cs. The inspector automatically enables/disables shader features, based on user selection and aides in setting up render state. For more information about each feature please hover over each property in the Unity Editor for a tooltip.

Material Inspector

The first portion of the inspector controls the material's render state. Rendering Mode determines when and how a material will be rendered. The aim of the MRTK/Standard shader is to mirror the rendering modes found in the Unity/Standard shader. The MRTK/Standard shader also includes an Additive rendering mode and Custom rendering mode for complete user control.

Rendering Mode Description
Opaque (Default) Suitable for normal solid objects with no transparent areas.
Cutout Allows creation of transparent effects that have hard edges between the opaque and transparent areas. In this mode, there are no semi-transparent areas, the texture is either 100% opaque, or invisible. This is useful when using transparency to create the shape of materials, such as vegetation.
Fade Allows the transparency values to entirely fade an object out, including any specular highlights or reflections it may have. This mode is useful if you want to animate an object fading in or out. It is not suitable for rendering realistic transparent materials such as clear plastic or glass because the reflections and highlights will also be faded out.
Transparent Suitable for rendering realistic transparent materials such as clear plastic or glass. In this mode, the material itself will take on transparency values (based on the texture’s alpha channel and the alpha of the tint colour). However, reflections and lighting highlights will remain visible at full clarity as is the case with real transparent materials.
Additive Enables an additive blending mode, which sums the previous pixel color with the current pixel color. This is the preferred transparency mode to avoid transparency sorting issues.
Custom Allows for every aspect of the rendering mode to be controlled manually. For advanced usage only.

Rendering Modes

Cull Mode Description
Off Disables face culling. Culling should only be set to Off when a two sided mesh is required.
Front Enables front face culling.
Back (Default) Enables back face culling. Back face culling should be enabled as often as possible to improve rendering performance.

Performance

One of the primary advantages to using the MRTK Standard shader over the Unity standard shader is performance. The MRTK Standard Shader is extensible to only utilize the features enabled. However, the MRTK Standard shader has also been written to deliver comparable aesthetic results as the Unity Standard shader, but at a much lower cost. One simple way to compare shader performance is via the number of operations that needs to be performed on the GPU. Of course, the magnitude of calculations may fluctuate by features enabled and other rendering configurations. But, in general, the MRTK Standard shader performs significantly less computation than the Unity Standard shader.

Unity Standard shader statistics example

Unity Standard Shader Statistics

MRTK Standard shader statistics example

MRTK Standard Shader Statistics

Note

These results can be generated by selecting and viewing a shader asset in the Unity inspector, then clicking the Compile and show code button.

Lighting

The MRTK/Standard uses a simple approximation for lighting. Because this shader does not calculate for physical correctness and energy conservation, it renders quickly and efficiently. Blinn-Phong is the primary lighting technique which is blended with Fresnel and image-based lighting to approximate physically-based lighting. The shader supports the following lighting techniques:

Directional light

The shader will respect the direction, color, and intensity of the first Unity Directional Light in the scene (if enabled). Dynamic point lights, spot lights, or any other Unity light will not be considered in real time lighting.

Spherical harmonics

The shader will use Light Probes to approximate lights in the scene using Spherical Harmonics, if enabled. Spherical harmonics calculations are performed per vertex to reduce calculation cost.

Lightmapping

For static lighting, the shader will respect lightmaps built by Unity's Lightmapping system. Simply mark the renderer as static (or lightmap static) to use lightmaps.

Hover light

Proximity light

Lightweight Scriptable Render Pipeline support

The MRTK contains an upgrade path to allow developers to utilize Unity's Lightweight Scriptable Render Pipeline (LWRP) with MRTK shaders. Tested in Unity 2019.1.1f1 and Lightweight RP 5.7.2 package. For instructions on getting started with the LWRP, see this page.

To perform the MRTK upgrade, select: Mixed Reality Toolkit -> Utilities -> Upgrade MRTK Standard Shader for Lightweight Render Pipeline

lwrp upgrade

After the upgrade occurs, the MRTK/Standard shader will be altered and any magenta (shader error) materials should be fixed. To verify the upgrade successfully occurred, check the console for: Upgraded Assets/MixedRealityToolkit/StandardAssets/Shaders/MixedRealityStandard.shader for use with the Lightweight Render Pipeline.

UGUI support

The MRTK Standard shading system works with Unity's built in UI system. On Unity UI components, the unity_ObjectToWorld matrix is not the transformation matrix of the local transform the Graphic component lives on but that of its parent Canvas. Many MRTK/Standard shader effects require object scale to be known. To solve this issue, the ScaleMeshEffect.cs will store scaling information into UV channel attributes during UI mesh construction.

Note, when using a Unity Image component, it is recommended to specify "None (Sprite)" for the Source Image to prevent Unity UI from generating extra vertices.

A Canvas within MRTK will prompt for the addition of a ScaleMeshEffect.cs when one is required:

scale mesh effect

Texture combiner

To improve parity with the Unity Standard shader per pixel metallic, smoothness, emissive, and occlusion values can all be controlled via channel packing. For example:

channel map example

When you use channel packing, you only have to sample and load one texture into memory instead of four separate ones. When you write your texture maps in a program like Substance or Photoshop, you can hand pack them like the following:

Channel Property
Red Metallic
Green Occlusion
Blue Emission (Greyscale)
Alpha Smoothness

Or, you can use the MRTK Texture Combiner Tool. To open the tool, select: Mixed Reality Toolkit -> Utilities -> Texture Combiner which will open the below window:

texture combiner example

This window can be automatically filled out by selecting a Unity Standard shader and clicking "Autopopulate from Standard Material." Or, you can manually specify a texture (or constant value) per red, green, blue, or alpha channel. The texture combination is GPU accelerated and does not require the input texture to be CPU accessible.

Additional feature documentation

Below are extra details on a handful of feature details available with the MRTK/Standard shader.

Primitive clipping

primitive clipping

Mesh outlines

Many mesh outline techniques are done using a post processing technique. Post processing provides great quality outlines, but can be prohibitively expensive on many Mixed Reality devices. You can find a scene that demonstrates usage of mesh outlines in the OutlineExamples scene under MRTK/Examples/Demos/StandardShader/Scenes/.

Mesh Outline

MeshOutline.cs and MeshOutlineHierarchy.cs can be used to render an outline around a mesh renderer. Enabling this component introduces an additional render pass of the object being outlined, but is designed to run performantly on mobile Mixed Reality devices and does not utilize any post processes. Limitations of this effect include it not working well on objects which are not watertight (or required to be two sided) and depth sorting issues can occur on overlapping objects.

The outline behaviors are designed to be used in conjunction with the MRTK/Standard shader. Outline materials are usually a solid unlit color, but can be configured to achieve a wide array of effects. The default configuration of a outline material is as follows:

Mesh Outline Material
  1. Depth Write - should be disabled for outline materials to make sure the outline does not prevent other objects from rendering.
  2. Vertex Extrusion - needs to be enabled to render the outline.
  3. Use Smooth Normals - this setting is optional for some meshes. Extrusion occurs by moving a vertex along a vertex normal, on some meshes extruding along the default normals will cause discontinuities in the outline. To fix these discontinuities, you can check this box to use another set of smoothed normals which get generated by MeshSmoother.cs

MeshSmoother.cs is a component which can be used to automatically generate smoothed normals on a mesh. This method groups vertices in a mesh that share the same location in space then averages the normals of those vertices. This process creates a copy of the underlying mesh and should be used only when required.

Smooth Normals Outline
  1. Smooth normals generated via MeshSmoother.cs.
  2. Default normals used, notice the artifacts around the cube corners.

Stencil testing

Built in configurable stencil test support to achieve a wide array of effects. Such as portals:

stencil test

Instanced color support

Instanced color support to give thousands of GPU instanced meshes unique material properties:

instanced properties

Triplanar mapping

Triplanar mapping is a technique to programmatically texture a mesh. Often used in terrain, meshes without UVs, or difficult to unwrap shapes. This implementation supports world or local space projection, the specification of blending smoothness, and normal map support. Note, each texture used requires 3 texture samples, so use sparingly in performance critical situations.

triplanar

Vertex extrusion

Vertex extrusion in world space. Useful for visualizing extruded bounding volumes or transitions in/out meshes.

normal map scale 1

Miscellaneous

A checkbox to control albedo optimizations. As an optimization albedo operations are disabled when no albedo texture is specified. This is useful for controlling remote texture loading.

Simply check this box:

albedo assignment

Per pixel clipping textures, local edge based anti aliasing, and normal map scaling are supported.

normal map scale 2

See also