[INCLUDE: materials_base.ini]

[FUNCTION: _PBR_EstimateF0]
; Schlick’s IOR-to-F0 approximation
ARGUMENTS = IOR
CODE = ' return math.pow( ( IOR - 1 ) / ( IOR + 1 ), 2 ) '

[FUNCTION: _PBR_EstimateF90]
; From Frostbite presentation:
; Moving Frostbite to Physically Based Rendering, page 78 here:
; https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
ARGUMENTS = IOR
CODE = ' return saturate( 50 * _PBR_EstimateF0(IOR) ) '

[DEFAULTS]
FilmIOR = $IOR

; Regular PBR glass shader
[TEMPLATE: Material_Glass EXTENDS _Base_Material_Custom]
; Main input parameters:
IOR = 1.5            ; index of refraction for glass, usualy, 1.5
FilmIOR = $IOR       ; redefine IOR for external film layer to increase reflections
ThicknessMult = 1.0  ; thicker glass passes less light through
ProfileFix = 0.04    ; adds thickness to glass for glazing angles

; Lighting parameters
BrightnessAdjustment =     ; set to alter brightness
ExtraLightsAdjustment = 0.3  ; intensity of extra lights

; Mask pass
MaskPass = 0             ; mask pass opacity
MaskPassColor = 0, 0, 0  ; mask pass color adjustment

; Specially adjusted parameters:
; If needed, you can override those, but it’s not gonna be accurate.
FresnelC = $" _PBR_EstimateF0( $FilmIOR ) "
FresnelMax = $" _PBR_EstimateF90( $FilmIOR ) "

; Actual replacement
@OUTPUT = SHADER_REPLACEMENT_0_GLASS_...
TAGS = NEW_GLASS
SHADER = smGlass
PROP_0_EXTIOR = extIOR, $IOR
PROP_0_EXTTHICKNESSMULT = extThicknessMult, $ThicknessMult
PROP_0_EXTTHICKNESSPROFILEFIX = extThicknessProfileFix, $ProfileFix
PROP_0_FRESNELC = fresnelC, $FresnelC
PROP_0_FRESNELMAXLEVEL = fresnelMaxLevel, $FresnelMax
PROP_0_KSDIFFUSE = ksDiffuse, ${BrightnessAdjustment:?}
PROP_0_KSAMBIENT = ksAmbient, $ExtraLightsAdjustment
PROP_0_EXTSATURATION = extSaturation, $" saturate(1 - def($Desaturate, 0)) "
PROP_0_EXTUSENORMALMAP = extUseNormalMap, $" $UseNormalMapWithAlpha and 2 or $UseNormalMap and 1 or 0 "
PROP_0_EXTREFRACTION = extRefraction, $" def($Refraction, 0) * 0.04 "
PROP_0_EXTREFRACTIONBIAS = extRefractionBias, $" def($RefractionBias, 0) * 4 "
PROP_0_EXTREFRACTIONNORMALFACTOR = extRefractionNormalFactor, $" def($RefractionNormalFactor, 40) "
PROP_0_EXTMASKPASS = extMaskPass, $MaskPass
PROP_0_EXTMASKPASSCOOLOR = extMaskPassColor, ${MaskPassColor:?}
PROP_0_EXTREFRACTIONRAINBOW = extRefractionRainbow, $" 1 - 0.1 * ${RefractionRainbow:?} "

EXTRA_MASK_PASS = $" $MaskPass > 0 and 1 or 0 "
EXTRA_MASK_PASS_OPACITY = $MaskPass
EXTRA_MASK_PASS_COLOR = ${MaskPassColor:?}

; Material for glass side, nothing but very blurry refraction
[TEMPLATE: Material_GlassSide EXTENDS Material_Glass]
ThicknessMult = 0
ExtraLightsAdjustment = 2
BrightnessAdjustment = 1
ThicknessMult = 0 ; pretty much fully transparent
Refraction = 1 ; full refraction
RefractionBias = 1 ; with full blur

; Glass shader with emissive map and other options for multi-channel emissive
[TEMPLATE: Material_MultiEmissiveGlass EXTENDS Material_Glass]
SHADER = smGlass_emissive

; Primitime attempt to recreate photoelasticity effect, nothing PBR about it
[TEMPLATE: Material_PhotoelasticGlass EXTENDS Material_Glass]
SHADER = smGlass_phel

FilmIOR = 3.2              ; looks better like this
PhotoelasticityMult = 0.5  ; doesn’t look good if too high
ProfileFix = 0.08          ; a bit thicker edge

; Ideally, there would be a texture for that, but we’re not going to draw
; all those textures for Kunos cars, so here are some parameters for perlin noise
; instead
PhotoelasticityBaseScale = 8      ; this parameter alters scale in general
PhotoelasticityNoiseScale = 0.04  ; deeply affects randomization process, careful with it
PhotoelasticitySpotsScale = 0.5   ; spots for limiting effect to some areas
PhotoelasticitySpotsOffset = 0.1  ; offset for spots, to try and fit effect nice on existing meshes
PhotoelasticityStripesScale = 4   ; how many stripes are there

PROP_0_EXTPHELMULT = extPhelMult, $PhotoelasticityMult
PROP_0_EXTPHELBASESCALE = extPhelBaseScale, $PhotoelasticityBaseScale
PROP_0_EXTPHELNOISESCALE = extPhelNoiseScale, $PhotoelasticityNoiseScale
PROP_0_EXTPHELSPOTSSCALE = extPhelSpotsScale, $PhotoelasticitySpotsScale
PROP_0_EXTPHELSPOTSOFFSET = extPhelSpotsOffset, $PhotoelasticitySpotsOffset
PROP_0_EXTPHELSTRIPESSCALE = extPhelStripesScale, $PhotoelasticityStripesScale

