; --
; Material library for car paints shaders, compatible with “ksPerPixelMultiMap_damage_dirt”.
; Intended for solid color car paints, compatible with Kunos content, doesn’t require new textures.
; In first half of this file, you can see available templates and parameters for them.
; --

; Global parameters, defined in [INCLUDE] section:
[DEFAULTS]
CarPaintMaterial = Carpaint
CarPaintVersionAware = 1 ; set to 4 to switch defaults to newer ones, or set to 9999 
  ; to always use newest features (warning: if doing so, use default configurations, 
  ; do not override reflectivity, speculars and all that, otherwise upcoming updates
  ; can break the way it looks)

; Simple solid car paint
[TEMPLATE: Material_CarPaint_Solid EXTENDS Material_CarPaint]
FresnelMax = 0.5
FresnelC = 0.08
AmbientSpecular = 0.3
SpecularBase = 0.2, 100

; Metallic car paint
[TEMPLATE: Material_CarPaint_Metallic EXTENDS Material_CarPaint]
FresnelMax = 1
FresnelC = 0.1
BrightnessAdjustment = 0.9 ; compensates for ambient specular
ColoredSpecular = 0.9
AmbientSpecular = 0.6
AmbientSpecularEXP = 2.5
ClearCoatThickness = 0.06

; Chrome car paint
[TEMPLATE: Material_CarPaint_Chrome EXTENDS Material_CarPaint]
FresnelMax = 1
FresnelC = 0.4
BrightnessAdjustment = 0.5
ColoredSpecular = 0.9
AmbientSpecular = 0.0
AmbientSpecularEXP = 2.5
ClearCoatThickness = 0.0

; Pearlescent car paint, with specular which might be of a different tint
[TEMPLATE: Material_CarPaint_Pearl EXTENDS Material_CarPaint]
FresnelMax = 1
FresnelC = 0.1
BrightnessAdjustment = 0.9 ; compensates for ambient specular
PearlescentSpecular = 1.0
AmbientSpecular = 1.0
AmbientSpecularEXP = 3.0
ClearCoatThickness = 0.09
SpecularBase = 1, 50

; Matte car paint
[TEMPLATE: Material_CarPaint_Matte EXTENDS Material_CarPaint]
FresnelMax = 1
FresnelC = 0.1
AmbientSpecular = 1.0
AmbientSpecularEXP = 4.0
SpecularBase = 0.3, 5
SpecularSun = 0, 1

; Crappy, bumpy and old car paint
[TEMPLATE: Material_CarPaint_Old EXTENDS Material_CarPaint]
NormalsNoise = 0.01
NormalsNoiseUV = 80
FresnelMax = 0.4
FresnelC = 0.04
AmbientSpecular = 1
SpecularBase = 0.4, 40
SpecularSun = 0.5, 1000

; Chameleon car paint, changing color depending on direction of view
[TEMPLATE: Material_CarPaint_Chameleon EXTENDS Material_CarPaint_Metallic]
ChameleonColorA = #33007f, 0.5  ; first alternative color and opacity
ChameleonColorB = #ffff00, 1    ; second alternative color and opacity
ChameleonExponent = 1.2         ; how colors are scaling; increase to see more of second color
ChameleonCenter = 0.45          ; how colors are scaling; increase to see more of first color

; Localized effects: change value to 1 to apply effects only where txDiffuse alpha is black (same as txDetail), set to INVERT to apply only where txDiffuse is white
UseDiffuseAlphaAsChameleonMask = 0  ; for chameleon color

; Specialized template for highly metallic car paint. Could be gold one, for example.
[TEMPLATE: Material_CarPaint_Metal EXTENDS Material_CarPaint_Metallic]
UseMetallicReflections = 1
FresnelMax = 1
FresnelC = 1
FlakesK = 1
ClearCoatThickness = 0.2
ColoredSpecular = 1.0
AmbientSpecular = 0.5
BrightnessAdjustment = 0.2

; Template for ChromaFlair car paint. Works like a more advanced version of chameleon car paint. Uses gradient texture for changing color depending on angle. X coordinate
; of said texture corresponds to viewing angle, Y coordinate is lighting angle. Here is a texture for Green/Purple 190 L ChromaPaint color:
; https://files.acstuff.ru/shared/V70m/tex.dds 
[TEMPLATE: Material_CarPaint_ChromaFlair EXTENDS Material_CarPaint]
CarPaintVersionAware = 4 ; Recently added car paint

; Localized effects: change value to 1 to apply effects only where txDiffuse alpha is black (same as txDetail), set to INVERT to apply only where txDiffuse is white
UseDiffuseAlphaAsChromaFlairMask = 0  ; for ChromaFlair

; How bright is specular/reflective component without flakes
ChromaFlairFlakesBase = 0.5

; Set gradient texture here:
GradientTexture = 

; Heavy on flakes, this shader really benefits from tiling fix:
ApplyTilingFix = 1

; Low specular exponent highlights the effect, but it shouldn’t blur reflections:
ClearCoatReflectionSharpness = 1

; Works especially good with bright colored specular:
; BaseSpecularColor = 0, 1, 0 
; ColoredSpecular = 1
; PearlescentSpecular = 0
; SpecularBase = 2, 20

; Generic extended car paint, basic version
; Supports tinted clear coat, colored specular and metallic reflections
[TEMPLATE: Material_CarPaint]

; Localized effects: change value to 1 to apply effects only where txDiffuse alpha is black (same as txDetail), set to INVERT to apply only where txDiffuse is white
UseDiffuseAlphaAsMask = 0           ; for custom specular color and metallic reflections
UseDiffuseAlphaAsClearCoatMask = 0  ; for clear coat, also affects sun specular

; Clear coat parameters
ClearCoatThickness = 0.0     ; thickness for absorbing light from underneath
ClearCoatTint = 0, 0, 0      ; tint for clear coat, replacing original color with a new one
ClearCoatIOR = 1.0           ; index of refraction for clear coat material, value bigger than 1 makes absorbing much less angle-dependant
ClearCoatIntensity = 1.0     ; how much clear coat contributes to final color
ClearCoatSpecular = 0        ; set to 1 to make clear coat specular white (as it should be), set to COLORED to make clear coat use tint color for specular
ClearCoatSpecular = $" $CarPaintVersionAware >= 2 and 1 or discard() "  ; enabled automatically with second version
ColoredBlackSpecular = 0     ; set to 1 to make colored specular active on colorless surfaces (greatly improves look of multi-colored skins)
ColoredBlackSpecular = $" $CarPaintVersionAware >= 4 and 1 or discard() "  ; enabled automatically with fourth version

; Reflections parameters
FresnelMax = 0.8             ; how reflective is the car at glancing angles
FresnelC = 0.16              ; how much of that reflection is there on surface facing viewer
UseMetallicReflections = 0.0 ; metallic reflections force them to be a specific tint, like gold does

; Extra parameters to tweak look of a car without changing textures
FlakesK = 0.1                ; flakes behavior
BrightnessAdjustment = 1.0   ; adjust car brightness if AO map was made for different settings
SpecularMult = 1.0           ; adjust specular brightness, just in case
ApplyTilingFix = 0           ; apply tiling fix to reduce tiling of txDetail
ApplyMatteFix = 0            ; apply matte fix: matte done with txMaps would no longer darken diffuse, txMaps would also affect clear coat
ApplyMatteFix = $" $CarPaintVersionAware >= 3 and 1 or discard() "  ; enabled automatically with third version
NormalsMode = DAMAGE         ; how txNormals is treated: DAMAGE for using them as damage, BASIC for applying them always
; SecondaryReflection = 0    ; secondary reflection intensity, for that double layered look

; Reflections and speculars
BaseSpecularColor = 1, 1, 1  ; specular color
ColoredSpecular = 0.0        ; use base color as specular color
PearlescentSpecular = 0.0    ; offset specular color from base color in a way similar to pearl car paints
AmbientSpecular = 0.0        ; ambient specular by Stereo
AmbientSpecularEXP = 2.5     ; concentration of specular ambient

; Option to apply txDiffuse as smoothness map
DiffuseSmoothness = 0, 0     ; min and max brightness values for txDiffuse

; ForceCarPaintMode = 0      ; force car paint mode, defaults to true for version >= 2
NormalizeAO = 0.0            ; normalize AO from txDiffuse to reduce its effect on lights and better combine it with vertex AO

; Set to 1 to use sun specular exponent for reflections sharpness, making clear coat act more correctly
ClearCoatReflectionSharpness = 0

; --
; Couple of experimental/broken car paints. Please avoid changing any values, as underlying
; shader most likely will change in the future.
; --

; Online, they call this one Gloss Flip Psychedelic Vinyl Wrap
; Here is the reference image: https://i.imgur.com/yOUQVjW.png
; Notice that it’s based on chameleon template: you can add chameleon effect to it as well.
[TEMPLATE: Material_CarPaint_RainbowChrome EXTENDS Material_CarPaint_Chameleon]
ChromeRainbowFlip = 0    ; flip rainbow
ChromeRainbowNegate = 1  ; negate rainbow
ChromeRainbowFocus = 1   ; compactness of rainbow spread
ChromeRainbowMult = 1    ; multiplier for final effect

; Disabling chameleon effect by default
ChameleonColorA = 0, 0
ChameleonColorB = 0, 0
ChameleonExponent = 1
ChameleonCenter = 0.5

; Localized effects: change value to 1 to apply effects only where txDiffuse alpha is black (same as txDetail), set to INVERT to apply only where txDiffuse is white
UseDiffuseAlphaAsRainbowMask = 0  ; for rainbow effect

; As experimental as it gets, messed up Ward anisotropic model producing rainbow speculars.
; Here is the reference image: https://i.imgur.com/dI32nOK.png
; Notice that it’s based on chameleon template: you can add chameleon effect to it as well.
[TEMPLATE: Material_CarPaint_Rainbow EXTENDS Material_CarPaint_Chameleon]
ChromeRainbowFlip = 0       ; flip rainbow
ChromeRainbowNegate = 0     ; negate rainbow
RainbowIntensity = 0.7      ; intensity of rainbow effect
AnisotropicIntensity = 0.0  ; intensity of plain white anisotropic specular
AnisotropicAmount = 0.9     ; anisotropy level of material
Gloss = 0.7                 ; gloss value for anisotropy calculation
BrightnessAdjustment = 0.8  ; changing Material_CarPaint default value: car paint like this should be darker, in general

; disabling chameleon effect by default
ChameleonColorA = 0, 0
ChameleonColorB = 0, 0
ChameleonExponent = 1
ChameleonCenter = 0.5

; Localized effects: change value to 1 to apply effects only where txDiffuse alpha is black (same as txDetail), set to INVERT to apply only where txDiffuse is white
UseDiffuseAlphaAsRainbowMask = 0  ; for rainbow effect

; --
; Here goes second half, actual implementation of those templates.
; If you want to go futher, here is some documentation for the whole format:
; https://github.com/ac-custom-shaders-patch/inipp
; --

[INCLUDE: materials_base.ini]
[SHADER_REPLACEMENT_0CARPAINT: _DeactivateDevMaterial]

[MIXIN: _Base_Material_CarPaint_Skins]
@ACTIVE = ${Skins:count}
SKINS = $Skins

[MIXIN: _Base_Material_CarPaint_ShadowsFix]
@ACTIVE = $" def($FixShadows, $CarPaintVersionAware >= 2) and 1 or 0 "
DOUBLE_FACE_SHADOW_BIASED = 1
PROP_0_SHADOWBIASMULT = shadowBiasMult, 0.5

[TEMPLATE: _Base_Material_CarPaint]
@OUTPUT = SHADER_REPLACEMENT_0_CARPAINT_...
TAGS = NEW_CARPAINT
MATERIALS = $" def($Materials, $CarPaintMaterial) "
@MIXIN = _Base_Material_CarPaint_Skins
@MIXIN = _Base_Material_CarPaint_ShadowsFix

[TEMPLATE: Material_CarPaint EXTENDS _Base_Material_CarPaint]
SHADER = smCarPaint

; Fallback for compatibility
FresnelMax = ${Reflectiveness:?}
FresnelC = $" ${Reflectiveness:?} * $FresnelF0 "

; Please avoid changing those values, those were calculated to look right for different car paints
SpecularBase = 0.5, 50
SpecularSun = 2, 5000
SunMultiplier = 6
FresnelEXP = 5  ; according to PBR, it should be 5

BaseSpecularColor = 1, 1, 1  ; specular color
ColoredSpecular = 0.0        ; use base color as specular color
PearlescentSpecular = 0.0    ; offset specular color from base color in a way similar to pearl car paints
AmbientSpecular = 0.0        ; ambient specular by Stereo
AmbientSpecularEXP = 2.5     ; concentration of specular ambient

SecondaryReflection = 0
SecondaryReflection = $" $CarPaintVersionAware >= 3 and ${SpecularBase:x} > 0.1 and ${SpecularBase:y} < 200 and ${SpecularSun:x} > 1 and ${SpecularSun:y} > 500 and 1 or discard() "

; Those weird property names allow, if needed, to override some values later on
; without worrying that numbers would change
PROP_0_KSDIFFUSE = ksDiffuse, $" 0.5 * $BrightnessAdjustment "
PROP_0_KSAMBIENT = ksAmbient, $" 0.45 * $BrightnessAdjustment "

PROP_0_KSSPECULAR = ksSpecular, $" ${SpecularBase:1} * $SpecularMult "
PROP_0_KSSPECULAREXP = ksSpecularEXP, ${SpecularBase:2}
PROP_0_SUNSPECULAR = sunSpecular, $" ${SpecularSun:1} * $SpecularMult * (1 + $SunMultiplier * $SunMultiplier) "
PROP_0_SUNSPECULAREXP = sunSpecularEXP, $" ${SpecularSun:2} * (1 + $SunMultiplier) "

PROP_0_FRESNELMAXLEVEL = fresnelMaxLevel, $FresnelMax
PROP_0_FRESNELC = fresnelC, $FresnelC
PROP_0_FRESNELEXP = fresnelEXP, $FresnelEXP
PROP_0_ISADDITIVE = isAdditive, $" def($ForceCarPaintMode, ($CarPaintVersionAware >= 2) or 0) ~= 0 and 2 or discard() "
PROP_0_EXTNORMALIZEAO = extNormalizeAO, $NormalizeAO

PROP_0_EXTFLAKESK = extFlakesK, $" '${SpecularBase:1}' == '6.2' and '${FlakesK}' == '6.6' and 0.01 or $FlakesK "

PROP_0_EXTSPECCOLOR = extSpecColor, $" $BaseSpecularColor == nil and 1 or ParseColor( $BaseSpecularColor ) "
PROP_0_EXTCOLOREDSPECULAR = extColoredSpecular, $" max( $ColoredSpecular, $PearlescentSpecular ) * ( ${ColoredBlackSpecular:bool} and -1 or 1 ) "
PROP_0_EXTPEARLSPECULAR = extPearlSpecular, $PearlescentSpecular
PROP_0_STAMBIENTSPEC = stAmbientSpec, $" $AmbientSpecular * $SpecularMult * 0.2 "
PROP_0_STAMBIENTEXP = stAmbientEXP, $AmbientSpecularEXP
PROP_0_EXTCARPAINTFLAGS = extCarPaintFlags, $" 
  (${ApplyTilingFix:bool} and 1 or 0)
  + (${ApplyMatteFix:bool} and 2 or 0)
  + (${NormalsMode:str} == 'BASIC' and 4 or 0)"
PROP_0_EXTSECONDARYREFLECTIONLAYER = extSecondaryReflectionLayer, $SecondaryReflection
PROP_0_EXTCOLOREDREFLECTIONS = extColoredReflections, $UseMetallicReflections

PROP_0_EXTCLEARCOATTINT = extClearCoatTint, $" ParseColor( $ClearCoatTint ) "
PROP_0_EXTCLEARCOATIOR = extClearCoatIOR, $ClearCoatIOR
PROP_0_EXTCLEARCOATINTENSITY = extClearCoatIntensity, $ClearCoatIntensity
PROP_0_EXTCLEARCOATTHICKNESS = extClearCoatThickness, $ClearCoatThickness
PROP_0_EXTCLEARCOARMASK = extClearCoarMask, $" ${UseDiffuseAlphaAsClearCoatMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsClearCoatMask:bool} and 1 or 0) "
PROP_0_EXTEFFECTSMASK = extEffectsMask, $" ${UseDiffuseAlphaAsMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsMask:bool} and 1 or 0) "
PROP_0_EXTEFFECTSCLEARCOATMASK = extClearCoatSpecular, $" ${ClearCoatSpecular:string} == 'COLORED' and 2 or (${ClearCoatSpecular:bool} and 1 or 0) "

PROP_0_EXTDIFFUSESMOOTHNESSFROM = extDiffuseSmoothnessFrom, ${DiffuseSmoothness:1:?}
PROP_0_EXTDIFFUSESMOOTHNESSTO = extDiffuseSmoothnessTo, ${DiffuseSmoothness:2:?}
PROP_0_EXTDIFFUSESMOOTHNESSEXP = extDiffuseSmoothnessEXP, $DiffuseSmoothnessEXP
PROP_0_EXTDIFFUSESMOOTHNESSBASE = extDiffuseSmoothnessBase, $DiffuseSmoothnessBase
PROP_0_EXTRASHARPLOCALREFLECTIONS = extExtraSharpLocalReflections, $" def( $LocalReflectionsSharpness, 0.001 ) * ( ${LocalReflectionsForce:bool} and -1 or 1 ) " 

MATERIAL_FLAG_0 = $ClearCoatReflectionSharpness

[TEMPLATE: Material_CarPaint_Old EXTENDS Material_CarPaint]
SHADER = smCarPaint_old
PROP_0_EXTNORMALSNOISE = extNormalsNoise, $NormalsNoise
PROP_0_EXTNORMALSNOISEUV = extNormalsNoiseUv, $NormalsNoiseUV

[TEMPLATE: Material_CarPaint_Chameleon EXTENDS Material_CarPaint]
SHADER = smCarPaint_chameleon
PROP_0_EXTCHAMELEON0COLOR = extChameleon0Color, $" ParseColor( ${ChameleonColorA:1::-1} ) " 
PROP_0_EXTCHAMELEON1COLOR = extChameleon1Color, $" ParseColor( ${ChameleonColorB:1::-1} ) "
PROP_0_EXTCHAMELEONEXP = extChameleonEXP, $ChameleonExponent
PROP_0_EXTCHAMELEONCENTER = extChameleonCenter, $ChameleonCenter
PROP_0_EXTCHAMELEON0ALPHA = extChameleon0Alpha, ${ChameleonColorA:-1}
PROP_0_EXTCHAMELEON1ALPHA = extChameleon1Alpha, ${ChameleonColorB:-1}
PROP_0_EXTCHAMELEONMASK = extChameleonMask, $" ${UseDiffuseAlphaAsChameleonMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsChameleonMask:bool} and 1 or 0) "

[TEMPLATE: Material_CarPaint_RainbowChrome EXTENDS Material_CarPaint_Chameleon]
SHADER = smCarPaint_rainbowChrome
PROP_0_EXTRAINBOWCHROMEK = extRainbowChromeK, $" 2 * $ChromeRainbowFocus * (${ChromeRainbowFlip:bool} and -1 or 1) "
PROP_0_EXTRAINBOWCHROMEMULT = extRainbowChromeMult, $" $ChromeRainbowMult * (${ChromeRainbowNegate:bool} and -1 or 1) "
PROP_0_EXTRAINBOWMASK = extRainbowMask, $" ${UseDiffuseAlphaAsRainbowMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsRainbowMask:bool} and 1 or 0) "

[TEMPLATE: Material_CarPaint_Rainbow EXTENDS Material_CarPaint_Chameleon]
SHADER = smCarPaint_rainbow
PROP_0_EXTRAINBOWMULT = extRainbowMult, $" $RainbowIntensity * (${ChromeRainbowFlip:bool} and -1 or 1) "
_glossInvSqr5 = $" pow(1 - $Gloss, 2) * 5 "
_anisAspect = $" sqrt(1 - $AnisotropicAmount * 0.9) "
PROP_0_EXTRAINBOWX = extRainbowX, $" $_glossInvSqr5 / $_anisAspect "
PROP_0_EXTRAINBOWY = extRainbowY, $" $_glossInvSqr5 * $_anisAspect "
PROP_0_EXTRAINBOWANISMULT = extRainbowAnisMult, $" max($AnisotropicIntensity, 0.0001) * (${ChromeRainbowNegate:bool} and -1 or 1) "
PROP_0_EXTRAINBOWMASK = extRainbowMask, $" ${UseDiffuseAlphaAsRainbowMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsRainbowMask:bool} and 1 or 0) "

[TEMPLATE: Material_CarPaint_ChromaFlair EXTENDS Material_CarPaint]
SHADER = smCarPaint_chromaFlair
PROP_0_EXTCHROMAFLAIRMASK = extChromaFlairMask, $" ${UseDiffuseAlphaAsChromaFlairMask:string} == 'INVERT' and 2 or (${UseDiffuseAlphaAsChromaFlairMask:bool} and 1 or 0) "
PROP_0_EXTCHROMAFLAIRFLAKESMULT = extChromaFlairFlakesMult, $ChromaFlairFlakesBase
RESOURCE_0 = txGradient
RESOURCE_FILE_0 = $GradientTexture
