# 3D Node Appearance

#### <a class="anchor" id="bkmrk--103"></a>Description

[Candera::Appearance](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_appearance.html "The class Appearance groups following render attributes: Material, Textures, RenderMode, and Shader. Render attributes define the distinctive visualization of a geometry like Mesh, Billboard, and PointSprite. Further, render attributes can be shared across multiple objects, which conserves memory and enables sharing of appearance characteristics. An Appearance object is mandatory for any object in order to get rendered. Per default all render attributes are initialized to null, which is a valid setting. If the Appearance is activated, all render attributes that are set become activated. If no RenderMode is defined (null), then the default RenderMode is used instead;.") groups following render attributes:

<div class="contents" id="bkmrk-candera%3A%3Amaterial%2C-c"><div class="contents"><div class="textblock">- [Candera::Material](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_material.html "The class Material describes the color attributes of an object's surface and is primarily used for li..."),
- [Candera::Texture](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_texture.html "Texture encapsulates a TextureImage and a set of attributes specifying how it is applied to a vertex'..."),
- [Candera::RenderMode](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html "The class RenderMode is an Appearance component that encapsulates polygon-level and per-fragment comp...")
- [Candera::Shader](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader.html "The class Shader is an Appearance component representing a graphical processing unit (GPU) program...") and
- [Candera::ShaderParamSetter](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html "ShaderParamSetter maintains a list of uniform parameters that are passed to a Shader.").

</div></div></div>Render attributes define the distinctive visualization of a geometry like [Candera::Mesh](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_mesh.html "The class Mesh represents a three-dimensional rigid body object defined by polygons. The Mesh's polygonal surface is defined by its VertexBuffer. Each Mesh must have exactly one Appearance set, in order to specify how the Mesh geometry is rendered."), [Candera::Billboard](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_billboard.html "The class Billboard represents a Plane that can align towards camera automatically, according to the alignment type set. Thus, Billboards are quite often used as "imposters" pretending to be a 3D geometry by showing a 2D image that is always facing the camera. In order to relief distinction between Billboard and PointSprite see following comparison: + Billboards support rectangular dimension and non-uniform scale, + Billboards support different rotation techniques (see Alignment), whereas PointSprites are aligned according to CenterEyeAlignment, exclusively. + Billboards take local rotation and scale into account (see class Transformable), whereas PointSprites ignore Transformable parameters others than position. + PointSprites have a performance advantage due to less geometry (one instead of 4 vertices). Overall recommendation: Use PointSprites for spherical shapes like particles, lens flares, sparkles, dust which are screen aligned (see Billboard::CameraUpAlignment). Further, use Billboards for world up oriented clouds, text, or yaw axis aligned trees, and signs, etc. Note: The Billboard - because of absent normals - does not support lighting. Belows sketch depicts the layout of a Billboard. W U:0,V:1 +-----+ U:1,V:1 H | X | Legend: texture coordinates: U,V; Width: W, Height: H, Local Center: X U:0,V:0 +-----+ U:1,V:0."), and [Candera::PointSprite](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_point_sprite.html "PointSprite represents a GL point primitive. The size (glPointSize) of the point is calculated by def..."). Further, render attributes can be shared across multiple objects, which conserve memory and enable sharing of appearance characteristics.

A [Candera::Appearance](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_appearance.html "The class Appearance groups following render attributes: Material, Textures, RenderMode, and Shader. Render attributes define the distinctive visualization of a geometry like Mesh, Billboard, and PointSprite. Further, render attributes can be shared across multiple objects, which conserves memory and enables sharing of appearance characteristics. An Appearance object is mandatory for any object in order to get rendered. Per default all render attributes are initialized to null, which is a valid setting. If the Appearance is activated, all render attributes that are set become activated. If no RenderMode is defined (null), then the default RenderMode is used instead;.") object is mandatory for any object in order to get rendered. Per default no Appearance attributes are attached.

If the Appearance is activated, all render attributes that are set become activated. If no RenderMode is defined (null), then the default RenderMode is used instead.

#### <a class="anchor" id="bkmrk--104"></a>**Appearance: Material**

##### <a class="anchor" id="bkmrk--105"></a>Material Attributes

[Candera::Material](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_material.html "The class Material describes the color attributes of an object's surface and is primarily used for li...") describes the color attributes of an object's surface and is primarily used for lighting computations. If no material is set, lighting calculations in associated shaders can not be applied.

The color attributes are defined as follows:

<div class="contents" id="bkmrk-ambient-rgb-color-th"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">Ambient</td><td valign="top">RGB color that interacts with the ambient attribute of light.</td></tr><tr><td valign="top">Emissive</td><td valign="top">RGB color that defines the self-lighting of the material. This color is visible, even when Material is unlighted.  
The emissive color attribute does neither interact with any type of light source nor with other 3D objects.</td></tr><tr><td valign="top">Diffuse</td><td valign="top">RGBA color that interacts with the diffuse attribute of light. The alpha value of the diffuse color, defines  
the alpha factor of the entire Material, supposed that alpha blending is enabled (For details see [Candera::RenderMode](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html "The class RenderMode is an Appearance component that encapsulates polygon-level and per-fragment comp...")).</td></tr><tr><td valign="top">Specular</td><td valign="top">RGB color that interacts with the specular attribute of light.</td></tr></tbody></table>

</div></div></div>Furthermore with Specular Power the sharpness of a specular highlight, if lit by specular light, can be defined.

##### <a class="anchor" id="bkmrk--106"></a>Create a new Material

```
    m_material = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">Material::Create</a>();
```

##### <a class="anchor" id="bkmrk--107"></a>Set Colors

This sample code shows how to change the ambient color, the code for changing the diffuse color, the emissive color or the specular color looks similarly.

```
    // Set ambient color
    if (node->GetAppearance() != 0 && node->GetAppearance()->GetMaterial() != 0){
        node->GetAppearance()->GetMaterial()->SetAmbient(m_color);
    }
    // end Set ambient color
```

#### **Appearance: Texture** 

##### <a class="anchor" id="bkmrk--108"></a>Description

[Candera::Texture](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_texture.html "Texture encapsulates a TextureImage and a set of attributes specifying how it is applied to a vertex'...") encapsulates a [Candera::TextureImage](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_texture_image.html "Abstract base class for Texture images.A texture image mainly encapsulates a texture handle in VRAM a...") and a set of attributes specifying how it is applied to a vertex's texture coordinate. [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") implements a sharing mechanism based on TextureImages. These hold the actual VRAM handle. Multiple textures can share one TextureImage object. The TextureImage manages its upload to the VRAM.

**Attention:**

<div class="contents" id="bkmrk-the-texture-is-only-"><div class="contents"><div class="textblock">- The Texture is only useable with an associated TextureImage.
- Do not share TextureImages, if TextureImage modification shall not be shared across Textures.
- In order to support MipMapping or repeated wrapping, the bitmap's width and height must be a power of 2 (n^2), like e.g. 2, 4, 8, 16 ,32, etc. However, height and width can be different, e.g.: 256x128.

</div></div></div>##### <a class="anchor" id="bkmrk--109"></a>Texture Filtering

<div class="contents" id="bkmrk-minification-and-mag"><div class="contents"><div class="textblock">- **Minification and magnification:**

</div></div></div>Following texture filters can be used to improve visual quality of Textures: Magnification to upscale, Minification to downscale.

<div class="contents" id="bkmrk-nearest-value-of-the"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">Nearest</td><td valign="top">Value of the texel that is nearest to the center of the pixel being textured.</td></tr><tr><td valign="top">Linear</td><td valign="top">The weighted average of the four texels that are closest to the center of the pixel being textured.</td></tr></tbody></table>

</div></div></div><div class="contents" id="bkmrk-mipmapping%3A-mipmappi"><div class="contents"><div class="textblock">- **Mipmapping:** MipMapping can be used to avoid texture aliasing effects.

<table class="doxtable"><tbody><tr><td valign="top">None</td><td valign="top">MipMapping disabled</td></tr><tr><td valign="top">Nearest</td><td valign="top">MipMapping uses nearest mip map level</td></tr><tr><td valign="top">Linear</td><td valign="top">MipMapping uses bilinear mipmap interpolation of the two nearest mip map levels</td></tr></tbody></table>

</div></div></div><div class="contents" id="bkmrk-wrapmode%3A"><div class="contents"><div class="textblock">- **WrapMode:**

</div></div></div>It specifies how the texture is wrapped:

<div class="contents" id="bkmrk-repeat-repeat-the-te"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">Repeat</td><td valign="top">Repeat the texture</td></tr><tr><td valign="top">ClampToEdge</td><td valign="top">Clamp fetches to the edge of the texture</td></tr><tr><td valign="top">Linear</td><td valign="top">MipMapping uses bilinear mipmap interpolation of the two nearest mip map levels</td></tr></tbody></table>

</div></div></div><div class="contents" id="bkmrk-maxanisotropy%3A"><div class="contents"><div class="textblock">- **MaxAnisotropy:**

</div></div></div>It specifies the maximum degree of anisotropy to account for in texture filtering for the Texture object. Anisotropic filtering improves the quality of the textures when they are not uniformly scaled because, for example, the textured triangle is not exactly facing the camera. The value of maxAnisotropy must be greater or equal to 1.0f (isotropy) and is limited by the max detail of anisotropy supported by hardware, which can be retrieved with the function Candera::RenderDevice::GetMaxAnisotropySupportedByDevice.

##### <a class="anchor" id="bkmrk--110"></a>Changing Texture Image And Setting Filters

```
    m_textureImage = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">BitmapTextureImage::Create</a>();
    m_textureImage->SetName("LabelTextureImage");
    static_cast<void>(m_textureImage->SetBitmap(m_bitmap));
    if (m_textureImage == 0) {
        FEATSTD_DEBUG_ASSERT(false);
        return; // memory is freed in destructor/UpdateBitmap()
    }


    m_texture = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">Texture::Create</a>();
    m_texture->SetName("LabelTexture");
    m_texture->SetTextureImage(m_textureImage);
    m_texture->SetWrapModeU(Texture::ClampToEdge);
    m_texture->SetWrapModeV(Texture::ClampToEdge);
    m_texture->SetMinificationFilter(Texture::MinMagLinear);
    m_texture->SetMagnificationFilter(Texture::MinMagLinear);
```

#### **Appearance: Render Mode** 

##### <a class="anchor" id="bkmrk--111"></a>Description

[Candera::RenderMode](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html "The class RenderMode is an Appearance component that encapsulates polygon-level and per-fragment comp...") is an Appearance component that encapsulates polygon-level and per-fragment compositing render attributes. If an object's Appearance has set RenderMode to null, then the default RenderMode is used instead. For details how to set the default render mode, see [Candera::Renderer::SetDefaultRenderMode](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer.html#a1fb82af52c979cf80cb24102cdf8bc1a).

If a Camera has a RenderMode attached \[camera-&gt;GetApperance()-&gt;SetRenderMode(...)\], then the Camera's RenderMode overrules the DefaultRenderMode during its render pass. Thus, all Nodes rendered by the Camera that do not have their own RenderMode set, use the RenderMode applied to the Camera.

If a RenderMode has set an inheritance bit for a certain render attribute, then the property of the base render mode is used, this is either the Camera's RenderMode if set or the DefaultRenderMode otherwise.

```
        bool m_isColorWriteRedEnabled;       // Default value: true
        bool m_isColorWriteGreenEnabled;     // Default value: true
        bool m_isColorWriteBlueEnabled;      // Default value: true
        bool m_isColorWriteAlphaEnabled;     // Default value: true
        bool m_isDepthWriteEnabled;          // Default value: true
        bool m_isDepthTestEnabled;           // Default value: true
        bool m_isStencilTestEnabled;         // Default value: false
        bool m_isBlendingEnabled;            // Default value: false
```

Set a rendermode:

```
    SharedPointer<RenderMode> rm = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">RenderMode::Create</a>();
    rm->SetBlendingEnabled(true);
    rm->SetBlendMode(RenderMode::SourceAlpha, RenderMode::InverseSourceAlpha, RenderMode::Add);
    rm->SetDepthTestEnabled(false);
    rm->SetDepthWriteEnabled(false);
    appearance->SetRenderMode(rm);

```

##### <a class="anchor" id="bkmrk--112"></a>Render Mode Attributes

<div class="contents" id="bkmrk-blending"><div class="contents"><div class="textblock">- **blending**

</div></div></div>The next chapter <span style="color: rgb(230, 126, 35);">[Color Blending](#bkmrk-appearance%3A-render-m-0)</span> describes possible blending operations in detail.

<div class="contents" id="bkmrk-culling"><div class="contents"><div class="textblock">- **culling**

</div></div></div>Culling determines which side of a polygon is removed before rasterisation.

<div class="contents" id="bkmrk-frontfaceculling-fro"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">FrontFaceCulling</td><td valign="top">Front of the polygon is removed</td></tr><tr><td valign="top">BackFaceCulling</td><td valign="top">Back of the polygon is removed</td></tr><tr><td valign="top">NoCulling</td><td valign="top">Front and back of the polygon are rendered</td></tr></tbody></table>

</div></div></div><div class="contents" id="bkmrk-winding"><div class="contents"><div class="textblock">- **winding**

</div></div></div>Winding defines the front face of a polygon. A polygon side is the front-face if its screen-space vertices are in the same order as the winding specifies.

<div class="contents" id="bkmrk-clockwise-clockwise-"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">ClockWise</td><td valign="top">Clockwise ordered vertices define the front of the polygon.</td></tr><tr><td valign="top">CounterClockWise</td><td valign="top">Counterclockwise ordered vertices define the front of the polygon.</td></tr></tbody></table>

</div></div></div>[Candera::RenderMode::ComparisonFunction](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html#a698f416c261d527fd8a4558ab9b45e55) can be used with depth-, stencil-, or sampler state operations. It specifies how the source (new) data is compared against the destination (existing) data before passing the comparison operations (storing the data).

<div class="contents" id="bkmrk-comparenever-never-p"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td valign="top">CompareNever</td><td valign="top">Never pass the comparison.</td></tr><tr><td valign="top">CompareLess</td><td valign="top">If source data is less than destination data, the comparison passes.</td></tr><tr><td valign="top">CompareEqual</td><td valign="top">If source data is equal than destination data, the comparison passes.</td></tr><tr><td valign="top">CompareLessEqual</td><td valign="top">If source data is less than or equal to destination data, the comparison passes.</td></tr><tr><td valign="top">CompareGreater</td><td valign="top">If source data is greater than destination data, the comparison passes.</td></tr><tr><td valign="top">CompareNotEqual</td><td valign="top">If source data is not equal to destination data, the comparison passes.</td></tr><tr><td valign="top">CompareGreaterEqual</td><td valign="top">If source data is greater than or equal to destination data, the comparison passes.</td></tr><tr><td valign="top">CompareAlways</td><td valign="top">Always pass the comparison.</td></tr></tbody></table>

</div></div></div><div class="contents" id="bkmrk-depth-bias"><div class="contents"><div class="textblock">- **depth bias**

</div></div></div>Depth bias that can be applied to co-planar primitives to reduce z-fighting, according to following function: *Depth bias = (max \* scaleFactor) + (r \* units)*; where *max* is the maximum depth slope of the triangle being rendered and *r* is an implementation-defined constant that is guaranteed to produce the smallest resolvable offset.

<div class="contents" id="bkmrk-olygons-that-are-cop"><div class="contents"><div class="contents"><div class="textblock"><dl class="note"><dt><p class="callout info">olygons that are coplanar can be made to appear not coplanar by adding a z-bias to each one. This is a technique commonly used to ensure that shadows, decals, or hidden-line images on coplanar surfaces are displayed properly. The depth bias is added before the depth test is performed but does not influence the original depth value written into depth buffer.</p>

</dt></dl></div></div></div></div>Via [Candera::RenderMode::SetDepthBias](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html#ae5e30756f98f874d6f05a8ba1b618b62) the depth bias can be set and [Candera::RenderMode::SetDepthTestEnabled](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html#a3983a4d136f976d1208a3bf0fc0832ee) and [Candera::RenderMode::SetDepthWriteEnabled](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_render_mode.html#a5417961fbefced86d218df5a62bb0935) enables it.

<div class="contents" id="bkmrk-stencil-buffer"><div class="contents"><div class="textblock">- **stencil buffer**

</div></div></div>StencilPlane specifies the face associated with the provided stencil function, which are *FrontFace*, *BackFace* and *FrontAndBackFace*. Front face stencil affects non-polygons and front-facing polygons whereas back face stencil affects back-facing polygons only.   
StencilOperation defines the stencil-buffer operation.

<div class="contents" id="bkmrk-settozero-set-the-st"><div class="textblock"><table class="doxtable"><tbody><tr><td style="background-color: rgb(206, 212, 217);" valign="top">**SetToZero**</td><td style="background-color: rgb(206, 212, 217);" valign="top">**Set the stencil-buffer entry to zero.**</td></tr><tr><td valign="top">Keep</td><td valign="top">Do not update the entry in the stencil buffer.</td></tr><tr><td valign="top">Replace</td><td valign="top">Replace the stencil-buffer entry with the reference value.</td></tr><tr><td valign="top">Increment</td><td valign="top">Increment the stencil-buffer entry, clamping to 2^n where n is the number of bits in the stencil buffer.</td></tr><tr><td valign="top">Decrement</td><td valign="top">Decrement the stencil-buffer entry, clamping to zero.</td></tr><tr><td valign="top">Invert</td><td valign="top">Invert the bits in the stencil-buffer entry.</td></tr><tr><td valign="top">IncrementWrap</td><td valign="top">Increment the stencil-buffer entry, wrapping to zero if the new value exceeds the maximum value</td></tr><tr><td valign="top">DecrementWrap</td><td valign="top">Decrement the stencil-buffer entry, wrapping to the maximum value if the new value is less than zero.</td></tr></tbody></table>

</div></div>#### **Appearance: Render Mode - Color Blending** 

##### <a class="anchor" id="bkmrk--113"></a>Description

When Blending is enabled, the output from the fragment shader is blended with the current contents of the frame buffer, rather than merely overwriting it. Blending is mostly used to make objects appear transparent, but can also produce various other effects. (eg. anti-aliasing, DOF, or multi-pass rendering.) There are multiple ways of using blending, but generally, the procedure is as follows:

<div class="contents" id="bkmrk-set-alpha-value-of-a"><div class="contents"><div class="textblock">- Set alpha value of a node
- Set alpha value of a material color
- Set alpha value of the material (this changes the alpha value of the materials diffuse color)
- Enable blending via RenderMode and use the alpha value of the texture in combination with [Candera::RenderMode::BlendMode](http://dev.doc.cgistudio.at/APILINK/struct_candera_1_1_render_mode_1_1_blend_mode.html)

</div></div></div>##### <a class="anchor" id="bkmrk--114"></a>Blend Operations

```
        enum BlendOperation
        {
            Add = 0,               
            Subtract = 1,          
            ReverseSubtract = 2,   
            Min = 3,               
            Max = 4                
        };
```

##### <a class="anchor" id="bkmrk--115"></a>Blend Factor

```
        enum BlendFactor
        {
            Zero = 0,                   
            One  = 1,                   
            SourceColor = 2,            
            InverseSourceColor = 3,     
            SourceAlpha = 4,            
            InverseSourceAlpha = 5,     
            DestColor = 6,              
            InverseDestColor = 7,       
            DestAlpha = 8,              
            InverseDestAlpha = 9,       
            ConstantColor = 10,         
            InverseConstantColor = 11,  
            ConstantAlpha = 12,         
            InverseConstantAlpha = 13,  
            SourceAlphaSaturate = 14    
        };
```

BlendMode combines the source and destination blend factors with the operation used in blending equation.

<div class="contents" id="bkmrk-rgb-blending-equatio"><div class="contents"><div class="textblock">- RGB blending equation: *final\_RGB = source\_RGB \* sourceRGBFactor (+operationRGB+) destination\_RGB \* destRGBFactor*.
- Alpha blending equation: *final\_alpha = source\_alpha \* sourceAlphaFactor (+operationAlpha+) destination\_alpha \* destAlphaFactor*.

</div></div></div>For the Blending Options ConstantColor, InverseConstantColor, ConstantAlpha, or InverseConstantAlpha a blending color can be set.

Set blending:

```
    SharedPointer<RenderMode> rm = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">RenderMode::Create</a>();
    rm->SetBlendingEnabled(true);
    rm->SetBlendMode(RenderMode::SourceAlpha, RenderMode::InverseSourceAlpha, RenderMode::Add);
    rm->SetDepthTestEnabled(false);
    rm->SetDepthWriteEnabled(false);
    appearance->SetRenderMode(rm);
```

#### **Appearance: Shader** 

##### <a class="anchor" id="bkmrk--116"></a>Description

[Candera::Shader](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader.html "The class Shader is an Appearance component representing a graphical processing unit (GPU) program...") is an Appearance component representing a graphical processing unit (GPU) program, thus a vertex and fragment shader pair.

<div class="contents" id="bkmrk-the-shader-program-c"><div class="contents"><div class="textblock">- The Shader program can be parametrized with uniforms and attributes. It's guaranteed by the shader that the program is only created once in VRAM.
- Shader is a device object that counts its uploads to VRAM automatically. Thus, a shader is only uploaded if it has not been uploaded before.
- Further, a shader is unloaded from VRAM, if the upload count is decreased to zero. Take care, that upload is invoked as many times as unload.

</div></div></div>[Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") and SceneComposer offer reference shaders of both vertex and fragment shaders.   
For details refer to section <span style="color: rgb(230, 126, 35);">[Shader Usage](https://doc316en.candera.eu/books/candera/chapter/tutorial-for-shader-usage)</span>.

In SceneComposer via the appearance of a node its ShaderProgram can be chanced easily. Further it is possible to change shaders, the default shader configuration, to create new shaders and combine two of them to a new ShaderProgram.

##### <a class="anchor" id="bkmrk--117"></a>The Default Shader Configuration in SceneComposer

<div class="contents" id="bkmrk-node-vertex-shader-f"><div class="contents"><div class="textblock"><table border="1" cellpadding="5" cellspacing="0"><tbody><tr bgcolor="#d4d4d4"><th>**Node**</th><th>**Vertex Shader**</th><th>**Fragment Shader**</th></tr><tr><td valign="top">Billboard</td><td valign="top">RefTrans</td><td valign="top">RefTex</td></tr><tr><td valign="top">Mesh without texture</td><td valign="top">RefTransLight1</td><td valign="top">RefColor</td></tr><tr><td valign="top">Mesh with texture</td><td valign="top">RefTransLight1</td><td valign="top">RefColorTex</td></tr><tr><td valign="top">MorphingMesh without texture</td><td valign="top">RefTransLight1Morph</td><td valign="top">RefColor</td></tr><tr><td valign="top">MorphingMesh with texture</td><td valign="top">RefTransLight1Morph</td><td valign="top">RefColorTex</td></tr><tr><td valign="top">PointSprite</td><td valign="top">RefTransPointSprite</td><td valign="top">RefPointSprite</td></tr><tr><td valign="top">SkyBox</td><td valign="top">RefTransCubeMap</td><td valign="top">RefCubeMapTex</td></tr></tbody></table>

</div></div></div>#### **Appearance: Shader Parameter Setters** 

##### <a class="anchor" id="bkmrk--118"></a>Description

The [Candera::ShaderParamSetter](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html "ShaderParamSetter maintains a list of uniform parameters that are passed to a Shader.") class is used to pass uniform parameters to a Shader program. The most relevant methods for this class are [Candera::ShaderParamSetter::SetUniform](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html#a2e95acd899a8578d4c2e62425a257b0c) which creates a new uniform (see example below):

```
    shaderParamSetter->SetUniform(m_uniformName, Shader::Float, &m_uniformValue);
```

and the [Candera::ShaderParamSetter::Activate](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html#a40f13a2080caf433d894e4f40b9e9468) which activates all the uniforms previously set by the SetUniform method in the Shader.

<div class="contents" id="bkmrk-in-a-class-derived-f"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">In a class derived from ShaderParamSetter, the methods which calculates the auto-uniforms (e.g. ModelViewProjectionMatrix4), should be invoked in the Activate method.</p>

</dd></dl></div></div></div>##### <a class="anchor" id="bkmrk--119"></a>Generic Shader Param Setters

[Candera::GenericShaderParamSetter](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_generic_shader_param_setter.html "GenericShaderParamSetter bundles uniform shader parameters that are calculated by Candera...") is a class derived from [Candera::ShaderParamSetter](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html "ShaderParamSetter maintains a list of uniform parameters that are passed to a Shader.") which bundles uniform shader parameters that are calculated by [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]"). The class interface offers, for each of these parameters, a specialized method which enables / disables the parameter.

```
    shaderParamSetter->SetModelMatrix4Enabled(true);
    shaderParamSetter->SetModelMatrix3Enabled(true);
    shaderParamSetter->SetNormalModelMatrix3Enabled(true);
    shaderParamSetter->SetModelViewMatrix4Enabled(true);
    shaderParamSetter->SetModelViewMatrix3Enabled(true);
    shaderParamSetter->SetNormalModelViewMatrix3Enabled(true);
    shaderParamSetter->SetModelViewProjectionMatrix4Enabled(true);
    shaderParamSetter->SetCameraLookAtVectorEnabled(true);
    shaderParamSetter->SetCameraPositionEnabled(true);
    shaderParamSetter->SetLightActivationEnabled(true);
    shaderParamSetter->SetMaterialActivationEnabled(true);
    shaderParamSetter->SetTextureActivationEnabled(true);
    shaderParamSetter->SetLightsCoordinateSpace(Light::World);
```

If a parameter is enabled then it will be calculated and passed to the shader, if disabled the parameter is neither calculated nor passed.

##### <a class="anchor" id="bkmrk--120"></a>Default Parameters

Some often needed shader parameters are enabled by default:

<div class="contents" id="bkmrk-modelviewprojectionm"><div class="contents"><div class="textblock">- ModelViewProjectionMatrix4
- LightActivation
- MaterialActivation
- TextureActivation

</div></div></div>If needed, for performance reasons, those parameters might be disabled.

##### <a class="anchor" id="bkmrk--121"></a>Generic Param Setters in SceneComposer

In SceneComposer the generic parameter setters are accessible through the graphic interface in two ways:

<div class="contents" id="bkmrk-as-customizable-unif"><div class="contents"><div class="textblock">- as customizable uniform setter, from the Toolbox panel in the Attachments bar. Once added in the Appearance node list this uniform setter should be configured in the Properties panel by enabling the desired uniforms.
- as a predefined uniform setter, from the Templates panel in the UniformSetters list.

</div></div></div>SceneComposer provides the following predefined uniform setters:

<div class="contents" id="bkmrk-transanisotropicligh"><div class="contents"><div class="textblock">- TransAnisotropicLightShaderParamSetter
- TransLightBumpMapShaderParamSetter
- TransLightShaderParamSetter
- TransLightSphereMapShaderParamSetter
- TransShaderParamSetter

</div></div></div>The table below shows some examples of shaders that can be set by each of the predefined shader parameter setters:

<div class="contents" id="bkmrk-shader-param-setter-"><div class="contents"><div class="textblock"><table border="1" cellpadding="5" cellspacing="0"><tbody><tr bgcolor="#d4d4d4"><th>**Shader Param Setter**</th><th>**Shader Program**</th></tr><tr><td>TransAnisotropicLightShaderParamSetter</td><td>RefTransAnisotropicLight1\_RefAnisotropicLight1SpecularTex</td></tr><tr><td>TransLightBumpMapShaderParamSetter</td><td>RefTransLight1BumpMap\_RefLight1BumpMap</td></tr><tr><td>TransShaderParamSetter</td><td>RefTransLight1\_RefColor</td></tr><tr><td>TransLightSphereMapShaderParamSetter</td><td>RefTransLight1SphereMap\_RefColorTex</td></tr><tr><td>TransLightShaderParamSetter</td><td>RefTransWorldLight1\_RefColor</td></tr></tbody></table>

</div></div></div>Shader parameter setters can be applied to a 3D node by selecting it in the panel and then drag-and-drop it in the Appearance list of the node.

#### **Customized Shader Parameter Setter** 

##### <a class="anchor" id="bkmrk--122"></a>Customized Shader Parameter Setter - Example

If for any reason the standard shader parameter setters are not sufficient, it is possible to create a customized shader parameter setter. However, a custom shader parameter setter cannot be configured in SceneComposer, it must be associated to the desired node by coding.

The following example explains how to create and use a customized shader parameter setter in a widget. Refer to the

<div class="contents" id="bkmrk-shaderparamsetterwid"><div class="contents"><div class="textblock">- *ShaderParamSetterWidget* in combination with the
- *ShaderParamSetterSolution*,   
    both present in the *cgi\_studio\_player* folder.

</div></div></div>The widget *ShaderParamSetterWidget* allows to set the u\_Material.emissive and the u\_MVPMatrix uniform parameters for any node using an appropriate shader program (for example, the nodes in the ShaderParamSetterSolution are using the **RefTransLight1\_RefColor** shader program).

##### <a class="anchor" id="bkmrk--123"></a>Simple Shader Parameter Setter for u\_Material.emissive &amp; u\_MVPMatrix Uniforms

In order to pass the u\_Material.emissive and u\_MVPMatrix uniforms to a shader program, the widget uses an instance of the class *SimpleShaderParameterSetter*:

```
    // Create uniform setter instance
    // m_shaderParamSetter is of type SharedPointer<SimpleShaderParamSetter> ;
    m_shaderParamSetter = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">SimpleShaderParamSetter::Create</a>();
    // end Create uniform setter instance
```

The only method that needs to be overridden in the SimpleShaderParameterSetter is [Candera::ShaderParamSetter::Activate](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_shader_param_setter.html#a40f13a2080caf433d894e4f40b9e9468).

The example *ShaderParamSetterWidget* uses this SimpleShaderParameterSetter by attaching the created instance to the node, which is associated to the widget:

```
    // Attach uniform setter
    if ( (GetNode() != 0) && (GetNode()->GetAppearance() != 0) ) {
        GetNode()->GetAppearance()->SetShaderParamSetter(m_shaderParamSetter);
    }
    // end Attach uniform setter
```

Whenever the widget property "Uniform Color" is modified, the widget sets the given color value (m\_color) as u\_Material.emissive uniform to the SimpleShaderParameterSetter:

```
    // Set u_Material.emissive uniform
    FEATSTD_UNUSED(m_shaderParamSetter->SetUniform(ShaderParamNames::GetUniformName(ShaderParamNames::MaterialEmissive), Shader::FloatVec4, reinterpret_cast<Float*>(&m_shaderColor[0]), 1));
    // end Set u_Material.emissive uniform
```

The node vertices are mapped from the model space to the screen space using the Model-View-Projection Matrix (u\_MVPMatrix). SimpleShaderParameterSetter calculates the u\_MVPMatrix using the node and camera attributes and then passes it to the shader:

```
    // Set u_MVPMatrix uniform
    const Matrix4 mvpMatrix = node.GetWorldTransform() * RenderDevice::GetActiveCamera()->GetViewProjectionMatrix();
    return shader.SetUniform( mvpMatrix.GetData(), ShaderParamNames::GetUniformName(ShaderParamNames::ModelViewProjectionMatrix4), Shader::FloatMat4 );
    // end Set u_MVPMatrix uniform
```

Modifications made using the SetUniform method become effective each time the camera renders the scene because the method Activate is invoked internally by the node.

#### **Multipass Appearance** 

##### <a class="anchor" id="bkmrk--124"></a>Description

A [Candera::MultiPassAppearance](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_multi_pass_appearance.html "A MultiPassAppearance enables a node to be rendered multiple times with different appearance settings...") is a dedicated Appearance which enables a node to be rendered multiple times with different appearance settings which are blended in order to achieve a certain visual result like e.g. fur or blurred rendering.

The sequence of render passes is created by chaining instances of MultiPassAppearance using the SetNextPass method:

```
    multiPassAppearance1->SetNextPass(multiPassAppearance2);
    multiPassAppearance2->SetNextPass(multiPassAppearance3);
```