[INCLUDE: common/navigators.ini]
[INCLUDE: common/materials_interior.ini]

[TEMPLATE: _Display_AppleCarPlay_Navigator EXTENDS Navigator_AndroidAuto]
@OUTPUT = NAVIGATOR_0
DYNAMIC_OPACITY = 0
Brightness = 1

[TEMPLATE: _Display_AppleCarPlay_RearCamera]
@ACTIVE = ${RearCameraPosition:exists}
@OUTPUT = RENDERING_CAMERA_...
DYNAMIC_TEXTURE_ID = camera_rear
POSITION = $RearCameraPosition
DIRECTION = 0, -0.5, -1
INCLUDE_CAR = 1
REFRESH_RATE = 20
INTERIOR_ONLY = 1
CLIP_NEAR = 0.005
FOV = 170
RESOLUTION = 2048, 1536
ACTIVE_BY_DEFAULT = 0
WITH_LIGHTING = 1

[TEMPLATE: _Display_AppleCarPlay_RearDistortedCamera]
@ACTIVE = ${RearCameraPosition:exists}
@OUTPUT = TEXTURE_PREPROCESSING_...
DYNAMIC_TEXTURE_ID = camera_rear_distorted
TEXTURE_SOURCE = dynamic::camera_rear
RESOLUTION = 512, 384
EFFECT = FISHEYE
INTENSITY = 1.5
EXPOSURE = 1
BRIGHTNESS = 0.7

[TEMPLATE: _Display_ExtraShape EXTENDS Material_DigitalScreen]
@ACTIVE = $" read('car/index', -1) == 0 "
@OUTPUT = EXTRA_SHAPE_...
MESH_NAME = $Meshes
PARENT = COCKPIT_HR
AlignPoints = 1
VERTICES_0 = $"
  local p1, p2, p3, p4 = ${P1:vec3}, ${P2:vec3}, ${P3:vec3}, ${P4:vec3}
  if ${AlignPoints:bool} then
    local d = (p4 - p3 + p1 - p2) / 4
    local i1 = (p1 + p2) / 2
    local i2 = (p3 + p4) / 2
    p1, p2 = i1 + d, i1 - d
    p3, p4 = i2 - d, i2 + d
  end
  local nm = (p4 - p1):cross(p2 - p1):normalizeSelf() * 0.001
  return { p1 + nm, p3 + nm, p2 + nm, p1 + nm, p4 + nm, p3 + nm }"
VERTICES_0_UV = 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1
ScreenScale = 480
Emissive = 2
MatrixType = TN

; Set AlignPoints to 0 to disable points alignment
[MIXIN: Display_ExtraShape]
_Display_Mesh = $" def($_Display_Mesh, 0) + 1 "
_Display_Resolution = $" ${Scale:number:or=1} * 1024 "
Meshes = __navigator_mesh_${_Display_Mesh}
@GENERATOR = _Display_ExtraShape
Resolution = $_Display_Resolution, $" math.ceil(${_Display_Resolution} * math.min(2, (${P3:vec3} - ${P2:vec3} + ${P4:vec3} - ${P1:vec3}):length() / (${P2:vec3} - ${P1:vec3} + ${P3:vec3} - ${P4:vec3}):length())) "
Size = $Resolution
Offset = 0, 0

; Main template plugging in the whole thing
; Important parameters: 
; • RearCameraPosition: point in car coordinates for position of rear camera (for reverse gear), or, if not set, rear camera will not be available;
; • aftermarketComponent: set it to 1 to allow usage of this thing for cars made before 2015.
[TEMPLATE: Display_AppleCarPlay]
_Resolution = $" ${Resolution:vec2} * def($Scale, 1) "
_Offset = $" def2($Offset, 0, 0) * def($Scale, 1) "
_Size = $" def($Size, ${Resolution:vec2} - def2($Offset, 0, 0)) * def($Scale, 1) "

@GENERATOR = _Display_AppleCarPlay_Navigator, Resolution = $_Resolution, Offset = $_Offset, Size = $" ${_Size:vec2} - vec2(0, 44) "
@GENERATOR = _Display_AppleCarPlay_RearCamera
@GENERATOR = _Display_AppleCarPlay_RearDistortedCamera
@OUTPUT = SCRIPTABLE_DISPLAY_0
MESHES = $Meshes
INTERACTIVE = 1
SCRIPT = apple_car_play/script.lua
COLOR = 1, 1, 1
KEEP_BACKGROUND = 1
RESOLUTION = $_Resolution
DISPLAY_POS = $_Offset ; 5, 7
DISPLAY_SIZE = $_Size ; 1013, 395
FONT_SCALE = 1
FORCE_UPDATE_AFTER = NAVIGATOR_0
DYNAMIC_ID = apple_car_play
ACTIVE_FOR_PLAYER_ONLY = 1
