Merge pull request #3290 from acemod/nametagsOptimization

Nametags optimization
This commit is contained in:
Thomas Kooi 2016-02-20 17:15:01 +01:00
commit 6048d15847
2 changed files with 170 additions and 134 deletions

View File

@ -7,74 +7,75 @@
* 1: Target <OBJECT>
* 2: Alpha <NUMBER>
* 4: Height offset <NUMBER>
* 5: Draw Type <NUMBER>
* 5: Draw name <BOOL>
* 5: Draw rank <BOOL>
* 6: Draw soundwave <BOOL>
*
* Return value:
* None
*
* Example:
* [ACE_player, bob, 0.5, height, ICON_NAME_SPEAK] call ace_nametags_fnc_drawNameTagIcon
* [ACE_player, bob, 0.5, height, true, true, true] call ace_nametags_fnc_drawNameTagIcon
*
* Public: No
*/
#include "script_component.hpp"
params ["_player", "_target", "_alpha", "_heightOffset", "_iconType"];
TRACE_1("drawName:", _this);
if ((_iconType == ICON_NONE) || {isObjectHidden _target}) exitWith {}; //Don't waste time if not visable
params ["", "_target", "", "_heightOffset"];
private ["_position", "_color", "_name", "_size", "_icon", "_scale"];
_fnc_parameters = {
params ["_player", "_target", "_alpha", "_heightOffset", "_drawName", "_drawRank", "_drawSoundwave"];
//Set Icon:
_icon = "";
_size = 0;
if (_iconType in [ICON_NAME_SPEAK, ICON_SPEAK]) then {
_icon = format ["%1%2%3",QUOTE(PATHTOF(UI\soundwave)), floor (random 10), ".paa"];
_size = 1;
_alpha = (_alpha max 0.2) + 0.2;//Boost alpha when speaking
} else {
if (_iconType == ICON_NAME_RANK) then {
_icon = format["\A3\Ui_f\data\GUI\Cfg\Ranks\%1_gs.paa",(toLower(rank _target))];
//Set Icon:
private _icon = "";
private _size = 0;
if (_drawSoundwave) then {
_icon = format [QUOTE(PATHTOF(UI\soundwave%1.paa)), floor random 10];
_size = 1;
} else {
if (_drawRank) then {
_icon = format["\A3\Ui_f\data\GUI\Cfg\Ranks\%1_gs.paa", toLower rank _target];
_size = 1;
};
};
//Set Text:
private _name = if (_drawName) then {
[_target, true, true] call EFUNC(common,getName)
} else {
""
};
//Set Color:
private _color = [1, 1, 1, _alpha];
if ((group _target) != (group _player)) then {
_color = +GVAR(defaultNametagColor); //Make a copy, then multiply both alpha values (allows client to decrease alpha in settings)
_color set [3, (_color select 3) * _alpha];
} else {
_color = [[1, 1, 1, _alpha], [1, 0, 0, _alpha], [0, 1, 0, _alpha], [0, 0, 1, _alpha], [1, 1, 0, _alpha]] select ((["MAIN", "RED", "GREEN", "BLUE", "YELLOW"] find (assignedTeam _target)) max 0);
};
private _scale = [0.333, 0.5, 0.666, 0.83333, 1] select GVAR(tagSize);
[
_icon,
_color,
[],
(_size * _scale),
(_size * _scale),
0,
_name,
2,
(0.05 * _scale),
"PuristaMedium"
]
};
if (_alpha < 0) exitWith {}; //Don't waste time if not visable
private _parameters = [_this, _fnc_parameters, _target, QGVAR(drawParameters), 0.1] call EFUNC(common,cachedCall);
_parameters set [2, _target modelToWorldVisual ((_target selectionPosition "pilot") vectorAdd [0,0,(_heightOffset + .3)])];
//Set Text:
_name = if (_iconType in [ICON_NAME, ICON_NAME_RANK, ICON_NAME_SPEAK]) then {
[_target, true, true] call EFUNC(common,getName)
} else {
""
};
//Set Color:
if ((group _target) != (group _player)) then {
_color = +GVAR(defaultNametagColor); //Make a copy, then multiply both alpha values (allows client to decrease alpha in settings)
_color set [3, (_color select 3) * _alpha];
} else {
_color = [[1, 1, 1, _alpha], [1, 0, 0, _alpha], [0, 1, 0, _alpha], [0, 0, 1, _alpha], [1, 1, 0, _alpha]] select ((["MAIN", "RED", "GREEN", "BLUE", "YELLOW"] find (assignedTeam _target)) max 0);
};
if (isNil "_color") then {
_color = [1, 1, 1, _alpha];
};
// Convert position to ASLW (expected by drawIcon3D) and add height offsets
_position = _target modelToWorldVisual ((_target selectionPosition "pilot") vectorAdd [0,0,(_heightOffset + .3)]);
_scale = [0.333, 0.5, 0.666, 0.83333, 1] select GVAR(tagSize);
drawIcon3D [
_icon,
_color,
_position,
(_size * _scale),
(_size * _scale),
0,
_name,
2,
(0.05 * _scale),
"PuristaMedium"
];
drawIcon3D _parameters;

View File

@ -15,108 +15,143 @@
*/
#include "script_component.hpp"
private ["_onKeyPressAlphaMax", "_defaultIcon", "_distance", "_alpha", "_icon", "_targets", "_pos2", "_vecy", "_relPos", "_projDist", "_pos", "_target", "_targetEyePosASL", "_ambientBrightness", "_maxDistance"];
private ["_defaultIcon", "_distance", "_alpha", "_icon", "_targets", "_relPos", "_projDist", "_target"];
//don't show nametags in spectator or if RscDisplayMPInterrupt is open
BEGIN_COUNTER(GVAR(onDraw3d));
// Don't show nametags in spectator or if RscDisplayMPInterrupt is open
if ((isNull ACE_player) || {!alive ACE_player} || {!isNull (findDisplay 49)}) exitWith {};
_ambientBrightness = ((([] call EFUNC(common,ambientBrightness)) + ([0, 0.4] select ((currentVisionMode ace_player) != 0))) min 1) max 0;
_maxDistance = _ambientBrightness * GVAR(PlayerNamesViewDistance);
_onKeyPressAlphaMax = if ((GVAR(showPlayerNames) in [3,4])) then {
2 + (GVAR(showNamesTime) - ACE_time); //after release 1 second of full opacity, 1 second of fading to 0
} else {
1
};
_defaultIcon = if (GVAR(showPlayerRanks)) then {
ICON_NAME_RANK;
} else {
ICON_NAME;
};
//When cursorTarget is on a vehicle show the nametag for the commander.
//If set to "Only On Keypress" settings, fade just like main tags
if (GVAR(showCursorTagForVehicles) && {_onKeyPressAlphaMax > 0}) then {
_target = cursorTarget;
if ((!(_target isKindOf "CAManBase")) && {!(_target in allUnitsUAV)}) then {
_target = effectiveCommander _target;
if ((!isNull _target) &&
{(side (group _target)) == (side (group ACE_player))} &&
{_target != ACE_player} &&
{GVAR(showNamesForAI) || {[_target] call EFUNC(common,isPlayer)}} &&
{!(_target getVariable ["ACE_hideName", false])}) then {
_distance = ACE_player distance _target;
_alpha = (((1 - 0.2 * (_distance - _maxDistance)) min 1) * GVAR(playerNamesMaxAlpha)) min _onKeyPressAlphaMax;
[ACE_player, _target, _alpha, _distance * 0.026, _defaultIcon] call FUNC(drawNameTagIcon);
};
// Determine flags from current settings
private _drawName = true;
private _drawRank = GVAR(showPlayerRanks);
private _enabledTagsNearby = false;
private _enabledTagsCursor = false;
private _onKeyPressAlphaMax = 1;
switch (GVAR(showPlayerNames)) do {
case 0: {
// Player names Disabled
_drawName = false;
_enabledTagsNearby = (GVAR(showSoundWaves) == 2);
_enabledTagsCursor = false;
};
case 1: {
// Player names Enabled
_enabledTagsNearby = true;
_enabledTagsCursor = false;
};
case 2: {
// Player names Only cursor
_enabledTagsNearby = (GVAR(showSoundWaves) == 2);
_enabledTagsCursor = true;
};
case 3: {
// Player names Only Keypress
_onKeyPressAlphaMax = 2 + (GVAR(showNamesTime) - ACE_time);
_enabledTagsNearby = (_onKeyPressAlphaMax) > 0 || (GVAR(showSoundWaves) == 2);
_enabledTagsCursor = false;
};
case 4: {
// Player names Only Cursor and Keypress
_onKeyPressAlphaMax = 2 + (GVAR(showNamesTime) - ACE_time);
_enabledTagsNearby = (GVAR(showSoundWaves) == 2);
_enabledTagsCursor = _onKeyPressAlphaMax > 0;
};
};
//"Only Cursor" mode, only show nametags for humans on cursorTarget
if ((GVAR(showPlayerNames) in [2,4]) && {_onKeyPressAlphaMax > 0}) then {
private _ambientBrightness = ((([] call EFUNC(common,ambientBrightness)) + ([0, 0.4] select ((currentVisionMode ace_player) != 0))) min 1) max 0;
private _maxDistance = _ambientBrightness * GVAR(PlayerNamesViewDistance);
private _camPosAGL = positionCameraToWorld [0, 0, 0];
private _camPosASL = AGLtoASL _camPosAGL;
private _vecy = (AGLtoASL positionCameraToWorld [0, 0, 1]) vectorDiff _camPosASL;
// Show nametag for the unit behind the cursor or its commander
if (_enabledTagsCursor) then {
_target = cursorTarget;
if ((!isNull _target) &&
{_target isKindOf "CAManBase"} &&
{(side (group _target)) == (side (group ACE_player))} &&
{_target != ACE_player} &&
{GVAR(showNamesForAI) || {[_target] call EFUNC(common,isPlayer)}} &&
{!(_target getVariable ["ACE_hideName", false])}) then {
_distance = ACE_player distance _target;
if (_distance > (_maxDistance + 5)) exitWith {};
_alpha = (((1 - 0.2 * (_distance - _maxDistance)) min 1) * GVAR(playerNamesMaxAlpha)) min _onKeyPressAlphaMax;
_icon = ICON_NONE;
if (GVAR(showSoundWaves) == 2) then { //icon will be drawn below, so only show name here
_icon = if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target}) then {ICON_NAME} else {_defaultIcon};
if !(_target isKindOf "CAManBase") then {
// When cursorTarget is on a vehicle show the nametag for the commander.
if !(_target in allUnitsUAV) then {
_target = effectiveCommander _target;
} else {
_icon = if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target} && {GVAR(showSoundWaves) > 0}) then {ICON_NAME_SPEAK} else {_defaultIcon};
_target = objNull;
};
};
if (isNull _target) exitWith {};
[ACE_player, _target, _alpha, _distance * 0.026, _icon] call FUNC(drawNameTagIcon);
if (_target != ACE_player &&
{(side group _target) == (side group ACE_player)} &&
{GVAR(showNamesForAI) || {[_target] call EFUNC(common,isPlayer)}} &&
{lineIntersectsSurfaces [_camPosASL, eyePos _target, ACE_player, _target] isEqualTo []} &&
{!isObjectHidden _target}) then {
_distance = ACE_player distance _target;
private _drawSoundwave = (GVAR(showSoundWaves) > 0) && {[_target] call FUNC(isSpeaking)};
// Alpha:
// - base value determined by GVAR(playerNamesMaxAlpha)
// - decreases when _distance > _maxDistance
// - increases when the unit is speaking
// - it's clamped by the value of _onKeyPressAlphaMax
private _alpha = (((1 + ([0, 0.2] select _drawSoundwave) - 0.2 * (_distance - _maxDistance)) min 1) * GVAR(playerNamesMaxAlpha)) min _onKeyPressAlphaMax;
if (_alpha > 0) then {
[ACE_player, _target, _alpha, _distance * 0.026, _drawName, _drawRank, _drawSoundwave] call FUNC(drawNameTagIcon);
};
};
};
if (((GVAR(showPlayerNames) in [1,3]) && {_onKeyPressAlphaMax > 0}) || {GVAR(showSoundWaves) == 2}) then {
_pos = positionCameraToWorld [0, 0, 0];
_targets = _pos nearObjects ["CAManBase", _maxDistance + 5];
if (!surfaceIsWater _pos) then {
_pos = ATLtoASL _pos;
};
_pos2 = positionCameraToWorld [0, 0, 1];
if (!surfaceIsWater _pos2) then {
_pos2 = ATLtoASL _pos2;
};
_vecy = _pos2 vectorDiff _pos;
// Show nametags for nearby units
if (_enabledTagsNearby) then {
// Find valid targets and cache them
private _targets = [[], {
private _nearMen = _camPosAGL nearObjects ["CAManBase", _maxDistance + 7];
_nearMen = _nearMen select {
_x != ACE_player &&
{(side group _x) == (side group ACE_player)} &&
{GVAR(showNamesForAI) || {[_x] call EFUNC(common,isPlayer)}} &&
{lineIntersectsSurfaces [_camPosASL, eyePos _x, ACE_player, _x] isEqualTo []} &&
{!isObjectHidden _x}
};
private _crewMen = [];
if (vehicle ACE_player != ACE_player) then {
_crewMen = (crew vehicle ACE_player) select {
_x != ACE_player &&
{(side group _x) == (side group ACE_player)} &&
{GVAR(showNamesForAI) || {[_x] call EFUNC(common,isPlayer)}} &&
{lineIntersectsSurfaces [_camPosASL, eyePos _x, ACE_player, _x, true, 1, "GEOM", "NONE"] isEqualTo []} &&
{!isObjectHidden _x}
};
};
(_nearMen + _crewMen)
}, missionNamespace, QGVAR(nearMen), 0.5] call EFUNC(common,cachedCall);
{
_target = _x;
private _target = _x;
_icon = ICON_NONE;
if ((GVAR(showPlayerNames) in [1,3]) && {_onKeyPressAlphaMax > 0}) then {
if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target} && {GVAR(showSoundWaves) > 0}) then {_icon = ICON_NAME_SPEAK;} else {_icon = _defaultIcon};
} else {
//showSoundWaves must be 2, only draw speak icon
if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target}) then {_icon = ICON_SPEAK;};
private _relPos = (visiblePositionASL _target) vectorDiff _camPosASL;
private _distance = vectorMagnitude _relPos;
private _projDist = _relPos vectorDistance (_vecy vectorMultiply (_relPos vectorDotProduct _vecy));
private _drawSoundwave = (GVAR(showSoundWaves) > 0) && {[_target] call FUNC(isSpeaking)};
private _alphaMax = _onKeyPressAlphaMax;
if ((GVAR(showSoundWaves) == 2) && _drawSoundwave) then {
_drawName = _drawSoundwave;
_drawRank = false;
_alphaMax = 1;
};
// Alpha:
// - base value determined by GVAR(playerNamesMaxAlpha)
// - decreases when _distance > _maxDistance
// - increases when the unit is speaking
// - it's clamped by the value of _onKeyPressAlphaMax unless soundwaves are forced on and the unit is talking
private _alpha = (((1 + ([0, 0.2] select _drawSoundwave) - 0.2 * (_distance - _maxDistance)) min 1) * GVAR(playerNamesMaxAlpha)) min _alphaMax;
if ((_icon != ICON_NONE) &&
{(side (group _target)) == (side (group ACE_player))} &&
{_target != ACE_player} &&
{GVAR(showNamesForAI) || {[_target] call EFUNC(common,isPlayer)}} &&
{!(_target getVariable ["ACE_hideName", false])}) then {
_targetEyePosASL = eyePos _target;
if (lineIntersects [_pos, _targetEyePosASL, ACE_player, _target]) exitWith {}; // Check if there is line of sight
_relPos = (visiblePositionASL _target) vectorDiff _pos;
_distance = vectorMagnitude _relPos;
_projDist = _relPos vectorDistance (_vecy vectorMultiply (_relPos vectorDotProduct _vecy));
_alpha = (((1 - 0.2 * (_distance - _maxDistance)) min 1) * GVAR(playerNamesMaxAlpha)) min _onKeyPressAlphaMax;
[ACE_player, _target, _alpha, _distance * 0.026, _icon] call FUNC(drawNameTagIcon);
if (_alpha > 0) then {
[ACE_player, _target, _alpha, _distance * 0.026, _drawName, _drawRank, _drawSoundwave] call FUNC(drawNameTagIcon);
};
nil
} count _targets;
};
END_COUNTER(GVAR(onDraw3d));