Skip to content

Scene materials (.MAT, PC only)

The Steam release adds one .MAT member to every scene archive. It is the serialised ZRenderMaterialBuffer of the PC renderer (engine/drawing/zrmaterial.cpp, _wintel/renderd3d/zrd3dmaterial.cpp in the executable's source strings): the material classes, their shader layers, and one material instance per id. PC mesh objects name a material (SPrimObject::lMaterialId, prm.md "PC version") where the PS2 prims carry a texture id and draw-mode bits. port/engine/system/_sdl/pcdata/pcmaterials.{h,cpp} parses it (PcParseMaterials); the prim converter (pcprims.cpp) turns each material into PS2 record fields.

Measured on the 68 scene archives of the Steam release.

File layout

All offsets are from the start of the member, all values little-endian.

Offset Field
0x0 offset of the class table: 32 words, record offsets (0 = none)
0x4 offset of the instance table: 2,048 words, indexed by material id (0 = no material)
0x8 0
0xc offset of a third 2,048-word table, one word per instance; small counts (0, 57, 25, 1, ...), plausibly how many objects use the material; not needed

The third table ends the file. A class or instance record is 0x30 bytes:

Offset Field
0x00 offset of a type name: "Standard", "StaticShadow", "Old", "Sprites", "WaterSurface", "ScatterMaterial"
0x04 instance: class index (into the class table)
0x0c instance: flags (0, 0x10, 0x16, 0x29, 0x2f, 0x34, 0x36, 0x40, 0x6c, 0x77, ...); not interpreted
0x10 instance: object type, the OTYP code: 8 mesh (PRIMTYPE_OBJECT), 2 sprites (PRIMTYPE_SPRITES_ARRAY)
0x14 instance: sub type, the STYP code: 0 standard, 1 tweened, 2 rigid, 3 weighted; for sprites the SPRITETYPE (1 array, 3 box, 4 box scale, 5 UV, 6 raw, 7 particle)
0x18 Surface material description for footsteps and collision. Zero selects description 1.
0x1c offset of the record's root entry: a CLAS list for a class, an INST list for an instance
0x20 2

The STYP and OTYP strings map to these codes in the PC executable at 0x00493620 ("Sprites" → 2, "Mesh" → 8; "Standard" 0, "Tweened" 1, "Rigid" 2, "Weighted" 3, "Array" 1, "ArrayBox" 3, "ArrayBoxScale" 4, "ArrayUV" 5, "ArrayRaw" 6, "ArrayParticle" 7).

Entries

Everything else is a tree of 16-byte entries:

Offset Field
0x0 tag: a FourCC stored reversed, so the little-endian word equals the multi-character constant ('NAME' is the bytes EMAN)
0x4 a
0x8 b
0xc value type: 0 float (b = 1: the value is a; else b floats at offset a), 1 string (b bytes at offset a), 2 int (b = 1: the value is a; else b words at offset a), 3 list (b entries at offset a)

A class (CLAS) holds its NAME ("Glow", "Standard", "SkinCloth", "Glass", "Mirror", "Bad", "StaticShadow", "StaticShadowTextureShadow", "WaterSurface", "ScatterMaterial", "Sprites", "GlassSurface", "Anisotropic", "StaticShadowTextureAdditive") and one SUBC per object kind, each with NAME, OTYP, STYP and its LAYE shader layers (NAME, TYPE "FX", PATH the effect file such as "Standard" or "StaticShadow.fx", IDEN the technique, VALI). An instance (INST) holds NAME, the material's name in the PC asset tree ("All_Level/GasFlask_Al_01", "_Glacier/Collision", "Characters/M13/WheelchairGuy_01_Head"), and BIND, the list of its bindings. Each binding is a list whose first entries are NAME and ENAB:

Binding Fields Meaning
RSTA "RenderState" BENA blend enable; BMOD blend mode string; OPAC opacity (float); ATST alpha test; AREF alpha reference 0..255; FENA fog; CULL "DontCare", "OneSided" or "TwoSided"; ZBIA z bias 0..7; ZOFF z offset (float) the PC executable reads them at 0x00471b90
TEXT TXID texture id (bit 31 marks an indirect reference), TILU, TILV, TILW ("TILED" wraps, "MIRRORED" mirrors, other values clamp); FMIN, FMAG, FMIP filters when present mapDiffuse, mapNormal, mapSpecularMask, mapIllumination, mapEnvironment, mapReflectionMask, mapReflectionFallOff, mapTranslucency
COLO VALU four floats v4DiffuseColor, v4SpecularColor, v4IlluminationColor, v4ReflectionColor, v4RimLightColor, v4SubSurfaceColor
BOOL VALU int BumpEnabled, SpecularEnabled, IlluminationEnabled, ReflectionEnabled, WobbleEnabled, ScrollEnabled, AlphaFadeEnabled, ParallaxEnabled, RimLightingEnabled, SubSurfaceEnabled, BumpDetailEnabled
FLTV VALU floats v4BumpScale, v4SpecularPower, vSpecularLevel, fWobble*, gm_vAlphaFadeDistScaleOffset, gm_vAlphaFadeAngleScaleOffset, fRimLight*, ...
SCRL SPED four floats (x, y used) texture scrolling speed; the binder (PC 0x00470000 region, tag 0x5343524c) hands it to the material's virtual +0x30, and the shaders add gm_vScrollPosition to the UVs
ENUM, SPRI, DMAP other binders the executable knows; not needed for the PS2 records

BMOD values: TRANS, TRANS_ON_OPAQUE, ADD, ADD_ON_OPAQUE, ADD_BEFORE_TRANS, TRANSADD_ON_OPAQUE, SHADOW, STATICSHADOW.

Texture ids are the PC .TEX ids (the slot in its 2,048-entry table; tex.md "PC version"). The converted TEX keeps them, so the prim converter writes them into lTextureId unchanged.

Layers and techniques

A class's LAYE entries name the render passes it takes part in. FUN_004975d0 (PC 0x4975d0) reads each layer's NAME and turns it into a pass index, then reads PATH (the effect file, ".fx" appended when missing) and IDEN, the technique identifier inside the effect (technique ... < string Identifier="..."; >):

Layer NAME Pass Layer NAME Pass
ZFill 0 PostFilterZPass 9
Ambient 1 PostFilterZPassAlpha 10
ShadowMap 2 PostFilterBloom 11
ShadowDrop 3 TextureProjection 12
ShadowAdd 4 RefractionMask 13
Lighting 5 FurFins 14
Refraction 6 FurShells 15
Reflection 7 TextureProjectionAdditive 16
Illuminate 8 ShadowSub 17

Passes 12 and 16 are the projected static shadows on characters: the PC draws a character again with layer 12 (TextureProjection, or TextureProjectionMPS for skinned meshes, in Standard20.fx) once per projection its draw entry holds, into its shadow buffer, and a projection whose texture word has bit 29 with layer 16 (TextureProjectionAdditive); pc-data.md: Static shadows on characters has the passes and the record they read. The StaticShadow, StaticShadowTextureShadow and StaticShadowTextureAdditive classes are the scenery's own shadow meshes (techniques ShadowDrop, ShadowAdd and Ambient, with vStaticShadowColor and m44StaticShadowProjectionMatrix; Static shadow materials), not these.

The effects ship in the scene archives next to the .MAT (Common.fx, StandardCommon.fx, Standard00/13/20/30.fx, WaterSurface.fx, ScatterBillboard.fx, Glow*.fx, Glass.fx, Mirror.fx, Skin*.fx, Cloth*.fx, StaticShadow*.fx, Sprite.fx, Bad.fx, Fur*.fx, Anisotropic*.fx, MesoStructure30.fx; the 00, 13, 20 and 30 suffixes are the shader models the techniques target). A technique variant has Rigid (one bone) or MPS (skinned) in its identifier (AmbientRigid, LightingMPS). Each BOOL binding switches a preprocessor symbol of the same name in capitals (BUMPENABLED, SPECULARENABLED, REFLECTION2DENABLED, SCROLLENABLED, ...), and the other bindings set the effect parameter of their NAME.

Static shadow materials

Materials of the type StaticShadow belong to the scenery's static shadow objects (prm.md "PC version": 0x80-byte objects with a projection matrix at words 0x38 to 0x74 and the colour at 0x78). Three classes share the type; the converter tells them apart by the class name (ClassFamily, pcmaterials.cpp):

Class Layers (NAME → PATH IDEN) Instances (M01, M12)
StaticShadow ShadowSub → StaticShadow.fx ShadowDrop; a second layer → StaticShadow.fx ShadowAdd _Glacier/StaticShadows/Subtractive (BMOD STATICSHADOW, TXID 0x80), _Glacier/StaticShadows/Additive (BMOD ADD)
StaticShadowTextureShadow → StaticShadowTextureShadow.fx Ambient Locations/M01/PlantShadow_M01_01a (STATICSHADOW, CULL TwoSided, the projected texture in mapDiffuse), M12's Effects/M12/WindowAdditive (STATICSHADOW despite its name)
StaticShadowTextureAdditive → StaticShadowTextureAdditive.fx Ambient M12's Effects/M12/Window_Additive (BMOD ADD)

The blend mode decides what a mesh does: STATICSHADOW darkens by a × (1 − vStaticShadowColor) (times the projected texture) through the shadow buffer, the ADD modes add colour × a (times the texture) to the picture. Every instance seen has ZBIA 7 and FENA 1, and the effects fog towards black, so the shadows fade with distance. pc-data.md: Scenery shadows has the formulas, the per-scene counts and what the port draws; build/m01/matdump.py FILE.MAT REGEX in the scenefx worktree prints an instance's bindings.

Standard materials

The Standard class (Standard*.fx, with Common.fx) is the PC's lit, normal-mapped surface. Its bindings, as the effects read them:

Binding Use
mapDiffuse, v4DiffuseColor the colour texture and its tint
BumpEnabled, mapNormal, v4BumpScale CalculateBumpNormalPS: the tangent-space normal tosigned(tex.agb) × v4BumpScale (x from the alpha, y from the green; v4BumpScale is (s, s, 0, 0), so z is rebuilt as √(1 − x² − y²) after the scale: below 1 flattens the bumps)
BumpDetailEnabled, mapNormalDetail, v4BumpDetailScale, vBumpDetailTileFactor a second normal map at uv × vBumpDetailTileFactor, added before z is rebuilt
ParallaxEnabled, mapParallax, vParallaxScale, vParallaxBias parallax offset eye.xy × (height × scale − bias)
SpecularEnabled, mapSpecularMask, v4SpecularColor, v4SpecularPower, vSpecularLevel the "Lighting" pass's specular: (N·H)^power.x × level × colour × mask.rgb
IlluminationEnabled, mapIllumination, v4IlluminationColor the "Illuminate" pass: self-illumination, vertex colour × v4IlluminationColor
ReflectionEnabled, mapEnvironment (a cube map), mapReflectionMask, mapReflectionFallOff, v4ReflectionColor, vReflectionLevel the "Reflection" pass: the cube map in the bump-perturbed reflection direction, times the mask, a falloff texture looked up at 1 − eye.z (tangent space) and the level
WobbleEnabled, fWobbleWaveLength, fWobbleUSpeed, fWobbleVSpeed, fWobblePosAmplitude, fWobbleNorAmplitude a vertex wave along the normal, cos(length × (time − u × uspeed − v × vspeed))
ScrollEnabled, SCRL SPED UV scrolling
AlphaFadeEnabled, gm_vAlphaFadeDistScaleOffset, gm_vAlphaFadeAngleScaleOffset distance and angle fades (Glow; below)
RimLightingEnabled, v4RimLightColor, fRimLightPower, fRimLightStrength, SubSurfaceEnabled, v4SubSurfaceColor, fSubSurfaceTransparency, mapTranslucency the Skin and Cloth effects' rim light and translucency

What the port does with them: the diffuse map, colour, render state and clamping become the strips record's fields (below), and an opaque Standard material with BumpEnabled and a normal map becomes a dot3 record (SPrimStripsDot3PS2, prm.md "PC version"): mapNormal is lNormalMapId, mapSpecularMask (with SpecularEnabled) lSpecularMapId, and v4SpecularColor × vSpecularLevel × 128 per channel lSpecularColor (the PS2's 0x80 is 1.0). With pc_shading (on by default in PC-data mode) the record also carries every term below in an SPcMaterialExtension, and the port draws it with the PC's formulas (the other classes and the blends the PS2 lacks too, Other classes) (pc-data.md, "PC material shading"); with pc_shading=0 the PS2 dot3 passes draw it, which take the normal map's RGB as it is with a fixed (N·H)^16 specular and no reflection, illumination, parallax or rim light.

The shader reference describes the vertex streams and lighting equations for these techniques.

Other classes

The Glow, Glass, GlassSurface, Mirror and Anisotropic classes (Glow*.fx, Glass.fx, GlassSurface.fx, Mirror.fx, Anisotropic*.fx; all have the instance type "Standard", and PcParseMaterials tells them apart by the class name). Their layers and what each draws (pc-data.md, "Other material classes", has the counts and what the port does):

Class Layers (NAME → PATH IDEN) Colour
Glow Ambient → Glow Ambient (AmbientRigid, AmbientMPS); Reflection → Glow Reflection (with ReflectionEnabled); PostFilterZPass Ambient: vertex colour × v4DiffuseColor × tex, unlit and not doubled; alpha vertex alpha × OPAC × CalculateAlphaFade (1 unless blended). Reflection (One, One): mask × cube × vertex colour × fade × fall-off, without v4ReflectionColor or a level
Glass Refraction → Glass.fx Ambient; Reflection → Standard Reflection; RefractionMask → Glass.fx RefractionMask; rigid: Lighting, Illuminate saturate(L·N) × light × v4DiffuseColor × tex × 2 (no ambient) over refracted frame × gm_vRefraction2DColor × gm_mapRefraction2DMask by tex.a, plus the specular × 2; opaque. Skinned (AmbientMPS): the bone lights and ambient instead of the stream
GlassSurface ZFill; Reflection → GlassSurface.fx Ambient; RefractionMask (N·H)^power × level × v4SpecularColor × mask × 2 plus the refracted frame, or with Reflection2DEnabled lerp(refraction, mapReflection2D × colour × mask, Fresnel) (no shipped GlassSurface enables it); opaque
Mirror ZFill; PostFilterZPass; Reflection → Mirror.fx Reflection; ShadowMap, ShadowDrop lit = (ambient + saturate(L·N) × light) × v4DiffuseColor × 2; MirrorEnabled and MirrorMaskEnabled: lerp(lerp(tex × lit, reflection, mask.a), mapMirrorColor × lit, colour.a); MirrorEnabled: lerp(reflection, tex × lit, tex.a); MirrorMaskEnabled: lerp(mapMirrorColor × lit, reflection, mask.a) with alpha colour.a; plus the specular × 2. reflection is mapReflection2D at the projected position plus bump.xy × gm_vMirrorBumpScale
Anisotropic Ambient → Anisotropic Ambient; ShadowMap, ShadowDrop; Lighting → Anisotropic Lighting; Reflection, Illuminate → Standard (ambient × tex + anisoMap(N·L, N·H).rgb × light × tex + anisoMap.a × light × v4AnisoColor) × 2, with v4DiffuseColor on the ambient and light

Their bindings beyond the Standard ones: Glow AlphaFadeEnabled with gm_vAlphaFadeDistScaleOffset (scale 1, scale 2, offset 1, offset 2) and gm_vAlphaFadeAngleScaleOffset (scale, offset, alpha 1, alpha 2): CalculateAlphaFade multiplies saturate(distance × scale + offset) for both ramps by lerp(alpha 2, alpha 1, saturate(cos × scale + offset)), the cosine between the eye-to-vertex ray and the normal, per vertex. Glass and GlassSurface: Refraction2DEnabled, gm_vRefraction2DScale, gm_vRefraction2DColor, gm_mapRefraction2DMask, Reflection2DEnabled, gm_vReflection2DScale, gm_vReflection2DColor, gm_mapReflection2DMask, gm_vFresnelMin, gm_vFresnelMax, gm_vFresnelPow. Mirror: MirrorEnabled, MirrorMaskEnabled, mapMirrorMask, mapMirrorColor, gm_vMirrorBumpScale. Anisotropic: anisoMap (clamped), v4AnisoColor.

The PC renders mapReflection2D once per camera per frame before the main scene (FUN_004b8a50, from FUN_004b8f80): it clears the target and its depth with the camera's background colour (FUN_004b1bb0 with ZCAMERA::BackCol, +0x54), writes stencil i over each reflector's visible pixels with its ZFill pass, clears depth, and for each draws the reflected view with stencil i, only the Ambient and Illuminate passes of the sub-prims whose destination has bit 0x08 or 0x10, with the mirror plane as an oblique near plane 0.1 units back (FUN_004b2650). The plane is the bound's shortest local axis through its centre (FUN_004b31d0); a reflector needs bit 0x10 (HAS_REFLECTION2D) in its object header's property flags (FUN_004b30c0), and bit 0x40 makes it two-sided. mapRefraction2D is copied after the opaque Ambient, shadow and light passes when render list 3 is not empty: alpha cleared, list 3's RefractionMask passes write alpha 1, then a StretchRect; the Refraction (6), Reflection (7) and Illuminate (8) passes follow. (ZRenderEntryReflectorD3D at 0x4b7710, a per-mirror variant, has no caller.)

Blend factors

ZRenderMaterialD3DFX (PC 0x494820) turns BMOD into the effect's BlendSrc, BlendDst and z write:

BMOD Source Destination Z write
TRANS SrcAlpha InvSrcAlpha yes
TRANS_ON_OPAQUE SrcAlpha InvSrcAlpha no
TRANSADD_ON_OPAQUE One InvSrcAlpha no
ADD, ADD_BEFORE_TRANS, ADD_ON_OPAQUE SrcAlpha One no
SHADOW Zero InvSrcColor no
STATICSHADOW SrcAlpha InvSrcAlpha no

The PS2 blend (A − B) × C + D has no One, InvSrcAlpha or Zero, InvSrcColor; the port draws those two with blend factors of their own when pc_shading gives the record the PC terms. Culling is 2 (D3DCULL_CW, back faces) unless CULL is "TwoSided" (1, none): "DontCare" culls as "OneSided" does.

Water surface materials

The WaterSurface class (25 instances, 40 water patches) draws with WaterSurface.fx, technique AmbientHQ_VS20_PS20 ("Ambient"); its "RefractionMask" technique writes alpha 1 on the water's pixels of the refraction target. The vertex shader moves the grid by the patch's sine waves (v4WaterPatchSineWaves, the PS2 SSineWave P, Q, amplitude and phase per wave) and the ripples (v4Dynamics.w), and builds the tangent space from tangent (1, 0, 0), binormal (0, 0, 1) and the wave normal (−Σ A p cos, 1, −Σ A q cos); the PC builds that grid in ZRenderObjectWaterPatchD3D (PC 0x4a1360) with UVs from the object's UV rectangle (prm.md "Water patch"). Bindings:

Binding Values Use in AmbientHQ_PS20
mapDiffuse, v4DiffuseColor a texture in 9 of 25 (M01, M04, the M10 fountain, M11, M12) not sampled; the diffuse colour only reaches the vertex colour, which the pixel shader ignores
BumpEnabled, mapNormal, v4BumpScale always on; scale 0.125 to 1.871 four samples of mapNormal at the UV scrolled four ways (pc-data.md "Water"), averaged, times the scale, z rebuilt
BumpDetailEnabled, mapNormalDetail, ParallaxEnabled off not used
SpecularEnabled, v4SpecularColor, v4SpecularPower, vSpecularLevel on; power 40 to 100, level 0.1 to 2.4 colour × (half · bump)^power × level, added to the reflection
Reflection2DEnabled, gm_vReflection2DScale, gm_vReflection2DColor on in 15 of 25; scale 0.04 to 0.4 the planar reflection target sampled at the screen position plus bump.xy × scale, times the colour
Refraction2DEnabled, gm_vRefraction2DScale, gm_vRefraction2DColor on in 23 of 25; scale 0.06 to 0.95 the frame behind at the screen position minus bump.xy × scale (or undistorted where that sample's mask alpha is not above 0.9), times the colour
gm_vFresnelMin, gm_vFresnelMax, gm_vFresnelPow 0 to 0.3, 0.5 to 1, 2 to 10 CalculateFresnelTerm: lerp(min, max, (1 − |bump · eye|)^pow)
ScrollEnabled, SCRL SPED on in 22 of 25; −0.008 to 0.1 gm_vScrollPosition, the normal-map scroll
gm_mapReflection2DMask, gm_mapRefraction2DMask in a few M02 instances not in WaterSurface.fx's pixel shader

The result is lerp(refraction, reflection, Fresnel) with both passes, reflection × Fresnel with the reflection only, and the refraction alone otherwise, written opaque with depth. PcParseMaterials keeps every value the shader uses (SPcMaterial: bReflection2D, bRefraction2D, the scales, colours and Fresnel range, fFresnelPow, vBumpScale, fSpecularPower, fSpecularLevel, bScrollSpeed/vScrollSpeed), and the prim converter appends them to the water record (SPcWaterExtension, pcwater.h). The port's waterpc program (port/shaders/water.hlsl) is this pixel shader, with the eye and half vectors computed per vertex as the vs_1_1 AmbientHQ_VS20 computes them; the reflection target is the PS2 engine's mirror-group render and the scroll position SPED × FrameTime, wrapped as the PS2 texture scroll (ZTextureBase::GetOffsetX). The PC's own scroll time base was not traced.

Scatter materials

The ScatterMaterial class (20 instances) draws scatter fields with ScatterBillboard.fx ("Ambient"; "PostFilterZPass" for the depth-of-field pass). Each field vertex becomes a camera-facing quad: offset.x × ScatterWidth × scale and offset.y × (ScatterHeight + random × ScatterRandomHeight / 255) × scale in view space, scale the vertex colour's alpha, the texture coordinates from v4ScatterBillboard[type × 4 + corner], mirrored by a flip bit; the colour is the texture times the vertex colour. Bindings:

Binding Values PS2 field
mapDiffuse the billboard texture strip lTextureId
RenderState TRANS, alpha test with AREF 76 to 252, fog lAlphaRef (the same number)
ScatterWidth, ScatterHeight, ScatterRandomHeight (FLTV, x used) 4 to 2,000 fWidth, fHeight, fRandomHeight (the PS2 M02 fields carry M02's 500, 1,000, 300)

M08's backdrop trees (Scatter_Trees) have OPAC 0.12, which the PS2 record has no field for.

What the prim converter takes from a material

PcParseMaterials flattens the instance into SPcMaterial. See pcmaterials.h for its texture slots, sampler modes, colours, shader flags and render state.

The alpha-fade parameters of the Glow class (gm_vAlphaFadeDistScaleOffset, gm_vAlphaFadeAngleScaleOffset) carry the same numbers as the PS2's angle-blend headers (SPrimHeaderStripDestinationAngleBlend): for the hideout's FOG_01, distance scale 1/700 and offset −800/700 are the PS2's m_fNear1 800 and m_fNear2 1500, and the angle pair (−1.5198, 1.0) fades between 70° and 0° (1 − cos 70° = 1/1.5198), with the third and fourth values the PS2's opacities 100 and 90 (as fractions). The converter does not build angle-blend headers (the port does not draw the PS2's angle blend either); with pc_shading the record carries both vectors and the port computes CalculateAlphaFade itself (Other classes).

The WaterSurface class (40 water patches; prm.md "Water patch") also binds mapNormal, specular colour and power, gm_vFresnelPow and scrolling, which the PS2 water patch has no field for; the converter appends them to the patch (above). Its reflection and refraction scales, colours and Fresnel range are the PS2 SPrimWaterPatch's m_fReflectScale, m_fRefractScale, m_pColor0, m_pColor1, m_fFresnelMin and m_fFresnelMax: the PS2 copies of M00's sea in M09 carry exactly M00's numbers (0.4, 0.7, 249 = 0.976 × 255, 0 and 1).