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,69 +7,63 @@
* 1: Target <OBJECT> * 1: Target <OBJECT>
* 2: Alpha <NUMBER> * 2: Alpha <NUMBER>
* 4: Height offset <NUMBER> * 4: Height offset <NUMBER>
* 5: Draw Type <NUMBER> * 5: Draw name <BOOL>
* 5: Draw rank <BOOL>
* 6: Draw soundwave <BOOL>
* *
* Return value: * Return value:
* None * None
* *
* Example: * 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 * Public: No
*/ */
#include "script_component.hpp" #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: //Set Icon:
_icon = ""; private _icon = "";
_size = 0; private _size = 0;
if (_iconType in [ICON_NAME_SPEAK, ICON_SPEAK]) then { if (_drawSoundwave) then {
_icon = format ["%1%2%3",QUOTE(PATHTOF(UI\soundwave)), floor (random 10), ".paa"]; _icon = format [QUOTE(PATHTOF(UI\soundwave%1.paa)), floor random 10];
_size = 1; _size = 1;
_alpha = (_alpha max 0.2) + 0.2;//Boost alpha when speaking } else {
} else { if (_drawRank) then {
if (_iconType == ICON_NAME_RANK) then { _icon = format["\A3\Ui_f\data\GUI\Cfg\Ranks\%1_gs.paa", toLower rank _target];
_icon = format["\A3\Ui_f\data\GUI\Cfg\Ranks\%1_gs.paa",(toLower(rank _target))];
_size = 1; _size = 1;
}; };
}; };
if (_alpha < 0) exitWith {}; //Don't waste time if not visable //Set Text:
private _name = if (_drawName) then {
//Set Text:
_name = if (_iconType in [ICON_NAME, ICON_NAME_RANK, ICON_NAME_SPEAK]) then {
[_target, true, true] call EFUNC(common,getName) [_target, true, true] call EFUNC(common,getName)
} else { } else {
"" ""
}; };
//Set Color: //Set Color:
if ((group _target) != (group _player)) then { 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 = +GVAR(defaultNametagColor); //Make a copy, then multiply both alpha values (allows client to decrease alpha in settings)
_color set [3, (_color select 3) * _alpha]; _color set [3, (_color select 3) * _alpha];
} else { } 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); _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 { private _scale = [0.333, 0.5, 0.666, 0.83333, 1] select GVAR(tagSize);
_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, _icon,
_color, _color,
_position, [],
(_size * _scale), (_size * _scale),
(_size * _scale), (_size * _scale),
0, 0,
@ -77,4 +71,11 @@ drawIcon3D [
2, 2,
(0.05 * _scale), (0.05 * _scale),
"PuristaMedium" "PuristaMedium"
]; ]
};
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)])];
drawIcon3D _parameters;

View File

@ -15,108 +15,143 @@
*/ */
#include "script_component.hpp" #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 {}; 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; // Determine flags from current settings
_maxDistance = _ambientBrightness * GVAR(PlayerNamesViewDistance); private _drawName = true;
private _drawRank = GVAR(showPlayerRanks);
_onKeyPressAlphaMax = if ((GVAR(showPlayerNames) in [3,4])) then { private _enabledTagsNearby = false;
2 + (GVAR(showNamesTime) - ACE_time); //after release 1 second of full opacity, 1 second of fading to 0 private _enabledTagsCursor = false;
} else { private _onKeyPressAlphaMax = 1;
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;
};
}; };
_defaultIcon = if (GVAR(showPlayerRanks)) then { private _ambientBrightness = ((([] call EFUNC(common,ambientBrightness)) + ([0, 0.4] select ((currentVisionMode ace_player) != 0))) min 1) max 0;
ICON_NAME_RANK; private _maxDistance = _ambientBrightness * GVAR(PlayerNamesViewDistance);
} else {
ICON_NAME;
};
//When cursorTarget is on a vehicle show the nametag for the commander. private _camPosAGL = positionCameraToWorld [0, 0, 0];
//If set to "Only On Keypress" settings, fade just like main tags private _camPosASL = AGLtoASL _camPosAGL;
if (GVAR(showCursorTagForVehicles) && {_onKeyPressAlphaMax > 0}) then { 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; _target = cursorTarget;
if ((!(_target isKindOf "CAManBase")) && {!(_target in allUnitsUAV)}) then { 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; _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);
};
};
};
//"Only Cursor" mode, only show nametags for humans on cursorTarget
if ((GVAR(showPlayerNames) in [2,4]) && {_onKeyPressAlphaMax > 0}) 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};
} else { } 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 { // Show nametags for nearby units
_pos = positionCameraToWorld [0, 0, 0]; if (_enabledTagsNearby) then {
_targets = _pos nearObjects ["CAManBase", _maxDistance + 5]; // Find valid targets and cache them
private _targets = [[], {
if (!surfaceIsWater _pos) then { private _nearMen = _camPosAGL nearObjects ["CAManBase", _maxDistance + 7];
_pos = ATLtoASL _pos; _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}
}; };
_pos2 = positionCameraToWorld [0, 0, 1]; private _crewMen = [];
if (!surfaceIsWater _pos2) then { if (vehicle ACE_player != ACE_player) then {
_pos2 = ATLtoASL _pos2; _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}
}; };
_vecy = _pos2 vectorDiff _pos; };
(_nearMen + _crewMen)
}, missionNamespace, QGVAR(nearMen), 0.5] call EFUNC(common,cachedCall);
{ {
_target = _x; private _target = _x;
_icon = ICON_NONE; private _relPos = (visiblePositionASL _target) vectorDiff _camPosASL;
if ((GVAR(showPlayerNames) in [1,3]) && {_onKeyPressAlphaMax > 0}) then { private _distance = vectorMagnitude _relPos;
if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target} && {GVAR(showSoundWaves) > 0}) then {_icon = ICON_NAME_SPEAK;} else {_icon = _defaultIcon}; private _projDist = _relPos vectorDistance (_vecy vectorMultiply (_relPos vectorDotProduct _vecy));
} else {
//showSoundWaves must be 2, only draw speak icon private _drawSoundwave = (GVAR(showSoundWaves) > 0) && {[_target] call FUNC(isSpeaking)};
if (([_target] call FUNC(isSpeaking)) && {(vehicle _target) == _target}) then {_icon = ICON_SPEAK;}; 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) && if (_alpha > 0) then {
{(side (group _target)) == (side (group ACE_player))} && [ACE_player, _target, _alpha, _distance * 0.026, _drawName, _drawRank, _drawSoundwave] call FUNC(drawNameTagIcon);
{_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);
}; };
nil nil
} count _targets; } count _targets;
}; };
END_COUNTER(GVAR(onDraw3d));