-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.lua
More file actions
833 lines (613 loc) · 23.2 KB
/
objects.lua
File metadata and controls
833 lines (613 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
-- local Log = LibImplex_Logger()
local WorldPositionToGuiRender3DPosition = WorldPositionToGuiRender3DPosition
local GuiRender3DPositionToWorldPosition = GuiRender3DPositionToWorldPosition
local GetWorldDimensionsOfViewFrustumAtDepth = GetWorldDimensionsOfViewFrustumAtDepth
local Set3DRenderSpaceToCurrentCamera = Set3DRenderSpaceToCurrentCamera
-- local GetUnitWorldPosition = GetUnitWorldPosition
local GetUnitRawWorldPosition = GetUnitRawWorldPosition
local CENTER = CENTER
local GuiRoot = GuiRoot
local Q = LibImplex.Q
local lerp = zo_lerp
local clampedPercentBetween = zo_clampedPercentBetween
local sqrt = math.sqrt
local tan = math.tan
local floor = math.floor
local abs = math.abs
-- local PI_360 = math.pi / 360
-- local function getK()
-- local fov = GetSetting(SETTING_TYPE_CAMERA, CAMERA_SETTING_THIRD_PERSON_FIELD_OF_VIEW)
-- return 2 * tan(fov * PI_360) -- 2 * tan(FOV/2 in rad)
-- end
-- ----------------------------------------------------------------------------
local POOL_CONTROL = LibImplex_MarkersControl
local MARKER_TEMPLATE_NAME = 'LibImplex_MarkerTemplate'
local pools = {}
local function createNewPool(context, markerTemplateName, prefix)
if not pools[context] then
local function factoryFunction(objectPool)
local marker = ZO_ObjectPool_CreateNamedControl(('$(parent)_%s_%s'):format(context, prefix or 'Marker'), markerTemplateName or MARKER_TEMPLATE_NAME, objectPool, POOL_CONTROL)
assert(marker ~= nil, 'Control was not created!')
return marker
end
local function resetFunction(control)
for i = 1, control:GetNumChildren() do
local childControl = control:GetChild(i)
childControl:SetHidden(true)
end
control:SetHidden(true)
end
pools[context] = ZO_ObjectPool:New(factoryFunction, resetFunction)
end
return pools[context]
end
local function getContextStats()
local stats = {}
for context, pool in pairs(pools) do
stats[context] = {
pool:GetActiveObjectCount(),
pool:GetTotalObjectCount(),
}
end
return stats
end
local DEFAULT_POOL_NAME = 'default'
local DEFAULT_POOL = createNewPool(DEFAULT_POOL_NAME)
-- ----------------------------------------------------------------------------
local _controls = {}
local updateableObjects = setmetatable({}, {__mode = "k"})
-- ----------------------------------------------------------------------------
--- @class Marker
--- @field position table|Vector Position
--- @field texture string Texture
--- @field control ZO_Object Control
--- @field updateFunction function|nil Update function
--- @field base Marker Base class
local Marker = LibImplex.class()
--- Constructor for Marker
--- @param position table|Vector X Y Z
--- @param orientation table Pitch Yaw Roll
--- @param texture string Texture
--- @param size table W x H
--- @param color table RGB
--- @param updateFunction function|nil Update function
function Marker:__init(pool, position, orientation, texture, size, color, updateFunction)
self.position = position
self[1], self[2], self[3] = position[1], position[2], position[3]
self.orientation = orientation
self.pool = pool
self.Update = updateFunction
local control, objectKey = self.pool:AcquireObject()
self.objectKey = objectKey
control:SetTexture(texture)
if color then control:SetColor(unpack(color)) end
_controls[self] = control
if pool ~= DEFAULT_POOL then
self.control = control
end
end
local function getUpdateableObjects()
return updateableObjects
end
--[[
--- Get distance to point
--- @param point Vector
--- @return number @Distance to point in space
function Marker:DistanceTo(point)
return (point - self.position):len()
end
--]]
function Marker:DistanceXZ(point)
local dx = point[1] - self.position[1]
local dz = point[3] - self.position[3]
return sqrt(dx * dx + dz * dz)
end
function Marker:Delete()
self.pool:ReleaseObject(self.objectKey)
end
function Marker:Set3DWorldPosition(x, y, z)
return _controls[self]:Set3DRenderSpaceOrigin(WorldPositionToGuiRender3DPosition(x, y, z))
end
function Marker:SetUseDepthBuffer(useDepthBuffer)
return _controls[self]:Set3DRenderSpaceUsesDepthBuffer(useDepthBuffer)
end
function Marker:SetTextureCoords(left, right, top, bottom)
return _controls[self]:SetTextureCoords(left, right, top, bottom)
end
function Marker:SetDrawLevel(drawLevel)
return _controls[self]:SetDrawLevel(drawLevel)
end
function Marker:SetAlpha(alpha)
return _controls[self]:SetAlpha(alpha)
end
function Marker:SetColor(r, g, b, a)
return _controls[self]:SetColor(r, g, b, a)
end
function Marker:SetHidden(isHidden)
return _controls[self]:SetHidden(isHidden)
end
function Marker:SetClampedToScreen(isClampedToScreen)
return _controls[self]:SetClampedToScreen(isClampedToScreen)
end
function Marker:SetClampedToScreenInsets(l, r, t, b)
return _controls[self]:SetClampedToScreenInsets(l, r, t, b)
end
function Marker:SetHandler(...)
return _controls[self]:SetHandler(...)
end
function Marker:SetMouseEnabled(isMouseEnabled)
return _controls[self]:SetMouseEnabled(isMouseEnabled)
end
function Marker:GetDrawLevel()
return _controls[self]:GetDrawLevel()
end
function Marker:GetAnchor()
return _controls[self]:GetAnchor()
end
function Marker:IsHidden()
return _controls[self]:IsHidden()
end
local MARKERS_CONTROL_2D = LibImplex_2DMarkers
local MARKERS_CONTROL_2D_NAME = 'LibImplex_2DMarkers'
local UI_WIDTH, UI_HEIGHT = GuiRoot:GetDimensions()
local NEGATIVE_UI_HEIGHT = -UI_HEIGHT
-- UI_HEIGHT_K = NEGATIVE_UI_HEIGHT / getK()
local cX, cY, cZ = 0, 0, 0
local rX, rY, rZ = 0, 0, 0
local uX, uY, uZ = 0, 0, 0
local fX, fY, fZ = 0, 0, 0
-- local pwX, pwY, pwZ = 0, 0, 0
local prwX, prwY, prwZ = 0, 0, 0
local cPitch, cYaw, cRoll = 0, 0, 0
local function UpdateVectors()
Set3DRenderSpaceToCurrentCamera(MARKERS_CONTROL_2D_NAME)
cX, cY, cZ = GuiRender3DPositionToWorldPosition(MARKERS_CONTROL_2D:Get3DRenderSpaceOrigin()) -- RW
fX, fY, fZ = MARKERS_CONTROL_2D:Get3DRenderSpaceForward()
rX, rY, rZ = MARKERS_CONTROL_2D:Get3DRenderSpaceRight()
uX, uY, uZ = MARKERS_CONTROL_2D:Get3DRenderSpaceUp()
-- TODO: normalize?
-- _, pwX, pwY, pwZ = GetUnitWorldPosition('player')
_, prwX, prwY, prwZ = GetUnitRawWorldPosition('player')
-- cPitch, cYaw, cRoll = MARKERS_CONTROL_2D:Get3DRenderSpaceOrientation()
end
--- @class Marker2D : Marker
local Marker2D = LibImplex.class(Marker)
local function _update2d(marker)
local markerControl = _controls[marker]
local mX, mY, mZ = marker[1], marker[2], marker[3]
local dX, dY, dZ = mX - cX, mY - cY, mZ - cZ
local Z = fX * dX + fY * dY + fZ * dZ
if Z < 0 then
markerControl:SetHidden(true)
-- if not marker[4] then
-- markerControl:SetHidden(true)
-- marker[4] = true
-- end
return
end
-- --------------------------------------------------------------------
local diffX, diffY, diffZ = prwX - mX, prwY - mY, prwZ - mZ
local distance = sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ)
local updateFunctions = marker.updateFunctions
for i = 1, #updateFunctions do
if updateFunctions[i](marker, distance, prwX, prwY, prwZ, fX, fY, fZ, rX, rY, rZ, uX, uY, uZ) then return end
end
-- --------------------------------------------------------------------
-- local X = rX * dX + rZ * dZ -- rY * dY can be ignored, rY = 0 because it is vector in XZ plane
-- local Y = uX * dX + uY * dY + uZ * dZ
local w, h = GetWorldDimensionsOfViewFrustumAtDepth(Z)
local offsetX, offsetY = (rX * dX + rZ * dZ) * UI_WIDTH / w, (uX * dX + uY * dY + uZ * dZ) * NEGATIVE_UI_HEIGHT / h
markerControl:SetAnchor(CENTER, GuiRoot, CENTER, offsetX, offsetY)
markerControl:SetDrawLevel(-Z)
marker[5], marker[6], marker[7] = offsetX, offsetY, -Z
markerControl:SetHidden(false)
-- if marker[4] then
-- markerControl:SetHidden(false)
-- marker[4] = false
-- end
end
function Marker2D:__init(pool, position, orientation, texture, size, color, ...)
self.base.__init(self, pool, position, orientation, texture, size, color, _update2d)
self.updateFunctions = {...}
-- self.offsetX, self.offsetY = 0, 0
self[5], self[6] = 0, 0
local control = _controls[self]
if size then control:SetDimensions(unpack(size)) end
self.distanceLabel = control:GetNamedChild('DistanceLabel')
updateableObjects[self] = true
end
function Marker2D:Delete()
Marker.Delete(self)
self.distanceLabel:SetHidden(true)
updateableObjects[self] = nil
end
--- @class Marker2DWS : Marker
local Marker2DWS = LibImplex.class(Marker)
local function _update2dws(marker)
local markerControl = _controls[marker]
local mX, mY, mZ = marker[1], marker[2], marker[3]
local diffX, diffY, diffZ = prwX - mX, prwY - mY, prwZ - mZ
local distance = sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ)
local updateFunctions = marker.updateFunctions
for i = 1, #updateFunctions do
if updateFunctions[i](marker, distance, prwX, prwY, prwZ, fX, fY, fZ, rX, rY, rZ, uX, uY, uZ) then return end
end
local dX, dY, dZ = mX - cX, mY - cY, mZ - cZ
-- local F = marker.F
-- local Fx, Fy, Fz = F[1], F[2], F[3]
local Fx, Fy, Fz = marker[8], marker[9], marker[10]
local numerator = Fx * dX + Fy * dY + Fz * dZ
local denominator = Fx * fX + Fy * fY + Fz * fZ
-- it is not entirely correct, parallel to the camera texture
-- should be treated as a special case and draw level
-- should be calculated other way
-- but this is "good enough (c)"
if denominator > 1e-6 and denominator < -1e-6 then
local t = numerator / denominator
-- if t < 0 then return end
local diX, diY, diZ = t * fX, t * fY, t * fZ
local D = diX * diX + diY * diY + diZ * diZ
markerControl:SetDrawLevel(-D)
end
end
function Marker2DWS:__init(pool, position, orientation, texture, size, color, ...)
self.base.__init(self, pool, position, orientation, texture, size, color, _update2dws)
self.updateFunctions = {...}
self[5], self[6] = 0, 0
local control = _controls[self]
control:SetTransformNormalizedOriginPoint(0.5, 0.5)
control:SetAnchor(CENTER, GuiRoot, CENTER)
control:SetSpace(SPACE_WORLD)
-- control:SetTransformScale(0.01)
local x, y, z = WorldPositionToGuiRender3DPosition(position[1], position[2], position[3])
control:SetTransformOffset(x, y, z)
control:SetTransformRotation(orientation[1], orientation[2], orientation[3])
if size then control:SetDimensions(unpack(size)) end
self.distanceLabel = control:GetNamedChild('DistanceLabel')
local q = Q.FromEuler(orientation[1], orientation[2], orientation[3])
local F = Q.RotateVectorByQuaternion({0, 0, 1}, q)
local U = Q.RotateVectorByQuaternion({0, 1, 0}, q)
local R = Q.RotateVectorByQuaternion({-1, 0, 0}, q)
self[ 8], self[ 9], self[10] = F[1], F[2], F[3]
self[11], self[12], self[13] = U[1], U[2], U[3]
self[14], self[15], self[16] = R[1], R[2], R[3]
updateableObjects[self] = true
end
function Marker2DWS:Delete()
Marker.Delete(self)
self.distanceLabel:SetHidden(true)
updateableObjects[self] = nil
end
--- @class Marker3DStatic : Marker
local Marker3DStatic = LibImplex.class(Marker)
function Marker3DStatic:__init(pool, position, orientation, texture, size, color)
Marker.__init(self, pool, position, orientation, texture, size, color) -- TODO: refactor
local control = _controls[self]
size = size or {1, 1}
control:Create3DRenderSpace()
control:Set3DLocalDimensions(unpack(size))
self:Move(position)
self:Orient(orientation)
control:Set3DRenderSpaceUsesDepthBuffer(select(4, unpack(orientation)))
control:SetHidden(false)
self.outline = {}
end
function Marker3DStatic:Outline()
self:RemoveOutline()
local tl = self:GetRelativePointCoordinates(TOPLEFT)
local tr = self:GetRelativePointCoordinates(TOPRIGHT)
local br = self:GetRelativePointCoordinates(BOTTOMRIGHT)
local bl = self:GetRelativePointCoordinates(BOTTOMLEFT)
self.outline[1] = LibImplex.Lines.Line(tl, tr)
self.outline[2] = LibImplex.Lines.Line(tr, br)
self.outline[3] = LibImplex.Lines.Line(br, bl)
self.outline[4] = LibImplex.Lines.Line(bl, tl)
end
function Marker3DStatic:RemoveOutline()
for i = 1, #self.outline do
self.outline[i]:Delete()
self.outline[i] = nil
end
end
function Marker3DStatic:Move(position)
self.position = position
local rendX, rendY, rendZ = WorldPositionToGuiRender3DPosition(unpack(position))
_controls[self]:Set3DRenderSpaceOrigin(rendX, rendY, rendZ)
end
function Marker3DStatic:Orient(orientation)
local pitch, yaw, roll, depthBuffer = unpack(orientation)
pitch = pitch or 0
yaw = yaw or 0
roll = roll or 0
_controls[self]:Set3DRenderSpaceOrientation(pitch, yaw, roll)
local q = Q.FromEuler(pitch, yaw, roll)
self.F = Q.RotateVectorByQuaternion({0, 0, 1}, q)
self.U = Q.RotateVectorByQuaternion({0, 1, 0}, q)
self.R = Q.RotateVectorByQuaternion({-1, 0, 0}, q)
end
function Marker3DStatic:DrawNormal(length)
if self.normal then
return self.normal:Move(self.position, self.position + self.F * length)
end
length = length or 10
self.normal = LibImplex.Lines.Line(self.position, self.position + self.F * length)
end
function Marker3DStatic:RemoveNormal()
if not self.normal then return end
self.normal:Delete()
self.normal = nil
end
function Marker3DStatic:Delete()
-- TODO: set something to default
Marker.Delete(self)
self:RemoveOutline()
local control = _controls[self]
control:Destroy3DRenderSpace()
control:SetDrawLevel(0)
control:SetAlpha(1)
control:SetTextureCoords(0, 1, 0, 1)
self.R = nil
self.U = nil
self.F = nil
end
local function getShift(anchor)
-- center => anchor
local right, up = 0, 0
if anchor == TOPLEFT then right = -0.5 up = 0.5
elseif anchor == TOP then right = 0 up = 0.5
elseif anchor == TOPRIGHT then right = 0.5 up = 0.5
elseif anchor == LEFT then right = -0.5 up = 0
elseif anchor == CENTER then right = 0 up = 0
elseif anchor == RIGHT then right = 0.5 up = 0
elseif anchor == BOTTOMLEFT then right = -0.5 up = -0.5
elseif anchor == BOTTOM then right = 0 up = -0.5
elseif anchor == BOTTOMRIGHT then right = 0.5 up = -0.5
else
error('Bad anchor')
end
return right, up
end
function Marker3DStatic:GetRelativePointCoordinates(anchorPoint, offsetRight, offsetUp, offsetForward)
offsetRight = offsetRight or 0
offsetUp = offsetUp or 0
offsetForward = offsetForward or 0
local w, h = _controls[self]:Get3DLocalDimensions()
local targetRight, targetUp = getShift(anchorPoint)
local width = w * 100
local height = h * 100
local totalRight = targetRight * width + offsetRight
local totalUp = targetUp * height + offsetUp
return self.position + self.R * totalRight + self.U * totalUp + self.F * offsetForward
end
-- ----------------------------------------------------------------------------
--- @class Marker3D : Marker3DStatic
local Marker3D = LibImplex.class(Marker3DStatic)
-- local cache = setmetatable({}, {__mode = 'k'})
-- local function ClearCache()
-- for k, _ in pairs(cache) do
-- cache[k] = nil
-- end
-- end
local function _update3d(marker)
local markerControl = _controls[marker]
local x, y, z = marker.position[1], marker.position[2], marker.position[3]
local diffX = prwX - x
local diffY = prwY - y
local diffZ = prwZ - z
local distance = sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ)
local updateFunctions = marker.updateFunctions
for i = 1, #updateFunctions do
if updateFunctions[i](marker, distance, prwX, prwY, prwZ, fX, fY, fZ, rX, rY, rZ, uX, uY, uZ) then return end
end
local dX, dY, dZ = x - cX, y - cY, z - cZ
--[[
local Z = fX * dX + fY * dY + fZ * dZ
if Z < 0 then return end
markerControl:SetDrawLevel(-Z) -- "2D" way
--]]
---[[
local F = marker.F
local Fx, Fy, Fz = F[1], F[2], F[3]
-- if cache[F] then
-- markerControl:SetDrawLevel(cache[F])
-- return
-- end
local numerator = Fx * dX + Fy * dY + Fz * dZ
local denominator = Fx * fX + Fy * fY + Fz * fZ
-- it is not entirely correct, parallel to the camera texture
-- should be treated as a special case and draw level
-- should be calculated other way
-- but this is "good enough (c)"
if denominator < 1e-6 and denominator > -1e-6 then
-- cache[F] = markerControl:GetDrawLevel()
return
else
local t = numerator / denominator
-- if t < 0 then return end
local diX, diY, diZ = t * fX, t * fY, t * fZ
local D = diX * diX + diY * diY + diZ * diZ
markerControl:SetDrawLevel(-D)
-- cache[F] = -D
end
--]]
end
function Marker3D:__init(pool, position, orientation, texture, size, color, ...)
Marker3DStatic.__init(self, pool, position, orientation, texture, size, color)
self.Update = _update3d
self.updateFunctions = {...}
--[[
local control = _controls[self]
size = size or {1, 1}
control:Create3DRenderSpace()
control:Set3DLocalDimensions(unpack(size))
local pitch, yaw, roll, depthBuffer = unpack(orientation)
pitch = pitch or 0
yaw = yaw or 0
roll = roll or 0
local rendX, rendY, rendZ = WorldPositionToGuiRender3DPosition(unpack(position))
control:Set3DRenderSpaceOrigin(rendX, rendY, rendZ)
control:Set3DRenderSpaceOrientation(pitch, yaw, roll)
control:Set3DRenderSpaceUsesDepthBuffer(depthBuffer)
control:SetHidden(false)
--]]
updateableObjects[self] = true
end
function Marker3D:Delete()
Marker3DStatic.Delete(self)
updateableObjects[self] = nil
end
-- ----------------------------------------------------------------------------
local Line = LibImplex.class()
local linePool = createNewPool('lines', 'LibImplex_LineTemplate', 'Line')
function Line:__init(endpoint1, endpoint2, color)
self.x1, self.y1, self.z1 = unpack(endpoint1)
self.x2, self.y2, self.z2 = unpack(endpoint2)
self.pool = linePool
local control, objectKey = self.pool:AcquireObject()
self.objectKey = objectKey
if color then control:SetColor(unpack(color)) end
_controls[self] = control
updateableObjects[self] = true
end
function Line:Delete()
self.pool:ReleaseObject(self.objectKey)
_controls[self] = nil
updateableObjects[self] = nil
end
function Line:Update()
local control = _controls[self]
local dX1, dY1, dZ1 = self.x1 - cX, self.y1 - cY, self.z1 - cZ
local Z1 = fX * dX1 + fY * dY1 + fZ * dZ1
if Z1 < 0 then
control:SetHidden(true)
return
end
-- ------------------------------------------------------------------------
local dX2, dY2, dZ2 = self.x2 - cX, self.y2 - cY, self.z2 - cZ
local Z2 = fX * dX2 + fY * dY2 + fZ * dZ2
if Z2 < 0 then
control:SetHidden(true)
return
end
-- --------------------------------------------------------------------
local X1 = rX * dX1 + rZ * dZ1
local Y1 = uX * dX1 + uY * dY1 + uZ * dZ1
local w1, h1 = GetWorldDimensionsOfViewFrustumAtDepth(Z1)
local scaleW1 = UI_WIDTH / w1
local scaleH1 = NEGATIVE_UI_HEIGHT / h1
control:SetAnchor(TOPLEFT, GuiRoot, CENTER, X1 * scaleW1, Y1 * scaleH1)
-- ------------------------------------------------------------------------
local X2 = rX * dX2 + rZ * dZ2
local Y2 = uX * dX2 + uY * dY2 + uZ * dZ2
local w2, h2 = GetWorldDimensionsOfViewFrustumAtDepth(Z2)
local scaleW2 = UI_WIDTH / w2
local scaleH2 = NEGATIVE_UI_HEIGHT / h2
control:SetAnchor(BOTTOMRIGHT, GuiRoot, CENTER, X2 * scaleW2, Y2 * scaleH2)
-- lineControl:SetDrawLevel(-Z1)
control:SetHidden(false)
end
function Line:Move(endpoint1, endpoint2)
self.x1, self.y1, self.z1 = unpack(endpoint1)
self.x2, self.y2, self.z2 = unpack(endpoint2)
end
-- ----------------------------------------------------------------------------
-- TODO: optimize lerp and clamp functions
local function ChangeAlphaWithDistance(minAlpha, maxAlpha, minDistance, maxDistance)
local function inner(marker, distance)
marker:SetAlpha(lerp(minAlpha, maxAlpha, clampedPercentBetween(minDistance, maxDistance, distance)))
end
return inner
end
local function Change3DLocalDimensionsWithDistance(minDimensions, maxDimensions, minDistance, maxDistance)
local function inner(marker, distance)
local d = lerp(minDimensions, maxDimensions, clampedPercentBetween(minDistance, maxDistance, distance))
marker:Set3DLocalDimensions(d, d)
end
return inner
end
local function HideIfTooFar(maxDistance)
local function inner(marker, distance)
marker:SetHidden(distance > maxDistance)
end
return inner
end
local function HideIfTooClose(minDistance)
local function inner(marker, distance)
marker:SetHidden(distance < minDistance)
end
return inner
end
-- ----------------------------------------------------------------------------
LibImplex = LibImplex or {}
LibImplex.GetUpdateableObjects = getUpdateableObjects
LibImplex.Context = {
GetContextStats = getContextStats
}
-- LibImplex.ClearCache = ClearCache
LibImplex.Marker = {
subclass = function() return LibImplex.class(Marker) end,
UpdateVectors = UpdateVectors,
}
local DefaultMarkers = {
Marker2D = Marker2D,
Marker3D = Marker3D,
Marker3DStatic = Marker3DStatic,
Marker2DWS = Marker2DWS,
_2D = Marker2D,
_3D = Marker3D,
_3DStatic = Marker3DStatic,
_2DWS = Marker2DWS
}
local contextMeta = {
__index = function(tbl, key)
local marker = DefaultMarkers[key]
if marker then
return function(...)
return marker(tbl._pool, ...)
end
end
end
}
local markerMeta = {
__index = function(_, key)
local marker = DefaultMarkers[key]
if marker then
return function(...)
return marker(DEFAULT_POOL, ...)
end
end
end,
__call = function(self, context)
local context_table = {
_pool = createNewPool(context or DEFAULT_POOL_NAME)
}
setmetatable(context_table, contextMeta)
return context_table
end
}
setmetatable(LibImplex.Marker, markerMeta)
-- LibImplex.Player = {
-- GetVector = GetPlayerVector,
-- GetVector = function() return VP end,
-- GetCoordinates = function() return VP[1], VP[2], VP[3] end,
-- GetOnScreenCoordinates = GetPlayerOnScreenCoordinates,
-- }
local function calculateEulerAngles()
local pitch = math.asin(-fY)
local yaw = math.atan2(fX, fZ)
local roll = math.atan2(rY, uY)
return pitch, yaw, roll
end
LibImplex.Camera = {
GetOrientation = calculateEulerAngles
}
LibImplex.UpdateFunction = {
HideIfTooFar = HideIfTooFar,
HideIfTooClose = HideIfTooClose,
ChangeAlphaWithDistance = ChangeAlphaWithDistance,
ChangeSize3DWithDistance = Change3DLocalDimensionsWithDistance,
}
LibImplex.Lines = {
Line = Line,
GetUpdateableLines = getUpdateableLines
}