Gorilla.Material.Default Unit Reference

Overview

The Gorilla.Material.Default unit provides the core material system for Gorilla3D, including the TGorillaDefaultMaterialSource component and related shader infrastructure. This unit implements a node-based shader generation system that automatically creates GLSL shaders based on enabled features and properties.

Architecture

The material system uses a hierarchical node structure to compose shaders:

TGorillaDefaultMaterialSource
└─ TGorillaDefaultMaterial
   ├─ TGorillaGLSLVertexShader (Node Builder)
   │  └─ Shader Nodes
   └─ TGorillaGLSLFragmentShader (Node Builder)
      └─ Shader Nodes

Core Classes

TGorillaDefaultMaterialSource

Primary material source component providing comprehensive rendering features.

Inheritance: TCustomMaterialSource → TGorillaDefaultMaterialSource

Platforms: All Gorilla3D supported platforms

Published Properties

Basic Material Properties
Property Type Default Description
Diffuse TAlphaColorF - Base diffuse color (RGB + alpha).
Ambient TAlphaColorF - Ambient color contribution.
Specular TAlphaColorF - Specular highlight color.
Emissive TAlphaColorF - Self-illumination color.
Shininess Single 32.0 Specular shininess exponent (higher = sharper highlights).
Shading Configuration
Property Type Default Description
ShadingModel TGorillaShadingModel smLambert Shading algorithm: smLambert, smPhong, smBlinnPhong, smPBR.
ShadingIntensity Single 1.0 Intensity of shading applied to diffuse and specular.
Brightness Single 1.0 Overall brightness multiplier.
UseLighting Boolean True Enable/disable lighting calculations.
UseSpecular Boolean True Enable/disable specular highlights.
Texture Properties
Property Type Description
Texture TBitmap Diffuse/albedo texture (texture unit 0).
NormalMap TBitmap Normal map texture.
SpecularMap TBitmap Specular map texture.
EmissiveMap TBitmap Emissive/glow map texture.
DisplacementMap TBitmap Displacement/height map texture.
OpacityMap TBitmap Opacity/alpha map texture.
EnvironmentMap TBitmap Environment cube map texture.
Texture Control Flags
Property Type Default Description
UseVertexColor Boolean False Use mesh vertex colors.
UseTexturing Boolean False Enable texture mapping.
UseTexture0 Boolean False Enable diffuse texture.
UseNormalMapping Boolean False Enable normal mapping.
UseBumpMapping Boolean False Enable bump mapping (grayscale).
UseSpecularMapping Boolean False Enable specular mapping.
UseEmissiveColoring Boolean False Enable emissive color.
UseEmissiveMapping Boolean False Enable emissive texture.
UseDisplacementMapping Boolean False Enable displacement mapping.
UseDisplacementTexture Boolean False Enable displacement texture.
UseOpacityMapping Boolean False Enable opacity texture.
UseParallaxOcclusionMapping Boolean False Enable POM (requires displacement texture).
UseDepthMap Boolean False Enable depth mapping.
PBR Texture Properties
Property Type Description
Texture TBitmap PBR albedo/base color texture.
PBRMetallicTexture TBitmap PBR metallic map (0=dielectric, 1=metal).
PBRRoughnessTexture TBitmap PBR roughness map (0=smooth, 1=rough).
PBRAOTexture TBitmap PBR ambient occlusion map.
EmissiveTexture / EmissiveMap TBitmap PBR emissive map.
PBRSpecularTexture TBitmap PBR specular map.
PBR Texture Flags
Property Type Default Description
UsePBRAlbedoTexture Boolean False Enable PBR albedo texture.
UsePBRMetallicTexture Boolean False Enable PBR metallic texture.
UsePBRRoughnessTexture Boolean False Enable PBR roughness texture.
UsePBRAOTexture Boolean False Enable PBR AO texture.
UsePBREmissiveTexture Boolean False Enable PBR emissive texture.
UsePBRSpecularTexture Boolean False Enable PBR specular texture.
PBR Parameters
Property Type Range Description
MetallicBias Single 0.0-1.0 Metalness value (0=dielectric, 1=metal).
RoughnessBias Single 0.0-1.0 Surface roughness (0=smooth, 1=rough).
AOBias Single 0.0-1.0 Ambient occlusion intensity.
Advanced Mapping
Property Type Default Description
NormalIntensity Single 1.0 Normal/bump intensity multiplier. The value differs between usage for regular normal mapping and parallax occlusion mapping (POM). When using POM the recommended value is 0.1.
InverseNormalMapping Boolean False Invert green channel (OpenGL vs DirectX convention).
ParallaxLevels Integer 64 POM raytracing steps (4-256, higher = better quality).
Effects and Features
Property Type Default Description
Shadows Boolean False Enable shadow reception.
ShadowMethod TGorillaShadowMethod smShadowMapping Shadow algorithm: smShadowMapping or smVSM.
Reflections Boolean False Enable reflection mapping.
ReflectionPower Single 0.5 Reflection intensity (0-1).
ReflectionPass TGorillaRenderPassController - Link an existing reflection render pass controller.
Refractions Boolean False Enable refraction mapping.
RefractionPower Single 0.8 Refraction intensity (0-1).
RefractionPass TGorillaRenderPassController - Link an existing refraction render pass controller.
UseRefractionDepth Boolean False The refraction render pass is able to provide depth information. To use this data instead of a separated DepthPass.
DepthMapping Boolean False Enable depth mapping.
DepthPass TGorillaRenderPassController - Link an existing depth render pass controller.
Environment Boolean False Enable environment mapping. To control reflection and refraction use ReflectionPower and RefractionPower properties.
EnvironmentPass TGorillaRenderPassController - Link an existing environment render pass controller.
Alpha and Transparency
Property Type Default Description
UseAlphaCutoff Boolean False Enable alpha testing (discard fragments).
AlphaCutoff Single 0.5 Alpha threshold (0-1).
UseFragmentDepthTransparency Boolean False Enable fragment depth rendering for transparency.
UseColorClipping Boolean False Enable color-based clipping.
ClippingColor TAlphaColorF - Color to discard in fragment shader.
Tonemapping
Property Type Default Description
Tonemapping TGorillaTonemapping tmNone Tonemapping operator: tmNone, tmReinhard, tmACESFilmic, etc.
TonemappingExposure Single 1.0 Exposure adjustment for tonemapping.
Material Behaviours
Property Type Description
Behaviours TGorillaMaterialBehaviourCollection Collection of attached material behaviours.
Time Management
Property Type Description
MeasureTime Boolean Enable time measurement for animated shaders.
StartTime Double Initial time value.
RenderTime Double Current render time.
LastTime Double Previous frame time.
DeltaTime Double Time delta between frames (read-only).
Render Pass Integration
Property Type Description
ShadowMapPass[Index] TGorillaRenderPassController Shadow map render pass (0-7).
Inputs TGorillaInputRenderPassMap Render pass inputs map.

Public Methods

constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;

procedure BeginChange();
procedure EndChange();

Batch multiple property changes to prevent shader rebuilds.

Example:

Material.BeginChange();
try
  Material.Diffuse := NewDiffuse;
  Material.Specular := NewSpecular;
  Material.Shininess := NewShininess;
finally
  Material.EndChange();
end;

Material Behaviour Management

function MaterialBehaviourAttached(ABehaviour: TGorillaMaterialBehaviour): Boolean;

Returns True if a specific behaviour is attached to this material source.

Parameters:

  • ABehaviour - The behaviour component to check

Returns: True if behaviour is attached, False otherwise

Example:

if Material.MaterialBehaviourAttached(WindBehaviour) then
  ShowMessage('Wind behaviour is active');

function IndexOfMaterialBehaviour(ABehaviour: TGorillaMaterialBehaviour): Integer;

Returns the numeric index of a behaviour in the collection, or -1 if not found.

Parameters:

  • ABehaviour - The behaviour component to find

Returns: Index (0-based) or -1 if not attached

Example:

var Index := Material.IndexOfMaterialBehaviour(WindBehaviour);
if Index >= 0 then
  ShowMessage('Found at index: ' + IntToStr(Index));

procedure AttachMaterialBehaviour(ABehaviour: TGorillaMaterialBehaviour);

Attaches a material behaviour to this material source. If the behaviour is already attached, it will be ignored.

Parameters:

  • ABehaviour - The behaviour component to attach

Example:

WindBehaviour := TGorillaMaterialWindBehaviour.Create(Self);
WindBehaviour.WindPower := 1.5;
Material.AttachMaterialBehaviour(WindBehaviour);

procedure DetachMaterialBehaviour(ABehaviour: TGorillaMaterialBehaviour);

Detaches a material behaviour from this material source. If the behaviour is not found, the method will ignore it.

Parameters:

  • ABehaviour - The behaviour component to detach

Example:

Material.DetachMaterialBehaviour(WindBehaviour);

procedure ClearMaterialsBehaviours();

Detaches all attached material behaviours from this material source.

Example:

Material.ClearMaterialsBehaviours();

TGorillaDefaultMaterial

Internal material class that performs actual rendering.

Inheritance: TCustomMaterial → TGorillaDefaultMaterial

Public Properties

Property Type Description
Source TGorillaDefaultMaterialSource Parent material source component.
ShaderData TGorillaShaderData Uniform buffer data structure.
Textures TGorillaTexturePool Texture management pool.
Behaviours TGorillaMaterialBehaviourCollection Material behaviours collection.

Public Methods

procedure DoApply(const Context: TContext3D); override;

Applies material to rendering context. Called automatically during rendering.


procedure DoInitialize(); override;
procedure DoUninitialize(); override;

Initialize/uninitialize material resources.


Data Structures

TGorillaShaderData

Packed record containing all uniform data for shaders. Transferred to GPU via uniform buffer objects for maximum performance.

Memory Layout: Carefully aligned for std140 layout (GLSL uniform buffer standard).

Fields

Field Type Description
ModelViewProjectionMatrix TMatrix3D Combined MVP matrix.
ViewMatrix TMatrix3D View/camera matrix.
ProjectionMatrix TMatrix3D Projection matrix.
TimeInfo TVector3D X=start time, Y=current time, Z=previous time, W=delta.
EyePos TVector3D Absolute camera position.
ViewDir TPoint3D Camera view direction.
ShadingModel FixedInt Active shading model.
Material TGorillaShaderMaterial Material properties (colors, etc.).
Lights Array Light sources (size platform-dependent).
LightsCount FixedInt Number of active lights.
Options FixedInt Shader options bitmask.
ShadingIntensity Single Shading intensity multiplier.
Brightness Single Brightness multiplier.
FogMode FixedInt Fog settings bitmask.
FogDensity Single Fog density.
FogStart Single Fog start distance.
FogEnd Single Fog end distance.
FogColor TAlphaColorF Fog color.
FrustumMin/Max TVector3D Camera frustum bounds.
SceneMin/Max TVector3D Scene bounds.
BoundaryMin/Max TVector3D Boundary bounds.
ViewSize TVector3D X/Y=viewport size, Z/W=near/far planes.
ClippingPlane TVector3D Clipping plane equation.
PassData TGorillaShaderPassData Render pass specific data.

TGorillaShaderPassData

Data specific to render passes (shadows, reflections, refractions).

Fields

Field Type Description
ShadowMapMatrix Array[0..7] of TMatrix3D Shadow projection matrices.
ReflectTextureMatrix TMatrix3D Reflection texture matrix.
RefractTextureMatrix TMatrix3D Refraction texture matrix.
ReflectionPower Single Reflection intensity.
RefractionPower Single Refraction intensity.
TranslucentDistance Single Translucent depth distance.
NormalIntensity Single Normal mapping intensity.
ParallaxLevels FixedInt POM raytracing levels.
PBRMetallic Single PBR metallic value.
PBRRoughness Single PBR roughness value.
PBRAO Single PBR ambient occlusion value.

Enumerations

TGorillaShadingModel

Shading model selection.

Value Description
smLambert Lambert diffuse shading (no specular).
smPhong Phong shading with specular highlights.
smBlinnPhong Blinn-Phong shading (optimized Phong).
smPBR Physically-Based Rendering.

TGorillaTonemapping

Tonemapping operators for HDR to LDR conversion.

Value Description
tmNone No tonemapping.
tmExposure Simple exposure adjustment.
tmNarkowicz ACES Narkowicz approximation.
tmHill ACES Hill operator.
tmHillBoost ACES Hill with exposure boost.
tmReinhard Reinhard operator.
tmLumaBasedReinhard Luminance-based Reinhard.
tmWhitePreservingLumaBasedReinhard White-preserving Reinhard.
tmRomBinDaHouse RomBinDaHouse operator.
tmFilmic Filmic operator.
tmUncharted2 Uncharted 2 operator.

TGorillaShadowMethod

Shadow mapping techniques.

Value Description
smShadowMapping Standard shadow mapping.
smVSM Variance Shadow Mapping.

Constants

Light Limits

GORILLA_MATERIAL_DEFAULT_LIGHTS = Platform-dependent
// Windows/Linux (OpenGL 4.3): 32 lights
// Linux (OpenGL 4.3): 32 lights
// macOS (Metal): 32 lights
// Android (GLES 3.0): 8 lights
// Android (GLES 2.0): 2 lights
// iOS (Metal): 8 lights

Usage Examples

Basic Material Setup

var
  Material: TGorillaDefaultMaterialSource;
begin
  Material := TGorillaDefaultMaterialSource.Create(Self);
  Material.ShadingModel := TGorillaShadingModel.smBlinnPhong;
  Material.Diffuse := TAlphaColorF.Create(0.7, 0.5, 0.3, 1.0);
  Material.Specular := TAlphaColorF.Create(1.0, 1.0, 1.0, 1.0);
  Material.Shininess := 32.0;
  Material.UseTexture0 := False;

  Mesh.MaterialSource := Material;
end;

Textured Material

Material.UseTexturing := True;
Material.UseTexture0 := True;
Material.Texture.LoadFromFile('diffuse.png');

Material.UseNormalMapping := True;
Material.NormalMap.LoadFromFile('normal.png');
Material.NormalIntensity := 1.0;

PBR Material

Material := TGorillaPBRMaterialSource.Create(Self);

// Texture-based PBR
Material.UsePBRAlbedoTexture := True;
Material.Texture.LoadFromFile('albedo.png');

Material.UsePBRMetallicTexture := True;
Material.MetallicTexture.LoadFromFile('metallic.png');

Material.UsePBRRoughnessTexture := True;
Material.RoughnessTexture.LoadFromFile('roughness.png');

Material.UsePBRAOTexture := True;
Material.AmbientOcclusionTexture.LoadFromFile('ao.png');

// Or use bias values
Material.MetallicBias := 0.8;
Material.RoughnessBias := 0.3;
Material.AOBias := 1.0;

Using Material Behaviours

var
  WindBehaviour: TGorillaWindBehaviour;
  Item: TGorillaMaterialBehaviourItem;
begin
  WindBehaviour := TGorillaWindBehaviour.Create(Self);
  WindBehaviour.Viewport := Viewport;

  Material.AttachMaterialBehaviour(WindBehaviour);
end;

Batching Changes

Material.BeginChange();
try
  Material.Diffuse := NewColor;
  Material.UseTexturing := True;
  Material.UseTexture0 := True;
  Material.Texture := NewTexture;

  Material.UseNormalMapping := True;
  Material.NormalMap := NewNormal;
finally
  Material.EndChange();
end;

Important Notes

  1. Shader Rebuilding: Changing material properties may trigger shader recompilation. Use BeginChange/EndChange to batch updates.

  2. Platform Light Limits: Number of supported lights varies by platform and graphics API version.

  3. Texture Units: Each texture type uses a specific texture unit. Ensure hardware supports required number of units.

  4. PBR Requirements: PBR shading requires modern graphics hardware. Fallback to Blinn-Phong on older devices.

  5. Uniform Buffer Alignment: TGorillaShaderData uses std140 layout. Manual modifications require understanding of GPU memory alignment.

  6. Material Behaviours: Behaviours are v1.1+ feature. Provide modular shader functionality without subclassing.

  7. Fog Integration: Fog properties are controlled by viewport, not material. Material must support fog rendering.

See Also