; Very WIP at the moment

[INCLUDE: materials_base.ini]

[DEFAULTS]
MapTurningLightsProperly = 1
DashHighlightColor = 2, 2, 2
DashIndicatorLag = 0.0
OpenDoorLightLag = 0.8
HeadlightsLag = 0.0
ReverseLightsLag = 0.5
ParkingLightsLag = 0.5
BrakingLightsLag = 0.5
TurningLightsLag = 0.8
LicenseLightsLag = 0.5
HeatingMult = 1
DevMode = 1

DefaultBrakingColor = 40, 0, 0
DefaultReverseColor = 40, 40, 40
DefaultOpenDoorLightColor = 16, 15.6, 6

[MIXIN: AlphaFromTxDiffuse]
PROP_... = emAlphaFromDiffuse, 1

[MIXIN: DebugEmissiveChannels]
@ACTIVE = $DevMode
DebugVal = ${Debug:number}
PROP_... = emSkipDiffuseMap, $" $DebugVal > 0.5 or discard() "
PROP_... = ksEmissive, $DebugVal, 0, 0
PROP_... = ksEmissive1, 0, $DebugVal, 0
PROP_... = ksEmissive2, 0, 0, $DebugVal
PROP_... = ksEmissive3, $DebugVal, $DebugVal, 0
PROP_... = ksEmissive4, 0, $DebugVal, $DebugVal
PROP_... = ksEmissive5, $DebugVal, 0, $DebugVal
PROP_... = ksEmissive6, $DebugVal, $DebugVal, $DebugVal

[MIXIN: DebugEmissiveMultiChannels]
@ACTIVE = $DevMode
DebugVal = ${Debug:number}
PROP_0 = emSkipDiffuseMap, $" $DebugVal > 0.5 "
PROP_... = ksEmissive, $" $UseEmissive0AsFallback and $DebugVal / 10 or $DebugVal ", 0, 0
PROP_... = ksEmissive1, 0, $DebugVal, 0
PROP_... = ksEmissive2, 0, 0, $DebugVal
PROP_... = ksEmissive3, $DebugVal, $DebugVal, 0
PROP_... = ksEmissive4, 0, $DebugVal, $DebugVal
PROP_... = ksEmissive5, $DebugVal, 0, $DebugVal
PROP_... = ksEmissive6, $DebugVal, $DebugVal, $DebugVal
PROP_... = ksEmissive7, $DebugVal, 0, 0
PROP_... = ksEmissive8, 0, $DebugVal, 0
PROP_... = ksEmissive9, 0, 0, $DebugVal
PROP_... = ksEmissive10, $DebugVal, $DebugVal, 0
PROP_... = ksEmissive11, 0, $DebugVal, $DebugVal
PROP_... = ksEmissive12, $DebugVal, 0, $DebugVal
PROP_... = ksEmissive13, $DebugVal, $DebugVal, $DebugVal
PROP_... = ksEmissive14, $DebugVal, 0, 0
PROP_... = ksEmissive15, 0, $DebugVal, 0
PROP_... = ksEmissive16, 0, 0, $DebugVal
PROP_... = ksEmissive17, $DebugVal, $DebugVal, 0
PROP_... = ksEmissive18, 0, $DebugVal, $DebugVal
PROP_... = ksEmissive19, $DebugVal, 0, $DebugVal
PROP_... = ksEmissive20, $DebugVal, $DebugVal, $DebugVal
PROP_... = ksEmissive21, $DebugVal, 0, 0
PROP_... = ksEmissive22, 0, $DebugVal, 0
PROP_... = ksEmissive23, 0, 0, $DebugVal
PROP_... = ksEmissive24, $DebugVal, $DebugVal, 0

[MIXIN: _CustomEmissive_Reset]
@ACTIVE = $DevMode
PROP_0 = emGeoChannel0P, 0 ; reset
PROP_1 = emGeoChannel0S, 0 ; reset
PROP_2 = emGeoChannel1P, 0 ; reset
PROP_3 = emGeoChannel1S, 0 ; reset
PROP_4 = emGeoChannel2P, 0 ; reset
PROP_5 = emGeoChannel2S, 0 ; reset
PROP_6 = emGeoChannel3P, 0 ; reset
PROP_7 = emGeoChannel3S, 0 ; reset
PROP_8 = emMaskColor0A, 0 ; reset
PROP_9 = emMaskColor0C, 0 ; reset
PROP_10 = emMaskColor0P, 0 ; reset
PROP_11 = emMaskColor1A, 0 ; reset
PROP_12 = emMaskColor1C, 0 ; reset
PROP_13 = emMaskColor1P, 0 ; reset
PROP_14 = emMaskColor2A, 0 ; reset
PROP_15 = emMaskColor2C, 0 ; reset
PROP_16 = emMaskColor2P, 0 ; reset
PROP_17 = emMaskColor3A, 0 ; reset
PROP_18 = emMaskColor3C, 0 ; reset
PROP_19 = emMaskColor3P, 0 ; reset
PROP_20 = emPoly00, 0 ; reset
PROP_21 = emPoly01, 0 ; reset
PROP_22 = emPoly02, 0 ; reset
PROP_23 = emPoly10, 0 ; reset
PROP_24 = emPoly11, 0 ; reset
PROP_25 = emPoly12, 0 ; reset
PROP_26 = emPoly20, 0 ; reset
PROP_27 = emPoly21, 0 ; reset
PROP_28 = emPoly22, 0 ; reset
PROP_29 = emPoly30, 0 ; reset
PROP_30 = emPoly31, 0 ; reset
PROP_31 = emPoly32, 0 ; reset
PROP_32 = emSkipDiffuseMap, 0 ; reset
PROP_33 = ksEmissive, 0 ; reset
PROP_34 = ksEmissive1, 0 ; reset
PROP_35 = ksEmissive2, 0 ; reset
PROP_36 = ksEmissive3, 0 ; reset
PROP_37 = ksEmissive4, 0 ; reset
PROP_38 = ksEmissive5, 0 ; reset
PROP_39 = ksEmissive6, 0 ; reset
PROP_40 = emDiffuseAvgColorAsMultiplier3, 0 ; reset
PROP_41 = emDiffuseAlphaAsMultiplier3, 0 ; reset
PROP_42 = emMirrorUVOffset, 0 ; reset
PROP_43 = emMirrorUV, 0 ; reset

[MIXIN: _CustomEmissive_ShaderGrass]
SHADER = smGlass_emissive

[MIXIN: _CustomEmissive_ShaderMultiMap]
SHADER = ksPerPixelMultiMap_emissive
SHADER_OVERRIDE_0 = ?NMDetail?, ksPerPixelMultiMap_NMDetail_emissive

[TEMPLATE: CustomEmissive EXTENDS _Base_Material_Custom]
@OUTPUT = SHADER_REPLACEMENT_9_CUSTOMEMISSIVE_...
@MIXIN = $" $CompatibleWithPBRGlass and '_CustomEmissive_ShaderGrass' or '_CustomEmissive_ShaderMultiMap' "
FILL_MISSING_TEXTURES = 1
Debug = 0
UseAreas = 0
UseAreasAsMask = 0
UseColorMasks = 0
UsePolys = 0
UseVertexMask = 0
VertexMaskAdditive = 0
VertexMaskSubtractive = 0
MirrorDir = 1, 0, 0
MirrorOffset = 0
AreasSubtractNext = 0
ColorSubtractNext = 0
PolySubtractNext = 0
PROP_0_CHANNELSMODE = emChannelsMode, 1
PROP_0_PROCEDURALMODE = emProceduralMode, $" 
  (
    (not ${StartWithWhite:bool} and 1 or 0)
    + (${UseAreas:bool} and (2 + (def($AreasSubtractive, false) and 8 or 4)) or 0) 
    + (${UseColorMasks:bool} and (16 + (def($ColorMasksSubtractive, false) and 64 or not def($ColorMasksAsMultiplier, false) and 32 or 0)) or 0) 
    + (${UsePolys:bool} and (128 + (def($PolysSubtractive, false) and 512 or not def($PolysMasksAsMultiplier, false) and 256 or 0)) or 0)
  )
  + ( ${AreasSubtractNext:bool} and 1024 or 0 )
  + ( ${ColorSubtractNext:bool} and 2048 or 0 )
  + ( ${PolySubtractNext:bool} and 4096 or 0 )
  + ( ${UseVertexMask:bool} and 8192 or 0 )
  + ( ${VertexMaskAdditive:bool} and 16384 or 0 )
  + ( ${VertexMaskSubtractive:bool} and 32768 or 0 )
  + ( ${UseAreasAsMask:bool} and 65536 or 0 )
  + ( ${UseRawUV:bool} and 131072 or 0 ) "
PROP_0_MIRRORDIR = emMirrorDir, $" ${MirrorDir:vec3}:normalize() "
PROP_0_MIRROROFFSET = emMirrorOffset, $MirrorOffset
PROP_0_MASKCOLORINPUTBLUR = emMaskColorInputBlur, $" def($ColorInputBlur, 0) "
; PROP_0_EMMASKRESULTDIFFUSEBLUR = emMaskResultDiffuseBlur, $" def($ApplyBlurredDiffuse, 0) "
; PROP_0_EMMASKRESULTNORMALALPHABLUR = emMaskResultNormalAlphaBlur, $" def($ApplyBlurredNormalAlpha, 0) "
@MIXIN = _CustomEmissive_Reset
@MIXIN = DebugEmissiveChannels, Debug = $Debug

[TEMPLATE: CustomEmissiveMulti EXTENDS _Base_Material_Custom]
SHADER = ksPerPixelMultiMap_emissiveExtra
FILL_MISSING_TEXTURES = 1
Debug = 0
MirrorDir = 1, 0, 0
MirrorOffset = 0
PROP_0_MIRRORDIR = emMirrorDir, $" ${MirrorDir:vec3}:normalize() "
PROP_0_MIRROROFFSET = emMirrorOffset, $MirrorOffset
; PROP_0_EMMASKRESULTDIFFUSEBLUR = emMaskResultDiffuseBlur, $" def($ApplyBlurredDiffuse, 0) "
; PROP_0_EMMASKRESULTNORMALALPHABLUR = emMaskResultNormalAlphaBlur, $" def($ApplyBlurredNormalAlpha, 0) "
PROP_0_EMUSECHANNEL0ASFALLBACK = emUseChannel0AsFallback, $" ${UseEmissive0AsFallback:str} == 'COVER_ALL' and 2 or def($UseEmissive0AsFallback, 0) "
PROP_0_EMSKIPEMISSIVEMAP = emSkipEmissiveMap, $" def($SkipEmissiveMap, 1) "
@MIXIN = _CustomEmissiveMulti_Reset
@MIXIN = DebugEmissiveMultiChannels, Debug = $Debug

[MIXIN: CustomEmissive_MirrorUV]
PROP_... = emMirrorUVOffset, $" def($Offset, 0.5) / def2( ${Resolution:vec2}, 1, 1 ).x "
PROP_... = emMirrorUV, $" -def2($Direction, 1, 0):normalize() "

[MIXIN: CustomEmissive_SkipDiffuseMap]
PROP_... = emSkipDiffuseMap, $" def($SkipDiffuseMap, true) "

[MIXIN: CustomEmissive_BounceBack]
PROP_... = extBounceBackMask, $" ${Channel:exists} and { ($Channel == 0) and 1 or 0 , ($Channel == 1) and 1 or 0, ($Channel == 2) and 1 or 0, ($Channel == 3) and 1 or 0 } or def4($Mask, 1, 1, 1, 1) "
PROP_... = extBounceBack, $" def($Intensity, 20) "

[MIXIN: CustomEmissive_Alpha]
PROP_... = emAlphaFromDiffuse, $" (def($FromDiffuseMap, false) and 1 or 0.01) * (def($NMMode, false) and -1 or 1) "

[MIXIN: CustomEmissive_UseDiffuseLuminocity]
_DiffLumFrom = $" ${From:exists} and ${From:vec4} or vec4(0,0,0,0) "
_DiffLumTo = $" ${To:exists} and ${To:vec4} or vec4(1,1,1,1) "
PROP_... = emSkipDiffuseMap, $" def($SkipDiffuseMap, true) "
PROP_... = emDiffuseAvgColorAsMultiplier0, $" lerp(vec4(1, 1, 1, 1), ${Multiplier:exists} and ${Multiplier:vec4} or (1 / ($_DiffLumTo - $_DiffLumFrom)), ${Mask:exists} and ${Mask:vec4} or 1 ) "
PROP_... = emDiffuseAvgColorAsMultiplier1, $" lerp(vec4(0, 0, 0, 0), ${Offset:exists} and ${Offset:vec4} or (-$_DiffLumFrom / ($_DiffLumTo - $_DiffLumFrom)), ${Mask:exists} and ${Mask:vec4} or 1 ) "
PROP_... = emDiffuseAvgColorAsMultiplier2, $" lerp(vec4(1, 1, 1, 1), ${Exponent:exists} and ${Exponent:vec4} or vec4(1,1,1,1), ${Mask:exists} and ${Mask:vec4} or 1 ) "
PROP_... = emDiffuseAvgColorAsMultiplier3, $" lerp(vec4(1, 1, 1, 1), ${Opacity:exists} and ${Opacity:vec4} or vec4(1,1,1,1), ${Mask:exists} and ${Mask:vec4} or 1 ) "

[MIXIN: CustomEmissive_UseDiffuseAlpha]
_DiffAlpFrom = $" ${From:exists} and ${From:vec4} or vec4(0,0,0,0) "
_DiffAlpTo = $" ${To:exists} and ${To:vec4} or vec4(1,1,1,1) "
PROP_... = emSkipDiffuseMap, $" def($SkipDiffuseMap, true) "
PROP_... = emDiffuseAlphaAsMultiplier0, $" ${Multiplier:exists} and ${Multiplier:vec4} or (1 / ($_DiffAlpTo - $_DiffAlpFrom)) "
PROP_... = emDiffuseAlphaAsMultiplier1, $" ${Offset:exists} and ${Offset:vec4} or (-$_DiffAlpFrom / ($_DiffAlpTo - $_DiffAlpFrom)) "
PROP_... = emDiffuseAlphaAsMultiplier2, $" ${Exponent:exists} and ${Exponent:vec4} or vec4(1,1,1,1) "
PROP_... = emDiffuseAlphaAsMultiplier3, $" ( ${Opacity:exists} and ${Opacity:vec4} or vec4(1,1,1,1) ) * (def($FromNormalMap, true) and -1 or 1) "

[MIXIN: CustomEmissive_Color]
UseColorMasks = 1
_ChannelColor = $" def( $Channel, 0 ) "
_ThresholdLevel = $" def( $ThresholdLevel, def( ${Threshold:1}, 0.95 ) ) "
_ThresholdSharpness = $" def( $ThresholdSharpness, def( ${Threshold:2}, 20 ) )"
PROP_... = $" $_ChannelColor == 3 and 'emMirrorChannel3As4' or $_ChannelColor == 2 and 'emMirrorChannel2As5' 
  or $_ChannelColor == 1 and 'emMirrorChannel1As6' ", $" def( $Mirror, 0 ) "
PROP_... = emMaskColor${_ChannelColor}C, $" vec4( lerp( 
    def( ${Color:vec3}, vec3(1, 0.5, 0) ), 
    def( ${Color:vec3}, vec3(1, 0.5, 0) ):normalize(), 
    def( $Normalization, 1 ) ), def( $Normalization, 1 )) "
PROP_... = emMaskColor${_ChannelColor}P, $_ThresholdSharpness, $" $_ThresholdSharpness * $_ThresholdLevel ", $" def2( $AlphaMinMax, 0, 0 ) "
PROP_... = emMaskColor${_ChannelColor}A, $" def( $Opacity, 1 ) "

[MIXIN: CustomEmissive_Area]
UseAreas = 1
_Resolution = $" def2( $Resolution, 1, 1 ) "
_Center = $" def( $Center, ${Start:vec2} + ${Size:vec2} / 2 ) "
_ChannelArea = $" def( $Channel, 0 ) "
PROP_... = $" $_ChannelArea == 3 and 'emMirrorChannel3As4' or $_ChannelArea == 2 and 'emMirrorChannel2As5' 
  or $_ChannelArea == 1 and 'emMirrorChannel1As6' ", $" def( $Mirror, 0 ) "
PROP_... = emGeoChannel${_ChannelArea}S, $" vec4(2 * ${_Resolution:vec2} / ${Size:vec2}, -2 * ${_Center:vec2} / ${Size:vec2})"
PROP_... = emGeoChannel${_ChannelArea}P, $" 1 / def2( ${CornerRadius:vec2}, 0, 0 ):clamp(0.001, 1) ", $" def($Exponent, 1) ", $" def( $Opacity, 1 ) "

[MIXIN: CustomEmissive_AreaMask]
UseAreas = 1
StartWithWhite = 1
UseAreasAsMask = 1
_Resolution = $" def2( $Resolution, 1, 1 ) "
_Center = $" def( $Center, ${Start:vec2} + ${Size:vec2} / 2 ) "
_ChannelAreaMask = $" def($_ChannelAreaMask, -1) + 1 "
PROP_... = emGeoChannel${_ChannelAreaMask}S, $" vec4(2 * ${_Resolution:vec2} / ${Size:vec2}, -2 * ${_Center:vec2} / ${Size:vec2})"
PROP_... = emGeoChannel${_ChannelAreaMask}P, $" 1 / def2( ${CornerRadius:vec2}, 0, 0 ):clamp(0.001, 1) ", $" def($Exponent, 1) ", $" def( $Opacity, 1 ) "

[MIXIN: CustomEmissive_Poly]
UsePolys = 1
_PolyResolution = $" def2( $Resolution, 1, 1 ) "
_ChannelPoly = $" def( $Channel, 0 ) "
PROP_... = $" $_ChannelPoly == 3 and 'emMirrorChannel3As4' or $_ChannelPoly == 2 and 'emMirrorChannel2As5' 
  or $_ChannelPoly == 1 and 'emMirrorChannel1As6' ", $" def( $Mirror, 0 ) "
PROP_... = emPoly${_ChannelPoly}0, $" ${P1:vec2} / ${_PolyResolution:vec2} ", $" ${P2:vec2} / ${_PolyResolution:vec2} "
PROP_... = emPoly${_ChannelPoly}1, $" ${P3:vec2} / ${_PolyResolution:vec2} ", $" ${P4:vec2} / ${_PolyResolution:vec2} "
PROP_... = emPoly${_ChannelPoly}2, $" -def($Sharpness, 1000) ", $" -def($Offset, 0) * def($Sharpness, 1000) ", $" def($Exponent, 1) ", $" def( $Opacity, 1 ) "

[MIXIN: CustomEmissive_VertexMask]
UseVertexMask = 1
PROP_... = emEmissiveArea0, ${Point0:1:3:vec3}, $" def(${Point0:4}, ${Point0:exists} and 1 or 0) "
PROP_... = emEmissiveArea1, ${Point1:1:3:vec3}, $" def(${Point1:4}, ${Point1:exists} and 1 or 0) "
PROP_... = emEmissiveArea2, ${Point2:1:3:vec3}, $" def(${Point2:4}, ${Point2:exists} and 1 or 0) "
PROP_... = emEmissiveArea3, ${Point3:1:3:vec3}, $" def(${Point3:4}, ${Point3:exists} and 1 or 0) "

[MIXIN: CustomEmissive_Rect]
@MIXIN = CustomEmissive_Area

[MIXIN: CustomEmissive_CoverAll]
@MIXIN = CustomEmissive_Rect, Start = 0, Size = $" def2( $Resolution, 1, 1 ) "

[MIXIN: CustomEmissive_Circle]
@MIXIN = CustomEmissive_Area, CornerRadius = 1, Exponent = $" def($Exponent, 0.1) "
