From fe910b64a0f245fed7354c69da1d9787072e5405 Mon Sep 17 00:00:00 2001
From: Andrey Kleshchev <andreykproductengine@lindenlab.com>
Date: Thu, 23 Jul 2020 20:52:41 +0300
Subject: SL-13513 Tree's trunk should decrease quality at higher distance

---
 indra/newview/llvotree.cpp | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

(limited to 'indra/newview')

diff --git a/indra/newview/llvotree.cpp b/indra/newview/llvotree.cpp
index 8e46ccd555..41099cb570 100644
--- a/indra/newview/llvotree.cpp
+++ b/indra/newview/llvotree.cpp
@@ -45,6 +45,8 @@
 #include "llviewertexturelist.h"
 #include "llviewerobjectlist.h"
 #include "llviewerregion.h"
+#include "llvolumemgr.h"
+#include "llvovolume.h"
 #include "llworld.h"
 #include "noise.h"
 #include "pipeline.h"
@@ -85,6 +87,9 @@ LLVOTree::LLVOTree(const LLUUID &id, const LLPCode pcode, LLViewerRegion *region
 	mFrameCount = 0;
 	mWind = mRegionp->mWind.getVelocity(getPositionRegion());
 	mTrunkLOD = 0;
+
+	// if assert triggers, idleUpdate() needs to be revised and adjusted to new LOD levels
+	llassert(sMAX_NUM_TREE_LOD_LEVELS == LLVolumeLODGroup::NUM_LODS);
 }
 
 
@@ -347,8 +352,11 @@ void LLVOTree::idleUpdate(LLAgent &agent, const F64 &time)
 		return;
 	}
 	
-	S32 trunk_LOD = sMAX_NUM_TREE_LOD_LEVELS ;
+	S32 trunk_LOD = sMAX_NUM_TREE_LOD_LEVELS ; // disabled
 	F32 app_angle = getAppAngle()*LLVOTree::sTreeFactor;
+	F32 distance = mDrawable->mDistanceWRTCamera * LLVOVolume::sDistanceFactor * (F_PI / 3.f);
+	F32 diameter = getScale().length(); // trees have very broken scale, but length rougtly outlines proper diameter
+	F32 sz = mBillboardScale * mBillboardRatio * diameter;
 
 	for (S32 j = 0; j < sMAX_NUM_TREE_LOD_LEVELS; j++)
 	{
@@ -357,7 +365,14 @@ void LLVOTree::idleUpdate(LLAgent &agent, const F64 &time)
 			trunk_LOD = j;
 			break;
 		}
-	} 
+	}
+
+    F32 tan_angle = (LLVOTree::sTreeFactor * 64 * sz) / distance;
+    S32 cur_detail = LLVolumeLODGroup::getDetailFromTan(ll_round(tan_angle, 0.01f)); // larger value, better quality
+
+    // for trunk_LOD lower value means better quality, but both trunk_LOD and cur_detail have 4 levels
+    trunk_LOD = llmax(trunk_LOD, LLVolumeLODGroup::NUM_LODS - cur_detail - 1);
+    trunk_LOD = llmin(trunk_LOD, sMAX_NUM_TREE_LOD_LEVELS);
 
 	if (mReferenceBuffer.isNull())
 	{
@@ -408,8 +423,9 @@ void LLVOTree::setPixelAreaAndAngle(LLAgent &agent)
 	LLVector3 lookAt = center - viewer_pos_agent;
 	F32 dist = lookAt.normVec() ;	
 	F32 cos_angle_to_view_dir = lookAt * LLViewerCamera::getInstance()->getXAxis() ;	
-	
-	F32 range = dist - getMinScale()/2;
+	F32 radius = getScale().length()*0.5f;
+	F32 range = dist - radius;
+
 	if (range < F_ALMOST_ZERO || isHUDAttachment())		// range == zero
 	{
 		mAppAngle = 180.f;
-- 
cgit v1.2.3